EDABOSS电子论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 694|回复: 0

[资料共享] 51单片机家用电扇控制器设计 vb上位机源程序+Proteus仿真

[复制链接]

1

主题

1

回帖

8

E币

技术员

Rank: 2

积分
3
发表于 2019-12-20 19:46:58 | 显示全部楼层 |阅读模式
这里是我找到的家用电扇控制设计,希望大家有用
vb写的上位机源码:
   1.png

单片机源程序如下:
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. uchar a[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
  6. uchar LED[4]={0xff,0xf3,0xeb,0xdb};
  7. uchar LED_2[4]={0xff,0xf5,0xed,0xdd};
  8. uchar motor[4]={0x80,0x60,0x30,0x00};
  9. uchar text[3][3]={1,2,3,4,5,6,7,8,9};
  10. char b=0x00;
  11. char c=0x00;
  12. char j=0x00;
  13. #define T_value (unsigned char)0x80
  14. unsigned char t=0X00;
  15. unsigned char T_Count;
  16. unsigned char T_1=0x50;
  17. unsigned char T_y=0x00;
  18. unsigned char T_z=0x00;
  19. unsigned char Tx;
  20. unsigned char x;
  21. sbit P3_2=P3^2;
  22. sbit E=P3^7;
  23. sbit RW=P3^6;
  24. sbit RS=P3^5;
  25. sfr T2MOD=0xc9;
  26. void delay_1(uint z)
  27. {
  28. uint n,y;
  29. for(n=z;n>0;n--)
  30. for(y=110;y>0;y--);
  31. }
  32. void lcd_com(uchar com)
  33. {RS=0;
  34. RW=0;
  35. P2=com;
  36. delay_1(5);
  37. E=1;
  38. delay_1(5);
  39. E=0;
  40. }
  41. void lcd_data(uchar dat)
  42. {RS=1;
  43. RW=0;
  44. P2=dat;
  45. delay_1(5);
  46. E=1;
  47. delay_1(5);
  48. E=0;
  49. }
  50. void disp_str(uchar n,uchar y,uchar *p)
  51. {if(n==0)
  52. lcd_com(0x80+y);
  53. else
  54. lcd_com(0xc0+y);
  55. while(*p)
  56. lcd_data(*p++);
  57. }
  58. void int_lcd(void)
  59. {
  60. lcd_com(0x01);
  61. lcd_com(0x3c);
  62. lcd_com(0x0c);
  63. }
  64. void delay(uint i)
  65. {uint j;
  66. for(j=0;j<i;j++);
  67. }
  68. uchar checkkey()
  69. {uchar i,j;
  70. i=0x0f;
  71. P1=i;
  72. j=P1;
  73. j=j&0x0f;
  74. if(j==0x0f&&t==0x00)return(0);
  75. else return(0xff);
  76. }

  77. uchar keyscan()
  78. {
  79. uchar scancode;
  80. uchar scanvalue;
  81. uchar n;
  82. uchar m=0;
  83. uchar k;
  84. uchar i,j;

  85. {
  86. scancode=0xf7;
  87. m=0x00;
  88. for(i=1;i<=4;i++)
  89. {k=0x80;
  90. P1=scancode;
  91. n=P1;
  92. for(j=0;j<4;j++)
  93. {
  94. if((n&k)==0)
  95. {scanvalue=m+j;
  96. while(checkkey()!=0);
  97. return(scanvalue);
  98. }
  99. else

  100. k=k>>1;

  101. }
  102. m=m+4;
  103. scancode=~scancode;
  104. scancode=scancode>>1;
  105. scancode=~scancode;
  106. }
  107. }
  108. }
  109. void init_tim0()
  110. {
  111. TMOD = 0x01;
  112. TH0 = 0xfe;
  113. TL0 = 0x00;

  114. TR0 = 1;
  115. ET0 = 1;
  116. EA = 1;
  117. }
  118. void init_tim1()
  119. {TMOD=0x11;
  120. TH1=0x4c;
  121. TL1=0x00;
  122. TR1=1;
  123. ET1=1;
  124. EA=1;
  125. }
  126. void init_tim2()
  127. { T2MOD = 0x01;
  128. T2CON = 0x34;
  129. TH2 = 0xFF;  
  130. TL2 = 0xDC;
  131. RCAP2H = 0xFF;
  132. RCAP2L = 0xDC;
  133. SCON = 0x50;
  134. ES=1;
  135. EA=1;
  136. }

  137. void key()
  138. {

  139. unsigned char str[4][10]={"","slow","middle","strong"};
  140. P0=0xff;
  141. Tx=motor[b];
  142. while(1)
  143. {
  144. if(T_Count == 0)
  145. {
  146. P3_2 = ~P3_2;
  147. Tx = T_value - Tx;
  148. T_Count = Tx;
  149. }

  150. if(checkkey()==0)continue;
  151. else
  152. {delay(0x10);
  153. if(checkkey()==0)continue;
  154. else
  155. {x=keyscan();
  156. if(a[x]==0x00)
  157. {j=0x01;
  158. b++;
  159. if(b==0x04)b=0x01;
  160. }
  161. if(a[x]==0x01&&j==0x01)
  162. {
  163. c++;
  164. if(c==0x03)c=0x00;
  165. }
  166. if(a[x]==0x02)
  167. {b=0;
  168. c=0;
  169. Tx=0x80;
  170. P3_2=0;
  171. j=0x00;
  172. T_y=0x00;
  173. T_z=0x00;
  174. int_lcd();
  175. P0=0xff;
  176. SBUF=0;
  177. }
  178. if(c==0x00&&a[x]!=0x02)
  179. {TR1=0;
  180. P0=LED[b];
  181. Tx=motor[b];
  182. P3_2=0X00;
  183. int_lcd();
  184. disp_str(0,1,"normal");
  185. disp_str(1,1,str[b]);
  186. t=0x00;
  187. SBUF=text[c][b-1];
  188. }
  189. if(c==0x01&&a[x]!=0x02)
  190. {P0=LED_2[b];
  191. T_y=0x00;
  192. init_tim1();
  193. int_lcd();
  194. disp_str(0,1,"nature");
  195. disp_str(1,1,str[b]);
  196. t=0x00;
  197. SBUF=text[c][b-1];
  198. }
  199. if(c==0x02&&a[x]!=0x02)
  200. {P0=0xfe;
  201. b=0x01;
  202. T_z=0x00;
  203. init_tim1();
  204. int_lcd();
  205. disp_str(0,1,"sleep");
  206. disp_str(1,1,str[b]);
  207. t=0x00;
  208. SBUF=text[c][b-1];
  209. }
  210. delay(0x10);
  211. }

  212. }
  213. }

  214. }


  215. void main()
  216. {
  217. P3_2 = 0;
  218. init_tim0();
  219. T_Count = 0X80;
  220. init_tim2();
  221. int_lcd();
  222. key();
  223. }void int_tim0() interrupt 1 using 1
  224. { TH0 = 0xfe;
  225. TL0 = 0x28;  
  226. T_Count--;
  227. }
  228. void int_time1() interrupt 3 using 2
  229. {TH1=0x4c;
  230. TL1=0x00;
  231. T_1--;
  232. if(T_z==0x00||T_y==0x00)
  233. {Tx=0X80;
  234. P3_2=0x00;
  235. }
  236. if(T_1==0X00)
  237. {T_y++;T_z++;
  238. T_1=0x50;
  239. if(c==0x01)
  240. {if(T_y==0x02)
  241. {
  242. Tx=motor[b];
  243. P3_2=0x00;
  244. }
  245. if(T_y==0x03)
  246. {Tx=0x80;
  247. T_y=0x00;
  248. P3_2=0x00;
  249. }
  250. }
  251. if(c==0x02)
  252. {if(T_z==0x02)
  253. {
  254. Tx=motor[1];
  255. P3_2=0x00;
  256. }
  257. if(T_z==0x04)
  258. {Tx=0x80;
  259. T_z=0x00;
  260. P3_2=0x00;
  261. }

  262. }
  263. }
  264. }
  265. void uart() interrupt 4 using 3
  266. {if(RI==1)
  267. {RI=0;
  268. t=SBUF;
  269. if(t==0x31)
  270. {b++;
  271. if(b==0x04)b=1;
  272. }
  273. if(t==0x32)
  274. {c++;
  275. if(c==0x03)c=0x00;
  276. }
  277. if(t==0x33)
  278. {b=0;
  279. c=0;
  280. Tx=0x80;
  281. P3_2=0;
  282. j=0x00;
  283. T_y=0x00;
  284. T_z=0x00;
  285. int_lcd();
  286. P0=0xff;
  287. SBUF=0;
  288. }
  289. }
  290. else TI=0;

  291. }
复制代码

3.png

电风扇控制器.7z

33.14 KB, 下载次数: 0, 下载积分: E币 -5 元

积分规则
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|EDABOSS电子论坛

GMT+8, 2024-4-24 17:12 , Processed in 0.042434 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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