马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
//DS1302时钟芯片驱动程序
#i nclude <reg51.h>
//下面是引脚连接关系
sbit clock_dat=P1^0;
sbit clock_clk=P1^1;
sbit clock_clk=P1^2;
sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;
void clock_out(unsigned char dd) {
ACC=dd;
clock_dat=a0;clock_clk=1;clock_clk=0;
clock_dat=a1;clock_clk=1;clock_clk=0;
clock_dat=a2;clock_clk=1;clock_clk=0;
clock_dat=a3;clock_clk=1;clock_clk=0;
clock_dat=a4;clock_clk=1;clock_clk=0;
clock_dat=a5;clock_clk=1;clock_clk=0;
clock_dat=a6;clock_clk=1;clock_clk=0;
clock_dat=a7;clock_clk=1;clock_clk=0;
}
unsigned char clock_in(void) {
clock_dat=1;
a0=clock_dat;
clock_clk=1;clock_clk=0;a1=clock_dat;
clock_clk=1;clock_clk=0;a2=clock_dat;
clock_clk=1;clock_clk=0;a3=clock_dat;
clock_clk=1;clock_clk=0;a4=clock_dat;
clock_clk=1;clock_clk=0;a5=clock_dat;
clock_clk=1;clock_clk=0;a6=clock_dat;
clock_clk=1;clock_clk=0;a7=clock_dat;
return(ACC);
}
unsigned char read_clock(unsigned char ord) {
unsigned char dd=0;
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_out(ord);
dd=clock_in();
clock_rst=0;
clock_clk=1;
return(dd);
}
void write_clock(unsigned char ord,unsigned char dd) {
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_out(ord);
clock_out(dd);
clock_rst=0;
clock_clk=1;
}
void main(void) {
unsigned char address,d;
//调用方法:
//DS1302内部分为32字节RAM和8字节时钟日历数据
//读内部RAM 0:
address=0;
d=read_clock(0xc0|address);
//读内部RAM 1:
address=1;
d=read_clock(0xc0|address);
//写内部RAM 0,写入5:
address=0;d=5;
write_clock(0xc1|address,d);
//读内部RAM 1,写入123:
address=1;d=123;
d=read_clock(0xc1|address,d);
//=================================
//读内部时钟日历数据0:秒:
address=0;
d=read_clock(0x80|address);
//读内部时钟日历数据1:分:
address=1;
d=read_clock(0x80|address);
//写内部时钟日历数据0:秒,写入03秒
address=0;d=3;
write_clock(0x80|address,d);
} |