0467.cC
海量文库 文档专家
当前位置:首页 >> >>

单片机驱动Ds1302时钟芯片的时钟系统设计包括源程序代码

单片机驱动 Ds1302 时钟芯片的时钟系统包括源程序代码 一.硬件设计
二.软件设计 #include <reg52.h> #define uchar unsigned char sbit ds1302_clk=P1^0; sbit ds1302_dat=P1^1;

sbit ds1302_rst=P1^2;
sbit ACC_0 = ACC^0; sbit ACC_1 = ACC^1; sbit ACC_2 = ACC^2; sbit ACC_3 = ACC^3; sbit ACC_4 = ACC^4; sbit ACC_5 = ACC^5; sbit ACC_6 = ACC^6; sbit ACC_7 = ACC^7;

void Read(void);

//写 DS1302 操作

void Write(void);

//读 DS1302 操作

uchar ReadRam_Byte(uchar address);

//从 Ram 中读出一个字节

uchar ReadRTC_Byte(uchar address);

//从 DS1302 中读出一个字节

uchar WriteRam_Byte(uchar address,uchar number);

//向 Ram 中写入一个字节

uchar WriteRTC_Byte(uchar address,uchar number);

//向 DS1302 中写入一个字节

void ReadRam(uchar data * buffer,uchar size);

//从 Ram 中读出多个字节

void ReadRTC(uchar data * buffer,uchar size);

//从 DS1302 中读出多个字节

void WriteRam(uchar data * buffer,uchar size);

//向 Ram 中写入多个字节

void WriteRTC(uchar data * buffer,uchar size);

//向 DS1302 中写入多个字节

uchar ReadSec(void);

//从 DS1302 中读出秒字节

uchar ReadMin(void);

//从 DS1302 中读出分字节

uchar ReadHr(void);

//从 DS1302 中读出时字节

uchar ReadDay(void);

//从 DS1302 中读出日字节

uchar ReadMonth(void);

//从 DS1302 中读出月字节

uchar ReadWeek(void);

//从 DS1302 中读出星期字节

uchar ReadYear(void);

//从 DS1302 中读出年字节

void WriteSec(uchar number);

//向 DS1302 中写入秒字节

void WriteMin(uchar number);

//向 DS1302 中写入分字节

void WriteHr(uchar number);

//向 DS1302 中写入时字节

void WriteDay(uchar number);

//向 DS1302 中写入日字节

void WriteMonth(uchar number);

//向 DS1302 中写入月字节

void WriteWeek(uchar number);

//向 DS1302 中写入星期字节

void WriteYear(uchar number);

//向 DS1302 中写入年字节

void WriteControl(uchar number);

//向慢速充电控制寄存器写入控制字节

void EnableWP(void);

//禁止写保护

void DisableWP(void);

//允许写保护

void Write(void) {
ds1302_dat = ACC_0; ds1302_clk = 1;

//写 DS1302 操作
//写 DS1302 //时钟上升沿,发送数据有效

ds1302_clk = 0;

//清零时钟总线

ds1302_dat = ACC_1;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_2;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_3;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_4;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_5;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_6;

ds1302_clk = 1; ds1302_clk = 0;

ds1302_dat = ACC_7;

ds1302_clk = 1; ds1302_clk = 0;

}

void Read(void) {
ACC_0 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_1 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_2 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_3 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_4 = ds1302_dat; ds1302_clk = 1;ds1302_clk = 0; ACC_5 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_6 = ds1302_dat; ds1302_clk = 1; ds1302_clk = 0; ACC_7 = ds1302_dat; ds1302_rst = 0; ds1302_clk = 1; }

//读 DS1302 操作
//读取 DS1302 数据 //时钟上升沿,发送数据有效 //清零时钟总线

uchar ReadRam_Byte(uchar address) {
ds1302_rst = 0; ds1302_clk = 0; ds1302_rst = 1; ACC = address;

//从 RAM 中读出一个字节
//复位引脚为低电平,所有数据传送中止 //清零时钟总线 //复位引脚为高电平,逻辑控制有效

Write(); ds1302_dat = 1; Read(); return ACC; }

//写 DS1302 操作 //读 DS1302 操作

uchar ReadRTC_Byte(uchar address)

//从 DS1302 中读出一个字节

{

ds1302_rst = 0;

//复位引脚为低电平,所有数据传送中止

ds1302_clk = 0;

//清零时钟总线

ds1302_rst = 1;

//复位引脚为高电平,逻辑控制有效

ACC = address;

Write();

//对 DS1302 写命令字和地址

ds1302_dat = 1;

Read();

//读 DS1302 操作

return ACC;

//返回读取的数据

}

uchar WriteRam_Byte(uchar address,uchar number) //向 RAM 中写入一个字节

{

ds1302_clk = 0; ds1302_rst = 0;ds1302_rst = 1;

ACC = address;

Write();

//对 DS1302 写命令字和地址

ACC = number;

Write();

//对 DS1302 写命令字和数据

ds1302_rst = 0;

ds1302_clk = 1;

return ACC;

}

uchar WriteRTC_Byte(uchar address,uchar number) //向 DS1302 中写入一个字节

{

ds1302_clk = 0;

ds1302_rst = 0;ds1302_rst = 1;

ACC = address;

ACC |= 0x80;

Write();

//对 DS1302 写命令字和地址

ACC = number;

Write();

//对 DS1302 写命令字和数据

ds1302_rst = 0; ds1302_clk = 1;

return ACC;

}

void ReadRam(uchar data * buffer,uchar size) //从 RAM 中读出多个字节

