#include <main.h>


unsigned int a=50;					//define variables.
unsigned int b=100,f=75;
unsigned long int c=0,d=0;
unsigned int flag=0;
unsigned int x=1;

unsigned int pot1=0;
unsigned int light=0;
unsigned int leds = 0;									//define variables end

#INT_TIMER2
void  TIMER2_isr(void) 
{
    a--;												// increment a in the interrupt routine half second
	if(!a)	flag+=0x02;
	b--;
	if(!b)  flag+=0x01;									// enable flag bit 0x01 and 0x08  1 sec
	f--;
	if(!f)  flag+=0x0c;									// increment a in the interrupt routine 750mS

	d++;
}


void func1(unsigned int);			// prototypes for our functions
void func2();
void func3();
void func4();
void func5();
									// prototypes for our functions end
#define LCD_ENABLE_PIN PIN_B2
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_DATA4 PIN_B4
#define LCD_DATA5 PIN_B5
#define LCD_DATA6 PIN_B6
#define LCD_DATA7 PIN_B7

#include <lcd.c>



void main()
{
	setup_adc_ports(AN0_TO_AN1);					// select a0 & a1 as analogue
	setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
	setup_timer_2(T2_DIV_BY_16,178,14);				//716 us overflow, 10.0 ms interrupt


	enable_interrupts(INT_TIMER2);					// enable timer 2 overflow interrupt
	enable_interrupts(GLOBAL);						// enable global interrupts
	
   lcd_init();										// initiate LCD

	while(TRUE)										// Main program while loop
	{

		if(flag & 0x01) func1(1);					// if flag 0x01 set - call our function 1
		if(flag & 0x02) func2();					// if flag 0x02 set - call our function 2
		if(flag & 0x04) func3();					// if flag 0x02 set - call our function 3

		func4();
		if(flag & 0x08) {
							if((pot1>128) && (input(Pin_A2)) && (light <=25))	{ func5();}					// flash led
							else {output_high(Pin_A4); leds=0;}	
							flag-=8;									// flash led off
						}

	}												// main program while loop end
	

}													// END PROGRAM

void func1(unsigned int x)
			{
					lcd_gotoxy(x,1);								// got lcd position ready for next character
					printf(lcd_putc," Hello World %3Lu",c);			// print to lcd clear screen and hello world and the value of c
					c++;											// increment c
					b=100;
					flag-=1;

			}

void func2()
			{
					lcd_gotoxy(21,2);									// got lcd position ready for next character
					printf(lcd_putc,"%6Lu  %4Lx",d,d);					// print to lcd the value of d in decimal and Hex
					printf("%Lu  %Lx\n\r",d,d);							// print to the virtual terminal the value of d in decimal and Hex
					a=50;
					flag-=2;
			}

void func3()
			{															// read analogue inputs
				set_adc_channel(0);
				delay_us(20);
				pot1 = read_adc();
				set_adc_channel(1);
				delay_us(20);
				light = read_adc();
				f=75;
				flag-=4;
			}
void func4()
			{															// display analog values on screen			
					lcd_gotoxy(1,2);									// got lcd position ready for next character
					printf(lcd_putc,"pot=%3U  lit=%3u",pot1,light);		// print to lcd clear screen and hello world and the value of c
			}

void func5()
			{
				if(leds)	{output_low(Pin_A4); leds=0;} 						// switch on led
				else		{output_high(Pin_A4); leds=1;}						//switch off led

			}
