51單片機(jī)電子時(shí)鐘完整版
#includereg52.h>
#define uint unsigned int
#define uchar unsigned char
#define w_Sec 0x80
#define w_Min 0x82
#define w_Hou 0x84
#define w_Day 0x86
#define w_Mou 0x88
#define w_xi 0x8a
#define w_Yea 0x8c // DS1302寫數(shù)據(jù)
#define r_Sec 0x81
#define r_Min 0x83
#define r_Hou 0x85
#define r_Day 0x87
#define r_Mou 0x89
#define r_xi 0x8b
#define r_Yea 0x8d //DS1302讀數(shù)據(jù)
#define w_protect 0x8e //DS1302寫保護(hù)
uchar code table[]="11-6-20"; //制作時(shí)間
uchar code time1[]="00:00:00"; //時(shí)間模式1,用于普通計(jì)時(shí)
uchar code time2[]="00:00:00:00"; //時(shí)間模式2,用于秒表計(jì)時(shí)
uchar code time3[]="00:00"; //時(shí)間模式3,用于鬧鐘設(shè)置
uchar code miaobiao[]="StopWatch";
uchar code naozhong[]="Alarm";
uchar code stuinfo1[]="ZLW0800730203";
uchar code stuinfo2[]="LWL0807030209";
uchar code xi1[]="Mon";
uchar code xi2[]="Tue";
uchar code xi3[]="Wed";
uchar code xi4[]="Thu";
uchar code xi5[]="Fri";
uchar code xi6[]="Sat";
uchar code xi7[]="Sun";
unsigned char code str1[]={"Temperature:"};
unsigned char code str2[]={" "};
uchar data disdata[5];
uint tvalue; //溫度值
uchar tflag; //溫度正負(fù)標(biāo)志 //18B20
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2; //1602控制端定義
sbit ACC_7 = ACC^7; //位尋址寄存器
sbit SCK = P1^1; //DS1302時(shí)鐘信號(hào)
sbit SDA = P1^0; //DS1302數(shù)據(jù)信號(hào)
sbit RST = P1^2 ; //DS1302片選信號(hào)
sbit k1=P1^4;
sbit k2=P1^5;
sbit k3=P1^6;
sbit k4=P1^7;
sbit alarm=P3^7;
sbit DQ = P2^7; //DS18B20
uint i,shi,fen,miao,haomiao,nian,yue,ri,num,x,menu,n,nshi=0,nfen=1; //nshi、nfen用來判別鬧鐘,與系統(tǒng)時(shí)間對(duì)比
uchar sshi,sfen,smiao,snian,syue,sri,sxi;
/***********************延時(shí)程序*********************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/********************************ds1302***************************************/
//***************向DS1302寫入一字節(jié)數(shù)據(jù)
void Write1302(uchar addr, uchar d)
{
unsigned char i;
SCK=0;
RST=1;
//寫入目標(biāo)地址:addr
for (i = 0; i 8; i ++)
{
if (addr 0x01)
{
SDA=1;
}
else
{
SDA=0;
} //并口轉(zhuǎn)串口
SCK=1; //上升沿寫數(shù)據(jù)
SCK=0;
addr = addr >> 1;
}
//寫入數(shù)據(jù):d
for (i = 0; i 8; i ++)
{
if (d 0x01)
{
SDA=1;
}
else
{
SDA=0;
}
SCK=1;
SCK=0;
d = d >> 1;
}
RST=0; //停止DS1302總線
}
//*************************從DS1302讀出一字節(jié)數(shù)據(jù)
unsigned char Read1302(uchar addr)
{
unsigned char i;
unsigned char temp;
unsigned char dat1=0;
unsigned char dat2=0;
SCK=0;
RST=1;
//寫入目標(biāo)地址:addr
for (i = 0; i 8; i ++)
{
if (addr 0x01)
{
SDA=1;
}
else
{
SDA=0;
}
SCK=1;
SCK=0;
addr = addr >> 1;
}
//輸出數(shù)據(jù):temp
for (i = 0; i 8; i ++)
{
temp = temp >> 1;
if (SDA)
{
temp |= 0x80;
}
else
{
temp = 0x7F;
}
SCK=1;
SCK=0;
}
RST=0; //停止DS1302總線
dat2 = temp/16; // 數(shù)據(jù)進(jìn)制轉(zhuǎn)換
dat1 = temp%16;
dat1 = dat2*10+dat1;
return dat1;
}
/*****************************1602顯示******************************************/
void write_add(uchar add) //1602控制字
{
RS=0;
P0=add;
EN=1;
delay(5);
EN=0;
}
void write_date(uchar date) //1602顯示字
{
RS=1;
P0=date;
EN=1;
delay(5);
EN=0;
}
void writesfm(uchar add,uchar date) //1602顯示時(shí)分秒
{
uchar s,g;
s = date/10;
g = date%10;
write_add(0x80+0x40+add);
write_date(0x30+s);
write_date(0x30+g);
}
void writenyr(uchar add,uchar date) //1602顯示年月日
{
uchar s,g;
s=date/10;
g=date%10;
write_add(0x80+0x00+add);
write_date(0x30+s);
write_date(0x30+g);
}
void writexi(uchar a)
{
write_add(0x80+0x00+11);
if(a==1)
{
for(i=0;i3;i++)
{
write_date(xi1[i]);
delay(5);
}
}
if(a==2)
{
for(i=0;i3;i++)
{
write_date(xi2[i]);
delay(5);
}
}
if(a==3)
{
for(i=0;i3;i++)
{
write_date(xi3[i]);
delay(5);
}
}
if(a==4)
{
for(i=0;i3;i++)
{
write_date(xi4[i]);
delay(5);
}
}
if(a==5)
{
for(i=0;i3;i++)
{
write_date(xi5[i]);
delay(5);
}
}
if(a==6)
{
for(i=0;i3;i++)
{
write_date(xi6[i]);
delay(5);
}
}
if(a==7)
{
for(i=0;i3;i++)
{
write_date(xi7[i]);
delay(5);
}
}
}
void writefuhao(void) //寫符號(hào)
{
write_add(0x80+0x00+2);
write_date(table[2]);
delay(10);
write_add(0x80+0x00+5);
write_date(table[2]);
delay(10);
write_add(0x80+0x40+2);
write_date(time1[2]);
delay(10);
write_add(0x80+0x40+5);
write_date(time1[2]);
delay(10);
}
51單片機(jī)相關(guān)文章:51單片機(jī)教程
單片機(jī)相關(guān)文章:單片機(jī)教程
單片機(jī)相關(guān)文章:單片機(jī)視頻教程
單片機(jī)相關(guān)文章:單片機(jī)工作原理
評(píng)論