#include "18F2520.h"
#include "f2520_regs.h"
#fuses INTRC_IO // Internal clock
#use delay(clock=4000000) // 4MHZ
#define RS PIN_A2 // LCD RS pin connected to uC PIN_A2
#define EN PIN_A1 // LCD EN pin connected to uC PIN_A1
void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
unsigned int16 low_byte,high_byte;
unsigned int16 full_value;
void main()
{
lcd_init(); // lcd initialization
TRISA = 0b00100000; // RA5 -input, rest as output
ADCON0 = 0b00010000; // analog channel 4, A/D on
ADCON1 = 0b00001010; // ref volt. VSS and VDD. AN4 to AN0 as analog
//ADCON2 = 0b10110100; // Right justified, 16 TAD, 1/4Tosc
//ADCON2 = 0b10110101; //FOSC/16
ADCON2 = 0b10111100;
ADON = 1;
while(1)
{
delay_us(10);
GO = 1; // to start conversion
while(GO==1); // wait for bit to be cleared
low_byte = ADRESL;
high_byte = ADRESH;
full_value = ((high_byte<<8)|low_byte);
lcd_cmd(0x80);
printf(lcd_data,"%4lu",full_value);
delay_ms(100);
}
}
void lcd_init()
{
lcd_cmd(0x30); // Configure the LCD in 8-bit mode, 1 line and 5x7 font
lcd_cmd(0x0c); // display on and cursor off
lcd_cmd(0x01); // clear display screen
lcd_cmd(0x06); // increment cursor
lcd_cmd(0x80); // set cursor to 1st line
}
void lcd_cmd(unsigned char c)
{
output_b(c);
output_low(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}
void lcd_data(unsigned char z)
{
output_b(z);
output_high(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}