新聞中心

DS1302控制代碼

作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
兩天的成果,
/*************************************/
#include
#define uint unsigned int
#define uchar unsigned char
uchar year,month,day,week,hours,minutes,seconds;
uchar flag1302,cnt;
/*************************************
控制按鍵
*************************************/
sbit K1=P1^0;
sbit K2=P1^2;
sbit K3=P1^4;
/*************************************
控制LCD液晶顯示
*************************************/
sbit lcdwrite=P2^5;
sbit lcddatecommand=P2^6;
sbit lcde=P2^7;
/*************************************
控制DS1302時(shí)鐘
*************************************/
sbit SCK=P3^6;
sbit RST=P3^5;
sbit SDA=P3^4;
/*************************************
延時(shí)子函數(shù)
*************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
{
for(y=0;y<=112;y++)
{
}
}
}
/*************************************
LCD寫(xiě)命令函數(shù)
*************************************/
void write_command(uchar command)
{
lcddatecommand=0; //開(kāi)啟寫(xiě)命令
lcdwrite=0; //只寫(xiě)不讀
P0=command;
delay(1);
lcde=1; //形成使能E高脈沖
delay(1);
lcde=0; //使能E拉低
}
/*************************************
LCD寫(xiě)數(shù)據(jù)函數(shù)
*************************************/
void write_date(uchar date)
{
lcddatecommand=1; //開(kāi)啟寫(xiě)數(shù)據(jù)
lcdwrite=0; //只寫(xiě)不讀
P0=date;
delay(1);
lcde=1; //形成使能E高脈沖
delay(1);
lcde=0; //使能E拉低
}
/*************************************
LCD屏幕初始化
*************************************/
void lcdInit()
{
lcde=0;
write_command(0x38);//設(shè)置16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
write_command(0x0c);//設(shè)置開(kāi)顯示,不顯示光標(biāo)
write_command(0x06);// 寫(xiě)一個(gè)字符后地址指針加1
write_command(0x01);//顯示清0,數(shù)據(jù)指針清0
}
/*************************************
DS1302寫(xiě)字節(jié)函數(shù)
*************************************/
void write_1302byte(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
SDA=date&0x01;
date=date>>1;
SCK=1;//將電平置為已知狀態(tài)
}
}
/*************************************
DS1302讀字節(jié)函數(shù)
*************************************/
uchar read_1302byte(uchar address)
{
uchar i,temp;
temp=0x00;
RST=0;
delay(1);
SCK=0;
delay(1);
RST=1;
delay(1);
write_1302byte(address);
for(i=0;i<8;i++)
{
if(SDA==1)
{
temp=temp|0x80; //每次傳輸?shù)妥止?jié)
}
temp=temp>>1;
SCK=1;
delay(1);
SCK=0;
delay(1);
}
return temp;
}
/*************************************
往DS1302中寫(xiě)數(shù)據(jù)
*************************************/
void write_1302(uchar address,uchar date)
{
RST=0;
delay(1);
SCK=0;
delay(1);
RST=1;
delay(1);
write_1302byte(address);
write_1302byte(date);
RST=0;
}
/*************************************
往DS1302中讀取數(shù)據(jù)
*************************************/
uchar read_1302(uchar address)
{
uchar date;
RST=0;
SCK=0;
RST=1;
write_1302byte(address);
date=read_1302byte(address);
SCK=1;
RST=0;
return date;
}
/*************************************
初始化時(shí)間
*************************************/
void Init_1302()
{
write_1302(0x8e,0x00); //寫(xiě)入不保護(hù)指令
write_1302(0x80,(14/10)<<4|(14%10));//14秒鐘
write_1302(0x82,(7/10)<<4|(7%10)); //7分鐘
write_1302(0x84,(3/10)<<4|(3%10)); //3小時(shí)
write_1302(0x86,(6/10)<<4|(6%10)); //6日
write_1302(0x88,(6/10)<<4|(6%10)); //6月
write_1302(0x8A,(4/10)<<4|(4%10)); //周四
write_1302(0x8C,(13/10)<<4|(13%10));//13年
write_1302(0x90,0xa5); //打開(kāi)充電功能,選擇2K電阻充電方式
write_1302(0x8e,0x80); //寫(xiě)保護(hù)
}
/*************************************
時(shí)鐘顯示
*************************************/
void DisplayHours(uchar hours)
{
uchar shi,ge;
shi=hours/10;
ge=hours%10;
write_command(0x06);
write_command(0x80+0x40);
write_date(shi+48);
write_date(ge+48);
write_date(:);
}
/*************************************
分鐘顯示
*************************************/
void DisplayMinutes(uchar minutes)
{
uchar shi,ge;
shi=minutes/10;
ge=minutes%10;
write_command(0x06);
write_command(0x80+0x43);
write_date(shi+48);
write_date(ge+48);
write_date(:);
}
上一頁(yè) 1 2 3 下一頁(yè)

關(guān)鍵詞: DS1302控制代

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