"); //-->
/******************************************************************
功能:利用8563将日期时间实时显示到数码管
*******************************************************************/
#include<ADuC7026.h>
#define count 0x06 // 在从机中读出的字节数(0代表读出一个字节)
#define time1 100
extern void lcdinitinal(void);
extern void numberdisplay(int row,int line,int num);
extern void chardisplay(int row,int line,unsigned char *zifu,int num);
extern void chinsesdisplay(int row,int line,unsigned char *zifu,int num);
extern void LCDclear();
extern void lcd_mesg(unsigned char add,unsigned char *adder1,int lengh);
extern void delayms(unsigned int n);
extern void delayus(unsigned int m);
// 8563地址定义 // 8563 从地址
#define PCF8563 0xA2 // 寄存器地址
int Byteaddress = 0x00;
int i = 0, dat[7];
int t="7";
int Stime[6]={0,10,10,10,4,9},Gtime[6]; // 起始时间和现在时间
unsigned char nowtime[8]={' ',' ',':',' ',' ',':',' ',' '};
unsigned char biaoti[]={"8563测试程序"};
unsigned char date[]={"日期: 2009.4.10"};
unsigned char time[]={"时间:10: 10: 00"};
unsigned char maohao[]={":"};
unsigned char space[]={" "};
void SysClkInit(unsigned char clock)
{
PLLKEY1=0xAA;
PLLCON="0x01"; // PLL配置
PLLKEY2=0x55;
POWKEY1=0x01;
POWCON="clock"; // CPU时钟配置
POWKEY2=0xF4;
}
void delay (int length)
{
while (length >0)
length--;
}
//十进制数转BCD码
int Decimal_BCD(int input)
{
int temp,result1=0;
temp=input/10;
result1|=(temp<<4);
temp=input%10;
temp&=(0x0f);
result1|=temp;
return result1;
}
//BCD码转十进制数
int BCD_Decimal(int input)
{
int temp,result1;
result1=0;
temp=(input>>4)*10;
result1=temp+(input&0x0f);
return result1;
}
void I2C_read(unsigned char slvadd,unsigned char subadd)
{
I2C1MTX = subadd; // 装载寄存器子地址
I2C1ADR = slvadd; // 装载从机地址
while ((I2C1FSTA & 0x30) != 0x00) {} // 等待FIFO发送完成
delay(10000); // 长延时
I2C1CNT = count; // 在从机中一次读出的字节数
I2C1ADR = slvadd+1; // 装载从机地址 (LSB = 1, 主机读)
for(i=0;i<7;i++)
{
while ((I2C1FSTA & 0xC0) != 0x80){}
dat[i]=I2C1MRX;
//delay(200);
}
I2C1CCNT = 0x80;
}
void I2C_write(unsigned char slvadd,unsigned char subadd,int data)
{
I2C1MTX = subadd; // 装载寄存器子地址
I2C1ADR = slvadd; // 装载从机地址
while ((I2C1FSTA & 0x30) != 0x00) {} // 等待FIFO发送完成
delay(time1);
I2C1MTX = data;
while ((I2C1FSTA & 0x30) != 0x00) {} // 等待FIFO发送完成
delay(time1);
I2C1CCNT = 0x80;
}
void init8563() // 8563IIC初始化
{
GP1CON = 0x2200; // 配置P1.2和P1.3为SCL和SDA
I2C1DIV = 0x3232;
I2C1CFG = 0x82; // 主机使能并且使能主机时钟
}
void write8563() // 写8563子程序
{
I2C_write(PCF8563,Byteaddress,0x20);
delay(200);
Byteaddress=0x02;
while(t>0)
{
if(Byteaddress!=0x06)
{
if(Byteaddress==0x02||Byteaddress==0x07)
{
I2C_write(PCF8563,Byteaddress,Decimal_BCD(Stime[i])&0x7F);
delay(200);
Byteaddress++;
t--;
i++;
}
else
{
I2C_write(PCF8563,Byteaddress,Decimal_BCD(Stime[i]));
delay(200);
Byteaddress++;
t--;
i++;
}
}
else
{
Byteaddress++;
t--;
}
}
Byteaddress = 0x00;
I2C_write(PCF8563,Byteaddress,0x00);
}
void read8563() // 读8563子程序
{
Byteaddress = 0x00;
I2C_write(PCF8563,Byteaddress,0x20);
delay(200);
Byteaddress = 0x02;
I2C_read(PCF8563,Byteaddress);
Gtime[0]=BCD_Decimal(dat[0]&0x7F);
Gtime[1]=BCD_Decimal(dat[1]&0x7F);
Gtime[2]=BCD_Decimal(dat[2]&0x3F);
Gtime[3]=BCD_Decimal(dat[3]&0x3F);
Gtime[4]=BCD_Decimal(dat[5]&0x1F);
Gtime[5]=BCD_Decimal(dat[6]);
Byteaddress = 0x00;
I2C_write(PCF8563,Byteaddress,0x00);
}
void main()
{
SysClkInit(0);
lcdinitinal(); // 液晶初始化
chinsesdisplay(1,2,biaoti,6); // 显示标题
chinsesdisplay(2,1,date,8);
chinsesdisplay(3,1,time,8);
// 配置8563起始日期,时间
init8563();
write8563();
// Timer配置
T1LD = 0x280000; // 配置计数器的值,大约为1秒,41.78MHz :0x280000, 10.44MHz : 0xA0000
T1CON = 0xC4; // 使能Timer1,周期模式,二进制数据格式,CLK/16
while(1)
{
while(T1VAL!=0){} // 等待计数为零
T1CON = 0x00; // 不使能Timer1
// 读8563现在时间
init8563();
read8563();
// 显示秒
chardisplay(3,8,space,2);
delayms(2);
numberdisplay(3,8,Gtime[0]);
// 显示分
if(Gtime[0]==0)
{
chardisplay(3,6,space,2);
delayms(5);
numberdisplay(3,6,Gtime[1]);
}
// 显示时
if(Gtime[1]==0)
{
chardisplay(3,4,space,2);
delayms(5);
numberdisplay(3,4,Gtime[2]);
}
T1LD = 0x280000; // 配置计数器的值,大约为1秒,41.78MHz :0x280000, 10.44MHz : 0xA0000
T1CON = 0xC4; // 使能Timer1,周期模式,二进制数据格式,CLK/16
}
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。