#if defined(__PCM__)
#include <16F877a.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define LCD_EN PIN_E0
#define LCD_RS PIN_E1
#define LCD_RW PIN_E2
//LCD->R/S R/W EN D0 D1 D2 D3 D4 D5 D6 D7
//PIN->RE0 RE1 RE2 RD0 RD1 RD2 RD3 RD4 RD5 RD6 RD7
void lcd_write(char data);
void lcd_init();
void lcd_newline();
void clr_lcd();
void main()
{
int16 adc_value;
float volts;
delay_ms(10);
output_bit(LCD_RW,0);
delay_ms(100);
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
lcd_init();
delay_us(20);
while(1)
{
clr_lcd();
adc_value=read_adc();
volts = (int)(adc_value * 5)/1023.0;
printf(lcd_write,"\fVoltage=%3.2fV",volts);
delay_ms(1000);
if(volts<=5.00)
{
output_bit(pin_b1,1);//Green LED to indicate safety voltage
}
else
{
output_bit(pin_b2,1);//Red LED to indicate vulnerable voltage
}
}
delay_ms(1000);
output_bit(LCD_RW,0);
}
void lcd_init()
{
output_bit(LCD_RS,0);
delay_ms(10);
lcd_write(0x01);
lcd_write(0x38);
lcd_write(0x06);
lcd_write(0x0f);
lcd_write(0x80);
delay_ms(100);
output_bit(LCD_RS,1);
}
void clr_lcd()
{output_bit(LCD_RS,0);
delay_ms(10);
lcd_write(0x01);
output_bit(LCD_RS,1);
delay_ms(1);
}
void lcd_newline()
{
output_bit(LCD_RS,0);
delay_ms(10);
lcd_write(0xc0);
output_bit(LCD_RS,1);
delay_ms(1);
}
void lcd_write(char data)
{
output_d(data);
delay_ms(1);
output_bit(LCD_EN,1);
delay_ms(1);
output_bit(LCD_EN,0);
delay_ms(1);
}