您的位置 首页 IOT

DS1302操控代码

两天的成果,*************************************includereg52h>defineuintunsignedintdefineucharunsigned

两天的效果,

/*************************************/
#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时钟
*************************************/
sbit SCK=P3^6;
sbit RST=P3^5;
sbit SDA=P3^4;
/*************************************
延时子函数
*************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x–)
{
for(y=0;y<=112;y++)
{
}
}
}
/*************************************
LCD写指令函数
*************************************/
void write_command(uchar command)
{
lcddatecommand=0; //敞开写指令
lcdwrite=0; //只写不读
P0=command;
delay(1);
lcde=1; //构成使能E高脉冲
delay(1);
lcde=0; //使能E拉低
}
/*************************************
LCD写数据函数
*************************************/
void write_date(uchar date)
{
lcddatecommand=1; //敞开写数据
lcdwrite=0; //只写不读
P0=date;
delay(1);
lcde=1; //构成使能E高脉冲
delay(1);
lcde=0; //使能E拉低
}
/*************************************
LCD屏幕初始化
*************************************/
void lcdInit()
{
lcde=0;
write_command(0x38);//设置16*2显现,5*7点阵,8位数据接口
write_command(0x0c);//设置开显现,不显现光标
write_command(0x06);// 写一个字符后地址指针加1
write_command(0x01);//显现清0,数据指针清0
}
/*************************************
DS1302写字节函数
*************************************/
void write_1302byte(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
SDA=date&0x01;
date=date>>1;
SCK=1;//将电平置为已知状况
}
}
/*************************************
DS1302读字节函数
*************************************/
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; //每次传输低字节
}
temp=temp>>1;
SCK=1;
delay(1);
SCK=0;
delay(1);
}
return temp;
}
/*************************************
往DS1302中写数据
*************************************/
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中读取数据
*************************************/
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;
}
/*************************************
初始化时刻
*************************************/
void Init_1302()
{
write_1302(0x8e,0x00); //写入不维护指令
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小时
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); //翻开充电功用,挑选2K电阻充电方法
write_1302(0x8e,0x80); //写维护
}
/*************************************
时钟显现
*************************************/
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(:);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/iot/255144.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部