{

register uchar a,b;

ds1302_clk = 0;

ds1302_rst = 0;

ds1302_rst = 1;

ACC = 0xff;

//命令字为 FFH

Write();

//对 DS1302 写命令字和地址

ds1302_dat = 1;

for(a=0;a<size;a++)

{

ACC_0 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_1 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_2 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_3 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_4 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_5 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_6 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_7 = ds1302_dat;

b = ACC;

*(buffer+a)=b;

ds1302_clk = 1; ds1302_clk = 0;

}

ds1302_rst = 0; ds1302_clk = 1;

}

void ReadRTC(uchar data * buffer,uchar size)

//从 DS1302 中读出多个字节

{

register uchar a,b;

ds1302_clk = 0;

ds1302_rst = 0;

ds1302_rst = 1;

ACC = 0xbf;

//命令字为 BFH

Write();

//对 DS1302 写命令字和地址

ds1302_dat = 1;

for(a=0;a<size;a++)

//读出 DS1302 数据

{

ACC_0 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_1 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_2 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_3 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_4 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_5 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_6 = ds1302_dat;

ds1302_clk = 1; ds1302_clk = 0; ACC_7 = ds1302_dat;

b = ACC; *(buffer+a)=b; ds1302_clk = 1; ds1302_clk = 0; } ds1302_rst = 0; ds1302_clk = 1; }

void WriteRam(uchar data * buffer,uchar size) //向 RAM 中写入多个字节

{

register uchar a;

ds1302_clk = 0;

ds1302_rst = 0;

ds1302_rst = 1;

ACC = 0xfe;

//命令字为 FE

Write();

//对 DS1302 写命令字和地址

for(a=0;a<size;a++)

{

ACC = *(buffer+a);

Write();

//对 DS1302 写数据

}

ds1302_rst = 0;

ds1302_clk = 1;

}

void WriteRTC(uchar data * buffer,uchar size) //向 DS1302 中写入多个字节

{//注意:连续写 DS1302 只能是 8 个字节一起写

register uchar a;

ds1302_clk = 0;

ds1302_rst = 0;

ds1302_rst = 1;

ACC = 0xbe;

//命令字为 BEH

Write();

//对 DS1302 写命令字和地址

for(a=0;a<size;a++)

{

ACC = *(buffer+a);

Write();

//对 DS1302 写数据

}

ds1302_rst = 0;

ds1302_clk = 1;

}

uchar ReadSec(void) {

//从 DS1302 中读出秒字节

return ReadRTC_Byte(0x81); }

uchar ReadMin(void) {
return ReadRTC_Byte(0x83); }

//从 DS1302 中读出分字节

uchar ReadHr(void)

//从 DS1302 中读出时字节

{

return ReadRTC_Byte(0x85);

}

uchar ReadDay(void)

//从 DS1302 中读出日字节

{

return ReadRTC_Byte(0x87);

}

uchar ReadMonth(void) {
return ReadRTC_Byte(0x89); }

//从 DS1302 中读出月字节

uchar ReadWeek(void) {
return ReadRTC_Byte(0x8B); }

//从 DS1302 中读出星期字节

uchar ReadYear(void) {
return ReadRTC_Byte(0x8D); }

//从 DS1302 中读出年字节

void WriteSec(uchar number) {
WriteRTC_Byte(0x80,number); }

//向 DS1302 中写入秒字节

void WriteMin(uchar number) {
WriteRTC_Byte(0x82,number); }

//向 DS1302 中写入分字节

void WriteHr(uchar number)

//向 DS1302 中写入时字节

{ WriteRTC_Byte(0x84,number);
}

void WriteDay(uchar number) {
WriteRTC_Byte(0x86,number); }

//向 DS1302 中写入日字节

void WriteMonth(uchar number) {
WriteRTC_Byte(0x88,number); }

//向 DS1302 中写入月字节

void WriteWeek(uchar number) {
WriteRTC_Byte(0x8A,number); }

//向 DS1302 中写入星期字节

void WriteYear(uchar number) {
WriteRTC_Byte(0x8C,number); }

//向 DS1302 中写入年字节

void WriteControl(uchar number) {
WriteRTC_Byte(0x90,number); }

//向慢速充电控制寄存器写入控制字节

void DisableWP(void) {
WriteRTC_Byte(0x8E,0); }

//禁止写保护

void EnableWP(void)

//允许写保护

{

WriteRTC_Byte(0x8E,0x80);

}

51 单片机驱动 Ds1302 时钟芯片的时钟系统设计包括源程序代码

void main(void) {

//主程序

uchar sec,min,hr,day,month,week,year;

uchar data buffer1[32] =

{32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,

63};

uchar data buffer2[8] = {45,45,15,13,3,2,01,0};

ReadRTC(buffer2,8);

DisableWP();

//禁止写保护

WriteControl(0xaa);

//向慢速充电控制寄存器写入控制字节

WriteRam(buffer1,31);

//写 DS1302RAM

WriteRTC(buffer2,8);

//写 DS1302 时钟

EnableWP();

//允许写保护

ReadRam(buffer1,31);

//读 DS1302RAM

ReadRTC(buffer2,8);

//读 DS1302 时钟

sec = ReadSec();

//读秒

min = ReadMin();

//读分

hr = ReadHr();

//读小时

day = ReadDay();

//读日

month = ReadMonth();

//读月

week = ReadWeek();

//读星期

year = ReadYear();

//读年

ACC = 0;

}


网站首页 | 网站地图
All rights reserved Powered by 0467资源网 0467.cc
copyright ©right 2014-2019。
文档资料库内容来自网络,如有侵犯请联系客服。liunxqq@126.com