PIC16F877A Example --- DS18B20

Publisher:BeaLaity0170Latest update time:2015-11-17 Source: eefocusKeywords:PIC16F877A  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include

__CONFIG(0x3F32); //Chip configuration word

 

 

#define LCDRS    RB2

#define LCDRW    RB1

#define LCDE     RB0

#define LCDDATA  PORTD

 

 

#define DS18B20  RE0          

#define TRIS_B20 TRISE0

 

 

void LCD1602_INIT(void);  

void WRITE_LCD_CMD (char cmd);

void WRITE_LCD_DATA (char data);

void LCD_Display(char *s);

void LCD_Display_location  (char line, char col);

 

void LCD1602_BUSY();

void US_delay ( int t);

 

void itoa10(unsigned char *buf, int i);

int strlen (const char *s);

void DS18B20_INIT();

void WRITE_DS18B20_CMD(char cmd);

float READ_DS18B20 ();

 

char bank1 DAT_BUFF1[]= "Temperature:";

 

char Found_DS18B20[]="DS1820 FOUND";

char DS18B20_NOT_Found[]="DS1820 NOT FOUND";

char bank1 DAT_BUFF2[]= "              ";

 

 

#define CLRLCD 0X01     //Clear screen command

#define LCDMOD 0X38     //8 bits, two lines, 5*7 dots

#define ROTATION 0X0F     //

#define CURMODE 0X06    //

#define ORG1   0X80     //LCD first row address

#define ORG2   0XC0     //LCD second row first address

 

 

#define SKIPROM 0XCC

#define READSCRACHPAD 0XBE

#define TCONVERT 0X44

#define DS18b20_RESET 10 

#define DSRECOVER 1

 

float temperature;

char bank1 atemperature[10];    // used for store ascii code of digital temperature */

 

void main()

{

    int len;

    ADCON1=0X07;               

 

    LCD1602_INIT();

     LCD_Display(DAT_BUFF1);

    LCDE=0;                    

     LCD_Display_location  (1,0);

     while (1)

     {

           DS18B20_INIT();

            WRITE_DS18B20_CMD(SKIPROM);

            WRITE_DS18B20_CMD(TCONVERT);

 

            TRIS_B20=0;

            DS18B20=0;

            TRIS_B20=1;

            while(1)

           { //Judge whether the conversion is completed

            if (DS18B20)  break; 

            else  continue;   

            }

           DS18B20_INIT();

            WRITE_DS18B20_CMD(SKIPROM);

            WRITE_DS18B20_CMD(READSCRACHPAD);

            temperature=READ_DS18B20();            

            itoa10(atemperature, (int) (temperature * 10));

                                                            // so use temperature * 10 to enlarge the temperature

            len=strlen(atemperature);

            atemperature[len]=atemperature[len-1];

            atemperature[len-1]='.';

            atemperature[len+1]=0;

           

            LCD_Display_location  (0,0);

           LCD_Display (DAT_BUFF1);                      //LCD first line displays Tempereture: characters

            LCD_Display_location  (1,0);

            LCD_Display (DAT_BUFF2);    

            LCD_Display_location  (1,2);

           LCD_Display (atemperature);                   //The second line of LCD displays the current temperature value

            LCD_Display (DAT_BUFF2);

           US_delay(5000);                              //Read the temperature value every 0.5 seconds   

 }

}

 

int strlen(const char * s)

{

     const char *cp;

    cp = s;

     while(*cp++)

     continue;

     return cp-s-1;

}

 

void LCD_Display(char *s)

