//延时函数,延时时间可根据具体要求调整voidDelayMs{unsignedinti,j;forfor;}
//定义dll 股票交易接口,数码管引脚和DS1302引脚sbitDIG_1=P2^0;sbitDIG_2=P2^1;sbitDIG_3=P2^2;sbitDIG_4=P2^3;sbitDIG_5=P2^4;sbitDIG_6=P2^5;
//定义数字对应的片段constunsignedcharCODE_NUM[]={0xC0,/*0*/0xF9,/*1*/0xA4,/*2*/0xB0,/*3*/0x99,/*4*/0x92,/*5*/0x82,/*6*/0xF8,/*7*/0x80,/*8*/0x98,/*9*/0xFF,/*熄灭*/};
//将BCD码转换为二进制unsignedcharBcdToBin{return*10+);}
//从DS1302读取一个字节数据unsignedcharDS1302_ReadByte{unsignedchari,dat=0;for{DS1302_CLK=0;dat>>=1;ifdat|=0x80;elsedat&=0x7F;DS1302_CLK=1;}returndat;}
//向DS1302写入一个字节数据voidDS1302_WriteByte{unsignedchari;for{DS1302_CLK=0;DS1302_DAT=dat&0x01;dat>>=1;_nop_;DS1302_CLK=1;}}
//从DS1302读取一组时间数据voidDS1302_ReadTime{unsignedchari;DS1302_RST=1;DS1302_CLK=1;DS1302_RST=0;DS1302_WriteByte;for{time_buf[i]=DS1302_ReadByte;ifDS1302_WriteByte;}DS1302_RST=1;}
//设置DS1302的时间voidDS1302_SetTime{unsignedchari;DS1302_RST=1;DS1302_CLK=1;DS1302_RST=0;DS1302_WriteByte;forDS1302_WriteByte;DS1302_RST=1;}//将时间字符串显示到数码管上voidShowTime{unsignedchari;//将BCD码转换为二进制for{time_buf[i]=BcdToBin;
//显示小时,分钟和秒switch{case0:DIG_6=1;break;case1:DIG_5=1;break;case2:DIG_4=1;break;case3:DIG_3=1;break;case4:DIG_2=1;break;case5:DIG_1=1;break;default:break;}
P0=CODE_NUM[time_buf[i]%10];DelayMs;
P0=CODE_NUM[time_buf[i]/10];DelayMs;
DIG_6=0;DIG_5=0;DIG_4=0;DIG_3=0;DIG_2=0;DIG_1=0;}}
voidmain{unsignedchartime_buf;//初始化DS1302DS1302_DAT=1;DS1302_CLK=1;DS1302_RST=0;DS1302_WriteByte;DS1302_WriteByte;DS1302_RST=1;DS1302_SetTime;while{//从DS1302读取时间数据DS1302_ReadTime;//显示时间数据到数码管ShowTime;
文章为作者独立观点,不代表股票交易接口观点