#include <main.h>
#define LCD_ENABLE_PIN PIN_A5
#define LCD_RS_PIN PIN_C0
#define LCD_RW_PIN PIN_C1
#define LCD_DATA4 PIN_C2
#define LCD_DATA5 PIN_C3
#define LCD_DATA6 PIN_C4
#define LCD_DATA7 PIN_C5

#include <lcd.c>

unsigned int a=50,b=0,c=0;						// define global variables

void main()
{
	setup_adc_ports(sAN0|sAN2);
	setup_adc(ADC_CLOCK_INTERNAL);

	 lcd_init();
   


	while(TRUE)							// start main while loop
	{

		  set_adc_channel(0);					// setup ADC channel 0
		  delay_us(20);   					// wait 20mS
		  b = read_adc();					// get ADC conversion
		  lcd_gotoxy(1,2);					// decide where to put the next character
		  printf(lcd_putc,"ADC Conv = %3u", b);			// print adc cov and value of b
		  
		  set_adc_channel(2);					// setup ADC channel 0
		  delay_us(20);   					// wait 20mS
		  c = read_adc();					// get ADC conversion
		  lcd_gotoxy(21,2);					// decide where to put the next character
		  printf(lcd_putc,"ADC Conv = %3u", c);			// print adc cov and value of b	
	
	
	       if(input(PIN_A1))	       				// is the button pressed
		     {							// yes so carry out routine
			while(input(PIN_A1));				// is the pin release - no wait  yes continue 
			lcd_gotoxy(1,1);
			printf(lcd_putc,"Hello World %3u", a);		// print hello world and value of A to LCD
			if(b)
			   a++;
			else
			   a--;						// increment a

		     }							// routine for if end

		if((b>60) && (c<50))					// if a> 60 and b>128 turn on led and print led on on line 3
		     {	
			output_high(LED);				// turn on led
			lcd_gotoxy(21,1);				// print on line 3
			printf(lcd_putc,"LED is ON  ");			// print LED ON
		     }
		else
		     {
			output_low(LED);				// tunr off led
			lcd_gotoxy(21,1);				// print on line 3
			printf(lcd_putc,"LED is Off ");			// print LED OFF		
		     
		     }
		     


	}								// end main while loop

}
