problem reading temperature value using a pic16f917

Discussion in 'Embedded Systems and Microcontrollers' started by andyforeverest, Apr 29, 2012.

  1. andyforeverest

    Thread Starter New Member

    Nov 3, 2009
    1
    0
    Hello all,

    I have a Picdem Mechatronics board with a pic16f917. I am using HI-TEC compiler and trying to read temperature value. I get all sorts of wrong info displayed on the LCD.

    I am including the C software here.

    Any help will be appreciated.

    Andrei


    #include <htc.h>
    #include "lcd.h"

    __CONFIG(CP_OFF & CPD_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSCIO & MCLRE_ON & FCMEN_OFF & IESO_OFF);

    #define _XTAL_FREQ 8000000UL

    unsigned short readTemp()
    {
    unsigned char j;
    unsigned short tempADC;

    ADCON0 = 0b00000001;
    tempADC = 0;

    for(j=0; j<5; j++)
    {
    GO=1;
    while(nDONE) ;
    tempADC = tempADC + ADRESH;
    __delay_us(40);
    }//for
    tempADC = tempADC/5;
    tempADC = (tempADC/2) - 50;
    return tempADC;

    }

    void main (void)
    {
    unsigned short temp;
    unsigned char ones, tens;

    OSCCON = 0x70; // 8MHz operation
    TRISA0 = 1; //pin is input
    TRISD7 = TRISD6 = 0;//pins are output
    OPTION_REG = 0x87;
    ANSEL = 0b00000001;//AN0

    ADCON0 = 0b00000001;//
    ADCON1 = 0b01010000;//

    CMCON0 = 0x07;//comparatoare off

    lcd_init();
    lcd_clear();

    while (1)
    {
    temp = readTemp();

    if ((temp<10) && (temp>=1))
    {
    ones=(unsigned char)temp;
    RD6 = RD7 = 0;
    }

    else if ((temp <20) && (temp>=10))
    {
    tens = 1;
    ones = (unsigned char)(temp - 10);
    RD6 = 0;
    RD7 = 0;
    }

    else if ((temp <30) && (temp >=20))
    {
    tens = 2;
    ones = (unsigned char)(temp - 20);
    RD6 = 0;
    RD7 = 1;
    }

    else if ((temp <40) && (temp >=30))
    {
    tens = 3;
    ones = (unsigned char)(temp - 30);
    RD7 = 1;
    RD6 = 0;
    }

    else if ((temp <50) && (temp >=40))
    {
    tens = 4;
    ones = (unsigned char)(temp - 40);
    RD6 = 1;
    RD7 = 1;
    }

    lcd_d1(ones);
    lcd_d2(tens);

    }
    }
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Put your code listing in between {code} and {/code} tags.
    Replace the { } braces above with [ ] braces.

    Then indent your procedures and program structures for easier reading.
    Next, try to break your main( ) code into functional modules and test and debug each module one at a time.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <htc.h>
    4. #include "lcd.h"
    5.  
    6. __CONFIG(CP_OFF & CPD_OFF & BOREN_OFF & PWRTE_ON &  WDTE_OFF & FOSC_INTOSCIO & MCLRE_ON & FCMEN_OFF &  IESO_OFF);
    7.  
    8. #define _XTAL_FREQ 8000000UL
    9.  
    10. unsigned short readTemp()
    11. {    
    12. unsigned char j;
    13. unsigned short tempADC;
    14.  
    15. ADCON0 = 0b00000001;
    16. tempADC = 0;
    17.  
    18. for(j=0; j<5; j++)
    19. {
    20. GO=1;
    21. while(nDONE) ;
    22. tempADC = tempADC + ADRESH;
    23. __delay_us(40);
    24. }//for
    25. tempADC = tempADC/5;
    26. tempADC = (tempADC/2) - 50;
    27. return tempADC;
    28.  
    29. }
    30.  
    31. void main (void)
    32. {
    33. unsigned short temp;
    34. unsigned char ones, tens;
    35.  
    36.   OSCCON = 0x70; // 8MHz operation  
    37.   TRISA0 = 1; //pin is input
    38.   TRISD7 = TRISD6 = 0;//pins are output
    39.   OPTION_REG = 0x87;
    40.   ANSEL = 0b00000001;//AN0
    41.    
    42.   ADCON0 = 0b00000001;//
    43.   ADCON1 = 0b01010000;//
    44.    
    45.   CMCON0 = 0x07;//comparatoare off
    46.    
    47.   lcd_init();
    48.   lcd_clear();
    49.    
    50.   while (1)
    51.     {
    52. temp = readTemp();
    53.  
    54. if ((temp<10) && (temp>=1))
    55. {
    56. ones=(unsigned char)temp;
    57. RD6 = RD7 = 0;
    58. }
    59.  
    60. else if ((temp <20) && (temp>=10))
    61. {
    62. tens = 1;
    63. ones = (unsigned char)(temp - 10);
    64. RD6 = 0;
    65. RD7 = 0;
    66. }
    67.  
    68. else if ((temp <30) && (temp >=20))
    69. {
    70. tens = 2;
    71. ones = (unsigned char)(temp - 20);
    72. RD6 = 0;
    73. RD7 = 1;
    74. }
    75.  
    76. else if ((temp <40) && (temp >=30))
    77. {
    78. tens = 3;
    79. ones = (unsigned char)(temp - 30);
    80. RD7 = 1;
    81. RD6 = 0;
    82. }
    83.  
    84. else if ((temp <50) && (temp >=40))
    85. {
    86. tens = 4;
    87. ones = (unsigned char)(temp - 40);
    88. RD6 = 1;
    89. RD7 = 1;
    90. }    
    91.  
    92. lcd_d1(ones);  
    93. lcd_d2(tens);
    94.  
    95.    }
    96. }        
    97.  
    98.  
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The section with tempADC:
    If ADRESH is 8 bit then its maximum value is 255. You then divide by 10, subtract 50 then repeat. I'm pretty sure that's going to give some unusual numbers.
     
    Last edited: Apr 29, 2012
Loading...