📄 ds18b20-lcd1602-c51.c
字号:
/*******************************************************************/
/* */
/* ME500单片机开发系统演示程序 - DS18B20温度显示 */
/* */
/* LCD1602显示 */
/* */
/* 版本: V1.0 (2006/11/20) */
/* 作者: gguoqing (Email: gguoqing@willar.com) */
/* 网站: www.willar.com(伟纳电子) www.mcusj.com(伟纳单片机世界) */
/* 邮箱: support@willar.com */
/* */
/*【版权】Copyright(C)伟纳电子 www.willar.com All Rights Reserved */
/*【声明】此程序仅用于学习与参考,引用请注明版权和作者信息! */
/* */
/*******************************************************************/
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P3^3; //定义DS18B20端口DQ
sbit BEEP=P3^7 ; //蜂鸣器驱动线
bit presence,flag;
bit compare_th,compare_tl,alarm_on_off=0,temp_th,temp_tl;
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit K1 = P1^4;
sbit K2 = P1^5;
sbit K3 = P1^6;
sbit K4 = P1^7;
uchar code cdis1[ ] = {" READ_ROMCORD "};
uchar code cdis2[ ] = {" "};
uchar code cdis3[ ] = {" DS18B20 ERR0R "};
uchar code cdis4[ ] = {" PLEASE CHECK "};
uchar code cdis5[ ] = {" TEMP: "};
uchar code cdis6[ ] = {"TH: TL: "};
unsigned char data temp_data[2] = {0x00,0x00};
unsigned char data temp_alarm[2] = {0x00,0x00};
unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00}; //温度值显示
unsigned char data display1[3] = {0x00,0x00,0x00}; //温度报警值显示
//unsigned char code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
// 0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
unsigned char data RomCode[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char code mytab1[8] = {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};
unsigned char code mytab2[8] = {0x01,0x1b,0x1d,0x19,0x1d,0x1b,0x01,0x00};//小喇叭
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
unsigned char Temp,temp_comp,timecount,count;
unsigned char crc;
void Disp_Temp_alarm(uchar addr,uchar num);
void spk(uchar addr);
void set_temp_alarm();
void temp_compare();
void beep();
/*******************************************************************/
void delay1(int ms)
{
unsigned char y;
while(ms--)
{
for(y = 0; y<250; y++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/******************************************************************/
/* */
/*检查LCD忙状态 */
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。 */
/* */
/******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*写指令数据到LCD */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。 */
/* */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*写显示数据到LCD */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。 */
/* */
/*******************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*自定义字符写入CGRAM */
/* */
/*******************************************************************/
void writetab()
{
unsigned char i;
lcd_wcmd(0x40); //写CGRAM
for (i = 0; i< 8; i++)
lcd_wdat(mytab1[i]);
for (i = 0; i< 8; i++)
lcd_wdat(mytab2[i]);
}
/*******************************************************************/
/* */
/* LCD初始化设定 */
/* */
/*******************************************************************/
void lcd_init()
{
delay1(15);
lcd_wcmd(0x01); //清除LCD的显示内容
lcd_wcmd(0x38); //16*2显示,5*7点阵,8位数据
delay1(5);
lcd_wcmd(0x38);
delay1(5);
lcd_wcmd(0x38);
delay1(5);
lcd_wcmd(0x0c); //显示开,关光标
delay1(5);
lcd_wcmd(0x06); //移动光标
delay1(5);
lcd_wcmd(0x01); //清除LCD的显示内容
delay1(5);
writetab(); //自定义字符写入CGRAM
}
/*******************************************************************/
/* */
/* 设定显示位置 */
/* */
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //数据指针=80+地址变量
}
/*******************************************************************/
/* */
/*us级延时函数 */
/* */
/*******************************************************************/
void Delay(unsigned int num)
{
while( --num );
}
/*******************************************************************/
/* */
/*初始化ds1820 */
/* */
/*******************************************************************/
Init_DS18B20(void)
{
DQ = 1; //DQ复位
Delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay(90); //精确延时 大于 480us
DQ = 1; //拉高总线
Delay(8);
presence = DQ; //如果=0则初始化成功 =1则初始化失败
Delay(100);
DQ = 1;
return(presence); //返回信号,0=presence,1= no presence
}
/*******************************************************************/
/* */
/* 读一个字节 */
/* */
/*******************************************************************/
ReadOneChar(void)
{
unsigned char i = 0;
unsigned char dat = 0;
for (i = 8; i > 0; i--)
{
DQ = 0; // 给脉冲信号
dat >>= 1;
DQ = 1; // 给脉冲信号
if(DQ)
dat |= 0x80;
Delay(4);
}
return (dat);
}
/*******************************************************************/
/* */
/* 写一个字节 */
/* */
/*******************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i = 0;
for (i = 8; i > 0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
/*******************************************************************/
/* */
/* 温度报警值写入DS18B20 */
/* */
/*******************************************************************/
Write_Temperature_alarm(void)
{
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x4e); //将设定的温度报警值写入 DS18B20
WriteOneChar(temp_alarm[0]); //写TH
WriteOneChar(temp_alarm[1]); //写TL
WriteOneChar(0x7f); //12位精确度
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x48); //把暂存器里的温度报警值拷贝到EEROM
}
/*******************************************************************/
/* */
/* 读取64位序列码 */
/* */
/*******************************************************************/
Read_RomCord(void)
{
unsigned char j;
Init_DS18B20();
WriteOneChar(0x33); // 读序列码的操作
for (j = 0; j < 8; j++)
{
RomCode[j] = ReadOneChar() ;
}
}
/*******************************************************************/
/* */
/*DS18B20的CRC8校验程序 */
/* */
/*******************************************************************/
uchar CRC8()
{
uchar i,x; uchar crcbuff;
crc=0;
for(x = 0; x <8; x++)
{
crcbuff=RomCode[x];
for(i = 0; i < 8; i++)
{
if(((crc ^ crcbuff)&0x01)==0)
crc >>= 1;
else {
crc ^= 0x18; //CRC=X8+X5+X4+1
crc >>= 1;
crc |= 0x80;
}
crcbuff >>= 1;
}
}
return crc;
}
/*******************************************************************/
/* */
/* 数据转换与显示 */
/* */
/*******************************************************************/
Disp_RomCode(uchar H_num)
{
uchar j;
if(H_num==1)
lcd_pos(0x00);
if(H_num==2)
lcd_pos(0x40);
for(j=0;j<8;j++)
{
Temp = RomCode[j];
display[0]=((Temp&0xf0)>>4);
if(display[0]>9)
{ display[0]=display[0]+0x37;}
else{display[0]=display[0]+0x30;}
lcd_wdat(display[0]); //高位数显示
display[1]=(Temp&0x0f);
if(display[1]>9)
{display[1]=display[1]+0x37;}
else {display[1]=display[1]+0x30;}
lcd_wdat(display[1]); //低位数显示
}
}
/*******************************************************************/
/* */
/* 读取温度 */
/* */
/*******************************************************************/
Read_Temperature(void)
{
uchar i;
TR0=0; //关中断,防止读数错误
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
Init_DS18B20();
WriteOneChar(0x55); //匹配ROM命令
for(i=0;i<8;i++)
WriteOneChar(RomCode[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -