單片機驅動DS1302時間DS18B20溫度12864液晶顯示完整程序設計
程序說明:這是一個用51單片機驅動DS1302時間模塊+DS18B20溫度傳感器模塊+12864液晶顯示完整程序設計實例,有四個鍵盤KEY0 到KEY3,key0是修改時間的 首先是秒到分到時到年到月到日到星期 key1是加1 key2是減一 在修改時間狀態(tài)再按一下key3的時候就出去并修改時間日期,在平時狀態(tài)按著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){//寫一個八位的數(shù)去DS1302 //上升效 ;;;選寫地址再寫數(shù)據(jù)
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; //數(shù)據(jù)
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;//根據(jù)實際情況定義端口ds1820
unsigned char key_time=0; //全局變量
//////////////////////////////////溫度
//ds18b20數(shù)字溫度傳感器模塊程序
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
51單片機相關文章:51單片機教程
單片機相關文章:單片機教程
單片機相關文章:單片機視頻教程
單片機相關文章:單片機工作原理
評論