本人单片机小白,最近忙于写一论文,因为专业并不是这...
时间:10-02
整理:3721RD
点击:
详细解释 一下代码,如果可以请联系我QQ 278582771 很急用,求大神帮忙,有酬谢
DS18B20.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dq=P1^5;
void sdelay(unsigned int i)
{
while(i--);
}
ds18b20on(void)//ds18b20初始化
{
uchar x=0;
dq=1;
sdelay(8);
dq=0;
sdelay(80);
dq=1;
sdelay(14);
sdelay(20);
}
//读一个字节
readds18b20(void)
{
uchar i=0,dat=0;
for(i=8;i>0;i--)
{
dq=0;
dat>>=1;
dq=1;
if(dq)
dat|=0x80;
sdelay(4);
}
return(dat);
}
//写一个字节
writeds18b20(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
dq=0;
dq=dat&0x01;
sdelay(5);
dq=1;
dat>>=1;
}
}
//读取温度
readtemperature(void)
{
uchar a=0,b=0;
uint t=0;
float tt=0;
ds18b20on();
writeds18b20(0xCC);//跳过ROM
writeds18b20(0x44);//开启温度转换
ds18b20on();
writeds18b20(0xCC);
writeds18b20(0xBE);//读暂存器
a=readds18b20();
b=readds18b20();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}
DS1302.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dsrst=P1^4;
sbit dssclk=P1^1;
sbit dsio=P1^0;
uint a[24]={0,0,0,0,0,0,1,0,0,1,9,0,0,0,0,0,0,0,1,0,1,0,9,0};
//分别对应秒秒分分时时日日月月年年
//后12位为闹钟位,用于存储闹钟数据
//1302write DS1302写数据
void dswrite(uchar dswrite)
{
uchar i,temp;
delay(1);
temp=dswrite;
for(i=0;i<8;i++)
{
dsio=temp&0x01;
delay(1);
dssclk=1;
delay(1);
dssclk=0;
temp=temp>>1;
}
}
//1302read Ds1302读数据
uchar dsread()
{
uchar i,temp;
delay(1);
temp=0;
for(i=0;i<8;i++)
{
temp=temp>>1;
if(dsio==1)temp=temp+0x80;
delay(1);
dssclk=1;
delay(1);
dssclk=0;
}
return temp;
}
//1302addshuju DS1302向某地址写入某数据
void dsaddshuju(uchar add,uchar dsshuju)
{
dsrst=0;
dssclk=0;
dsrst=1;
dswrite(add);// 地址,命令
dswrite(dsshuju);// 写1Byte数据
dssclk=1;
dsrst=0;
dsio=0;
}
//1302on DS1302初始化
void dson()
{
uchar i;
dsaddshuju(0x8c,(a[11]<<4)+a[10]);
for(i=0;i<5;i++)
{
dsaddshuju(0x88-i*2,(a[9-i*2]<<4)+a[8-i*2]);
}
}
LCD1602.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P2^2;
sbit lcdrw=P2^3;
sbit lcden=P2^4;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}//1毫秒左右延时
//lcdwrite() LCD写指令
void lcdwrite(uchar write)
{
lcdrs=0;
P0=write;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdshuju() LCD写数据
void lcdshuju(uchar shuju)
{
lcdrs=1;
P0=shuju;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdon() LCD显示初始化
void lcdon()
{
lcden=0;
lcdwrite(0x38);//显示模式
lcdwrite(0x0c);//开显示,光标不闪烁
lcdwrite(0x06);//光标设置
lcdwrite(0x01);
}
主程序
#include<reg52.h>
#include"lcd1602.h"
#include"ds1302.h"
#include"ds18b20.h"
#define uint unsigned int
#define uchar unsigned char
uint b[6];
//年月日时分秒
uchar code row1[]={"2009-10-01 "};
uchar code row2[]={"00:00:00"};
uchar year1[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年
uchar year2[12]={31,29,31,30,31,30,31,31,30,31,30,31};//润年
uchar j[6]={0x85,0x88,0x8b,0x84+0x40,0x87+0x40,0x8a+0x40};//LCD地址
uchar i,k,jj=0,w,clock=0,bigclock=0,c=0,num;
//i循环数 k温度缓存 jj地址位
uint temp;
sbit b1=P3^0;//设置
sbit b2=P3^1;//上调
sbit b3=P3^2;//下调
sbit b4=P3^3;//转换
sbit b5=P3^4;//闹钟
sbit speaker=P3^5;
lcdscan()
{
for(i=0;i<6;i++)
{
lcdwrite(j[i]);
lcdshuju(a[11-i*2]+0x30);
lcdwrite(j[i]+0x01);
lcdshuju(a[10-i*2]+0x30);
}
}
void key()
{
if(b1==0)
{
dsaddshuju(0x80,(a[1]<<4)+a[0]+0x80);
while(b1==0)
{
for(i=0;i<6;i++)
{
b[i]=a[11-i*2+clock]*10+a[10-i*2+clock];
}
if(b4==0)
{
delay(3);
jj++;
if(jj==6)
{
jj=0;
}
}
if(b2==0)
{
delay(3);
b[jj]++;
if(b[5]==60)b[5]=0;
if(b[4]==60)b[4]=0;
if(b[3]==24)b[3]=0;
if((b[0]%4==0 && b[0]%100!=0) || b[0]%400==0){if(b[2]>year2[b[1]-1])b[2]=1;}
else {if(b[2]>year1[b[1]-1])b[2]=1;}
if(b[1]==13)b[1]=1;
if(b[0]==100)b[0]=0;
}
if(b3==0)
{
delay(3);
b[jj]--;
if(b[5]==-1)b[5]=59;
if(b[4]==-1)b[4]=59;
if(b[3]==-1)b[3]=23;
if((b[0]%4==0 && b[0]%100!=0) || b[0]%400==0){if(b[2]<=0)b[2]=year2[b[1]-1];}
else {if(b[2]<=0)b[2]=year1[b[1]-1];}
if(b[1]==0)b[1]=12;
if(b[0]==-1)b[0]=99;
}
for(i=0;i<6;i++)
{
a[11-i*2+clock]=b[i]/10;
a[10-i*2+clock]=b[i]%10;
}
lcdwrite(j[jj]);
lcdshuju(0);
lcdwrite(j[jj]+1);
lcdshuju(0);
delay(200);
lcdwrite(j[jj]);
lcdshuju(a[11-2*jj+clock]+0x30);
lcdwrite(j[jj]+1);
lcdshuju(a[10-2*jj+clock]+0x30);
delay(200);
if(b1==1)
{
dsaddshuju(0x80,(a[1]<<4)+a[0]);
dson();
}
if(b5==0)clock=12;
}
}
}
void main()
{
speaker=0;
lcdrw=0;
dson();
lcdon();
lcdwrite(0x83);
for(i=0;i<10;i++)
{
lcdshuju(row1[i]);
delay(1);
}
lcdwrite(0x84+0x40);
for(i=0;i<8;i++)
{
lcdshuju(row2[i]);
delay(1);
}
dsrst=0;
dssclk=0;
while(1)
{
for(i=0;i<5;i++)
{
dsrst=1;
dswrite(0x81+i*2);
k=dsread();
dsrst=0;
a[i*2]=k&0x0f;
a[i*2+1]=(k>>4)&0x0f;
}
dsrst=1;
dswrite(0x8d);
k=dsread();
dsrst=0;
a[10]=k&0x0f;
a[11]=(k>>4)&0x0f;
delay(100);
lcdscan();
key();
if(b5==0)
{
for(i=0;i<12;i++)
{
if(a[i+12]==a[i])num++;
}
if(num==12)bigclock=1;
else num=0;
lcdwrite(0x8e);
lcdshuju('&');
}
else
{
lcdwrite(0x8e);
lcdshuju(0x00);
}
if(bigclock==1)
{
speaker=1;
c++;
if(c==100){bigclock=0;speaker=0;}
}
temp=readtemperature();//temp是uint型的。不然会出现温度到25后回00的现象。可能是由于十六进制与十进制的差异。
lcdwrite(0x8d+0x40);
lcdshuju(temp/100+0x30);
lcdwrite(0x8e+0x40);
lcdshuju(temp/10%10+0x30);
}
}
开发板: 伟嵌科技----小强开发板
硬件连接: DS1302 io - P1.0 rst - P1.4 clk - P1.1
DS18B20 dq - P1.5
LCD1602 RS- P2.2 RW - P2.3 EN - P2.4 DATAPORT - P0
DS18B20.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dq=P1^5;
void sdelay(unsigned int i)
{
while(i--);
}
ds18b20on(void)//ds18b20初始化
{
uchar x=0;
dq=1;
sdelay(8);
dq=0;
sdelay(80);
dq=1;
sdelay(14);
sdelay(20);
}
//读一个字节
readds18b20(void)
{
uchar i=0,dat=0;
for(i=8;i>0;i--)
{
dq=0;
dat>>=1;
dq=1;
if(dq)
dat|=0x80;
sdelay(4);
}
return(dat);
}
//写一个字节
writeds18b20(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
dq=0;
dq=dat&0x01;
sdelay(5);
dq=1;
dat>>=1;
}
}
//读取温度
readtemperature(void)
{
uchar a=0,b=0;
uint t=0;
float tt=0;
ds18b20on();
writeds18b20(0xCC);//跳过ROM
writeds18b20(0x44);//开启温度转换
ds18b20on();
writeds18b20(0xCC);
writeds18b20(0xBE);//读暂存器
a=readds18b20();
b=readds18b20();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}
DS1302.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dsrst=P1^4;
sbit dssclk=P1^1;
sbit dsio=P1^0;
uint a[24]={0,0,0,0,0,0,1,0,0,1,9,0,0,0,0,0,0,0,1,0,1,0,9,0};
//分别对应秒秒分分时时日日月月年年
//后12位为闹钟位,用于存储闹钟数据
//1302write DS1302写数据
void dswrite(uchar dswrite)
{
uchar i,temp;
delay(1);
temp=dswrite;
for(i=0;i<8;i++)
{
dsio=temp&0x01;
delay(1);
dssclk=1;
delay(1);
dssclk=0;
temp=temp>>1;
}
}
//1302read Ds1302读数据
uchar dsread()
{
uchar i,temp;
delay(1);
temp=0;
for(i=0;i<8;i++)
{
temp=temp>>1;
if(dsio==1)temp=temp+0x80;
delay(1);
dssclk=1;
delay(1);
dssclk=0;
}
return temp;
}
//1302addshuju DS1302向某地址写入某数据
void dsaddshuju(uchar add,uchar dsshuju)
{
dsrst=0;
dssclk=0;
dsrst=1;
dswrite(add);// 地址,命令
dswrite(dsshuju);// 写1Byte数据
dssclk=1;
dsrst=0;
dsio=0;
}
//1302on DS1302初始化
void dson()
{
uchar i;
dsaddshuju(0x8c,(a[11]<<4)+a[10]);
for(i=0;i<5;i++)
{
dsaddshuju(0x88-i*2,(a[9-i*2]<<4)+a[8-i*2]);
}
}
LCD1602.H
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P2^2;
sbit lcdrw=P2^3;
sbit lcden=P2^4;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}//1毫秒左右延时
//lcdwrite() LCD写指令
void lcdwrite(uchar write)
{
lcdrs=0;
P0=write;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdshuju() LCD写数据
void lcdshuju(uchar shuju)
{
lcdrs=1;
P0=shuju;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdon() LCD显示初始化
void lcdon()
{
lcden=0;
lcdwrite(0x38);//显示模式
lcdwrite(0x0c);//开显示,光标不闪烁
lcdwrite(0x06);//光标设置
lcdwrite(0x01);
}
主程序
#include<reg52.h>
#include"lcd1602.h"
#include"ds1302.h"
#include"ds18b20.h"
#define uint unsigned int
#define uchar unsigned char
uint b[6];
//年月日时分秒
uchar code row1[]={"2009-10-01 "};
uchar code row2[]={"00:00:00"};
uchar year1[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年
uchar year2[12]={31,29,31,30,31,30,31,31,30,31,30,31};//润年
uchar j[6]={0x85,0x88,0x8b,0x84+0x40,0x87+0x40,0x8a+0x40};//LCD地址
uchar i,k,jj=0,w,clock=0,bigclock=0,c=0,num;
//i循环数 k温度缓存 jj地址位
uint temp;
sbit b1=P3^0;//设置
sbit b2=P3^1;//上调
sbit b3=P3^2;//下调
sbit b4=P3^3;//转换
sbit b5=P3^4;//闹钟
sbit speaker=P3^5;
lcdscan()
{
for(i=0;i<6;i++)
{
lcdwrite(j[i]);
lcdshuju(a[11-i*2]+0x30);
lcdwrite(j[i]+0x01);
lcdshuju(a[10-i*2]+0x30);
}
}
void key()
{
if(b1==0)
{
dsaddshuju(0x80,(a[1]<<4)+a[0]+0x80);
while(b1==0)
{
for(i=0;i<6;i++)
{
b[i]=a[11-i*2+clock]*10+a[10-i*2+clock];
}
if(b4==0)
{
delay(3);
jj++;
if(jj==6)
{
jj=0;
}
}
if(b2==0)
{
delay(3);
b[jj]++;
if(b[5]==60)b[5]=0;
if(b[4]==60)b[4]=0;
if(b[3]==24)b[3]=0;
if((b[0]%4==0 && b[0]%100!=0) || b[0]%400==0){if(b[2]>year2[b[1]-1])b[2]=1;}
else {if(b[2]>year1[b[1]-1])b[2]=1;}
if(b[1]==13)b[1]=1;
if(b[0]==100)b[0]=0;
}
if(b3==0)
{
delay(3);
b[jj]--;
if(b[5]==-1)b[5]=59;
if(b[4]==-1)b[4]=59;
if(b[3]==-1)b[3]=23;
if((b[0]%4==0 && b[0]%100!=0) || b[0]%400==0){if(b[2]<=0)b[2]=year2[b[1]-1];}
else {if(b[2]<=0)b[2]=year1[b[1]-1];}
if(b[1]==0)b[1]=12;
if(b[0]==-1)b[0]=99;
}
for(i=0;i<6;i++)
{
a[11-i*2+clock]=b[i]/10;
a[10-i*2+clock]=b[i]%10;
}
lcdwrite(j[jj]);
lcdshuju(0);
lcdwrite(j[jj]+1);
lcdshuju(0);
delay(200);
lcdwrite(j[jj]);
lcdshuju(a[11-2*jj+clock]+0x30);
lcdwrite(j[jj]+1);
lcdshuju(a[10-2*jj+clock]+0x30);
delay(200);
if(b1==1)
{
dsaddshuju(0x80,(a[1]<<4)+a[0]);
dson();
}
if(b5==0)clock=12;
}
}
}
void main()
{
speaker=0;
lcdrw=0;
dson();
lcdon();
lcdwrite(0x83);
for(i=0;i<10;i++)
{
lcdshuju(row1[i]);
delay(1);
}
lcdwrite(0x84+0x40);
for(i=0;i<8;i++)
{
lcdshuju(row2[i]);
delay(1);
}
dsrst=0;
dssclk=0;
while(1)
{
for(i=0;i<5;i++)
{
dsrst=1;
dswrite(0x81+i*2);
k=dsread();
dsrst=0;
a[i*2]=k&0x0f;
a[i*2+1]=(k>>4)&0x0f;
}
dsrst=1;
dswrite(0x8d);
k=dsread();
dsrst=0;
a[10]=k&0x0f;
a[11]=(k>>4)&0x0f;
delay(100);
lcdscan();
key();
if(b5==0)
{
for(i=0;i<12;i++)
{
if(a[i+12]==a[i])num++;
}
if(num==12)bigclock=1;
else num=0;
lcdwrite(0x8e);
lcdshuju('&');
}
else
{
lcdwrite(0x8e);
lcdshuju(0x00);
}
if(bigclock==1)
{
speaker=1;
c++;
if(c==100){bigclock=0;speaker=0;}
}
temp=readtemperature();//temp是uint型的。不然会出现温度到25后回00的现象。可能是由于十六进制与十进制的差异。
lcdwrite(0x8d+0x40);
lcdshuju(temp/100+0x30);
lcdwrite(0x8e+0x40);
lcdshuju(temp/10%10+0x30);
}
}
开发板: 伟嵌科技----小强开发板
硬件连接: DS1302 io - P1.0 rst - P1.4 clk - P1.1
DS18B20 dq - P1.5
LCD1602 RS- P2.2 RW - P2.3 EN - P2.4 DATAPORT - P0