📄 ds18d20_test.c
字号:
#include <reg52.h>
#include <intrins.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#define ds1302_set0(x) x=0
#define ds1302_set1(x) x=1
sbit ds1302_rst=P3^7;
sbit ds1302_sclk=P2^0;
sbit ds1302_io=P2^1;
void ds1302_initial();
void ds1302_senddata(uchar dsend);
uchar ds1302_receivedata(void);
/*******************寄存器********************/
unsigned char Ds1302Reg[7]={0}; //ds1302内部日期寄存器 (00-06)
unsigned char Date[14]={0};
/*********************************************/
sbit dp = P0 ^4; //定义温度器件的数据脚位
bit temp_flag;
unsigned int idata temp_data;
unsigned char idata temp_buff[9];
unsigned char idata id_buff[8];
unsigned char *p_tempdata;
unsigned char code crc_table[256] ={
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
void delay(unsigned char us)
{
while (us--);
}
void init_18b20(void)
{
dp =1;
_nop_();
dp =0;
delay(80); //delay 530us
dp =1;
delay(14); //delay 100us
if (dp == 0)
temp_flag =1; //detec 18b20 success!
else
temp_flag =0; //detec 18b20 fail!
delay(20);
dp =1;
}
void write_temp(unsigned char wr)
{ //写入单字节数
unsigned char i;
for (i =0;i<8;i++)
{
dp =0;
_nop_();
dp =wr&0x01;
delay(5); //delya 45us
dp =1;
wr >>=1;
}
}
unsigned char read_byte(void)
{ //读取单字节数
unsigned char i,u=0;
for (i =0;i<8;i++)
{
dp =0;
u >>=1;
dp =1;
if (dp==1)
u |=0x80;
delay(4);
}
return(u);
}
void read_bytes(unsigned char j)
{
unsigned char i;
for (i =0;i<j;i++)
{
*p_tempdata=read_byte();
p_tempdata++;
}
}
unsigned char crc_conter(unsigned char j)
{
unsigned char i,crc_data =0;
for (i =0;i <j;i++)
{
crc_data =crc_table[crc_data ^ temp_buff[i]];
}
return(crc_data);
}
void get_temp(void)
{
read_bytes(9);
if (crc_conter(9)==0)
{
temp_data =(temp_buff[1]*0x100)+temp_buff[0];
temp_data *=0.625;
}
}
void temp_config(void)
{ //重新配置报警限定值和分辨率
init_18b20();
write_temp(0xcc); //skip rom
write_temp(0x4e); //write scratchpad
write_temp(0x19); //上限
write_temp(0x1a); //下限
write_temp(0x7f); //set 11 bit (0.125)
init_18b20();
write_temp(0xcc); //skip rom
write_temp(0x48); //保存设定值
init_18b20();
write_temp(0xcc); //skip rom
write_temp(0xb8); //回调设定值
}
void read_tempID(void)
{
init_18b20();
write_temp(0x33);
read_bytes(8);
}
void ds1302_senddata(uchar dsend)
{
uchar i;
for(i=0;i<8;i++)
{
ds1302_io=(bit)(dsend&0x01);
delay(20);
ds1302_set1(ds1302_sclk);
dsend=dsend>>1;
delay(20);
ds1302_set0(ds1302_sclk);
delay(20);
}
}
uchar ds1302_receivedata(void)
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
dat=dat>>1;
if(ds1302_io) dat=dat|0x80;
else dat=dat&0x7f;
delay(20);
ds1302_set1(ds1302_sclk);
delay(20);
ds1302_set0(ds1302_sclk);
delay(20);
}
return(dat);
}
void Ds1302Write(char data1,char data2)
{
ds1302_set0(ds1302_rst);
_nop_();
_nop_();
delay(20);
ds1302_set0(ds1302_sclk);
_nop_();
_nop_();
delay(20);
ds1302_set1(ds1302_rst);
_nop_();
_nop_();
delay(20);
ds1302_senddata(data1);
ds1302_senddata(data2);
_nop_();
_nop_();
delay(20);
ds1302_set1(ds1302_sclk);
_nop_();
_nop_();
delay(20);
ds1302_set0(ds1302_rst);
}
char Ds1302Read(char Addr)
{
unsigned char DataReturn;
ds1302_set0(ds1302_rst);
_nop_();
_nop_();
delay(20);
ds1302_set0(ds1302_sclk);
_nop_();
_nop_();
delay(20);
ds1302_set1(ds1302_rst);
_nop_();
_nop_();
delay(20);
ds1302_senddata(Addr);
DataReturn=ds1302_receivedata();
ds1302_set1(ds1302_sclk);
_nop_();
_nop_();
delay(20);
ds1302_set0(ds1302_rst);
return(DataReturn);
}
void Ds1302ReadAll()
{
unsigned char i;
for(i=0;i<7;i++)
{
Ds1302Reg[i]=Ds1302Read(0x81|((i<<1)&0xfe));
if (i==0)
{
if (Ds1302Reg[i]&0x80)
{
Ds1302Write(0x8e,0x00);
Ds1302Write(0x80,0x00);
Ds1302Write(0x8e,0x80);
}
}
}
}
void Ds1302DataConver()
{
char Counter=0;
Ds1302ReadAll();
//秒 分 时
Date[5]=Ds1302Reg[0]%0x10;
Date[4]=((Ds1302Reg[0]>>4)&0x07);
Date[3]=Ds1302Reg[1]%0x10;
Date[2]=(Ds1302Reg[1]>>4);
Date[1]=Ds1302Reg[2]%0x10;
Date[0]=((Ds1302Reg[2]>>4)&0x01);
//日 月
Date[9]=Ds1302Reg[3]%0x10;
Date[8]=((Ds1302Reg[3]>>4)&0x03);
Date[7]=Ds1302Reg[4]%0x10;
Date[6]=((Ds1302Reg[4]>>4)&0x01);
//星期
Date[10]=Ds1302Reg[5];
//年 基数 2000
Date[11]=Ds1302Reg[6]%0x10;
Date[12]=((Ds1302Reg[6]>>4)&0x0f);
for(Counter=0;Counter<13;Counter++)
Date[Counter]+='0';
}
void ds1302_initial()
{
ds1302_set0(ds1302_sclk);
ds1302_set0(ds1302_rst);
ds1302_set0(ds1302_io);
Ds1302Write(0x8e,0x00);
Ds1302Write(0x80,0x00);
Ds1302Write(0x82,0x55);
Ds1302Write(0x84,0x23);
Ds1302Write(0x86,0x31);
Ds1302Write(0x88,0x12);
Ds1302Write(0x8a,0x03);
Ds1302Write(0x8c,0x08);
Ds1302Write(0x90,0xa9);
Ds1302Write(0x8e,0x80);
}
void delay_500(void)
{
unsigned int a,i;
while (a--);
while (i);
}
void main(void)
{
p_tempdata =id_buff;
read_tempID();
temp_config();
ds1302_initial();
for (;;)
{
init_18b20();
write_temp(0xcc); //skip rom
write_temp(0x44);
init_18b20();
write_temp(0xcc);
write_temp(0xbe);
p_tempdata =temp_buff;
get_temp();
// delay_500();
Ds1302DataConver();
delay_500();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -