Reading 2 channel on 16F877
hi,
Please post your FULL program listing.
Use the 'Manage Attachments' button lower down the REPLY window.
Code is as follows
void main(void)
{
unsigned short adc_value;
unsigned short number_in_thousands_place, number_in_hundreds_place, number_in_tens_place, number_in_unit_place;
unsigned short remaining_three_digit_number, remaining_two_digit_number;
GIE=0; // we don't want interrupts
init_a2d(); // Initialise A/D Module
lcd_init(); // Initialise LCD
lcd_clear(); // Clear LCD
for(;
{
adc_value = read_a2d(0);
DelayMs(40);
lcd_goto(0); // Select first line
lcd_puts("VAL1:");
number_in_thousands_place = (adc_value / 1000);
remaining_three_digit_number = (adc_value % 1000);
number_in_hundreds_place = (remaining_three_digit_number / 100);
remaining_two_digit_number = (remaining_three_digit_number % 100);
number_in_tens_place = (remaining_two_digit_number / 10);
number_in_unit_place = (remaining_two_digit_number % 10);
lcd_putch(number_in_thousands_place | 0x30);
lcd_putch(number_in_hundreds_place | 0x30);
lcd_putch(number_in_tens_place | 0x30);
lcd_putch(number_in_unit_place | 0x30);
adc_value = read_a2d(1);
DelayMs(40);
lcd_goto(0x40); // Select Second line
lcd_puts("VAL2:");
number_in_thousands_place = (adc_value / 1000);
remaining_three_digit_number = (adc_value % 1000);
number_in_hundreds_place = (remaining_three_digit_number / 100);
remaining_two_digit_number = (remaining_three_digit_number % 100);
number_in_tens_place = (remaining_two_digit_number / 10);
number_in_unit_place = (remaining_two_digit_number % 10);
lcd_putch(number_in_thousands_place | 0x30);
lcd_putch(number_in_hundreds_place | 0x30);
lcd_putch(number_in_tens_place | 0x30);
lcd_putch(number_in_unit_place | 0x30);
DelayMs(100);
}
}
void init_a2d(void)
{
TRISA = 0x1F; // RA0 - RA4 input , RA5 output - (is used for LCD)
ADCON0=0; // select Fosc/2
ADCON1=0x82; // select Right justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}
unsigned short read_a2d(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)
continue;
return ((ADRESH << 8) | ADRESL); // return 10 bit result
}
LCD functions work fine. I am separating each decimal value and displaying it on the LCD
I am using 4Mhz Oscillator
Kindly let me know what might be the possible error.