#include
本文引用地址:http://m.butianyuan.cn/article/201611/321024.htm__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)
{ //判斷轉(zhuǎn)換是否完成
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第二行顯示當(dāng)前溫度值
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;
}
評論