|
#include<reg51.h>
#include<ch454cmd.h>
//寄存器宏定义
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define WRITE_PROTECT 0x8E
unsigned char time[3]={0x12,0x12,0x12}; //秒,分,时寄存器初始值
unsigned char code tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //0~~9段码 abcdefgh,共阳
/*************************ds1302与at89s52引脚连接********************/
unsigned char i,k,n,addr,kk,s;
sbit RST=P3^7;// P1^2
sbit CLK=P3^5;// P1^0
sbit IO=P3^6; // P1^1
sbit ACC_7 = ACC^7;
extern UINT8 CH454_Read(unsigned int read_cmd );
extern void ch454_write( UINT16 cmd );
unsigned char mBCD[16] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f,0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71 };//BCD
unsigned int count;
/***********************延时程序=a*2us**************************************/
void delay(unsigned int a)
{
while(a--);
}
/******************************************************************************/
void Write1302 ( unsigned char addr,dat )
{
unsigned char i,temp;
RST = 0; //CE引脚为低,数据传送中止
CLK = 0; //清零时钟总线
RST = 1; //CE引脚为高,逻辑控制有效
//发送地址
for ( i = 8; i > 0; i-- ) //循环8次移位
{
CLK = 0;
temp = addr;
IO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
CLK = 1;
}
//发送数据
for ( i=8; i>0; i-- )
{
CLK = 0;
temp = dat;
IO = (bit)(temp&0x01);
dat >>= 1;
CLK = 1;
}
RST = 0;
}
//数据读取子程序
unsigned char Read1302 ( unsigned char addr )
{
unsigned char i,temp,dat1,dat2;
RST = 0;
CLK=0;
RST = 1;
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
CLK = 0;
temp = addr;
IO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
CLK = 1;
}
//读取数据
for ( i=8; i>0; i-- )
{
ACC_7=IO;
CLK = 0;
ACC>>=1;
CLK = 1;
}
RST=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}
/*******************************************************************************/
void disp()
{
unsigned char m;
if(time[0]!=85)
{
m=time[0];
ch454_write( CH454_DIG0_LO | mBCD[m%10]);
ch454_write( CH454_DIG1_LO | mBCD[m/10]);
}
if(time[1]!=85)
{
m=time[1];
ch454_write( CH454_DIG2_LO | mBCD[m%10]);
ch454_write( CH454_DIG3_LO | mBCD[m/10]);
}
if(time[2]!=85)
{
m=time[2];
ch454_write( CH454_DIG6_LO | mBCD[m%10]);
ch454_write( CH454_DIG7_LO | mBCD[m/10]);
}
}
/*void init( void )//初始化
{
IE1 = 0;
EX1 = 1;
EA = 1;
} */
/*void int1( void ) interrupt 2 using 1 // INT1中断服务程序
{
ch454_key = CH454_Read( 0x0700 );
}*/
/********************************************************************************************/
void InitTimer0(void)
{
TMOD = 0x01;
TH0=(65536-50000)/128;
TL0=(65536-50000)%128;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void itr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count == 20)
{
count = 0;
time[2] = Read1302(0x85);
time[1] = Read1302(0x83);
time[0] = Read1302(0x81);
disp();
}
//TH0=(65536-50000)/256;
//TL0=(65536-50000)%256;
}
//========初始化
void init_1()
{
//delay(100);
Write1302(0x8e,0x00); //WP=0 写操作
Write1302(0x90,0xa5); //开启涓流充电 10101010
Write1302(0x80,time[0]);
Write1302(0x82,time[1]);
Write1302(0x84,time[2]);
Write1302(0x8e,0x80); //WP=1 写保护
// delay(100);
}
/************************主程序**********************************************/
void main(void)
{
unsigned char m;
ch454_write(0x441);//开显示
ch454_write(CH454_NO_BCD);//直接译码方式
// InitTimer0();
init_1();
count = 0;
while(1)
{
time[2] = Read1302(0x85);
time[1] = Read1302(0x83);
time[0] = Read1302(0x81);
if(time[0]!=85)
{
m=time[0];
ch454_write( CH454_DIG0_LO | mBCD[m%10]);
ch454_write( CH454_DIG1_LO | mBCD[m/10]);
}
if(time[1]!=85)
{
m=time[1];
ch454_write( CH454_DIG2_LO | mBCD[m%10]);
ch454_write( CH454_DIG3_LO | mBCD[m/10]);
}
if(time[2]!=85)
{
m=time[2];
ch454_write( CH454_DIG6_LO | mBCD[m%10]);
ch454_write( CH454_DIG7_LO | mBCD[m/10]);
}
}
/*RST = 0;
CLK = 0;
IO = 0; */
// while(1);
}
以上函数是DS1302程序,显示的数码管是接在CH454上,这段程序可以实现钟显示,可是,一但把主函数中的if去掉就会显示85,另外,被屏蔽掉的部分是用中断实现的,但是显示结果会是正常显示一下,然后显示85,再正常,再显示85...在中断中加入if(time!=85)就又不行了,被折腾几天了,向高手求助,,WHY???? |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|