Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <system.h>
  4. #include "alt_types.h"
  5. #include "altera_up_avalon_parallel_port.h"
  6. #include "sys/alt_irq.h"
  7. #include "sys/alt_timestamp.h"
  8. #include <unistd.h>
  9. #include <sys/alt_alarm.h>
  10. #include "includes.h"
  11. #include <stdbool.h>
  12.  
  13. typedef enum POZIOM_TEMP{
  14. POZIOM_0 = 0,
  15. POZIOM_1 = 2,
  16. POZIOM_2 = 4,
  17. POZIOM_3 = 8,
  18. POZIOM_4 = 16,
  19. POZIOM_5 = 32,
  20. POZIOM_6 = 64,
  21. POZIOM_7 = 128,
  22. POZIOM_8 = 256,
  23. POZIOM_9 = 512,
  24. POZIOM_10 = 1024,
  25. }TEMP;
  26.  
  27. struct room{
  28. int rooms[6];
  29. bool error;
  30. };
  31.  
  32. #define SW17 0x00000001
  33. #define SW16 0x00000002
  34. #define SW15 0x00000004
  35. #define SW14 0x00000008
  36. #define SW13 0x00000010
  37. #define SW12 0x00000020
  38. #define SW11 0x00000040
  39. #define SW10 0x00000080
  40. #define SW9 0x00000100
  41. #define SW8 0x00000200
  42. #define SW7 0x00000400
  43. #define SW6 0x00000800
  44. #define SW5 0x00001000
  45. #define SW4 0x00002000
  46. #define SW3 0x00004000
  47. #define SW2 0x00008000
  48. #define SW1 0x00010000
  49. #define SW0 0x00020000
  50. #define KEY1 0x00000002
  51. #define KEY2 0x00000004
  52. #define KEY3 0x00000008
  53.  
  54. #define LED0 0x00000001
  55. #define LED1 0x00000002
  56. #define LED2 0x00000004
  57. #define LED3 0x00000008
  58. #define LED4 0x00000010
  59. #define LED5 0x00000020
  60. #define LED6 0x00000040
  61. #define LED7 0x00000080
  62. #define LED8 0x00000100
  63. #define LED9 0x00000200
  64. #define LED10 0x00000400
  65. #define LED11 0x00000800
  66. #define LED12 0x00001000
  67. #define LED13 0x00002000
  68. #define LED14 0x00004000
  69. #define LED15 0x00008000
  70. #define LED16 0x00010000
  71. #define LED17 0x00020000
  72. #define SEGA 0x01
  73. #define SEGB 0x02
  74. #define SEGC 0x04
  75. #define SEGD 0x08
  76. #define SEGE 0x10
  77. #define SEGF 0x20
  78. #define SEGG 0x40
  79.  
  80. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  81. #define ONE SEGB | SEGC
  82. #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
  83. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  84. #define FOUR SEGB | SEGC | SEGF | SEGG
  85. #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
  86. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  87. #define SEVEN SEGA | SEGB | SEGC
  88. #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
  89. #define NINE SEGA | SEGB| SEGC | SEGD | SEGF | SEGG
  90. #define R SEGE | SEGG
  91. #define E SEGA | SEGD | SEGE | SEGF | SEGG
  92. #define NIC SEGG
  93.  
  94. #define SW_BASE_ADDRESS 0x1010f0
  95. #define KEY_BASE_ADDRESS 0x1010e0
  96. #define LEDG_BASE_ADDRESS 0x101130
  97. #define LEDR_BASE_ADDRESS 0x101120
  98. #define HEX3_BASE_ADDRESS 0x101110
  99. #define HEX7_BASE_ADDRESS 0x101100
  100.  
  101. #define TASK_STACKSIZE 2048
  102. OS_STK task1_stk[TASK_STACKSIZE];
  103. OS_STK task2_stk[TASK_STACKSIZE];
  104. OS_STK task3_stk[TASK_STACKSIZE];
  105. OS_STK task4_stk[TASK_STACKSIZE];
  106. OS_STK task5_stk[TASK_STACKSIZE];
  107. OS_STK task6_stk[TASK_STACKSIZE];
  108. OS_STK task7_stk[TASK_STACKSIZE];
  109. OS_STK task8_stk[TASK_STACKSIZE];
  110.  
  111. OS_EVENT *SWBox1;
  112.  
  113. #define TASK1_PRIORITY 1
  114. #define TASK2_PRIORITY 2 //leds
  115. #define TASK3_PRIORITY 3 //r1
  116. #define TASK4_PRIORITY 4 //r2
  117. #define TASK5_PRIORITY 5 //r3
  118. #define TASK6_PRIORITY 6 //r4
  119. #define TASK7_PRIORITY 7 //r5
  120. #define TASK8_PRIORITY 8 //errory
  121.  
  122. #define SEGA 0x01
  123. #define SEGB 0x02
  124. #define SEGC 0x04
  125. #define SEGD 0x08
  126. #define SEGE 0x10
  127. #define SEGF 0x20
  128. #define SEGG 0x40
  129.  
  130. void light_hex(int address, int stat, int shift)
  131. {
  132.  
  133. switch (stat) {
  134. case 0:
  135. IOWR(address, 0, ((ZERO) << shift));
  136. break;
  137. case 1:
  138. IOWR(address, 0, ((ONE) << shift));
  139. break;
  140. case 2:
  141. IOWR(address, 0, ((TWO) << shift));
  142. break;
  143. case 3:
  144. IOWR(address, 0, ((THREE) << shift));
  145. break;
  146. case 4:
  147. IOWR(address, 0, ((FOUR) << shift));
  148. break;
  149. case 5:
  150. IOWR(address, 0, ((FIVE) << shift));
  151. break;
  152. case 6:
  153. IOWR(address, 0, ((SIX) << shift));
  154. break;
  155. case 7:
  156. IOWR(address, 0, ((SEVEN) << shift));
  157. break;
  158. case 8:
  159. IOWR(address, 0, ((EIGHT) << shift));
  160. break;
  161. case 9:
  162. IOWR(address, 0, ((NINE) << shift));
  163. break;
  164. case 50:
  165. IOWR(address, 0, ((NIC) << shift));
  166. break;
  167. case 100:
  168. IOWR(address, 0, ((R) << shift));
  169. break;
  170. case 1000:
  171. IOWR(address, 0, ((E) << shift));
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177.  
  178.  
  179. void task1(void* pdata)
  180. {
  181. int f[10];
  182. int state,i,direction;
  183.  
  184. for (i = 0; i<10; i++)
  185. f[i] = 0;
  186.  
  187. struct room r;
  188. r.error = false;
  189. for (i = 0; i < 6; i++)
  190. r.rooms[i]=0;
  191.  
  192. while (1)
  193. {
  194. if (IORD(SW_BASE_ADDRESS, 0) == 0)
  195. {
  196. for (i = 0; i <10; i++)
  197. f[i] = 1;
  198. r.error = false;
  199. }
  200.  
  201. if (IORD(KEY_BASE_ADDRESS, 0) & KEY3)
  202. direction = 1;
  203. else
  204. direction = 0;
  205.  
  206.  
  207. if (direction == 1)
  208. {
  209. switch (state)
  210. {
  211. case 32: //cd1
  212. if (f[0] == 1)
  213. {
  214. if (r.rooms[1] == 0)
  215. {
  216. r.error=true;
  217. }
  218. else
  219. {
  220. r.rooms[1]--;
  221. r.rooms[5]++;
  222. }
  223. f[0] = 0;
  224. }
  225. break;
  226.  
  227. case 64: //cd2
  228. if (f[1] == 1)
  229. {
  230. r.rooms[1]++;
  231. f[1] = 0;
  232. }
  233. break;
  234.  
  235. case 128: //cd3
  236. if (f[2] == 1)
  237. {
  238. r.rooms[1]++;
  239. f[2] = 0;
  240. }
  241. break;
  242.  
  243. case 256: //cd4
  244. if (f[3] == 1)
  245. {
  246. r.rooms[5]++;
  247. f[3] = 0;
  248. }
  249. break;
  250.  
  251. case 512: //cd5
  252. if (f[4] == 1)
  253. {
  254. r.rooms[5]++;
  255. f[4] = 0;
  256. }
  257. break;
  258.  
  259. case 1024: //cd6
  260. if (f[5] == 1)
  261. {
  262. if (r.rooms[1] == 0)
  263. {
  264. r.error = true;
  265. }
  266. else
  267. {
  268. r.rooms[1]--;
  269. r.rooms[4]++;
  270. }
  271. f[5] = 0;
  272. }
  273. break;
  274.  
  275. case 2048: //cd7
  276. if (f[6] == 1)
  277. {
  278. r.rooms[4]++;
  279. f[6] = 0;
  280. }
  281. break;
  282.  
  283. case 4096: //cd8
  284. if (f[7] == 1)
  285. {
  286. r.rooms[4]++;
  287. f[7] = 0;
  288. }
  289. break;
  290.  
  291. case 8192: //cd9
  292. if (f[8] == 1)
  293. {
  294. if (r.rooms[2] == 0)
  295. {
  296. r.error = true;
  297. }
  298. else
  299. {
  300. r.rooms[2]--;
  301. r.rooms[3]++;
  302. }
  303. f[8] = 0;
  304. }
  305. break;
  306.  
  307. case 16384: //cd10
  308. if (f[9] == 1)
  309. {
  310. if (r.rooms[2] == 0)
  311. {
  312. r.error = true;
  313. }
  314. else
  315. {
  316. r.rooms[2]--;
  317. r.rooms[4]++;
  318. }
  319. f[9] = 0;
  320. }
  321. break;
  322. default:
  323. break;
  324. }
  325. }
  326.  
  327. else
  328. {
  329. switch (state)
  330. {
  331. case 32: //cd1
  332. if (f[0] == 1)
  333. {
  334. if (r.rooms[5] == 0)
  335. {
  336. r.error = true;
  337. }
  338. else
  339. {
  340. r.rooms[5]--;
  341. r.rooms[1]++;
  342. }
  343. f[0] = 0;
  344. }
  345. break;
  346.  
  347. case 64: //cd2
  348. if (f[1] == 1)
  349. {
  350. if (r.rooms[1] == 0)
  351. {
  352. r.error = true;
  353. }
  354. else
  355. {
  356. r.rooms[1]--;
  357. }
  358. f[1] = 0;
  359. }
  360. break;
  361.  
  362. case 128: //cd3
  363. if (f[2] == 1)
  364. {
  365. if (r.rooms[1] == 0)
  366. {
  367. r.error = true;
  368. }
  369. else
  370. {
  371. r.rooms[1]--;
  372. }
  373. f[2] = 0;
  374. }
  375. break;
  376.  
  377. case 256: //cd4
  378. if (f[3] == 1)
  379. {
  380. if (r.rooms[5] == 0)
  381. {
  382. r.error = true;
  383. }
  384. else
  385. {
  386. r.rooms[5]--;
  387. }
  388. f[3] = 0;
  389. }
  390. break;
  391.  
  392. case 512: //cd5
  393. if (f[4] == 1)
  394. {
  395. if (r.rooms[5] == 0)
  396. {
  397. r.error = true;
  398. }
  399. else
  400. {
  401. r.rooms[5]--;
  402. }
  403. f[4] = 0;
  404. }
  405. break;
  406.  
  407. case 1024: //cd6
  408. if (f[5] == 1)
  409. {
  410. if (r.rooms[4] == 0)
  411. {
  412. r.error = true;
  413. }
  414. else
  415. {
  416. r.rooms[4]--;
  417. r.rooms[1]++;
  418. }
  419. f[5] = 0;
  420. }
  421. break;
  422.  
  423. case 2048: //cd7
  424. if (f[6] == 1)
  425. {
  426. if (r.rooms[4] == 0)
  427. {
  428. r.error = true;
  429. }
  430. else
  431. {
  432. r.rooms[4]--;
  433. }
  434. f[6] = 0;
  435. }
  436. break;
  437.  
  438. case 4096: //cd8
  439. if (f[7] == 1)
  440. {
  441. if (r.rooms[4] == 0)
  442. {
  443. r.error = true;
  444. }
  445. else
  446. {
  447. r.rooms[4]--;
  448. }
  449. f[7] = 0;
  450. }
  451. break;
  452.  
  453. case 8192: //cd9
  454. if (f[8] == 1)
  455. {
  456. if (r.rooms[3] == 0)
  457. {
  458. r.error = true;
  459. }
  460. else
  461. {
  462. r.rooms[3]--;
  463. r.rooms[2]++;
  464. }
  465. f[8] = 0;
  466. }
  467. break;
  468.  
  469. case 16384: //cd10
  470. if (f[9] == 1)
  471. {
  472. if (r.rooms[4] == 0)
  473. {
  474. r.error = true;
  475. }
  476. else
  477. {
  478. r.rooms[4]--;
  479. r.rooms[2]++;
  480. }
  481. f[9] = 0;
  482. }
  483. break;
  484. default:
  485. break;
  486. }
  487. }
  488. OSMboxPostOpt(SWBox1, r.rooms, OS_POST_OPT_BROADCAST);
  489. OSTimeDlyHMSM(0, 0, 0, 10);
  490. }
  491. }
  492.  
  493. void task2(void* pdata)
  494. {
  495. while (1)
  496. {
  497. INT8U err;
  498. struct room *room_led;
  499. int *rooms;
  500. rooms = OSMboxPend(SWBox1, 0, &err);
  501. int leds_to_light = 0;
  502.  
  503. if(rooms[1] > 0)
  504. leds_to_light += LED1;
  505. if(rooms[2] > 0)
  506. leds_to_light += LED2;
  507. if(rooms[3] > 0)
  508. leds_to_light += LED3;
  509. if(rooms[4] > 0)
  510. leds_to_light += LED4;
  511. if(rooms[5] > 0)
  512. leds_to_light += LED5;
  513.  
  514. IOWR(LEDG_BASE_ADDRESS, 0, leds_to_light);
  515. OSTimeDlyHMSM(0, 0, 0, 10);
  516. }
  517. }
  518.  
  519. void task3(void* pdata)
  520. {
  521. while (1)
  522. {
  523. INT8U err;
  524. struct room *room1;
  525. room1 = OSMboxPend(SWBox1, 0, &err);
  526. light_hex(HEX3_BASE_ADDRESS, room1->rooms[1], 0);
  527. OSTimeDlyHMSM(0, 0, 0, 10);
  528. }
  529. }
  530.  
  531. void task4(void* pdata)
  532. {
  533. while (1)
  534. {
  535. INT8U err;
  536. struct room *room2;
  537. room2 = OSMboxPend(SWBox1, 0, &err);
  538. light_hex(HEX3_BASE_ADDRESS, room2->rooms[2], 8);
  539. OSTimeDlyHMSM(0, 0, 0, 10);
  540. }
  541. }
  542.  
  543. void task5(void* pdata)
  544. {
  545. while (1)
  546. {
  547. INT8U err;
  548. struct room *room3;
  549. room3 = OSMboxPend(SWBox1, 0, &err);
  550. light_hex(HEX3_BASE_ADDRESS, room3->rooms[3], 16);
  551. OSTimeDlyHMSM(0, 0, 0, 10);
  552. }
  553. }
  554. void task6(void* pdata)
  555. {
  556. while (1)
  557. {
  558. INT8U err;
  559. struct room *room4;
  560. room4 = OSMboxPend(SWBox1, 0, &err);
  561. light_hex(HEX3_BASE_ADDRESS, room4->rooms[4], 24);
  562. OSTimeDlyHMSM(0, 0, 0, 10);
  563. }
  564. }
  565. void task7(void* pdata)
  566. {
  567. while (1)
  568. {
  569. INT8U err;
  570. struct room *room5;
  571. room5 = OSMboxPend(SWBox1, 0, &err);
  572. light_hex(HEX7_BASE_ADDRESS, room5->rooms[5], 0);
  573. OSTimeDlyHMSM(0, 0, 0, 10);
  574. }
  575. }
  576. void task8(void* pdata)
  577. {
  578. while (1)
  579. {
  580. INT8U err;
  581. struct room *room_error;
  582. room_error = OSMboxPend(SWBox1, 0, &err);
  583. if(room_error->error == true)
  584. {
  585. IOWR(LEDR_BASE_ADDRESS, 0, LED1);
  586. light_hex(HEX7_BASE_ADDRESS, 100, 8);
  587. light_hex(HEX7_BASE_ADDRESS, 100, 16);
  588. light_hex(HEX7_BASE_ADDRESS, 1000, 24);
  589. }
  590. OSTimeDlyHMSM(0, 0, 0, 10);
  591. }
  592. }
  593.  
  594. int main(void)
  595. {
  596.  
  597. SWBox1 =OSMboxCreate((void*)0);
  598.  
  599. OSTaskCreateExt(task1,
  600. NULL,
  601. (void *)&task1_stk[TASK_STACKSIZE-1],
  602. TASK1_PRIORITY,
  603. TASK1_PRIORITY,
  604. task1_stk,
  605. TASK_STACKSIZE,
  606. NULL,
  607. 0);
  608.  
  609.  
  610. OSTaskCreateExt(task2,
  611. NULL,
  612. (void *)&task2_stk[TASK_STACKSIZE-1],
  613. TASK2_PRIORITY,
  614. TASK2_PRIORITY,
  615. task2_stk,
  616. TASK_STACKSIZE,
  617. NULL,
  618. 0);
  619.  
  620. OSTaskCreateExt(task3,
  621. NULL,
  622. (void *)&task3_stk[TASK_STACKSIZE-1],
  623. TASK3_PRIORITY,
  624. TASK3_PRIORITY,
  625. task3_stk,
  626. TASK_STACKSIZE,
  627. NULL,
  628. 0);
  629.  
  630. OSTaskCreateExt(task4,
  631. NULL,
  632. (void *)&task4_stk[TASK_STACKSIZE-1],
  633. TASK4_PRIORITY,
  634. TASK4_PRIORITY,
  635. task4_stk,
  636. TASK_STACKSIZE,
  637. NULL,
  638. 0);
  639.  
  640. OSTaskCreateExt(task5,
  641. NULL,
  642. (void *)&task5_stk[TASK_STACKSIZE-1],
  643. TASK5_PRIORITY,
  644. TASK5_PRIORITY,
  645. task5_stk,
  646. TASK_STACKSIZE,
  647. NULL,
  648. 0);
  649.  
  650. OSTaskCreateExt(task6,
  651. NULL,
  652. (void *)&task6_stk[TASK_STACKSIZE-1],
  653. TASK6_PRIORITY,
  654. TASK6_PRIORITY,
  655. task6_stk,
  656. TASK_STACKSIZE,
  657. NULL,
  658. 0);
  659. OSTaskCreateExt(task7,
  660. NULL,
  661. (void *)&task7_stk[TASK_STACKSIZE-1],
  662. TASK7_PRIORITY,
  663. TASK7_PRIORITY,
  664. task7_stk,
  665. TASK_STACKSIZE,
  666. NULL,
  667. 0);
  668. OSTaskCreateExt(task8,
  669. NULL,
  670. (void *)&task8_stk[TASK_STACKSIZE-1],
  671. TASK8_PRIORITY,
  672. TASK8_PRIORITY,
  673. task8_stk,
  674. TASK_STACKSIZE,
  675. NULL,
  676. 0);
  677.  
  678.  
  679. OSStart();
  680. return 0;
  681. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement