unsigned int ADC_LUX, ADC_ADJ;
unsigned long int TCOUNT;
unsigned short int var1,var2,var3;
// Interrupt Handler
void interrupt()
{
// Timer0 Interrupt - Freq = 15.26 Hz - Period = 0.065536 seconds
if (INTCON.TMR0IF == 1) // timer 0 interrupt flag
{
TCOUNT = TCOUNT + 1;
if (TCOUNT == 100000) //332300 COUNT EQUALS TO 8 HOURS
TCOUNT = 0;
// GPIO.GP4 = ~GPIO.GP4; // Toggle PORTB bit0 LED
INTCON.TMR0IF = 0; // clear the flag
INTCON.TMR0IE = 1; // reenable the interrupt
TMR0 = 0; // reset the timer preset count
}
}
void main() {
ADC_Init();
ADCON0 = 0x00;
ANSEL = 0X03; // AN0 & AN1 CONFIGURED AS ANALOGUE INPUT
TRISIO = 0b00000011;
//GPIO = 0X00;
CMCON = 0x07;
var1 = 0;
var2 = 0;
var3 = 0;
//Timer0 Registers Prescaler= 256 - TMR0 Preset = 0 - Freq = 15.26 Hz - Period = 0.065536 seconds
OPTION_REG.T0CS = 0; // bit 5 TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
OPTION_REG.T0SE = 0; // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
OPTION_REG.PSA = 0; // bit 3 Prescaler Assignment bit...0 = Prescaler is assigned to the Timer0
OPTION_REG.PS2 = 1; // bits 2-0 PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1 = 1;
OPTION_REG.PS0 = 1;
TMR0 = 0; // preset for timer register
// Interrupt Registers
INTCON = 0; // clear the interrpt control register
INTCON.TMR0IE = 1; // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
INTCON.TMR0IF = 0; // bit2 clear timer 0 interrupt flag
INTCON.GIE = 1; // bit7 global interrupt enable
while(1)
{
ADC_ADJ = ADC_READ(0);
ADC_ADJ = ADC_ADJ/1; // divide by 1 for maximum sensitivity @ 10 bit resolution
ADC_LUX = ADC_Read(1);
ADC_LUX = ADC_LUX/1;
if (ADC_LUX <= 540 && var1 == 0)
{
GPIO.GP5 = 1;
TCOUNT = 0;
}
if (TCOUNT >= 980 || ADC_LUX >= 710)
{
GPIO.GP5 = 0;
var1 = 1;
}
if (ADC_LUX >= 710)
{
var1 = 0;
}
}
}