adc problem,pic16f887

Thread Starter

iokh

Joined Jun 26, 2010
4
Hi,
I trying a simple program , interfacing two sensor (lm35)to pic 16f887 in an0 and an1 and display the temperature in lcd, the first sensor read right the second sensor gave me wrong read Pls help, thanks.





Here is my code :_
long temp_res;
char temp[12];
long tmp_res;
char tmp[12];
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main() {
ANSEL = 0b11111111; // Configure AN2 pin as analog
ANSELH = 0; // Configure other AN pins as digital I/O
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISA =0b11111111;
TRISE =0b11111111;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
for(;;) {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
tmp_res = ADC_Read(1); // Get 10-bit results of AD conversion
temp_res = (temp_res *500)/1023;
tmp_res = (temp_res *500)/1023;
longToStr(temp_res,temp);
lcd_out(1,1,"temp1 = ");
lcd_out(2,1,temp);
lcd_chr_cp('c') ;
delay_ms(2000);
longToStr(tmp_res,tmp);
lcd_out(1,1,"temp2 = ");
lcd_out(2,1,tmp);
lcd_chr_cp('c') ;
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);

}}
 

bertus

Joined Apr 5, 2008
22,923
Hello,

The fault is here:

Rich (BB code):
temp_res = ADC_Read(0);  // Get 10-bit results of AD conversion
               tmp_res = ADC_Read(1);   // Get 10-bit results of AD conversion
               temp_res =  (temp_res *500)/1023;
               tmp_res =  (temp_res *500)/1023;
This should probably be:

Rich (BB code):
temp_res = ADC_Read(0);  // Get 10-bit results of AD conversion
               tmp_res = ADC_Read(1);   // Get 10-bit results of AD conversion
               temp_res =  (temp_res *500)/1023;
               tmp_res =  (tmp_res *500)/1023;
Bertus
 
Last edited:
Top