PIC16F1619 connecting LM35 temperature-sensor to LCD

Discussion in 'Embedded Systems and Microcontrollers' started by Cliff Karlsson, May 16, 2017.

  1. Cliff Karlsson

    Thread Starter New Member

    May 16, 2017
    4
    0
    I am really new to PIC microcontrollers and have purchased a Microship curiosity board http://www.microchip.com/Developmenttools/ProductDetails.aspx?PartNO=DM164137 which uses a PIC16F1619 microcontroller. I managed to get the value of a potentiometer displayed in a LCD using the code below. But I want to connect a LM35 temperature-sensor http://www.ti.com/lit/ds/symlink/lm35.pdf to the board and show the temperature instead of the potentiometer.

    I have changed this part:

    TRISCbits.TRISC3 = 1; //Sensor is connected to RC3...set as input
    ANSELCbits.ANSC3 = 1; //analog
    ADCON0 = 0b00011101; //select RC3 as source of ADC and enable the module (AN7)

    But I guess that I need to alter the sketch some more. Can anyone help me with the code for adding the sensor and showing it in the lcd?

    Code (Text):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <htc.h>
    4. #define _XTAL_FREQ 500000
    5.  
    6. #define E_bit PORTAbits.RA0
    7. #define RS_bit PORTAbits.RA2
    8. #define CLEAR 0x01
    9. #define ADDRESS 0x80
    10.  
    11. void init (void);
    12. void init_display(void);
    13. void print_char(char data);
    14. void print_instr(char instr);
    15. void print_text(const char *data);
    16. void EE(void);
    17. void E(void);
    18.  
    19.  
    20. void main(void)
    21.     {
    22.     unsigned char digits[5];
    23.     unsigned int adcval;
    24.  
    25.     init();
    26.     print_instr(CLEAR);
    27.  
    28.     TRISCbits.TRISC0 = 1;         //Potentiometer is connected to RC0...set as input
    29.     ANSELCbits.ANSC0 = 1;         //analog
    30.     ADCON0 = 0b00010001;          //select RC0 as source of ADC and enable the module (AN4)
    31.     ADCON1 = 0b00010000;          //left justified - FOSC/8 speed - Vref is Vdd
    32.  
    33.  
    34.     for(;;)
    35.         {
    36.     adcval = adc();             //grab the top 8 MSbs
    37.         __delay_ms(5);             //delay for AT LEAST 5ms
    38.         digits[0] = adcval / 1000 + 48;
    39.         digits[1] = (adcval / 100) % 10 + 48;
    40.         digits[2] = (adcval / 10) % 10 + 48;
    41.         digits[3] = adcval % 10 + 48;
    42.         digits[4] = '\0';
    43.  
    44.         print_text(digits);
    45.  
    46.      
    47.         }
    48.     }
    49.  
    50.     int adc(void) {
    51.     __delay_us(5);                //wait for ADC charging cap to settle
    52.     GO = 1;
    53.     while (GO) continue;          //wait for conversion to be finished
    54.  
    55.     return (ADRESH << 8) | ADRESL;              
    56. }
    57.  
    58.  
    59. void init (void)
    60.     {
    61.     LATA=0;
    62.     LATB=0;
    63.     PORTA=0;
    64.     PORTB=0;
    65.     ANSELA=0;
    66.     ANSELB=0;
    67.     TRISA=0;
    68.     TRISB=0;
    69.     init_display();
    70.     }
    71.  
    72.  
    73. void init_display(void)
    74.     {
    75.         // See the datasheet flow chart for the procedure below
    76.         // This part initiates the LCD in 4-bit mode...
    77.                 __delay_ms(25);
    78.         PORTB=0x30;
    79.         EE();
    80.         PORTB=0x30;
    81.         EE();
    82.         PORTB=0x30;
    83.         EE();
    84.         PORTB=0x20;
    85.         EE();
    86.  
    87.         // Set LCD properties...
    88.         print_instr(0x28);    // 2: Dual line (even though it is single line)
    89.         print_instr(0x0C);    // 3: Display on, cursor, blink
    90.         print_instr(CLEAR);    // 4: Clear
    91.         print_instr(0x06);    // 5: Entry mode set
    92.  
    93. // LCD is now initialized...
    94.     }
    95.  
    96. /*******************************************/
    97. // Print chars to LCD with 4-bit method
    98. /*******************************************/
    99. void print_char(char data)
    100.     {
    101.         //PORTC = (data >> 4) & 0x0F;
    102.                 PORTB = data & 0xF0;
    103.         RS_bit=1; // RS
    104.         E();
    105.  
    106.         //PORTC = data & 0x0F;
    107.                 PORTB = (data << 4) & 0xF0;
    108.         RS_bit=1; // RS
    109.         E();
    110.                 PORTB=0;
    111.                 PORTA=0;
    112.     }
    113.  
    114. /*******************************************/
    115. // Print instruction to LCD with 4-bit method
    116. /*******************************************/
    117. void print_instr(char instr)
    118.     {
    119.         //PORTC = (instr >> 4) & 0x0F;
    120.                 PORTB = instr & 0xF0;
    121.         E();
    122.  
    123.         //PORTC = instr & 0x0F;
    124.                 PORTB = (instr << 4) & 0xF0;
    125.         E();
    126.                 PORTB=0;
    127.                 PORTA=0;
    128.     }
    129.  
    130. /*******************************************/
    131. // Toggle E to execute command/instruction
    132. // With a litle bit longer delay than std.
    133. /*******************************************/
    134. void EE(void)
    135.     {
    136.         __delay_ms(5);
    137.         E_bit=0;
    138.         __delay_ms(5);
    139.         E_bit=1;
    140.         __delay_ms(5);
    141.         E_bit=0;
    142.         __delay_ms(5);
    143.     }
    144.  
    145. /*******************************************/
    146. // Toggle E to execute command/instruction
    147. /*******************************************/
    148. void E(void)
    149.     {
    150.         __delay_us(25);
    151.         E_bit=0;
    152.         __delay_us(25);
    153.         E_bit=1;
    154.         __delay_us(25);
    155.         E_bit=0;
    156.         __delay_us(25);
    157.     }
    158.  
    159. void print_text(const char *data)
    160.     {
    161.     char i=0;
    162.     while(data[i])
    163.         {
    164.         print_char(data[i++]);
    165.         }
    166.     print_instr(ADDRESS+0x60);
    167.     i=0;
    168.     while(data[i]&&(i<8))
    169.         {
    170.         print_char(data[i++]);
    171.         }
    172.     if (i==8)
    173.         {
    174.         print_instr(ADDRESS+0x40);
    175.         while(data[i])
    176.             {
    177.             print_char(data[i++]);
    178.             }
    179.         }
    180.  
    181.     }
     
  2. paulfjujo

    Member

    Mar 6, 2014
    23
    4
    • Hello,
    • to separate pending problems
    • did you allready test the display function like this ?
    you want to use a LM35 0mV at 0°C then 10mV/°C
    so you will get only 200mV at 20°C

    with LM335 you will get 2730mV at 0°C ( because calibrated in °Kelvin)
    and 2930mV at 20°C

    Wich one sensor ?
    Can this PIC use a internal +Vref for ADC ?

    • Code (Text):
      1.  
      2. [*]
      3. for( adcval=0; adcval<1023; adcval++)
      4.         {
      5.    // adcval = adc();             //grab the top 8 MSbs
      6.         __delay_ms(5);             //delay for AT LEAST 5ms
      7.         digits[0] = adcval / 1000 + 48;
      8.         digits[1] = (adcval / 100) % 10 + 48;
      9.         digits[2] = (adcval / 10) % 10 + 48;
      10.         digits[3] = adcval % 10 + 48;
      11.         digits[4] = '\0';
      12.  
      13.         print_text(digits);
      14.  
      15.      
      16.         }
      17.  
      18.  
      19. [*]
     
Loading...