|
- #include<reg52.h>
- #include<intrins.h>
- #define MAIN_Fosc 11059200UL
- /*自定义类型明*/
- typedef unsigned char INT8U;
- typedef unsigned char uchar;
- typedef unsigned int INT16U;
- typedef unsigned int uint;
- /*声明硬件的接口*/
- sbit TSCLK = P1^0;//时钟线
- sbit TIO = P1^1; //数据线
- sbit TRST = P1^2; //使能端
- sbit DU = P2^6;//段选
- sbit WE = P2^7;//位选
- /*共阳极数码管段选码*/
- uchar code table[]={
- // 0 1 2 3 4 5
- 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
- // 6 7 8 9 . 关显示
- 0x82, 0xf8, 0x80, 0x90, 0x7f, 0xff
- };
- /*位选码*/
- uchar code T_COM[] = {
- //1 2 3 4 5 6
- 0x01, 0x02, 0x04, 0x08, 0x10, 0x20
- };
- /*延时函数*/
- void Delay_Ms(INT16U ms)
- {
- INT16U i;
- do{
- i = MAIN_Fosc/96000;
- while(--i);
- }while(--ms);
- }
- /*显示函数*/
- void Display(uchar Hour,Min,Sec)
- {
- //第一位数码管 小时十位
- DU = 0;
- P0 = table[Hour/10];
- DU = 1;
- DU = 0;
- WE = 0;
- P0 = T_COM[0];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- //第二位数码管 小时个位
- DU = 0;
- P0 = table[Hour%10] | 0x7f;// 0111 1111 显示小数点
- DU = 1;
- DU = 0;
- WE = 0;
- P0 = T_COM[1];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- //第三位数码管
- DU = 0;
- P0 = table[Min/10];
- DU = 1;
- DU = 0;
- WE = 0;
- P0 = T_COM[2];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- //第四位数码管
- DU = 0;
- P0 = table[Min%10] | 0x7f;
- DU = 1;
- DU = 0;
- WE = 0;
- P0 = T_COM[3];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- //第五位数码管
- DU = 0;
- P0 = table[Sec/10];
- DU = 1;
- DU = 0;
-
- WE = 0;
- P0 = T_COM[4];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- //第六位数码管
- DU = 0;
- P0 = table[Sec%10];
- DU = 1;
- DU = 0;
- WE = 0;
- P0 = T_COM[5];
- WE = 1;
- WE = 0;
- Delay_Ms(3);
- }
- //写DS1302
- void Writer_DS1302_DAT(uchar cmd,uchar dat) //命令和数据需要给出
- {
- uchar i;
- TRST = 0;
- TSCLK = 0;
- TRST = 1;
- for(i = 0; i < 8; i++)
- {
- TSCLK = 0;
- TIO = cmd & 0x01;
- TSCLK = 1;//拉高时钟总线接收数据
- dat >>= 1;//右移一位 往最低位移 左移往最高位移 从低位开始发送 需右移 把八位数据出 从高位发送需左移
- }
- for(i = 0; i < 8; i++)
- {
- TSCLK = 0;
- TIO = dat & 0x01;
- TSCLK = 1;
- dat >>= 1;
- }
- }
- //读DS1302数据
- uchar Read_DS1302_DAT(uchar cmd)
- {
- uchar i, dat;
- TRST = 0;
- TSCLK = 0;
- TRST = 1;
- for(i = 0;i < 8; i++)// 写入命令
- {
- TSCLK = 0;
- TIO = cmd & 0x01;
- TSCLK = 1;
- dat >>= 1; //
- }
- for(i = 0; i < 8 ; i++)// 读八次
- {
- TSCLK = 0;
- dat >>= 1;
- if(TIO) dat |= 0x80;
- TSCLK = 1;
- }
- return dat;
- }
- //数据转BCD
- uchar Dat_Chg_BCD(uchar dat)
- {
- uchar dat1,dat2;
- dat1 = dat / 10;
- dat2 = dat % 10;
- dat2 = dat2 + dat1 * 16;
- return dat2;
- }
- //BCD转数据
- uchar BCD_Chg_Dat(uchar dat)
- {
- uchar dat1,dat2;
- dat1 = dat / 16;
- dat2 = dat % 16;
- dat2 = dat2 + dat1 * 10;
- return dat2;
- }
- void main()
- {
- uchar i;
- uchar Sec,Min,Hour;
- Writer_DS1302_DAT(0x8e, 0);
- Writer_DS1302_DAT(0x80,Dat_Chg_BCD(30));
- Writer_DS1302_DAT(0x82,Dat_Chg_BCD(15));
- Writer_DS1302_DAT(0x84,Dat_Chg_BCD(19));
- Writer_DS1302_DAT(0x8e,0x80);
- while(1)
- {
- Writer_DS1302_DAT(0x8e,0);
- Sec = BCD_Chg_Dat(Read_DS1302_DAT(0x81));
- Min = BCD_Chg_Dat(Read_DS1302_DAT(0x83));
- Hour = BCD_Chg_Dat(Read_DS1302_DAT(0x85));
- Writer_DS1302_DAT(0x8e,0x80);
- for(i = 0;i < 50; i++)
- {
- Display(Hour,Min,Sec);
- }
- }
- }
复制代码
为什么仿真的时候 数码管显示数字很乱 |
|