您的位置 首页 知识

PIC16F877A例程—DS18B20

includepich>includepic1687xh>__CONFIG(0x3F32);芯片配置字defineLCDRSRB2defineLCDRWRB1defineLCDERB0de

#include

#include

__CONFIG(0x3F32); //芯片装备字

#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 //清屏指令

#define LCDMOD 0X38 //8位,两行,5*7点

#define TURNON 0X0F //

#define CURMODE 0X06 //

#define ORG1 0X80 //LCD 榜首行首地址

#define ORG2 0XC0 //LCD 第二行首地址

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

{ //判别转化是否完结

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榜首行显现Tempereture: 字符

LCD_Display_location (1,0);

LCD_Display (DAT_BUFF2);

LCD_Display_location (1,2);

LCD_Display (atemperature); //LCD第二行显现当时温度值

LCD_Display (DAT_BUFF2);

US_delay(5000); //每隔0.5秒读取一次温度值

}

}

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;

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/260714.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部