📄 ds1302.h
字号:
/*DS1302的操作*/
#define uint unsigned int
#define uchar unsigned char
#define nop _nop_
#define SECOND 0
#define MINUTE 1
#define HOUR 2
#define DAY 3
#define MONTH 4
#define WEEK 5
#define YEAR 6
//#include"font.h"
//#include"intrins.h"
//#include "lcd12864.h"
//向DS1302某地址写1字节的数据/命令
void write_B(uchar addr,uchar ucda)
{
uchar i,temp;
sclk=0;
rst=0;
nop();
rst=1;//打开数据传送
for(i=0;i<8;i++) //写命令8位
{
sclk=0;
temp=addr;
io=(bit)(temp&0x01);
addr>>=1;
sclk=1; //上升沿写入
nop();
nop();
}
for(i=0;i<8;i++) //写数据8位
{
sclk=0;
temp=ucda;
io=(bit)(temp&0x01);
ucda>>=1;
sclk=1; //上升沿写入
nop();nop();
}
rst=0;
}
//从ds1302某地址读数据
/*在最后一个数据发送完了后,该脉冲的
下降沿就会输出数据的第一位!*/
uchar read_B(uchar addr)
{
uchar i,temp,dat1,dat2;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
sclk=0;
temp=addr;
io=(bit)(temp&0x01);
addr>>=1;
sclk=1; //上升沿写入
nop();
}
for(i=0;i<8;i++)
{
bit7=io;
sclk=1; //下降沿读取数据
sclk=0;
nop();
ACC>>=1;
}
rst=0;
dat1=ACC; //BCD与十六进制转化
dat2=dat1/16;
dat1=dat1%16;
dat1=dat1+dat2*16;
return dat1;
}
//设置ds1302的时间
/* 秒,分,时,日,月,周,年 注意顺序;
sel= 0 1 2 3 4 5 6*/
void set_1302(uchar sel,uchar newdate) //newdate是十进制,要转化为BCD
{
uchar temp,ge,shi;
temp=newdate;
shi=temp/10;
ge=temp%10;
newdate=(shi<<4)&0xf0+ge;
write_B(0x8e,0x00); //关闭写保护
write_B(0x80+sel*2,temp); //这里的newdate已是BCD了
write_B(0x8e,0x80); //允许写保护
}
//从ds1302读出1字节的数据显示到屏幕x,y上;
/* 秒,分,时,日,月,周,年
sel= 0 1 2 3 4 5 6 */
void dis_1302(uchar x,uchar y,uchar sel)
{
uchar shi,ge,temp,num;
temp=read_B(0x81+sel*2); //读的是BCD码
//BCD要转化为十进制
num=((temp&0x70)>>4)*10+(temp&0x0f);//num为十进制
shi=num/10;
ge=num%10;
dis_ch(x,y,number+shi*16);
dis_ch(x,y+1,number+ge*16);
}
//在ds1302中读星期值,并用汉字显示
void dis_1302_xingqi(uchar x,uchar y)
{
uchar ge,temp,num;
temp=read_B(0x81+WEEK*2); //读的是BCD码
//BCD转化为十进制
num=((temp&0x70)>>4)*10+(temp&0x0f);//num为十进制
ge=num%10; //星期的十位是0,所以可以只考虑个位
dis_hz(x,y,xingqi); //星
dis_hz(x,y+1,xingqi+32); //期
dis_hz(x,y+2,xingqi+(ge+1)*32);//汉字显示星期几
}
//初始化DS1302
void ds1302_init(void)
{
write_B(0x8e,0x00); //禁止写保护
/*初始化日期、月份等不能设为0;*/
/* write_B(0x80,0x00); //秒位初始化
write_B(0x82,0x01); //分钟初始化
write_B(0x84,0x01); //小时初始化
write_B(0x86,0x01); //日数初始化
write_B(0x88,0x01); //月份初始化
write_B(0x8a,0x04); //星期初始化
write_B(0x8c,0x09); //年份初始化
*/
write_B(0x8e,0x80); //允许写保护
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -