赞
踩
- #include "reg52.h"
- #include "ds1302.h"
- #include "iic.h"
- sfr P4 = 0xC0;
-
- sbit R1=P3^0;
- sbit R2=P3^1;
- sbit R3=P3^2;
- sbit R4=P3^3;
-
- sbit C1=P4^4;
- sbit C2=P4^2;
- sbit C3=P3^5;
- sbit C4=P3^4;
-
- code unsigned char Seg_Table[] =
- {
- 0xc0, //0
- 0xf9, //1
- 0xa4, //2
- 0xb0, //3
- 0x99, //4
- 0x92, //5
- 0x82, //6
- 0xf8, //7
- 0x80, //8
- 0x90, //9
- 0x88, //A
- 0x83, //b
- 0xc6, //C
- 0xa1, //d
- 0x86, //E
- 0x8e //F
- };
-
- unsigned char Write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
- unsigned char Read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
- unsigned char Timer[7]={0x59,0x09,0x23,0x10,0x04,0x03,0x24};
- unsigned char led4=0;
- void DelaySMG(unsigned int t)
- {
- while(t--);
- }
- void Delay1ms() //@12.000MHz
- {
- unsigned char i, j;
-
- i = 12;
- j = 169;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay(unsigned char t)
- {
- unsigned char i;
- for(i=0;i<t;i++)
- {
- Delay1ms();
- }
- }
- void SelectHc573(unsigned char channel,unsigned char value)
- {
- P0=value;
- switch(channel)
- {
- case 4:
- P2 = ( P2 & 0x1f ) | 0x80;
- break;
- case 5:
- P2 = ( P2 & 0x1f ) | 0xa0;
- break;
- case 6:
- P2 = ( P2 & 0x1f ) | 0xc0;
- break;
- case 7:
- P2 = ( P2 & 0x1f ) | 0xe0;
- break;
- case 0:
- P2 = ( P2 & 0x1f ) | 0x00;
- break;
- }
- P2 = ( P2 & 0x1f ) | 0x00;
- }
-
-
- void ShowSMG_Bit(unsigned char pos,unsigned char value)
- {
- SelectHc573(6,0x01<<pos);
- SelectHc573(7,value);
- DelaySMG(500);
- SelectHc573(6,0x01<<pos);
- SelectHc573(7,0xff);
- }
-
- void ShowSMG_All(unsigned char value)
- {
- SelectHc573(6,0xff);
- SelectHc573(7,value);
- }
-
-
- //1)通过读取DS1302 RTC芯片,获取时间数据;
- //初始化
- void Init_DS1302()
- {
- unsigned char i;
- Write_Ds1302_Byte(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- Write_Ds1302_Byte(Write[i],Timer[i]);
- }
- Write_Ds1302_Byte(0x8e,0x80);
- }
- //不断读取时钟数据
- void Read_DS1302()
- {
- unsigned char i;
- for(i=0;i<7;i++)
- {
- Timer[i]=Read_Ds1302_Byte(Read[i]);
- }
- }
- //时间界面
- void ShowSMG_Timer()
- {
- ShowSMG_Bit(0,Seg_Table[Timer[2]/16]);
- ShowSMG_Bit(1,Seg_Table[Timer[2]%16]);
- ShowSMG_Bit(2,0xbf);
- ShowSMG_Bit(3,Seg_Table[Timer[1]/16]);
- ShowSMG_Bit(4,Seg_Table[Timer[1]%16]);
- ShowSMG_Bit(5,0xbf);
- ShowSMG_Bit(6,Seg_Table[Timer[0]/16]);
- ShowSMG_Bit(7,Seg_Table[Timer[0]%16]);
- }
- //2)通过EEPROM实现数据记录功能;
- /*输入数据起始时间(时):E2PROM内部地址0;
- 输入数据起始时间(分):E2PROM内部地址1;
- 输入数据(高字节):E2PROM内部地址2;
- 输入数据(低字节):E2PROM内部地址3;*/
- unsigned char h=0; //记录输入4位数据的起始时和分数据
- unsigned char min=0;
- void Write_Eeprom(unsigned char add,unsigned char value)
- {
- I2CStart();
- I2CSendByte(0xa0);
- I2CWaitAck();
- I2CSendByte(add);
- I2CWaitAck();
- I2CSendByte(value);
- I2CSendAck(1);
- I2CStop();
- }
- unsigned char Read_Eeprom(unsigned char add)
- {
- unsigned char dat;
- I2CStart();
- I2CSendByte(0xa0);
- I2CWaitAck();
- I2CSendByte(add);
- I2CWaitAck();
-
- I2CStart();
- I2CSendByte(0xa1);
- I2CWaitAck();
- dat=I2CReceiveByte();
- I2CSendAck(1);
- I2CStop();
- return dat;
- }
- //3)通过LED指示灯完成试题要求的状态指示功能;
-
- //4)通过数码管、按键完成试题要求的数据显示和界面切换功能。
- unsigned char stat=1; //记录页面号
- unsigned int Egg=0; //记录加的值
-
- unsigned char Egg_OK()
- {
- if((Egg/1000)!=0) //代表着四位数字记录完成
- {
- //完成之后可以向存储器中存入数据
- unsigned int Eggt=Egg;
- unsigned char LSB,MSB;
- LSB=Eggt;
- MSB=Eggt>>8;
- Write_Eeprom(0x02,MSB);
- Delay(5);
- Write_Eeprom(0x03,LSB);
- Delay(5);
- return 0;
- }
- else
- {
- return 1;
- }
- }
- //输入界面
- void Show_Scan()
- {
- ShowSMG_Bit(0,Seg_Table[12]);
- ShowSMG_Bit(1,0xff);
- ShowSMG_Bit(2,0xff);
- ShowSMG_Bit(3,0xff);
- if(Egg>999)
- {
- ShowSMG_Bit(4,Seg_Table[Egg/1000]);
- }
- else
- {
- ShowSMG_Bit(4,0xff);
- }
- if(Egg>99)
- {
- ShowSMG_Bit(5,Seg_Table[Egg/100%10]);
- }
- else
- {
- ShowSMG_Bit(5,0xff);
- }
- if(Egg>9)
- {
- ShowSMG_Bit(6,Seg_Table[Egg/10%10]);
- }
- else
- {
- ShowSMG_Bit(6,0xff);
- }
- if(Egg>0)
- {
- ShowSMG_Bit(7,Seg_Table[Egg%10]);
- }
- else
- {
- ShowSMG_Bit(7,0xff);
- }
- }
- //记录界面
- void Show_Note()
- {
- ShowSMG_Bit(0,Seg_Table[14]);
- ShowSMG_Bit(1,0xff);
- ShowSMG_Bit(2,0xff);
- ShowSMG_Bit(3,Seg_Table[h/16]);
- ShowSMG_Bit(4,Seg_Table[h%16]);
- ShowSMG_Bit(5,0xbf);
- ShowSMG_Bit(6,Seg_Table[min/16]);
- ShowSMG_Bit(7,Seg_Table[min%16]);
- }
- //总页面
- unsigned char led=0x00;
- unsigned char Led_OK()
- {
- unsigned int Egg_f=0;
- unsigned char LSB,MSB;
- MSB=Read_Eeprom(0x02);
- Delay(5);
- LSB=Read_Eeprom(0x03);
- Egg_f=MSB;
- Egg_f=(Egg_f<<8)|LSB;
- if(Egg_f<Egg)
- {
- return 1;
- }
- else
- {
- return 0;
- }
-
- }
- void ShowSMG()
- {
- if(stat==1)
- {
- ShowSMG_Timer();
- if(led4==0)
- {
- led=~0x01;
- SelectHc573(4,led);
- }
- else
- {
- led=~0x09;
- SelectHc573(4,led);
- }
- }
- else if(stat==2)
- {
- Show_Scan();
- if(led4==0)
- {
- led=~0x02;
- SelectHc573(4,led);
- }
- else
- {
- led=~0x0a;
- SelectHc573(4,led);
- }
- }
- else if(stat==3)
- {
- Show_Note();
- if(led4==0)
- {
- led=~0x04;
- SelectHc573(4,led);
- }
- else
- {
- led=~0x0c;
- SelectHc573(4,led);
- }
- }
- }
- //按键
- void Scan_key()
- {
-
- C1=C2=C3=C4=1;
- R1=R3=R4=1;
- R2=0;
- if(C1==0) //S6
- {
- Delay(2);
- if(C1==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+0;
- }
- }
- while(C1==0)
- {
- ShowSMG();
- }
- }
- if(C2==0) //S10
- {
- Delay(2);
- if(C2==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+1;
- }
- }
- while(C2==0)
- {
- ShowSMG();
- }
- }
- if(C3==0) //S14
- {
- Delay(2);
- if(C3==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+2;
- }
- }
- while(C3==0)
- {
- ShowSMG();
- }
- }
- if(C4==0) //S18
- {
- Delay(2);
- if(C4==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+3;
- }
- }
- while(C4==0)
- {
- ShowSMG();
- }
- }
- C1=C2=C3=C4=1;
- R2=R1=R4=1;
- R3=0;
- if(C1==0) //S5
- {
- Delay(2);
- if(C1==0)
- {
- Egg=0;
- }
- while(C1==0)
- {
- ShowSMG();
- }
- }
- if(C2==0) //S9
- {
- Delay(2);
- if(C2==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+4;
- }
- }
- while(C2==0)
- {
- ShowSMG();
- }
- }
- if(C3==0) //S13
- {
- Delay(2);
- if(C3==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+5;
- }
- }
- while(C3==0)
- {
- ShowSMG();
- }
- }
- if(C4==0) //S17
- {
- Delay(2);
- if(C4==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+6;
- }
- }
- while(C4==0)
- {
- ShowSMG();
- }
- }
- C1=C2=C3=C4=1;
- R2=R3=R1=1;
- R4=0;
- if(C1==0) //S4
- {
- Delay(2);
- if(C1==0)
- {
- stat++;
- if(stat==2) //开始记录数据
- {
- h=Timer[2];
- min=Timer[1];
- Write_Eeprom(0xa0,h);
- Write_Eeprom(0xa2,min);
- Egg=0;//同时Egg开始记录获取的数据
- }
- if(stat==3)
- {
- led4=Led_OK();
- }
- if(stat==4)
- {
- stat=1;
- }
- }
- while(C1==0)
- {
- ShowSMG();
- }
- }
- if(C2==0) //S8
- {
- Delay(2);
- if(C2==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+7;
- }
- }
- while(C2==0)
- {
- ShowSMG();
- }
- }
- if(C3==0) //S12
- {
- Delay(2);
- if(C3==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+8;
- }
- }
- while(C3==0)
- {
- ShowSMG();
- }
- }
- if(C4==0) //S16
- {
- Delay(2);
- if(C4==0)
- {
- if(stat==2&&Egg_OK())
- {
- Egg=Egg*10+9;
- }
- }
- while(C4==0)
- {
- ShowSMG();
- }
- }
- }
-
-
-
- void Init_Sys()
- {
- SelectHc573(0,0x00);
- SelectHc573(5,0x00);
- SelectHc573(4,0xff);
- ShowSMG_All(0xff);
- Init_DS1302(); //时钟初始化
- }
-
- void main()
- {
- Init_Sys();
- while(1)
- {
- Read_DS1302(); //不断读取时钟
- Scan_key(); //扫描按键
- ShowSMG();
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。