adc problem,pic16f887

Discussion in 'The Projects Forum' started by iokh, Feb 19, 2011.

  1. iokh

    Thread Starter New Member

    Jun 26, 2010
    4
    0
    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.

    [​IMG]

    [​IMG]

    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);

    }}
     
  2. bertus

    Administrator

    Apr 5, 2008
    15,638
    2,343
    Hello,

    The fault is here:

    Code ( (Unknown Language)):
    1.  
    2. temp_res = ADC_Read(0);  // Get 10-bit results of AD conversion
    3.                tmp_res = ADC_Read(1);   // Get 10-bit results of AD conversion
    4.                temp_res =  (temp_res *500)/1023;
    5.                tmp_res =  (t[COLOR=Red]e[/COLOR]mp_res *500)/1023;
    6.  
    This should probably be:

    Code ( (Unknown Language)):
    1.  
    2. temp_res = ADC_Read(0);  // Get 10-bit results of AD conversion
    3.                tmp_res = ADC_Read(1);   // Get 10-bit results of AD conversion
    4.                temp_res =  (temp_res *500)/1023;
    5.                tmp_res =  (tmp_res *500)/1023;
    6.  
    Bertus
     
    Last edited: Feb 19, 2011
  3. iokh

    Thread Starter New Member

    Jun 26, 2010
    4
    0
    thank you . it worked
     
Loading...