/* Calculate upper- and lower-limits */
#define LOWERLIMIT ((15*1023)/50) /* 1.5 volt limit */
#define UPPERLIMIT ((45*1023)/50) /* 4.5 volt limit */
/* Variable to hold adc-value.
I assume 10-bit ADC which gives values from 0 to 1023 */
unsigned int adcvalue;
void main(void)
{
/* Setup the hardware */
input(PIN_A0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports( ALL_ANALOG );
/* Infinite loop is the heart of every embedded program */
while(1) {
/* Read adc channel 0,
I assume these functions do what the name suggests */
set_adc_channel(0);
adcvalue = read_adc();
/* Control the relay */
if (adcvalue < LOWERLIMIT) {
/* Do whatever needs to be done when at low voltage */
output_high(PIN_C0); /* Set pin C0 high, this should switch ON the relay */
}
else if (adcvalue > UPPERLIMIT) {
/* Do whatever needs to be done when at high voltage */
output_high(PIN_C0); /* Set pin C0 high, this should switch ON the relay */
}
else {
/* Do whatever needs to be done when between low and high levels */
output_low(PIN_C0); /* Set pin C0 low, this should switch OFF the relay */
}
} /* Go back to while(1) */
}