|
所谓测试版本只是搞了DS1302的读取工作和初始化工作,把从DS1302读出来的时间值显示到一块16*64的点阵屏幕上来
未增加时间设置功能,大家可以自己扩展
仿真文件只是代表理论值,很多设计细节为考虑,请大家不要直接根据仿真文件制作实物。
#include
//#include
#include "ds1302.c"
sbit STR=P3^2; //锁存
sbit CLK=P3^3; //时钟
sbit Data= P3^4; //红数据
sbit E= P1^4; //使能
unsigned char idata disbuff[8];//定义缓冲区
unsigned char code hanzi[][16]={
0x00,0x00,0x1C,0x22,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x22,0x1C,0x00,0x00,//0
0x00,0x00,0x08,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,//1
0x00,0x00,0x1C,0x22,0x41,0x41,0x01,0x02,0x0C,0x10,0x20,0x40,0x40,0x7F,0x00,0x00,//2
0x00,0x00,0x1C,0x22,0x41,0x01,0x02,0x1C,0x02,0x01,0x01,0x41,0x22,0x1C,0x00,0x00,//3
0x00,0x00,0x0C,0x0C,0x14,0x14,0x24,0x24,0x44,0x44,0xFF,0x04,0x04,0x04,0x00,0x00,//4
0x00,0x00,0x7F,0x40,0x40,0x40,0x5C,0x62,0x41,0x01,0x01,0x81,0x42,0x3C,0x00,0x00,//5
0x00,0x00,0x1C,0x22,0x41,0x40,0x5C,0x62,0x41,0x41,0x41,0x41,0x22,0x1C,0x00,0x00,//6
0x00,0x00,0x7F,0x01,0x02,0x02,0x04,0x04,0x04,0x08,0x08,0x08,0x08,0x08,0x00,0x00,//7
0x00,0x00,0x1C,0x22,0x41,0x41,0x22,0x1C,0x22,0x41,0x41,0x41,0x22,0x1C,0x00,0x00,//8
0x00,0x00,0x1C,0x22,0x41,0x41,0x41,0x41,0x23,0x1D,0x01,0x41,0x22,0x1C,0x00,0x00,//9
0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00, //冒号 No.10
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //空格 No.11
};
void sendbyte(unsigned char bbyte1)
{
unsigned char i;
for(i=0;i<8;i++)
{
Data=bbyte1&0x80;
CLK=0; CLK=1;
bbyte1<<=1;
}
}
void main(void)
{
unsigned char i;
E=0;
while(1)
{
Init_1302();
ReadTime();
disbuff[0]=hour/16;disbuff[1]=hour%16;
disbuff[3]=minute/16;disbuff[4]=minute%16;
disbuff[6]=second/16;disbuff[7]=second%16;
if(second%2==0){disbuff[2]=11;disbuff[5]=11;}
else{disbuff[2]=10;disbuff[5]=10;}
P1=0xff;
for(i=0;i<16;i++)
{
E=0;
sendbyte(hanzi[disbuff[0]]);
sendbyte(hanzi[disbuff[1]]);
sendbyte(hanzi[disbuff[2]]);
sendbyte(hanzi[disbuff[3]]);
sendbyte(hanzi[disbuff[4]]);
sendbyte(hanzi[disbuff[5]]);
sendbyte(hanzi[disbuff[6]]);
sendbyte(hanzi[disbuff[7]]);
P1=i;
STR=0;STR=1;
E=1;
}
}
}
//DS1302管脚定义
sbit T_CLK = P2^3;
sbit T_IO = P2^2;
sbit T_RST = P2^1;
sbit ACC0 = ACC^0;
sbit ACC1 = ACC^1;
sbit ACC2 = ACC^2;
sbit ACC3 = ACC^3;
sbit ACC4 = ACC^4;
sbit ACC5 = ACC^5;
sbit ACC6 = ACC^6;
sbit ACC7 = ACC^7;
//------------------------------------------------------------------------------------------
//时钟数据(7字节BCD码)格式为: 秒 分 时 日 月星期 年-----------------------------
unsigned char idata Time[7];//={0x30,0x42,0x23,0x18,0x06,0x04,0x09};
//------------------------------------------------------------------------------------------
unsigned char idata second,minute,hour;
/*******************************************************************************************
* 函数名: WriteByteDS1302
* 功能描述: 向DS1302中写入一个字节数据
* 入口参数: 无
* 出口参数: 无
*******************************************************************************************/
void WriteByteDS1302(unsigned char Data)
{
unsigned char i;
ACC = Data;
for(i=8; i>0; i--)
{
T_IO = ACC0;
T_CLK = 1;
T_CLK = 0;
ACC = ACC >> 1;
}
}
/*******************************************************************************************
* 函数名: ReadByteDS1302
* 功能描述: 在DS1302中读取一个字节数据
* 入口参数: 无
* 出口参数: 无
*******************************************************************************************/
unsigned char ReadByteDS1302(void)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
return(ACC);
}
/*******************************************************************************************
* 函数名: WriteDS1302
* 功能描述: 向DS1302某个指定地址中写入数据
* 入口参数: 无
* 出口参数: 无
*******************************************************************************************/
void WriteDS1302(unsigned char Addr,unsigned char Data)
{
T_RST = 0;
T_CLK = 0;
T_RST = 1;
WriteByteDS1302(Addr);
WriteByteDS1302(Data);
T_CLK = 1;
T_RST = 0;
}
/*******************************************************************************************
* 函数名: ReadDS1302
* 功能描述: 读取DS1302某个指定地址中数据
* 入口参数: 无
* 出口参数: 无
*******************************************************************************************/
unsigned char ReadDS1302(unsigned char Addr)
{
unsigned char Data;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
WriteByteDS1302(Addr);
Data = ReadByteDS1302();
T_CLK = 1;
T_RST = 0;
return(Data);
}
/*******************************************************************************************
* 函数名: ReadTime
* 功能描述: 读取DS1302中的时间值
* 入口参数: 无
* 出口参数: 无
*******************************************************************************************/
void ReadTime()
{
second = ReadDS1302(0x81);
minute = ReadDS1302(0x83);
hour = ReadDS1302(0x85);
}
/*****************************************************************************
//设置1302的初始时间(自动初始化)
//-设置1302的初始时间(2010年4月12日16时09分00秒星期一)
******************************************************************************/
void Init_1302(void)
{
unsigned char f;
if(ReadDS1302(0xc1) != 0xaa){
WriteDS1302(0x8e,0x00);//允许写操作
WriteDS1302(0x84,0x16);//小时
WriteDS1302(0x82,0x09);//分钟
WriteDS1302(0x80,0x00);//秒
WriteDS1302(0x90,0xa5);//充电
WriteDS1302(0xc0,0xaa);//写入初始化标志RAM(第00个RAM位置)
for(f=0;f<60;f=f+2){//清除闹钟RAM位为0
WriteDS1302(0xc2+f,0x00);
}
WriteDS1302(0x8e,0x80);//禁止写操作
}
}
仿真文件及测试工程包下载,仿真环境:proteus7.4sp3
测试代码.rar
(40.45 KB, 下载次数: 42)
[ 本帖最后由 飞雪008 于 2010-4-21 14:12 编辑 ]
|
|