{

    int len;

     int atmp;

    len=strlen(s);

     for (atmp=0; atmp

     WRITE_LCD_DATA(s[atmp]);

}

 

 

void LCD1602_BUSY()

{

     // char tmpbusy;

     TRISB &= 0xF8;

     TRISD=0XFF;

     LCDRS=0;

     LCDRW=1;

    LCDE=1;                   

     while(LCDDATA & 0X80);

}

 

 

void LCD_Display_location  (char line, char col)

{

     char pos;

     LCD1602_BUSY();

     pos = line * 0X40 + 0X80 + col;

     WRITE_LCD_CMD(pos);

}

 

void WRITE_LCD_DATA(char data)

{

    LCD1602_BUSY();

     TRISB & =0XF8;

     TRISD=0;

     LCDDATA=data;

     LCDRS=1;

     LCDRW=0;

    LCDE=0;

     asm("NOP");

    asm("NOP");

    LCDE=1;

}

 [page]

void WRITE_LCD_CMD(char cmd)

{

    LCD1602_BUSY();

    TRISB & =0XF8;

    TRISD=0;

     LCDDATA=cmd;

     LCDRS=0;

    LCDRW=0;

    LCDE=0;

    asm("NOP");

    asm("NOP");

    LCDE=1;

}

 

void LCD1602_INIT(void)

{

    LCD1602_BUSY();

     TRISB &= 0XF8;

    TRISD=0;

 

     WRITE_LCD_CMD(CLRLCD);

     WRITE_LCD_CMD(LCDMOD);

     WRITE_LCD_CMD(TURNON);

     WRITE_LCD_CMD(CURMODE);

     LCD_Display_location  (0,0);      

}

 

 

void DS18B20_INIT()

{

     TRIS_B20=0;

     DS18B20=0;

     US_delay(20);  

     TRIS_B20=1;  

     US_delay(10);   

}

 

 

void WRITE_DS18B20_CMD(char cmd)

{

     char tmp;

     char i;

    TRIS_B20=0;

     for(tmp=8;tmp>0;tmp--)

     {

          TRIS_B20=0;

           DS18B20= 0;

           asm ("NOP");

           asm ("NOP");

          asm ("NOP");

          asm ("NOP");

          asm ("NOP");

           if (cmd & 0x01)

          {

          TRIS_B20=1;         

           US_delay(1);         

           for (i=5;i>0;i--);

           }

           else

          {

           DS18B20=0 ;

           US_delay(1);

           for (i=5;i>0;i--);    

           TRIS_B20=1;

          }

           cmd=cmd/2;

    }

}

    

 

float READ_DS18B20 ()

{

     char tmp=0x01;

     float t;

     union

    {

     char c[2];

     int x;

     }temp;

     temp.x=0;

 

     while (tmp)

        // read first 8 bits

           TRIS_B20=0;

           DS18B20=0;

           asm("NOP");

           TRIS_B20=1;     

 

            if (DS18B20)    

            temp.c[0] |= tmp;

            tmp=tmp<<1;

            US_delay(2);

    }

     tmp=1;

    while (tmp)

        // read first 8 bits

           TRIS_B20=0;

           DS18B20=0;

           asm("NOP");

 

            TRIS_B20=1;       // release the bus  

           asm("NOP");

            if (DS18B20)      // "1" presented

            temp.c[1] |= tmp;

           tmp=tmp<<1;

           US_delay(2);

    }

     t=((float) temp.x)/16.0 ;

     return t;

}

 

 

void itoa10(unsigned char *buf, int i)

{

     unsigned int rem;

     unsigned char *s,length=0;

 

     s = buf;

     if (i == 0)

     *s++ = '0';

     else

    {

          if (i < 0)

         {

          *buf++ = '-';

         s = buf;

          i = -i;

         }

          while (i)

         {

          ++length;

         rem = i % 10;

         *s++ = rem + '0';

          i /= 10;

          }

          for(rem=0; ((unsigned char)rem)

         {

          *(buf+length) = *(buf+((unsigned char)rem));

          *(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));

          *(buf+(length-((unsigned char)rem)-1)) = *(buf+length);

         }

    }

     *s=0;

}

 

void US_delay( int i)

{

        unsigned char j;

        while(i--)

    {

           j=3;

              while(j--);

        }

}

Keywords:PIC16F877A  DS18B20 Reference address:PIC16F877A Example --- DS18B20

Previous article:PIC16F877A Routine Program --- PWM
Next article:PIC16F877A Example Program --- LCD1602

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号