定时控制系统可以显示时间,并独立控制两组继电器的开关状态,具体功能如下: (1)显示时间,包括星期,小时,分钟; (2)分别间歇控制2个继电器的开关,各继电器的导通时间和断开时间可以独立设置,以分钟为单位。例如间歇继电器A导通n分钟,关闭m分钟,如此循坏。
求教各位帮忙看下单片机程序问题在哪里。多谢了。- #include<reg51.h>
- #define uChar unsigned char
- #define uInt unsigned int
- uChar a[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //段选0-9
- uChar b[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位选
- uChar second=50,minute=59,hour=23,day=07,count;
- sbit Key1 = P3^0; //继电器确认设置
- sbit Key2 = P3^1; //调位
- sbit Key3 = P3^2; //加一
- sbit Key4 = P3^3; //切换
-
- sbit L1=P1^0; //继电器控制口
- sbit L2=P1^1;
- uChar a1=01,a2=01; //间歇继电器1的休息时间a1与工作时间a2
- uChar b1=01,b2=01; //间歇继电器2的休息时间b1与工作时间b2
- uChar a11=01,a21=01,b11=01,b21=01; //记录间歇继电器组设置后的状态
- /*********************延迟函数********************/
- void Delay(uInt t)
- {
- while(t)
- {
- t--;
- }
- }
- /*********************时分秒显示函数*******************/
- void Dispaly1(uChar day,uChar hour,uChar minute)
- {
- /*********************第一位数码管*********************/
- P2=b[0];
- P0=a[day/10];
- Delay(10);
- /*********************第二位数码管*********************/
- P2=b[1];
- P0=a[day%10];
- Delay(10);
- /*********************第三位数码管*********************/
- P2=b[2];
- P0=0x40;
- Delay(10);
- /*********************第四位数码管*********************/
- P2=b[3];
- P0=a[hour/10];
- Delay(10);
- /*********************第五位数码管*********************/
- P2=b[4];
- P0=a[hour%10];
- Delay(10);
- /*********************第六位数码管*********************/
- P2=b[5];
- P0=0x40;
- Delay(10);
- /*********************第七位数码管*********************/
- P2=b[6];
- P0=a[minute/10];
- Delay(10);
- /*********************第八位数码管*********************/
- P2=b[7];;
- P0=a[minute%10];
- Delay(10);
- }
- /*********************继电器状态显示函数********************/
- void Dispaly2(uChar a1,uChar a2,uChar b1,uChar b2)
- {
- P2=b[0];
- P0=a[a1/10];
- Delay(10);
- P2=b[1];
- P0=a[a1%10];
- Delay(10);
- P2=b[2];
- P0=a[a2/10];
- Delay(10);
- P2=b[3];
- P0=a[a2%10];
- Delay(10);
- P2=b[4];
- P0=a[b1/10];
- Delay(10);
- P2=b[5];
- P0=a[b1%10];
- Delay(10);
- P2=b[6];
- P0=a[b2/10];
- Delay(10);
- P2=b[7];
- P0=a[b2%10];
- Delay(10);
- }
- /*********************时钟按键扫描函数*********************/
- void Keyscan1()
- {
- static uChar j=0;
- /*时钟调位和数值加一功能*/
- if(Key2==0)
- {
- Delay(10);
- if(Key2==0)
- while(!Key2);
- j++;
- }
- if(j%4==1)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- day++;
- if(day==8)
- day=1;
- }
- }
- if(j%4==2)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- hour++;
- if(hour==24)
- hour=0;
- }
- }
- if(j%4==3)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- minute++;
- if(minute==60)
- minute=0;
- }
- }
- }
- /*间歇继电器扫描函数*/
- void Keyscan2()
- {
- static uChar n=0;
- /*调位和加一功能*/
- if(n%4==1)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- a1++;
- if(a1==10) //继电器1休息时间不超过10分钟
- a1=1;
- }
- }
- if(n%4==2)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- a2++;
- if(a2==10) //继电器1工作时间不超过10分钟
- a2=1;
- }
- }
- if(n%4==3)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- b1++;
- if(b1==10) //继电器2休息时间不超过10分钟
- b1=1;
- }
- }
- if(n%4==0)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- b2++;
- if(b2==10) //继电器2工作时间不超过10分钟
- b2=1;
- }
- }
- }
- /************************************************/
- /***************主函数***************************/
- /************************************************/
- void main()
- {
- TMOD=0x01; /*定时器以方式一工作*/
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;/*10ms计时*/
- EA=1;
- ET0=1;/*允许定时器0中断*/
- TR0=1;/*打开定时器0*/
- while(1)
- {
- static uChar h=0;
- /*时钟和继电器切换功能*/
- if(Key4==0)
- {
- Delay(10);
- if(Key4==0)
- while(!Key4);
- h++;
- }
- if(h%2==0)/*如果按键偶数次则显示时钟*/
- {
- Dispaly1(day,hour,minute);
- Keyscan1();
- }
- if(h%2==1)/*如果按键奇数次则显示日期*/
- {
- Dispaly2(a1,a2,b1,b2);
- Keyscan2();
- Delay(10);
- if(Key1==0) //按键K4按下确定设置并退出
- {
- Delay(10);
- a11=a1;
- a21=a2;
- b11=b1;
- b21=b2;
- break;
-
- }
- }
- }
- }
- /**********************中断函数**************************/
- void time0_int(void) interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- count++;
- if(count==100)/*10ms??ê±£???100′??ò??o?1s*/
- {
- count=0;
- second++;
- if(second==60)
- {
- second=0;
- minute++;
- if(L1==1)
- {
- a11--;
- if(a11==0)
- {
- L1=0;
- a11=a1;
- }
- }
-
- if(a21==0)
- {
- L1=1;
- a21=a2;
- }
- else if(L1==0)
- {
- a21--;
- }
- if(L2==1)
- {
- b11--;
- if(b11==0)
- {
- L2=0;
- b11=b1;
- }
- }
- if(b21==0)
- {
- L2=1;
- b21=b2;
- }
- else if(L2==0)
- {
- b21--;
- }
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- day++;
- hour=0;
- minute=0;
- second=0;
- if(day==7)
- {
- day=01;
- hour=0;
- minute=0;
- second=0;
- }
- }
- }
- }
- }
复制代码 |