DS18B20温度1602显示
如图所示:
程序如下:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit lcdrw=P3^6;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]="temperature:";
void delayus(uint z)
{while(--z);}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1602com(uchar com)
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void Write_1602date(uchar date)
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init()
{
dula=0;
wela=0;
lcden=0;
lcdrw=0;
Write_1602com(0x38);
Write_1602com(0x01);
Write_1602com(0x0c);
Write_1602com(0x06);
}
void Write_sfm(uchar add,uchar date) //1250
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=date%1000/100;
shi=date%1000%100/10;
ge=date%1000%100%10;
Write_1602com(0x80+0x40+add);
Write_1602date(0x30+qian);
Write_1602date(0x30+bai);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
bit DS18B20_init() // DS18b20初始函数
{
bit i;
DS=1;
_nop_();
DS=0;
delayus(75); //75*6.5+12=500
DS=1;
delayus(2);//2*6.5+12=25
i=DS;
delayus(20);//20*6.5+12=132
DS=1;
_nop_();
return i;
}
void Write_DS18B20byte(uchar date)//写
{
uchar i;
for(i=0;i<8;i++)
{
DS=0;
_nop_();
DS=date&0x01;
delayus(15);//6.5*15=110
DS=1;
_nop_();
date>>=1;
}
}
uchar Read_DS18B20byte() //读
{
uchar i,j,date;
for(i=0;i<8;i++)
{
DS=0;
_nop_();
DS=1;
delayus(1);//6.5+12=18.5
j=DS;
delayus(10);//6.5*10+12=72
DS=1;
_nop_();
date=(j<<7)|(date>>1);
return date;
}
}
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0x44);
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0xbe);
L= Read_DS18B20byte();
M= Read_DS18B20byte();
i=M;
i<<=8;
i|=L;
i=i*0.0625*10+0.5;
void main()
{
uint i,num,a;
uchar L,M;
init();
Write_1602com(0x80);
for(num=0;num<12;num++)
{
Write_1602date(table[num]);
delayms(5);
}
while(1)
{
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0x44);
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0xbe);
L= Read_DS18B20byte();
M= Read_DS18B20byte();
i=M;
i<<=8;
i|=L;
i=i*0.0625*10+0.5;
Write_sfm(5,a);
}
}
应该是你DS18B20部分的时序有问题,没有符合one-wire总线的要求。
好的 那我试试、我就感觉这样可以显示的 可是就不知道为什么显示不出来
还是不行 你帮我看看我的程序哪里出了问题了 拜托了
你的读部分的程序return放在了for循环里,暂时只看到这个。
这是我自己的读DS18B20部分的代码,你可以参考下,写得很清晰。
- /*
- * @Prototype:extern u8 DS18B20_readByte(void)
- * @Argument:None
- * @Ret-val:reading result
- * @Note:read one byte from DS18B20
- */
- extern u8 DS18B20_readByte(void)
- {
- u8 i = 0x00; /* count variable */
- u8 r = 0x00; /* result variable */
- for(i = 0x00;i< 0x08;i++)
- {
- /* STEP-1:read from LSB */
- r = r >> 0x01;
- /* STEP-2:make bus is in low level status and produce a read signal */
- DS18B20_DQ = 0;
- /* STEP-3:wait for 5us */
- DELAY_5us();
- /* STEP-4:release bus and ready for reading data */
- DS18B20_DQ = 1;
- /* STEP-5:wait about 5us again */
- DELAY_5us();
- /* STEP-6:read bus status */
- if(DS18B20_DQ)
- {
- r = r | 0x80;
- }
- /* STEP-7:following the timing,it requires wait about 30us */
- DELAY_30us();
- /* STEP-8:pull up bus for next use */
- DS18B20_DQ = 1;
- }
- return r;
- }
你的能多点测温吗?求程序
好了 谢谢谢谢
程序如下:
#include<reg52.h>
#include"LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
sbit TSCLK=P1^0;
sbit TIO=P1^1;
sbit TEN=P1^2;
uchar Year,Month,Day,Hour,Min,Sec;
void delayms(uint z) //延时函数ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1302date(uchar com,uchar dat) //DS1302写数据
{
uchar i;
TEN=0;
TSCLK=0;
TEN=1;
for(i=0;i<8;i--)
{
TSCLK=0;
TIO=com&0x01;
TSCLK=1;
com>>=1;
}
for(i=0;i<8;i--)
{
TSCLK=0;
TIO=dat&0x01;
TSCLK=1;
dat>>=1;
}
}
uchar Read_1302date(uchar com) //DS1302读数据
{
uchar i,dat;
TEN=0;
TSCLK=0;
TEN=1;
for(i=0;i<8;i++)
{
TSCLK=0;
TIO=com&0x01;
TSCLK=1;
com>>=1;
}
for(i=0;i<8;i++)
{
TSCLK=0;
dat>>=1;
if(TIO) dat|=0x80; //如果TIO为高电平写1 否则写0
TSCLK=1;
}
return dat;
}
uchar date_BCD(uchar dat) //数据转BCD
{
uchar dat1,dat2;
dat1=dat/10;
dat2=dat%10;
dat2=dat2+dat1*16;
return dat2;
}
uchar BCD_date(uchar dat) //BCD转数据
{
uchar dat1,dat2;
dat1=dat/16;
dat2=dat%16;
dat2=dat2+dat1*10;
return dat2;
}
void main()
{
init();
Write_1302date(0x8e,0);//关闭写保护
Write_1302date(0x80,date_BCD(30)); //秒
Write_1302date(0x82,date_BCD(15)); //分
Write_1302date(0x84,date_BCD(5)); //时
Write_1302date(0x86,date_BCD(30)); //日
Write_1302date(0x88,date_BCD(9)); //月
Write_1302date(0x8c,date_BCD(17));;//年
Write_1302date(0x8e,0x80);//开启写保护
while(1)
{
Write_1302date(0x8e,0);//关闭写保护
Sec=BCD_date(Read_1302date(0x81));
Min=BCD_date(Read_1302date(0x83));
Hour=BCD_date(Read_1302date(0x85));
Day=BCD_date(Read_1302date(0x87));
Month=BCD_date(Read_1302date(0x89));
Year=BCD_date(Read_1302date(0x8d));
Write_1302date(0x8e,0x80);//开启写保护
ONE_Write_sfm(7,Year);
ONE_Write_sfm(10,Month);
ONE_Write_sfm(13,Day);
TWO_Write_sfm(6,Hour);
TWO_Write_sfm(9,Min);
TWO_Write_sfm(12,Sec);
}
}
1602.h程序如下:
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4; //使能端
sbit lcdrs=P3^5; //数据命令选择端
sbit lcdrw=P3^6; //读写控制端
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table3[]="Date:20 - - ";
uchar code table4[]="Time: - - ";
void delay(uint z) //延时函数ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1602com(uchar com) //1602写命令
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void Write_1602date(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void ONE_Write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
Write_1602com(0x80+add);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
void TWO_Write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
Write_1602com(0x80+0x40+add);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
lcdrw=0;
Write_1602com(0x38);
Write_1602com(0x01);
Write_1602com(0x0c);
Write_1602com(0x06);
Write_1602com(0x80);
for(num=0;num<15;num++)
{
Write_1602date(table3[num]);
delay(5);
}
Write_1602com(0x80+0x40);
for(num=0;num<15;num++)
{
Write_1602date(table4[num]);
delay(5);
}
}
帮我看一下这个程序 ,他显示的数值不是我自己设定的 而且每次复位都会变
这是DS1302液晶1602显示
完成啦啦啦啦
完成啦啦啦啦
成功了就好。
帮我看看DS1302呗,,
具体是什么问题?
就是我写的值在1602上显示不出来 它显示别的数字
你的写程序for循环中应该是i++
打开开关显示我设置的置 然后马上变成这样
打开开关显示我设置的置 然后马上变成这样
85是DS1302中非常经典的错误,应该是你的读写时序还存在问题。晚点我会补段代码上来。