锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

DS18B20-64位序列码读取程序

时间:2024-05-03 04:37:09

/*******************************************************************/
/* */
/*51hei单片机开发系统演示程序 - DS18B20-64位序列码读取程序 */
/* *
* 1602显示 *
* *
* 工作芯片:C89C516 晶振频率:11.0592MHz *
* *
/*******************************************************************/
/*读取DS18B20的64位序列码并显示在1602液晶上,如果读取正确结果,则在
液晶第一行显示DS18B20 OK,第二行显示序列码,如果读取失败,则在液晶上
显示DS18B20 ERR0R PLEASE CHECK ,用户可通过更改18B20自己外接。 */
#include
#include

#define uchar unsigned char
#define uint unsigned int

sbit DQ = P2^2; //定义DS18B20端口DQ
sbit BEEP=P2^3 ; //驱动线

bit presence ;

sbit LCD_RS = P3^5;
sbit LCD_RW = P3^6;
sbit LCD_EN = P3^4;

uchar code cdis1[ ] = {" DS18B20 OK "};
uchar code cdis2[ ] = {" "};
uchar code cdis3[ ] = {" DS18B20 ERR0R "};
uchar code cdis4[ ] = {" PLEASE CHECK "};

unsigned char data display[2] = {0x00,0x00};

unsigned char data RomCode[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

unsigned char Temp;
unsigned char crc;

void beep();

#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 =0;
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;
delay1(5);
}

/*******************************************************************/
/* */
/*写显示数据到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;
delay1(5);
}

/*******************************************************************/
/* */
/* 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+地址变量
}

/*******************************************************************/
/* */
/*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; //读取存在信号
Delay(100);
DQ = 1;

return(presence); //返回信号,0=presence,1= no presence
}

/*******************************************************************/
/* */
/* 读一位(bit) */
/* */
/*******************************************************************/
uchar read_bit(void)
{
unsigned char i;
DQ = 0; //将DQ 拉低开始读时间隙
DQ = 1; // then return high
for (i=0; i<3; i++); // 延时15μs
return(DQ); // 返回 DQ 线上的电平值
}

/*******************************************************************/
/* */
/* 读一个字节 */
/* */
/*******************************************************************/
ReadOneChar(void)
{
unsigned char i = 0;
unsigned char dat = 0;

//for (i = 8; i > 0; i--)
// {
// read_bit();
// DQ = 0; // 给脉冲信号
// dat >>= 1;
// DQ = 1; // 给脉冲信号
for (i=0;i<8;i++)
{ // 读取字节,每次读取一个字节
if(read_bit()) dat|=0x01<

// if(DQ)
// dat |= 0x80;
Delay(4);
}

return (dat);
}

/*******************************************************************/
/* */
/* 写一位 */
/* */
/*******************************************************************/
void write_bit(char bitval) {
DQ = 0; // 将DQ 拉低开始写时间隙
if(bitval==1) DQ =1; // 如果写1,DQ 返回高电平
Delay(5); // 在时间隙内保持电平值,
DQ = 1; // Delay函数每次循环延时16μs,因此delay(5) = 104μs
}

/*******************************************************************/
/* */
/* 写一个字节 */
/* */
/*******************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i = 0;
unsigned char temp;
// for (i = 8; i > 0; i--)
// {
for (i=0; i<8; i++) // 写入字节, 每次写入一位
{
// DQ = 0;
// DQ = dat&0x01;
// Delay(5);

// DQ = 1;
temp = dat>>i;
temp &= 0x01;
write_bit(temp);
// dat>>=1;

}
Delay(5);
}

/*******************************************************************/
/* */
/* 读取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 j;
uchar H_num=0x40; //LCD第二行初始位置

for(j=0;j<8;j++)
{
Temp = RomCode[j];

display[0]=((Temp&0xf0)>

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章