pic16f913 and ds18b20

Discussion in 'Programmer's Corner' started by climb123, Jan 24, 2013.

  1. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    Hello, I have a problem with a temperature sensor ds18b20. How can I display the temperature on a LCD display?

    #include <htc.h>
    #ifndef _XTAL_FREQ
    #define _XTAL_FREQ 8000000
    #endif
    #include "lcd.h"


    #define Skip_ROM 0xCC
    #define Convert_T 0x44
    #define Read_scratchpad 0xBE

    #define Port_18B20 RA0
    #define Tx_18B20 TRISA0 = 0
    #define Rx_18B20 TRISA0 = 1
    unsigned char purva;
    unsigned char vtora;
    unsigned char treta;
    unsigned char y;
    unsigned char reset();
    void write(char WRT);
    unsigned char read();


    int main(void)
    {
    unsigned temp;
    unsigned short tempL, tempH, fraction;
    unsigned int i = 0;


    lcd_init();
    lcd_goto(0);
    lcd_puts("DS18B20"); //LCD display for the starting part
    lcd_goto(0x40);
    lcd_puts("Degree= ");
    while(1) // create an infinite loop
    {

    if(!reset())
    {
    write(Skip_ROM);
    write(Convert_T);
    __delay_ms(750);

    reset();
    write(Skip_ROM);
    write(Read_scratchpad);

    tempL = read(); //read low temp value
    tempH = read(); //read high temp value
    i=((unsigned int)tempH << 8 ) | (unsigned int)tempL; //put both value in one variable

    y=i%100;
    purva=(i-y)/100;
    treta=i%10;
    vtora=(y-treta)/10;
    lcd_goto(0x40);

    }
    }
    while(1) continue;
    }


    unsigned char reset()
    {
    Tx_18B20; // Tris = 0 (output)
    Port_18B20 = 0; // set pin# to low (0)
    __delay_us(480); // 1 wire require time delay
    Rx_18B20; // Tris = 1 (input)
    __delay_us(60); // 1 wire require time delay

    if (Port_18B20 == 0) // if there is a presence pluse
    {
    __delay_us(480);
    return 0; // return 0 ( 1-wire is presence)
    }
    else
    {
    __delay_us(480);
    return 1; // return 1 ( 1-wire is NOT presence)
    }
    }

    void write(char WRT)
    {
    char i,Cmd;
    Cmd=WRT;
    Rx_18B20; // set pin# to input (1)

    for(i = 0; i < 8; i++)
    {
    if((Cmd & (1<<i))!= 0)
    {
    // write 1
    Tx_18B20; // set pin# to output (0)
    Port_18B20 = 0; // set pin# to low (0)
    __delay_us(1); // 1 wire require time delay
    Rx_18B20; // set pin# to input (release the bus)
    __delay_us(60); // 1 wire require time delay
    }
    else
    {
    //write 0
    Tx_18B20; // set pin# to output (0)
    Port_18B20 = 0; // set pin# to low (0)
    __delay_us(60); // 1 wire require time delay
    Rx_18B20; // set pin# to input (release the bus)
    }
    }
    }

    unsigned char read()
    {
    char i,result = 0;
    Rx_18B20; // TRIS is input(1)
    for(i = 0; i < 8; i++)
    {
    Tx_18B20; // TRIS is output(0)
    Port_18B20 = 0; // genarate low pluse for 2us
    __delay_us(2);
    Rx_18B20; // TRIS is input(1) release the bus
    if(Port_18B20 != 0)
    result |= 1<<i;
    __delay_us(60); // wait for recovery time
    }
    return result;
    }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Zero: Learn to use code tags

    One: get temperature reading

    Two: Convert reading to a string

    Three: Send string to LCD
     
  3. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    y=i%100;
    purva=(i-y)/100;
    treta=i%10;
    vtora=(y-treta)/10;

    Here I convert 2 byte in a number, but i don't know how to convert to a string.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    either add 0x30 to the integer value 0-9, or use itoa()....
     
  5. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    http://pic.hitechworld.org/data/hitachi_lcd.html

    Have a look my LCD code. It is 4bit, but maybe it is of use to you since you can see how I handle strings. And I also have a putchar function.

    All this LCD stuff is a lot of detail work, and you need to work this out for yourself. No one can know what's best for you.

    Other can show you their code, but they can not do the work for you.

    If you can not convert an unsigned char into a C string you need to learn more C at first.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Begin at the beginning.First get a message to display on the LCD. Don't worry about the temperature. For example display the classic Hello World. Once you have that you can move on.
     
  7. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    I know how to write on LCD, but a can't show the temp, when I write
    y=i%100;
    purva=(i-y)/100;
    treta=i%10;
    vtora=(y-treta)/10;
    t1=(0x30+purva);
    t2=(0x30+vtora);
    t3=(0x30+treta);
    lcd_putch("t1");
    lcd_putch("t2");
    lcd_putch("t3");
    the lcd show ||
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Then your LCD is not working because you are literally displaying t1t2t3.

    Try

    lcd_putch(t1);
    lcd_putch(t2);
    lcd_putch(t3);
     
  9. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    I already try this. I write
    but it fills the entire LCD with "B"
     
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Look at the itoa() function, not sure which library it might be in, Integer to ASCII function, search your library reference.
     
  11. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    This is the code for lcd, but it show me only 000. Is anyone working with this sensor to help me with the program?

    buffer[0]=i/100+'0';
    buffer[1]=(i%100)/10+'0';
    buffer[2]=i %10+'0';
    buffer[3]=0;
    lcd_init();
    lcd_goto(0);
    lcd_puts("DS18B20"); //LCD display for the starting part
    lcd_goto(0x40);
    lcd_puts(buffer);
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Does the LCD display the "ds18b20" string correct. Also you have been told to use the itoa() function. Please look in the Hi-Tech C install folder. Here you will find a folder named docs. And inside here you will find a nice manual. Look up the itoa() function. And no more nonsense please
     
  13. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    I use itoa(), but LCD show me only 0
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    First show how you use the itoa function. Second then we ask question in order to help you. It is rude to just ignore those questions. So once more. Can you confirm that this line do as it should.
    Code ( (Unknown Language)):
    1. lcd_puts("DS18B20"); //LCD display for the starting part
    2.  
     
  15. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    Sorry. Yes lcd_puts("DS18B20") displayed correctly.
    This is the code for itoa
    Code ( (Unknown Language)):
    1. int main(void)
    2. {  
    3.        
    4.     unsigned temp;
    5.     unsigned short tempL, tempH, fraction;
    6.     unsigned int i = 0 ;   
    7.     unsigned char buffer [20];
    8.     lcd_init();
    9.     lcd_goto(0);
    10.     lcd_puts("DS18B20");  //LCD display for the starting part
    11.     lcd_goto(0x40);
    12.     itoa(buffer,i,20);
    13.     lcd_puts(buffer);
    14.     __delay_ms(5000);
    15.  
    16.  
    17.     while(1)    // create an infinite loop
    18.     {      
    19.             if(!reset())
    20.             {
    21.             write(Skip_ROM);       
    22.             write(Convert_T);      
    23.             __delay_ms(1000);
    24.            
    25.             reset();
    26.             write(Skip_ROM);       
    27.             write(Read_scratchpad);
    28.             tempL = read();         //read low temp value
    29.             tempH = read();         //read high temp value
    30.         i=((unsigned int)tempH << 8 ) | (unsigned int)tempL;        //put both value in one variable
    31.  
    32.             lcd_goto(0x40);
    33.  
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Hi try this
    Code ( (Unknown Language)):
    1.  
    2. itoa(buffer,12345,10);
    3.  
    The prototype fro the itoa function is
    Code ( (Unknown Language)):
    1.  
    2. char * itoa (char * buf, int val, int base)
    3.  
    You got the two first correct. But the int base. Shall be set to the number system you want. Use 10 for decimal number, and 16 for hexadecimal numbers. I think 8 or 2 will give you octal or binary numbers. If the test line work. You can suspect something is wrong with the reading of the temperature sensor. Will you be able to use the hardware debugger in MPLAB?
     
  17. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    Yes, test line work, but again show me 0, so the reading of temperature is wrong. I haven't debugger.
     
  18. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    If you have a PICKIT 2 or 3 you will be able to do hardware debugging on that chip
     
  19. climb123

    Thread Starter Member

    Jan 24, 2013
    30
    0
    I have pickit3 on sure electronics, but when i try to connect to MPLAB can't find my device, so i can't use debuger
     
  20. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Try hard-coding your output to be displayed to the LCD. Instead of reading the sensor, give the part of your code that formats your data for output something like 1234, that way, you can work on just getting your LCD formatting right.
     
Loading...