找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1330|回复: 0
打印 上一主题 下一主题
收起左侧

单片机定时控制系统,继电器到设定时间亮起来后,时钟停了,八位数码管不显示

[复制链接]
跳转到指定楼层
楼主

定时控制系统可以显示时间,并独立控制两组继电器的开关状态,具体功能如下:

(1)显示时间,包括星期,小时,分钟;

(2)分别间歇控制2个继电器的开关,各继电器的导通时间和断开时间可以独立设置,以分钟为单位。例如间歇继电器A导通n分钟,关闭m分钟,如此循坏。


求教各位帮忙看下单片机程序问题在哪里。多谢了。
  1. #include<reg51.h>
  2. #define uChar unsigned char
  3. #define uInt unsigned int
  4. uChar a[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};  //段选0-9
  5. uChar b[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};                           //位选
  6. uChar second=50,minute=59,hour=23,day=07,count;
  7. sbit Key1 = P3^0; //继电器确认设置
  8. sbit Key2 = P3^1; //调位
  9. sbit Key3 = P3^2; //加一
  10. sbit Key4 = P3^3; //切换

  11. sbit L1=P1^0;                //继电器控制口
  12. sbit L2=P1^1;
  13. uChar a1=01,a2=01;        //间歇继电器1的休息时间a1与工作时间a2
  14. uChar b1=01,b2=01;        //间歇继电器2的休息时间b1与工作时间b2
  15. uChar a11=01,a21=01,b11=01,b21=01;        //记录间歇继电器组设置后的状态
  16. /*********************延迟函数********************/
  17. void Delay(uInt t)
  18. {
  19. while(t)
  20. {
  21. t--;
  22. }
  23. }
  24. /*********************时分秒显示函数*******************/
  25. void Dispaly1(uChar day,uChar hour,uChar minute)
  26. {
  27. /*********************第一位数码管*********************/
  28. P2=b[0];
  29. P0=a[day/10];
  30. Delay(10);
  31. /*********************第二位数码管*********************/
  32. P2=b[1];
  33. P0=a[day%10];
  34. Delay(10);
  35. /*********************第三位数码管*********************/
  36. P2=b[2];
  37. P0=0x40;
  38. Delay(10);
  39. /*********************第四位数码管*********************/
  40. P2=b[3];
  41. P0=a[hour/10];
  42. Delay(10);
  43. /*********************第五位数码管*********************/
  44. P2=b[4];
  45. P0=a[hour%10];
  46. Delay(10);
  47. /*********************第六位数码管*********************/
  48. P2=b[5];
  49. P0=0x40;
  50. Delay(10);
  51. /*********************第七位数码管*********************/
  52. P2=b[6];
  53. P0=a[minute/10];
  54. Delay(10);
  55. /*********************第八位数码管*********************/
  56. P2=b[7];;
  57. P0=a[minute%10];
  58. Delay(10);
  59. }
  60. /*********************继电器状态显示函数********************/
  61. void Dispaly2(uChar a1,uChar a2,uChar b1,uChar b2)
  62. {
  63. P2=b[0];
  64. P0=a[a1/10];
  65. Delay(10);

  66. P2=b[1];
  67. P0=a[a1%10];
  68. Delay(10);

  69. P2=b[2];
  70. P0=a[a2/10];
  71. Delay(10);

  72. P2=b[3];
  73. P0=a[a2%10];
  74. Delay(10);

  75. P2=b[4];
  76. P0=a[b1/10];
  77. Delay(10);

  78. P2=b[5];
  79. P0=a[b1%10];
  80. Delay(10);

  81. P2=b[6];
  82. P0=a[b2/10];
  83. Delay(10);

  84. P2=b[7];
  85. P0=a[b2%10];
  86. Delay(10);
  87. }
  88. /*********************时钟按键扫描函数*********************/
  89. void Keyscan1()
  90. {
  91. static uChar j=0;
  92. /*时钟调位和数值加一功能*/
  93. if(Key2==0)
  94. {
  95. Delay(10);
  96. if(Key2==0)
  97. while(!Key2);
  98. j++;
  99. }
  100. if(j%4==1)
  101. {
  102. if(Key3==0)
  103. {
  104. Delay(10);
  105. if(Key3==0)
  106. while(!Key3);
  107. day++;
  108. if(day==8)
  109. day=1;
  110. }
  111. }
  112. if(j%4==2)
  113. {
  114. if(Key3==0)
  115. {
  116. Delay(10);
  117. if(Key3==0)
  118. while(!Key3);
  119. hour++;
  120. if(hour==24)
  121. hour=0;
  122. }
  123. }
  124. if(j%4==3)
  125. {
  126. if(Key3==0)
  127. {
  128. Delay(10);
  129. if(Key3==0)
  130. while(!Key3);
  131. minute++;
  132. if(minute==60)
  133. minute=0;
  134. }
  135. }
  136. }

  137. /*间歇继电器扫描函数*/
  138. void Keyscan2()
  139. {
  140. static uChar n=0;
  141. /*调位和加一功能*/
  142. if(n%4==1)         
  143. {
  144. if(Key3==0)
  145. {
  146. Delay(10);
  147. if(Key3==0)
  148. while(!Key3);
  149. a1++;
  150. if(a1==10)          //继电器1休息时间不超过10分钟
  151. a1=1;
  152. }
  153. }
  154. if(n%4==2)
  155. {
  156. if(Key3==0)
  157. {
  158. Delay(10);
  159. if(Key3==0)
  160. while(!Key3);
  161. a2++;
  162. if(a2==10)           //继电器1工作时间不超过10分钟
  163. a2=1;
  164. }
  165. }
  166. if(n%4==3)
  167. {
  168. if(Key3==0)
  169. {
  170. Delay(10);
  171. if(Key3==0)
  172. while(!Key3);
  173. b1++;
  174. if(b1==10)                //继电器2休息时间不超过10分钟
  175. b1=1;
  176. }
  177. }
  178. if(n%4==0)
  179. {
  180. if(Key3==0)
  181. {
  182. Delay(10);
  183. if(Key3==0)
  184. while(!Key3);
  185. b2++;
  186. if(b2==10)                //继电器2工作时间不超过10分钟
  187. b2=1;
  188. }
  189. }
  190. }

  191. /************************************************/
  192. /***************主函数***************************/
  193. /************************************************/
  194. void main()
  195. {                                                
  196. TMOD=0x01;          /*定时器以方式一工作*/
  197. TH0=(65536-10000)/256;
  198. TL0=(65536-10000)%256;/*10ms计时*/
  199. EA=1;
  200. ET0=1;/*允许定时器0中断*/
  201. TR0=1;/*打开定时器0*/
  202. while(1)
  203. {
  204. static uChar h=0;
  205. /*时钟和继电器切换功能*/
  206. if(Key4==0)
  207. {
  208. Delay(10);
  209. if(Key4==0)
  210. while(!Key4);
  211. h++;
  212. }
  213. if(h%2==0)/*如果按键偶数次则显示时钟*/
  214. {
  215. Dispaly1(day,hour,minute);
  216. Keyscan1();
  217. }

  218. if(h%2==1)/*如果按键奇数次则显示日期*/
  219. {
  220. Dispaly2(a1,a2,b1,b2);

  221. Keyscan2();
  222. Delay(10);
  223. if(Key1==0)                                //按键K4按下确定设置并退出
  224.         {
  225.                 Delay(10);
  226.                 a11=a1;
  227.                 a21=a2;
  228.                 b11=b1;
  229.                 b21=b2;
  230.                 break;
  231.                
  232.         }
  233. }
  234. }
  235. }
  236. /**********************中断函数**************************/
  237. void time0_int(void) interrupt 1
  238. {
  239. TH0=(65536-10000)/256;
  240. TL0=(65536-10000)%256;
  241. count++;
  242. if(count==100)/*10ms??ê±£???100′??ò??o?1s*/
  243. {
  244. count=0;
  245. second++;
  246. if(second==60)
  247. {
  248. second=0;
  249. minute++;
  250. if(L1==1)
  251.                         {
  252.                                 a11--;
  253.                                 if(a11==0)
  254.                                 {
  255.                                         L1=0;
  256.                                         a11=a1;
  257.                                 }
  258.                         }
  259.                
  260.                         if(a21==0)
  261.                         {
  262.                                 L1=1;
  263.                                 a21=a2;
  264.                         }
  265.                         else if(L1==0)
  266.                         {
  267.                                 a21--;
  268.                         }
  269.                         if(L2==1)
  270.                         {
  271.                                 b11--;
  272.                                 if(b11==0)
  273.                                 {
  274.                                         L2=0;
  275.                                         b11=b1;
  276.                                 }
  277.                         }

  278.                         if(b21==0)
  279.                         {
  280.                                 L2=1;
  281.                                 b21=b2;
  282.                         }
  283.                         else if(L2==0)
  284.                         {
  285.                                 b21--;
  286.                         }
  287. if(minute==60)
  288. {
  289. minute=0;
  290. hour++;
  291. if(hour==24)
  292. {
  293. day++;
  294. hour=0;
  295. minute=0;
  296. second=0;
  297. if(day==7)
  298. {
  299. day=01;
  300. hour=0;
  301. minute=0;
  302. second=0;
  303. }
  304. }
  305. }
  306. }
  307. }
复制代码

2.png (133.04 KB, 下载次数: 68)

2.png

1.png (136.02 KB, 下载次数: 67)

1.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表