当前位置:首页 > 温度传感器序列号测试程序
//DS18B20序列号测试程序 #include
#define uchar unsigned char #define uint unsigned int
sbit LCD_RS = P2^0; /*定义LCD控制端口*/ sbit LCD_RW = P2^1; sbit LCD_EN = P2^2; sbit DS=P3^4; uchar tab1[16];
uchar code tab[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; void delay(int ms)
{ int i;
while(ms--) {
for(i = 0; i< 250; i++) {
_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; _nop_(); _nop_(); _nop_(); _nop_();
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; _nop_(); _nop_(); _nop_(); _nop_(); LCD_EN = 1; _nop_(); _nop_(); _nop_(); _nop_();
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; _nop_(); _nop_(); _nop_(); _nop_(); LCD_EN = 1; _nop_(); _nop_(); _nop_(); _nop_(); LCD_EN = 0; }
/*******************************************************************/ /* */ /* 设定显示位置 */ /* */ /*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos|0x80); //数据指针=80+地址变量 }
/*******************************************************************/ /* */ /* LCD初始化设定 */ /* */ /*******************************************************************/
void lcd_init()
{
lcd_wcmd(0x38); //16*2显示,5*7点阵,8位数据 delay(5);
lcd_wcmd(0x38); delay(5);
lcd_wcmd(0x38); delay(5);
lcd_wcmd(0x0c); //显示开,关光标 delay(5);
lcd_wcmd(0x06); //移动光标 delay(5);
lcd_wcmd(0x01); //清除LCD的显示内容 delay(5); lcd_wcmd(0x06); //向右移动光标 delay(5); }
void dsreset(void) //send reset and initialization command 18B20复位,初始化函数 {
uint i; DS=0; i=103;
while(i>0)i--; DS=1; i=4;
while(i>0)i--; }
bit tmpreadbit(void) //read a bit 读1位数据函数 {
uint i; bit dat;
DS=0;i++; //i++ for delay DS=1;i++;i++; dat=DS;
i=8;while(i>0)i--; return (dat); }
uchar tmpread(void) //read a byte date 读1字节函数 {
uchar i,j,dat; dat=0;
for(i=1;i<=8;i++) {
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里 }
return(dat); }
void tmpwritebyte(uchar dat) //write a byte to ds18b20 向1820写一个字节数据函数 {
uint i;
共分享92篇相关文档