#include <main.h>										// include the main.h file
#define LCD_ENABLE_PIN PIN_D2							// pin definitions for the LCD
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>										// include the lcd.c file pin definitions must be above this

#define delay 400

unsigned int value[5];									// readings of ADCs
unsigned long int sum=0;
unsigned long long int product=0;
unsigned int onesec;
unsigned onesec_flag=0;
unsigned long int freq=0,frequency;
unsigned int mins=59,secs=40,hrs=23;


unsigned int adc_reading(unsigned int);					// function to determine the adc values send adc channel return adc value

void payload1(void);
void payload2(void);
void payload3(void);
void display_vals(void);
void calcs(void);
void frequency_counter(void);
void clock(void);

#INT_TIMER2
void  TIMER2_isr(void) 
{
	onesec++;
	if(onesec==40) {onesec=0;onesec_flag=1;frequency=freq;freq=0;}
}


#INT_EXT
void  EXT_isr(void) 
{
	freq++;
}





void main()		
{	


	setup_timer_2(T2_DIV_BY_16,240,13);		//1.9 ms overflow, 25.0 ms interrupt

													// start pof main function (program start)
	unsigned int q=0,val;									// define local variable q as unsigned int
	setup_adc_ports(AN0_TO_AN3);						// setup adc ports 0 to 3
	setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);		// setup the adc clock
	enable_interrupts(INT_TIMER2);
	enable_interrupts(INT_EXT);
	enable_interrupts(GLOBAL);


	lcd_init();											// initiate LCD

	while(TRUE)											// start endless loop main program loop
		{		
			value[0]=adc_reading(0);					// adc conversion on channel 0
			value[1]=adc_reading(1);					// adc conversion on channel 1
			value[2]=adc_reading(2);					// adc conversion on channel 2
			value[3]=adc_reading(3);					// adc conversion on channel 3
			payload1();									// call payload 1
			payload2();
			if(input(PIN_B0)) payload3();				// call payload 3 if button 1(0) pressed.
			if(input(pin_b5)) display_vals();			// if pin_b5 high - call display vals
			if(input(pin_b6)) calcs();
			if(input(pin_b7))  frequency_counter();
			clock();

		}												// end point of the main program loop 
	 
}


void clock(void)
	{
		if(onesec_flag)
			{
				onesec_flag=0;
				secs++; if (secs==60){secs=0;mins++;}
				if (mins==60) {mins=0; hrs++;}
				if (hrs==24) {hrs=0;}
				if(input(pin_b4))		
						{
							lcd_gotoxy(1,1);
							printf(lcd_putc,"Hrs - %2u ", hrs);
							lcd_gotoxy(1,2);
							printf(lcd_putc,"MINs- %2u ", mins);
							lcd_gotoxy(21,1);
							printf(lcd_putc,"Secs- %2u ", secs);
						}
			}
	}
void calcs(void)
	{
		sum=value[0];
		sum+=value[1];
		sum+=value[2];
		sum+=value[3];

		product=value[0];
		product*=value[1];		
		product*=value[2];
		product*=value[3];
		printf(lcd_putc,"\f");
		printf(lcd_putc,"sum = %6Lu",sum);
		lcd_gotoxy(21,1);
		printf(lcd_putc,"prod = %10Lu",product);
		lcd_gotoxy(21,2);
		printf(lcd_putc,"vals %3u %3u %3u %3u",value[0],value[1],value[2],value[3]);
		delay_ms(1500);	
		printf(lcd_putc,"\f");
	}


void frequency_counter(void)
	{
		if(onesec_flag)
			{


				onesec_flag=0;

				printf(lcd_putc,"\f Freq = %5luHz", frequency);
				frequency=0;
			}
	}


void display_vals(void)
	{
		if(value[3]>127)												// if conversion 3 > 127
			{
					Lcd_gotoxy(1,1);									// gotoxy 1,1 
					printf(lcd_putc,"val1 = %3u", value[0]);			// print channel 0 conversion
					Lcd_gotoxy(1,2);									// gotoxy 1,2 
					printf(lcd_putc,"val2 = %3u", value[1]);			// print channel 1 conversion
					Lcd_gotoxy(21,1);									// gotoxy 21,1 
					printf(lcd_putc,"val3 = %3u", value[2]);			// print channel 2 conversion
					Lcd_gotoxy(21,2);									// gotoxy 21,2 
					printf(lcd_putc,"val4 = %3u", value[3]);			// print channel 3 conversion
			}
	}

void payload3(void)
	{
		unsigned int count=0,done=0;									// define local variables
		output_low(pin_e1);												// output low pin e1

		while(!done)													// while done =0
			{
				if(count<value[2]) output_high(PIN_E0);					//if count < conversion channel 2 output high pin E0
				else output_low(PIN_E0);								//otherwise output low E0
				count++;												// increment count
				if(count==255) count=0;									//if count = 255 reset count to zero
				if(!input(pin_b0)) done=1;								// if pin B0 is low set done to 1
			}

		output_low(pin_E0);												// terminate the PWM signal
	}


void payload2(void)
	{																						// payload 2
		if((value[1]>119) && (value[0]<173)){ output_low(pin_C2);output_low(PIN_C4); }
		if(value[1]<119) { output_high(pin_C2);output_low(PIN_C4); }
		if(value[1]>173) { output_low(pin_C2);output_high(PIN_C4); }

	}

void payload1(void)
	{																						// payload 1
		if(value[0]==127){ output_low(pin_C0);output_low(PIN_C1); }
		if(value[0]<127) { output_high(pin_C0);output_low(PIN_C1); }
		if(value[0]>127) { output_low(pin_C0);output_high(PIN_C1); }

	}

unsigned int adc_reading(unsigned int channel)												// read the adcs
	{
		set_adc_channel(channel);															// select channel
		delay_us(30);																		// wait
		return(read_adc());																	// return the value of the conversion
	}



