Advertisement
Sothian

altbezprzerwac

Oct 28th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <system.h>
  4. #include <definitions.h>
  5. //Nowe definicje
  6. #include "altera_up_avalon_parallel_port.h"
  7. #include "sys/alt_irq.h"
  8. /* Przełączniki */
  9. #define SW0 0x00000001
  10. #define SW1 0x00000002
  11. #define SW2 0x00000004
  12. #define SW3 0x00000008
  13. #define SW4 0x00000010
  14. #define SW5 0x00000020
  15. #define SW6 0x00000040
  16. #define SW7 0x00000080
  17. #define SW8 0x00000100
  18. #define SW9 0x00000200
  19. #define SW10 0x00000400
  20. #define SW11 0x00000800
  21. #define SW12 0x00001000
  22. #define SW13 0x00002000
  23. #define SW14 0x00004000
  24. #define SW15 0x00008000
  25. #define SW16 0x00010000
  26. #define SW17 0x00020000
  27.  
  28. /* PushButtony */
  29. #define KEY1 0x00000002
  30. #define KEY2 0x00000004
  31. #define KEY3 0x00000008
  32.  
  33. /* Ledy */
  34. #define LED0 0x00000001
  35. #define LED1 0x00000002
  36. #define LED2 0x00000004
  37. #define LED3 0x00000008
  38. #define LED4 0x00000010
  39. #define LED5 0x00000020
  40. #define LED6 0x00000040
  41. #define LED7 0x00000080
  42. #define LED8 0x00000100
  43. #define LED9 0x00000200
  44. #define LED10 0x00000400
  45. #define LED11 0x00000800
  46. #define LED12 0x00001000
  47. #define LED13 0x00002000
  48. #define LED14 0x00004000
  49. #define LED15 0x00008000
  50. #define LED16 0x00010000
  51. #define LED17 0x00020000
  52.  
  53. /* Segmenty HEX */
  54. #define SEGA 1
  55. #define SEGB 2
  56. #define SEGC 4
  57. #define SEGD 8
  58. #define SEGE 16
  59. #define SEGF 32
  60. #define SEGG 64
  61.  
  62.  
  63. /* Litery i cyfry z hex 7 segmentowych */
  64. #define HEX0 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF)
  65. #define HEX1 (SEGB|SEGC)
  66. #define HEX2 (SEGA|SEGB|SEGG|SEGE|SEGD)
  67. #define HEX3 (SEGA|SEGB|SEGC|SEGD|SEGG)
  68. #define HEX4 (SEGF|SEGG|SEGB|SEGC)
  69. #define HEX5 (SEGA|SEGF|SEGG|SEGC|SEGD)
  70. #define HEX6 (SEGA|SEGF|SEGG|SEGC|SEGD|SEGE)
  71. #define HEX7 (SEGA|SEGB|SEGC)
  72. #define HEX8 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF|SEGG)
  73. #define HEX9 (SEGA|SEGB|SEGC|SEGD|SEGF|SEGG)
  74. #define HEXE (SEGA|SEGF|SEGG|SEGE|SEGD)
  75. #define HEXr (SEGE|SEGG)
  76. #define HEXA (SEGA|SEGB|SEGC|SEGE|SEGF|SEGG)
  77. #define HEXF (SEGA|SEGE|SEGF|SEGG)
  78. #define HEXC (SEGA|SEGE|SEGF|SEGD)
  79. #define HEXD (SEGB|SEGC|SEGD|SEGE|SEGG)
  80. //Struktura trzymajaca uchwyty
  81. struct alt_up_dev {
  82. alt_up_parallel_port_dev *uchwytSW;
  83. alt_up_parallel_port_dev *uchwytLEDR;
  84. alt_up_parallel_port_dev *uchwytLEDG;
  85. alt_up_parallel_port_dev *uchwytHEX3;
  86. };
  87.  
  88.  
  89. int SW0on, SW0off, SW1on, SW1off, SW2on, SW2off, SW3on, SW3off, SW4on, SW4off, SW5on, SW5off, SW6on, SW6off, SW7on, SW7off, SW8on, SW8off, SW9on, SW9off, SW10on, SW10off, SW11on, SW11off, SW12on, SW12off, SWerr, SWerrFMon, SWerrFMoff;
  90.  
  91. const int ledOnDelay = 1;
  92. const int ledOffDelay = 2;
  93. const int hexOnDelay = 0;
  94. const int hexOffDelay = 3;
  95. const int errOffDelay = 3;
  96.  
  97.  
  98. //logika
  99. void interrupt_handler(struct alt_up_dev *up_dev, alt_u32 id) {
  100. int state = 0;
  101. //Odczyt switchy
  102. int switches = alt_up_parallel_port_read_data(up_dev->uchwytSW);
  103. //Sprawdzenie ktory slajder zostal wykorzystany
  104. state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  105.  
  106. //Ustawianie timerow:
  107. //0
  108. if ((state & SW0) && SW0on == 0) {
  109. SW0on = alt_nticks();
  110. SW0off = 0;
  111. }
  112. if (!(state & SW0) && SW0off == 0) {
  113. SW0off = alt_nticks();
  114. SW0on = 0;
  115. }
  116. //1
  117. if ((state & SW1) && SW1on == 0) {
  118. SW1on = alt_nticks();
  119. SW1off = 0;
  120. }
  121. if (!(state & SW1) && SW1off == 0) {
  122. SW1off = alt_nticks();
  123. SW1on = 0;
  124. }
  125. //2
  126. if ((state & SW2) && SW2on == 0) {
  127. SW2on = alt_nticks();
  128. SW2off = 0;
  129. }
  130. if (!(state & SW2) && SW2off == 0) {
  131. SW2off = alt_nticks();
  132. SW2on = 0;
  133. }
  134. //3
  135. if ((state & SW3) && SW3on == 0) {
  136. SW3on = alt_nticks();
  137. SW3off = 0;
  138. }
  139. if (!(state & SW3) && SW3off == 0) {
  140. SW3off = alt_nticks();
  141. SW3on = 0;
  142. }
  143. //4
  144. if ((state & SW4) && SW4on == 0) {
  145. SW4on = alt_nticks();
  146. SW4off = 0;
  147. }
  148. if (!(state & SW4) && SW4off == 0) {
  149. SW4off = alt_nticks();
  150. SW4on = 0;
  151. }
  152. //5
  153. if ((state & SW5) && SW5on == 0) {
  154. SW5on = alt_nticks();
  155. SW5off = 0;
  156. }
  157. if (!(state & SW5) && SW5off == 0) {
  158. SW5off = alt_nticks();
  159. SW5on = 0;
  160. }
  161. //6
  162. if ((state & SW6) && SW6on == 0) {
  163. SW6on = alt_nticks();
  164. SW6off = 0;
  165. }
  166. if (!(state & SW6) && SW6off == 0) {
  167. SW6off = alt_nticks();
  168. SW6on = 0;
  169. }
  170. //7
  171. if ((switches & SW7) && SW7on == 0) {
  172. SW7on = alt_nticks();
  173. SW7off = 0;
  174. }
  175. if (!(switches & SW7) && SW7off == 0) {
  176. SW7off = alt_nticks();
  177. SW7on = 0;
  178. }
  179. //8
  180. if ((switches & SW8) && SW8on == 0) {
  181. SW8on = alt_nticks();
  182. SW8off = 0;
  183. }
  184. if (!(switches & SW8) && SW8off == 0) {
  185. SW8off = alt_nticks();
  186. SW8on = 0;
  187. }
  188. //9
  189. if ((switches & SW9) && SW9on == 0) {
  190. SW9on = alt_nticks();
  191. SW9off = 0;
  192. }
  193. if (!(switches & SW9) && SW9off == 0) {
  194. SW9off = alt_nticks();
  195. SW9on = 0;
  196. }
  197. //10
  198. if ((switches & SW10) && SW10on == 0) {
  199. SW10on = alt_nticks();
  200. SW10off = 0;
  201. }
  202. if (!(switches & SW10) && SW10off == 0) {
  203. SW10off = alt_nticks();
  204. SW10on = 0;
  205. }
  206. //11
  207. if ((switches & SW11) && SW11on == 0) {
  208. SW11on = alt_nticks();
  209. SW11off = 0;
  210. }
  211. if (!(switches & SW11) && SW11off == 0) {
  212. SW11off = alt_nticks();
  213. SW11on = 0;
  214. }
  215. //12
  216. if ((switches & SW12) && SW12on == 0) {
  217. SW12on = alt_nticks();
  218. SW12off = 0;
  219. }
  220. if (!(switches & SW12) && SW12off == 0) {
  221. SW12off = alt_nticks();
  222. SW12on = 0;
  223. }
  224.  
  225. switch (state) {
  226. case 0:
  227. //Wygraszenie gdy zaden switch nie jest aktywny
  228.  
  229. if (alt_nticks() - SW0off >= ledOffDelay * alt_ticks_per_second()) {
  230. IOWR(LEDS_RED_BASE, 0, 0);
  231. }
  232. if (alt_nticks() - SW0off >= hexOffDelay * alt_ticks_per_second()) {
  233. IOWR(HEX_3_BASE, 0, 0);
  234. }
  235.  
  236. if (alt_nticks() - SW1off >= ledOffDelay * alt_ticks_per_second()) {
  237. IOWR(LEDS_RED_BASE, 0, 0);
  238. }
  239. if (alt_nticks() - SW1off >= hexOffDelay * alt_ticks_per_second()) {
  240. IOWR(HEX_3_BASE, 0, 0);
  241. }
  242.  
  243. if (alt_nticks() - SW2off >= ledOffDelay * alt_ticks_per_second()) {
  244. IOWR(LEDS_RED_BASE, 0, 0);
  245. }
  246. if (alt_nticks() - SW2off >= hexOffDelay * alt_ticks_per_second()) {
  247. IOWR(HEX_3_BASE, 0, 0);
  248. }
  249.  
  250. if (alt_nticks() - SW3off >= ledOffDelay * alt_ticks_per_second()) {
  251. IOWR(LEDS_RED_BASE, 0, 0);
  252. }
  253. if (alt_nticks() - SW3off >= hexOffDelay * alt_ticks_per_second()) {
  254. IOWR(HEX_3_BASE, 0, 0);
  255. }
  256.  
  257. if (alt_nticks() - SW4off >= ledOffDelay * alt_ticks_per_second()) {
  258. IOWR(LEDS_RED_BASE, 0, 0);
  259. }
  260. if (alt_nticks() - SW4off >= hexOffDelay * alt_ticks_per_second()) {
  261. IOWR(HEX_3_BASE, 0, 0);
  262. }
  263.  
  264. if (alt_nticks() - SW5off >= ledOffDelay * alt_ticks_per_second()) {
  265. IOWR(LEDS_RED_BASE, 0, 0);
  266. }
  267. if (alt_nticks() - SW5off >= hexOffDelay * alt_ticks_per_second()) {
  268. IOWR(HEX_3_BASE, 0, 0);
  269. }
  270.  
  271. if (alt_nticks() - SW6off >= ledOffDelay * alt_ticks_per_second()) {
  272. IOWR(LEDS_RED_BASE, 0, 0);
  273. }
  274. if (alt_nticks() - SW6off >= hexOffDelay * alt_ticks_per_second()) {
  275. IOWR(HEX_3_BASE, 0, 0);
  276. }
  277.  
  278.  
  279. break;
  280. case 1:
  281.  
  282. if (alt_nticks() - SW0on >= ledOnDelay * alt_ticks_per_second()) {
  283. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED1);
  284. }
  285. if (alt_nticks() - SW0on >= hexOnDelay * alt_ticks_per_second()) {
  286. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX2));
  287. }
  288.  
  289.  
  290. if ((switches & SW7) && (switches & SW8)) {
  291. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED17);
  292. //alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXE << 24) | (HEXr << 16) | (HEXr << 8));
  293. } else if (switches & SW7) {
  294.  
  295. if (alt_nticks() - SW7on >= ledOnDelay * alt_ticks_per_second()) {
  296. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED7);
  297. }
  298. if (alt_nticks() - SW7on >= hexOnDelay * alt_ticks_per_second()) {
  299. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEXA << 8));
  300. }
  301.  
  302. } else if (switches & SW8) {
  303. if (alt_nticks() - SW8on >= ledOnDelay * alt_ticks_per_second()) {
  304. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED8);
  305. }
  306. if (alt_nticks() - SW8on >= hexOnDelay * alt_ticks_per_second()) {
  307. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEXF << 8));
  308. }
  309. }
  310. break;
  311. case 2:
  312. if (alt_nticks() - SW1on >= ledOnDelay * alt_ticks_per_second()) {
  313. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED1);
  314. }
  315. if (alt_nticks() - SW1on >= hexOnDelay * alt_ticks_per_second()) {
  316. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX2));
  317. }
  318.  
  319. break;
  320. case 4:
  321.  
  322. if (alt_nticks() - SW2on >= ledOnDelay * alt_ticks_per_second()) {
  323. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED2);
  324. }
  325. if (alt_nticks() - SW2on >= hexOnDelay * alt_ticks_per_second()) {
  326. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX3));
  327. }
  328. break;
  329. case 8:
  330. if (alt_nticks() - SW3on >= ledOnDelay * alt_ticks_per_second()) {
  331. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED3);
  332. }
  333. if (alt_nticks() - SW3on >= hexOnDelay * alt_ticks_per_second()) {
  334. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX4));
  335. }
  336.  
  337. if (((switches & SW9) && (switches & SW10)) ||
  338. ((switches & SW9) && (switches & SW11)) ||
  339. ((switches & SW9) && (switches & SW12)) ||
  340. ((switches & SW10) && (switches & SW11)) ||
  341. ((switches & SW10) && (switches & SW12)) ||
  342. ((switches & SW11) && (switches & SW12))) {
  343. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED17);
  344. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXE << 24) | (HEXr << 16) | (HEXr << 8)));
  345. } else if (switches & SW9) {
  346. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED9);
  347. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXC << 16) | (HEX1 << 8)));
  348. } else if (switches & SW10) {
  349. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED10);
  350. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXC << 16) | (HEX2 << 8)));
  351. } else if (switches & SW11) {
  352. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED11);
  353. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXD << 16) | (HEX1 << 8)));
  354. } else if (switches & SW12) {
  355. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED12);
  356. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, ((HEXD << 16) | (HEX2 << 8)));
  357. }
  358. break;
  359. case 16:
  360. if (alt_nticks() - SW4on >= ledOnDelay * alt_ticks_per_second()) {
  361. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED4);
  362. }
  363. if (alt_nticks() - SW4on >= hexOnDelay * alt_ticks_per_second()) {
  364. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX5));
  365. }
  366. break;
  367. case 32:
  368. if (alt_nticks() - SW5on >= ledOnDelay * alt_ticks_per_second()) {
  369. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED5);
  370. }
  371. if (alt_nticks() - SW5on >= hexOnDelay * alt_ticks_per_second()) {
  372. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX6));
  373. }
  374. break;
  375. case 64:
  376. if (alt_nticks() - SW6on >= ledOnDelay * alt_ticks_per_second()) {
  377. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED6);
  378. }
  379. if (alt_nticks() - SW6on >= hexOnDelay * alt_ticks_per_second()) {
  380. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEX7));
  381. }
  382. break;
  383.  
  384. default:
  385. alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (HEXE << 16) | (HEXr << 8) | HEXr);
  386. alt_up_parallel_port_write_data(up_dev->uchwytLEDG, 0);
  387. alt_up_parallel_port_write_data(up_dev->uchwytLEDR, LED17);
  388. break;
  389. }
  390.  
  391. }
  392.  
  393. int main() {
  394.  
  395. IOWR(LEDS_GREEN_BASE, 0, 0);
  396. IOWR(LEDS_RED_BASE, 0, 0);
  397. IOWR(HEX_3_BASE, 0, 0);
  398. SW0on = SW0off = SW1on = SW1off = SW2on = SW2off = SW3on = SW3off = SW4on = SW4off = SW5on = SW5off = SW6on = SW6off = SW7on = SW7off = SW8on = SW8off = SW9on = SW9off = SW10on = SW10off = SW11on = SW11off = SW12on = SW12off = SWerr = SWerrFMon = SWerrFMoff = 0;
  399.  
  400. while (1) {
  401.  
  402. struct alt_up_dev up_dev;
  403. alt_up_parallel_port_dev *uchwytSW = alt_up_parallel_port_open_dev("/dev/SW_SLIDERS");
  404. alt_up_parallel_port_dev *uchwytLEDR = alt_up_parallel_port_open_dev("/dev/LEDS_RED");
  405. alt_up_parallel_port_dev *uchwytLEDG = alt_up_parallel_port_open_dev("/dev/LEDS_GREEN");
  406. alt_up_parallel_port_dev *uchwytHEX3 = alt_up_parallel_port_open_dev("/dev/HEX_3");
  407.  
  408. if (uchwytSW != NULL) {
  409. up_dev.uchwytSW = uchwytSW;
  410. }
  411. if (uchwytLEDG != NULL) {
  412. up_dev.uchwytLEDG = uchwytLEDG;
  413. }
  414. if (uchwytLEDR != NULL) {
  415. up_dev.uchwytLEDR = uchwytLEDR;
  416. }
  417. if (uchwytHEX3 != NULL) {
  418. up_dev.uchwytHEX3 = uchwytHEX3;
  419. }
  420. alt_irq_register(1, (void *) &up_dev, (void *) interrupt_handler);
  421.  
  422. alt_up_parallel_port_set_interrupt_mask(uchwytSW, 0xff);
  423.  
  424. };
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement