电路设计,软件开发,专业项目团队承接电子产品开发!


ISPRO下载型编程器
火爆销售中

RS232/TTL转换器
全IC串口窃电

RS232/TTL转换线
全IC串口窃电

ByteBlasterMV Cable
CPLD/FPGA并口下载线

ByteBlaster II Cable ISPRO下载型编程器
支持S51/AVR/PIC
超级51开发板套件
前卫实用超值

PC/MCU/CPLD
三合一学习套件

全新万年历方案
AVR单片机开发板
 

Min51单片机仿真机
迷你型

ARM7开发板
仅售260元!
 

RS-232/RS-485双向接口电平转换器  

51单片机升级STM32
火爆销售中
MPLAB-ICD2
火爆销售中
LED51开发板
玩具又名图文显示棒

面板模块/开发板
经济危机明智决择

       

GSM MODEM GSM猫

SMS短信模块 工业级/全自动
火爆销售中

大功率数传电台

高速数传电台

您当前位置:电子技术资料 -> 外围驱动 -> 时钟 ->

祝您阅读愉快!

ISPRO下载型编程器

RS232 TO TTL转换器
↑↑本文相关产品介绍↑↑

完整的DS1302 C51驱动程序


类别:技术文章
来源:未知
作者:未知
关键字:DS1302
加入日期:2010-8-3

完整的DS1302 C51驱动程序

// DS1302时钟芯片 C 源程序
/*中国电子网*/
/*www.ec66.com*/
#pragma small
#include <reg52.h>

//下面是引脚连接关系
sbit ds1302_dat=P3^6;
sbit ds1302_clk=P2^6;
sbit ds1302_rst=P2^4;

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;

unsigned char ReadRam_Byte(unsigned char address); // 从Ram中读出一个字节
unsigned char ReadRTC_Byte(unsigned char address); // 从RTC中读出一个字节
unsigned char WriteRam_Byte(unsigned char address,unsigned char number); // 向
Ram中写入一个字节
unsigned char WriteRTC_Byte(unsigned char address,unsigned char number); // 向
RTC中写入一个字节
void ReadRam(unsigned char data * buffer,unsigned char size); // 从Ram中读出多
个字节
void ReadRTC(unsigned char data * buffer,unsigned char size); // 从RTC中读出多
个字节
void WriteRam(unsigned char data * buffer,unsigned char size); // 向Ram中写入多
个字节
void WriteRTC(unsigned char data * buffer,unsigned char size); // 向RTC中写入多
个字节
unsigned char ReadSec(void); // 从RTC中读出秒字节
unsigned char ReadMin(void); // 从RTC中读出分字节
unsigned char ReadHr(void); // 从RTC中读出时字节
unsigned char ReadDate(void); // 从RTC中读出日字节
unsigned char ReadMonth(void); // 从RTC中读出月字节
unsigned char ReadDay(void); // 从RTC中读出星期字节
unsigned char ReadYear(void); // 从RTC中读出年字节
void WriteSec(unsigned char number); // 向RTC中写入秒字节
void WriteMin(unsigned char number); // 向RTC中写入分字节
void WriteHr(unsigned char number); // 向RTC中写入时字节
void WriteDate(unsigned char number); // 向RTC中写入日字节
void WriteMonth(unsigned char number); // 向RTC中写入月字节
void WriteDay(unsigned char number); // 向RTC中写入星期字节
void WriteYear(unsigned char number); // 向RTC中写入年字节
void WriteControl(unsigned char number);// 向慢速充电控制寄存器写入控制字节
void EnableWP(void); // 禁止写保护
void DisableWP(void); // 允许写保护

unsigned char ReadRam_Byte(unsigned char address) // 从Ram中读出一个字节
{
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = address;
ACC <<= 1;
ACC |= 0xc1;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ds1302_dat = 1;
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;
return ACC;
}

unsigned char ReadRTC_Byte(unsigned char address) // 从RTC中读出一个字节
{
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = address;
ACC <<= 1;
ACC |= 0x81;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ds1302_dat = 1;
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;
return ACC;
}

unsigned char WriteRam_Byte(unsigned char address,unsigned char number) // 向
Ram中写入一个字节
{
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = address;
ACC <<= 1;
ACC |= 0xc0;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ACC = number;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ds1302_rst = 0;
ds1302_clk = 1;
}

unsigned char WriteRTC_Byte(unsigned char address,unsigned char number) // 向
RTC中写入一个字节
{
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = address;
ACC <<= 1;
ACC |= 0x80;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ACC = number;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

ds1302_rst = 0;
ds1302_clk = 1;
}

void ReadRam(unsigned char data * buffer,unsigned char size) // 从Ram中读出多
个字节
{
register unsigned char a,b;
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = 0xff;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

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(unsigned char data * buffer,unsigned char size) // 从RTC中读出多
个字节
{
register unsigned char a,b;
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = 0xbf;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

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 WriteRam(unsigned char data * buffer,unsigned char size) // 向Ram中写入多
个字节
{
register unsigned char a;
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = 0xfe;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

for(a=0;a<size;a++)
{
ACC = *(buffer+a);

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;
}
ds1302_rst = 0;
ds1302_clk = 1;
}

void WriteRTC(unsigned char data * buffer,unsigned char size) // 向RTC中写入多
个字节
{
// 注意:连续写RTC只能是8个字节一起写
register unsigned char a;
ds1302_clk = 0;
ds1302_rst = 0;
ds1302_rst = 1;
ACC = 0xbe;

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;

for(a=0;a<size;a++)
{
ACC = *(buffer+a);

ds1302_dat = ACC_0;
ds1302_clk = 1;
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;
}
ds1302_rst = 0;
ds1302_clk = 1;
}

unsigned char ReadSec(void) // 从RTC中读出秒字节
{
return ReadRTC_Byte(0);
}

unsigned char ReadMin(void) // 从RTC中读出分字节
{
return ReadRTC_Byte(1);
}

unsigned char ReadHr(void) // 从RTC中读出时字节
{
return ReadRTC_Byte(2);
}

unsigned char ReadDate(void) // 从RTC中读出日字节
{
return ReadRTC_Byte(3);
}

unsigned char ReadMonth(void) // 从RTC中读出月字节
{
return ReadRTC_Byte(4);
}

unsigned char ReadDay(void) // 从RTC中读出星期字节
{
return ReadRTC_Byte(5);
}

unsigned char ReadYear(void) // 从RTC中读出年字节
{
return ReadRTC_Byte(6);
}

void WriteSec(unsigned char number) // 向RTC中写入秒字节
{
WriteRTC_Byte(0,number);
}

void WriteMin(unsigned char number) // 向RTC中写入分字节
{
WriteRTC_Byte(1,number);
}

void WriteHr(unsigned char number) // 向RTC中写入时字节
{
WriteRTC_Byte(2,number);
}

void WriteDate(unsigned char number) // 向RTC中写入日字节
{
WriteRTC_Byte(3,number);
}

void WriteMonth(unsigned char number) // 向RTC中写入月字节
{
WriteRTC_Byte(4,number);
}

void WriteDay(unsigned char number) // 向RTC中写入星期字节
{
WriteRTC_Byte(5,number);
}

void WriteYear(unsigned char number) // 向RTC中写入年字节
{
WriteRTC_Byte(6,number);
}

void WriteControl(unsigned char number) // 向慢速充电控制寄存器写入控制字节
{
WriteRTC_Byte(8,number);
}

void EnableWP(void) // 禁止写保护
{
WriteRTC_Byte(7,0);
}

void DisableWP(void) // 允许写保护
{
WriteRTC_Byte(7,0x80);
}

void main(void)
{
unsigned char sec,min,hr,date,month,day,year,number;
unsigned char 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,5
8,59,60,61,62,63};
unsigned char data buffer2[8] = {45,45,15,13,3,2,01,0};
ReadRTC(buffer2,8);
EnableWP();

WriteControl(0xaa);

WriteRam(buffer1,32);
WriteRTC(buffer2,8);
DisableWP();
ReadRam(buffer1,32);
ReadRTC(buffer2,8);
sec = ReadSec();
min = ReadMin();
hr = ReadHr();
date = ReadDate();
month = ReadMonth();
day = ReadDay();
year = ReadYear();
ACC = 0;
}
/*注意事项:
1.每次上电,必须把秒寄存器高位(第7位)设置为0,时钟才能走时。
2.如果需要写入数据和时钟日历信息,必须把“写保护”寄存器设置成为0,方法入下:
EnableWP();
3.使能“慢速充电”,方法如下:
WriteControl(0xaa);
*/

/*
内部时钟日历地址图,始终日历用BCD码储存
0:秒
1:分
2:小时
3:日期
4:月份
5:星期
6:年
7:写保护寄存器
8:慢速充电寄存器
*/


您可能还会关注的相关技术资料:

中国电子网
www.EC66.com
QQ:34740080 EMAIL:iloveaoe@163.com
最佳浏览:1024*768
Copyright (C) 2001-2010 EC66.COM All Rights Reserved
Aoe/帝国 制作维护
粤ICP备18061901号