微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机驱动DS1302时间DS18B20温度12864液晶显示完整程序设计

单片机驱动DS1302时间DS18B20温度12864液晶显示完整程序设计

时间:12-11 来源:互联网 点击:

程序说明:这是一个用51单片机驱动DS1302时间模块+DS18B20温度传感器模块+12864液晶显示完整程序设计实例,有四个键盘KEY0 到KEY3,key0是修改时间的 首先是秒到分到时到年到月到日到星期 key1是加1 key2是减一 在修改时间状态再按一下key3的时候就出去并修改时间日期,在平时状态按着key3再按着key2 就会打开lcd 的灯 单单是只按key3那就是把灯关了。

文件一:DS1302驱动.c

#includereg52.h>

//ds1302
sbit sclk=P0^3;
sbit io=P0^4;
sbit rst=P0^5;
sbit acc0=ACC^0;
sbit acc1=ACC^1;
sbit acc2=ACC^2;
sbit acc3=ACC^3;
sbit acc4=ACC^4;
sbit acc5=ACC^5;
sbit acc6=ACC^6;
sbit acc7=ACC^7;

delay()
{
unsigned char i;
i=10;
i=10;
i=10;
i=10;
i=10;
i=10;
i=10;}
w_1302(unsigned char dat,unsigned char shu){//写一个八位的数去DS1302 //上升效 ;;;选写地址再写数据

ACC=dat;//地址
sclk=0;
rst=1;//
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;
delay();
sclk=1;///


//sclk=0;
ACC=shu; //数据
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;
delay();
sclk=1;///

rst=0;
sclk=0;
} ///写完毕
unsigned char r_1302(unsigned char ADD){

sclk=0;
rst=1;
ACC=ADD; //地址
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;/////
delay();
sclk=1;
delay();
sclk=0;
acc0=io;
delay();
sclk=1;
delay();
sclk=0;
acc1=io;
delay();
sclk=1;
delay();
sclk=0;
acc2=io;
delay();
sclk=1;
delay();
sclk=0;
acc3=io;
delay();
sclk=1;
delay();
sclk=0;
acc4=io;
delay();
sclk=1;
delay();
sclk=0;
acc5=io;
delay();
sclk=1;
delay();
sclk=0;
acc6=io;
delay();
sclk=1;
delay();
sclk=0;
acc7=io;
delay();

rst=0;
sclk=0;
return(ACC);
}
ds1302_c()
{
w_1302(0x80,0x80);
w_1302(0x8e,0x00);

w_1302(0x80,0x50); //;秒
w_1302(0x82,0x06); // 分=0
w_1302(0x84,0x01); //时 =0
w_1302(0x86,0x22); //
w_1302(0x88,0x11);//
w_1302(0x8a,0x04);//
w_1302(0x8b,0x07);//

w_1302(0x8e,0x80);
w_1302(0x90,0xa6);
w_1302(0x80,0x00);
}
main(){
rst=0;
ds1302_c();

for(;;){
P2=r_1302(0x81);
}
}

---------------------------------------------------------------------------------------------------------------
文件2:完全版 有关灯键.c

#includereg52.h>
sbit di = P2^7; ////////定义引脚
sbit rw = P2^6;
sbit e = P2^5;
sbit cs1 = P2^4;
sbit cs2 = P2^1;
sbit lcd_d = P2^0;
sbit key0 = P2^3;
sbit key1 = P2^2;
sbit key2 = P1^0;
sbit key3 = P1^1;
sbit P07 = P0^7;
sbit P06 = P0^6;
sbit P05 = P0^5;
sbit P04 = P0^4;
sbit P03 = P0^3;
//ds1302
sbit sclk=P1^4;
sbit io=P1^3;
sbit rst=P1^2;
sbit acc0=ACC^0;
sbit acc1=ACC^1;
sbit acc2=ACC^2;
sbit acc3=ACC^3;
sbit acc4=ACC^4;
sbit acc5=ACC^5;
sbit acc6=ACC^6;
sbit acc7=ACC^7;
typedef unsigned char byte;
typedef unsigned int word;
sbit KEY=P3^7;//根据实际情况定义端口ds1820
unsigned char key_time=0; //全局变量


//////////////////////////////////温度
//ds18b20数字温度传感器模块程序

void delay1(word useconds)
{
for(;useconds>0;useconds--);
}

//复位
byte ow_reset(void)
{
byte presence;
KEY = 0; //pull DQ line low
delay1(29); // leave it low for 480us
KEY = 1; // allow line to return high
delay1(3); // wait for presence
presence = KEY; // get presence signal
delay1(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part

//从 1-wire

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top