#include <reg52.h>
sbit DS1302_IO = P2^3;
sbit DS1302_CE = P1^3;
sbit DS1302_CK = P1^7;
sbit P25 = P2^5;
sbit P26 = P2^6;
bit flag200ms = 0; //200ms定时标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
void Xinqi(unsigned char week);
void Configtimer0(unsigned int ms);
void InitDS1302();
void DS1302BurstRead(unsigned char *dat);
void ShowLed(unsigned char index, unsigned char num, unsigned char point);
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1,0x86, 0x8E
};
unsigned char LedBuff[8] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF
};
void main()
{
unsigned char time[8]; //当前时间数组
EA = 1; //开总中断
ConfigTimer0(1); //T0定时1ms
InitDS1302(); //初始化实时时钟
while (1)
{
if (flag200ms) //每200ms读取依次时间
{
flag200ms = 0;
DS1302BurstRead(time); //读取DS1302当前时间
ShowLed(3,time[0]>>4,0);
ShowLed(2,time[0]&0xF,0);
ShowLed(7,time[2]>>4,0);
ShowLed(6,time[2]&0xF,1);
ShowLed(5,time[1]>>4,0);
ShowLed(4,time[1]&0xF,1);
Xinqi(time[5]);
}
}
}
void ShowLed(unsigned char index, unsigned char num, unsigned char point)
{
LedBuff[index] = LedChar[num]; //输入数字转换为数码管字符0~F
if (point != 0)
{
LedBuff[index] &= 0x7F; //point不为0时点亮当前位的小数点
}
}
void Xinqi(unsigned char week)
{
unsigned char code Led2[7]={
0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF
};
LedBuff[8] = Led2[week-1];
}
/* 发送一个字节到DS1302通信总线上 */
void DS1302ByteWrite(unsigned char dat)
{
unsigned char mask;
for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位移出
{
if ((mask&dat) != 0) //首先输出该位数据
DS1302_IO = 1;
else
DS1302_IO = 0;
DS1302_CK = 1; //然后拉高时钟
DS1302_CK = 0; //再拉低时钟,完成一个位的操作
}
DS1302_IO = 1; //最后确保释放IO引脚
}
/* 由DS1302通信总线上读取一个字节 */
unsigned char DS1302ByteRead()
{
unsigned char mask;
unsigned char dat = 0;
for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位读取
{
if (DS1302_IO != 0) //首先读取此时的IO引脚,并设置dat中的对应位
{
dat |= mask;
}
DS1302_CK = 1; //然后拉高时钟
DS1302_CK = 0; //再拉低时钟,完成一个位的操作
}
return dat; //最后返回读到的字节数据
}
/* 用单次写操作向某一寄存器写入一个字节,reg-寄存器地址,dat-待写入字节 */
void DS1302SingleWrite(unsigned char reg, unsigned char dat)
{
DS1302_CE = 1; //使能片选信号
DS1302ByteWrite((reg<<1)|0x80); //发送写寄存器指令
DS1302ByteWrite(dat); //写入字节数据
DS1302_CE = 0; //除能片选信号
}
/* 用单次读操作从某一寄存器读取一个字节,reg-寄存器地址,返回值-读到的字节 */
unsigned char DS1302SingleRead(unsigned char reg)
{
unsigned char dat;
DS1302_CE = 1; //使能片选信号
DS1302ByteWrite((reg<<1)|0x81); //发送读寄存器指令
dat = DS1302ByteRead(); //读取字节数据
DS1302_CE = 0; //除能片选信号
return dat;
}
void DS1302BurstWrite(unsigned char *dat)
{
unsigned char i;
DS1302_CE = 1;
DS1302ByteWrite(0xBE);
for(i=0; i<8; i++)
{
DS1302ByteWrite(dat);
}
DS1302_CE = 0;
}
void DS1302BurstRead(unsigned char *dat)
{
unsigned char i;
DS1302_CE = 1;
DS1302ByteWrite(0xBF);
for(i=0; i<8; i++)
{
dat = DS1302ByteRead();
}
DS1302_CE = 0;
}
/* DS1302初始化,如发生掉电则重新设置初始时间 */
void InitDS1302()
{
unsigned char dat;
unsigned char code InitTime[] = { //2013年10月8日 星期二 12:30:00
0x00,0x30,0x12, 0x08, 0x10, 0x02, 0x13
};
DS1302_CE = 0; //初始化DS1302通信引脚
DS1302_CK = 0;
dat = DS1302SingleRead(0); //读取秒寄存器
if ((dat & 0x80) != 0) //由秒寄存器最高位CH的值判断DS1302是否已停止
{
DS1302SingleWrite(7, 0x00); //撤销写保护以允许写入数据
DS1302BurstWrite(InitTime); //设置DS1302为默认的初始时间
}
}
/* 配置并启动T0,ms-T0定时时间 */
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp; //临时变量
tmp = 12000000 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 12; //补偿中断响应延时造成的误差
T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
void LedScan()
{
static unsigned char i = 0;
P26 = 1;
P0 = 0xFF;
switch (i)
{
case 0: P25 = 0;P0 = 0x80;
P25 = 1;i++; P0=LedBuff[0]; break;
case 1: P25 = 0;P0 = 0x40;
P25 = 1;i++; P0=LedBuff[1]; break;
case 2: P25 = 0;P0 = 0x20;
P25 = 1;i++; P0=LedBuff[2]; break;
case 3: P25 = 0;P0 = 0x10;
P25 = 1;i++; P0=LedBuff[3]; break;
case 4: P25 = 0;P0 = 0x08;
P25 = 1;i++; P0=LedBuff[4]; break;
case 5: P25 = 0;P0 = 0x04;
P25 = 1;i++; P0=LedBuff[5]; break;
case 6: P25 = 0;P0 = 0x02;
P25 = 1;i++; P0=LedBuff[6]; break;
case 7: P25 = 0;P0 = 0x01;
P25 = 1;i++; P0=LedBuff[7]; break;
case 8: P25 = 0;P26 = 0;i=0; P0=LedBuff[8];P26=1; break;
default: break;
}
}
/* T0中断服务函数,执行200ms定时 */
void InterruptTimer0() interrupt 1
{
static unsigned char tmr200ms = 0;
TH0 = T0RH; //重新加载重载值
TL0 = T0RL;
LedScan();
tmr200ms++;
if (tmr200ms >= 200) //定时200ms
{
tmr200ms = 0;
flag200ms = 1;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
谢谢分享
一周热门 更多>