c语言编程温度传感器代码,温度传感器c程序
DS18B20温度显示演示程序-LCD1602显示采用C语言编写开机时对DS18B20进行检测,如果DS18B20检测不正常,LCD1602显示:DS18B20ERRORPLEASECHECK蜂鸣器报警。DS18B20检测正常,LCD1602显示:DS18B20OKTEMP:100.8℃如果温度值高位为0,将不显示出来。你可以通过拔插DS18B20查看DS18B20的检测功能。/*ME3...
DS18B20温度显示演示程序-LCD1602显示
采用C语言编写
开机时对DS18B20进行检测,如果DS18B20检测不正常,LCD1602显示:
DS18B20 ERROR
PLEASE CHECK
蜂鸣器报警。
DS18B20检测正常,LCD1602显示:
DS18B20 OK
TEMP: 100.8℃
如果温度值高位为0,将不显示出来。
你可以通过拔插DS18B20查看DS18B20的检测功能。
/* ME300B单片机开发系统演示程序 - DS18B20温度显示 */
/* LCD1602显示 */
/* 作者: gguoqing */
/*Copyright(C)伟纳电子 www.willar.com All Rights Reserved */
/*******************************************************************/
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P3^3 ; //定义DS18B20端口DQ
sbit BEEP=P3^7 ; //蜂鸣器驱动线
bit presence ;
sbit LCD_RS = P2^0 ;
sbit LCD_RW = P2^1 ;
sbit LCD_EN = P2^2 ;
uchar code cdis1[ ] = {" DS18B20 OK"} ;
uchar code cdis2[ ] = {" TEMP: . C "} ;
uchar code cdis3[ ] = {" DS18B20 ERR0R "} ;
uchar code cdis4[ ] = {" PLEASE CHECK"} ;
unsigned char data temp_data[2] = {0x00,0x00} ;
unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00} ;
unsigned char code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09} ;
void beep() ;
unsigned char code mytab[8] = {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00} ;
#define delayNOP() ; {_nop_() ;_nop_() ;_nop_() ;_nop_() ;} ;
/*******************************************************************/
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 ;
}
/* 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) ;
}
/* 设定显示位置 */
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80) ; //数据指针=80+地址变量
}
更多推荐
所有评论(0)