Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include "8051.h" // zbior definiuj.cy rejestry procesora
  2.  
  3. #define TH0_RELOAD 0xF1
  4. #define TL0_RELOAD 0x00
  5. #define TIK 1
  6.  
  7. int limit = 20;
  8. int limit6 = 20;
  9. int glob=1;
  10. int state=0;
  11. int state1=0;
  12. int state2=0;
  13. int state3=0;
  14. int state6=0;
  15.  
  16. char nad;
  17. char odb;
  18. char a,b,c,d;
  19.  
  20. int btn_before=1;
  21. int btn_before1=1;
  22. int btn_before2=1;
  23. int btn_before3=1;
  24. int btn_before6=1;
  25.  
  26. int btn_current;
  27. int btn_current1;
  28. int btn_current2;
  29. int btn_current3;
  30. int btn_current6;
  31.  
  32. int debounce_cnt=0;
  33. int debounce_cnt1=0;
  34. int debounce_cnt2=0;
  35. int debounce_cnt3=0;
  36. int debounce_cnt6=0;
  37.  
  38. void timer0_init(void)
  39. {
  40. TH0 = TH0_RELOAD; // za.adowanie czasu odliczania
  41. TL0 = TL0_RELOAD; // TH0 . starszy bajt, TL0 - m.odszy
  42. TMOD = TMOD | 0x01; // tryb nr 1 uk.adu TIMER 0
  43. TR0 = 1; // TIMER 0 start
  44. ET0 = 1; // odblokowanie przerwa. od TIMER 0
  45. }
  46.  
  47. void timer_isr (void) __interrupt (1) __using (0)
  48. {
  49. static int count=0;
  50. TH0 = TH0_RELOAD; // za.adowanie czasu odliczania
  51. TL0 = TL0_RELOAD;
  52. count++;
  53. if (count==TIK)
  54. {
  55. glob*=2;
  56. if(glob>8)
  57. {
  58. glob=1;
  59. }// dodatkowy licznik przerwa.
  60. count=0; // umo.liwiaj.cy uzyskanie dodatk.
  61.  
  62. }
  63. }
  64.  
  65. void odbjur (void) __interrupt (4) __using (0)
  66. {
  67. if (RI==1)
  68. {
  69. // Tutaj nale.y umie.ci. obs.ug. odbioru
  70. odb=SBUF;
  71. d=odb&0x0F;
  72. c=(odb&0xF0)>>4;
  73.  
  74. RI = 0;
  75. TI = 1;
  76. }
  77. }
  78. void serial_init(void)
  79. {
  80. SM0 = 1; // Ustawienie trybu pracy nr 2
  81. SM1 = 0;
  82. TI = 1; // Ustawienie bitu TI pozwalające
  83. // na rozpoczęcie nadawania w pętli głównej
  84. RI = 0;
  85. ES = 1; // Dopuszczenie przerwań od układu
  86. // transmisji szeregowej
  87. PCON = PCON & 0x7F;
  88. }
  89.  
  90. void main(void)
  91. {
  92. int tab[10]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
  93. char a1=0, a2=0, a3=0, a4=0;
  94. EA = 0; // zablokowanie przerwa.
  95. timer0_init(); // przygotowanie uk.adu Timer0
  96. serial_init();
  97. EA = 1; // odblokowanie przerwa.
  98. REN = 1;
  99. P2=0xFF; // wygaszenie wszystkich diod
  100.  
  101. while(1)
  102. {
  103. P2=0xFF; // wygaszenie wszystkich diod
  104.  
  105.  
  106. btn_current2 = P3_6;
  107. //przycisk b
  108. if(state2 == 0)
  109. {
  110. if(btn_before2 != btn_current2)
  111. {
  112. state2=1;
  113. btn_before2 = btn_current2;
  114. }
  115. }
  116. else if(state2 == 1)
  117. {
  118. if(btn_before2 == btn_current2)
  119. {
  120. if(debounce_cnt2 >= limit)
  121. {
  122. state2=2;
  123. a3++;
  124. if(a3>9)
  125. {
  126. a3=0;
  127. }
  128. }
  129.  
  130. else
  131. debounce_cnt2 = debounce_cnt2 + 1;
  132. }
  133. else
  134. {
  135. state2 = 0;
  136. btn_before2 = btn_current2;
  137. debounce_cnt2 = 0;
  138. }
  139. }
  140.  
  141. else if(state2==2)
  142. {
  143. if(btn_before2 != btn_current2)
  144. {
  145. state2 = 0;
  146. btn_before2 = btn_current2;
  147. debounce_cnt2 = 0;
  148. }
  149. }
  150.  
  151. btn_current3 = P3_7;
  152. //przycisk do a
  153. if(state3 == 0)
  154. {
  155. if(btn_before3 != btn_current3)
  156. {
  157. state3=1;
  158. btn_before3 = btn_current3;
  159. }
  160. }
  161. else if(state3 == 1)
  162. {
  163. if(btn_before3 == btn_current3)
  164. {
  165. if(debounce_cnt3 >= limit)
  166. {
  167. state3=2;
  168. a4++;
  169. if(a4>9)
  170. {
  171. a4=0;
  172. }
  173. }
  174.  
  175. else
  176. debounce_cnt3 = debounce_cnt3 + 1;
  177. }
  178. else
  179. {
  180. state3 = 0;
  181. btn_before3 = btn_current3;
  182. debounce_cnt3 = 0;
  183. }
  184. }
  185.  
  186. else if(state3==2)
  187. {
  188. if(btn_before3 != btn_current3)
  189. {
  190. state3 = 0;
  191. btn_before3 = btn_current3;
  192. debounce_cnt3 = 0;
  193. }
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. btn_current6 = P3_3;
  201.  
  202. if(P3_3==0){
  203. if(TI == 1)
  204. {
  205. nad = a4;
  206. nad = nad << 4;
  207. nad = nad|a3;
  208. SBUF=nad;
  209. TI=0;
  210. }
  211.  
  212. }
  213. /*if(state6 == 0)
  214. {
  215. if(btn_before6 != btn_current6)
  216. {
  217. state6=6;
  218. btn_before6 = btn_current6;
  219. }
  220. }
  221. else if(state6 == 1)
  222. {
  223. if(btn_before6 == btn_current6)
  224. {
  225. if(debounce_cnt6 >= limit6)
  226. {
  227. state6=2;
  228. P2_3=!P2_3;
  229. if(TI == 1)
  230. {
  231. nad = a4;
  232. nad = nad << 4;
  233. nad = nad|a3;
  234. SBUF=nad;
  235. TI=0;
  236. }
  237. }
  238.  
  239. else
  240. debounce_cnt6 = debounce_cnt6 + 1;
  241. }
  242. else
  243. {
  244. state6 = 2;
  245. btn_before6 = btn_current6;
  246. debounce_cnt6 = 0;
  247. }
  248. }
  249.  
  250. else if(state6 == 2)
  251. {
  252. if(btn_before6 != btn_current6)
  253. {
  254. state6 = 0;
  255. btn_before6 = btn_current6;
  256. debounce_cnt6 = 0;
  257. }
  258. }
  259. */
  260. P1=glob;
  261.  
  262. if(glob==8)
  263. {
  264. P0=tab[a4];
  265. }
  266. if (glob==4)
  267. {
  268. P0=tab[a3];
  269. }
  270. if(glob==2)
  271. {
  272. P0=tab[c];
  273. }
  274. if(glob==1)
  275. {
  276. P0=tab[d];
  277. }
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement