Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <system.h>
  3. #include "altera_up_avalon_character_lcd.h"
  4. #include "includes.h"
  5. #include "altera_up_avalon_parallel_port.h"
  6. #include <stddef.h>
  7. #include "sys/alt_alarm.h"
  8. #include "alt_types.h"
  9.  
  10. #define  SW0 0x00000001
  11. #define  SW1 0x00000002
  12. #define  SW2 0x00000004
  13. #define  SW3 0x00000008
  14. #define  SW4 0x00000010
  15. #define  SW5 0x00000020
  16. #define  SW6 0x00000040
  17. #define  SW7 0x00000080
  18. #define  SW8 0x00000100
  19. #define  SW9 0x00000200
  20. #define  SW10 0x00000400
  21. #define  SW11 0x00000800
  22. #define  SW12 0x00001000
  23. #define  SW13 0x00002000
  24. #define  SW14 0x00004000
  25. #define  SW15 0x00008000
  26. #define  SW16 0x00010000
  27. #define  SW17 0x00020000
  28.  
  29. #define  KEY1 0x00000002
  30. #define  KEY2 0x00000004
  31. #define  KEY3 0x00000008
  32.  
  33. #define  LED0 0x00000001
  34. #define  LED1 0x00000002
  35. #define  LED2 0x00000004
  36. #define  LED3 0x00000008
  37. #define  LED4 0x00000010
  38. #define  LED5 0x00000020
  39. #define  LED6 0x00000040
  40. #define  LED7 0x00000080
  41. #define  LED8 0x00000100
  42. #define  LED9 0x00000200
  43. #define  LED10 0x00000400
  44. #define  LED11 0x00000800
  45. #define  LED12 0x00001000
  46. #define  LED13 0x00002000
  47. #define  LED14 0x00004000
  48. #define  LED15 0x00008000
  49. #define  LED16 0x00010000
  50. #define  LED17 0x00020000
  51.  
  52. #define  SEGA 0x01      //    __A__
  53. #define  SEGB 0x02      //   |     |
  54. #define  SEGC 0x04      // F |     | B
  55. #define  SEGD 0x08      //   |_____|
  56. #define  SEGE 0x10      //   |  G  |
  57. #define  SEGF 0x20      // E |     | C
  58. #define  SEGG 0x40      //   |_____|
  59. //-------------------------//
  60.  
  61. /* Definition of Task Stacks */
  62. #define TASK_STACKSIZE 2048
  63. OS_STK pokoj1_stk[TASK_STACKSIZE];
  64. OS_STK pokoj2_stk[TASK_STACKSIZE];
  65. OS_STK pokoj3_stk[TASK_STACKSIZE];
  66. OS_STK pokoj4_stk[TASK_STACKSIZE];
  67. OS_STK pokoj5_stk[TASK_STACKSIZE];
  68.  
  69. OS_EVENT *SWBox1;
  70. OS_EVENT *LEDBox1;
  71.  
  72. struct o {
  73.     int led;
  74.     int nad;
  75. };
  76.  
  77. alt_up_parallel_port_dev *uchwytLEDR;
  78. alt_up_character_lcd_dev *uchwytLCD;
  79.  
  80. alt_alarm alarm;
  81. int flag;
  82. int nad;
  83.  
  84. int ledg = 0;
  85. int ledr = 0;
  86.  
  87. int pokoje[6] = { 0, 0, 0, 0, 0, 0 };
  88.  
  89. void pokoj1(void* pdata) {
  90.     printf("Task1");
  91.     int s2 = 0;
  92.     int s3 = 0;
  93.     int sw = 0;
  94.     INT8U err;
  95.     int czas;
  96.     int flaga_zgas = 0;
  97.     int czas2;
  98.     int flaga_zgas2 = 0;
  99.     while (1) {
  100.         sw = IORD(PARALLEL_PORT_0_BASE, 0);
  101.         OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  102.  
  103.         if (!(sw & SW2) && !(sw & SW3)) {
  104.             OSTimeDlyHMSM(0, 0, 0, 10);
  105.             s2 = 0;
  106.             s3 = 0;
  107.             continue;
  108.         }
  109.         INT8U err;
  110.  
  111.         int kierunek = sw & SW17;
  112.         if (sw & SW2 && s2 == 0) {
  113.             s2 = 1;
  114.             if (kierunek != 0) {
  115.                 if (pokoje[2] > 0) {
  116.                     pokoje[1]++;
  117.                 }
  118.             } else {
  119.                 pokoje[1]--;
  120.             }
  121.         }
  122.         if (!(sw & SW2)) {
  123.             s2 = 0;
  124.         }
  125.  
  126.         if (sw & SW3 && s3 == 0) {
  127.             s3 = 1;
  128.             if (kierunek != 0) {
  129.                 pokoje[1]--;
  130.             } else {
  131.                 if (pokoje[4] > 0) {
  132.                     pokoje[1]++;
  133.                 }
  134.             }
  135.         }
  136.         if (!(sw & SW3)) {
  137.             s3 = 0;
  138.         }
  139.  
  140.         if (pokoje[1] > 0) {
  141.             ledg |= LED1;
  142.         }
  143.         if (pokoje[1] < 0) {
  144.             if (flaga_zgas2 == 0) {
  145.                 czas2 = alt_nticks();
  146.                 flaga_zgas2 = 1;
  147.             }
  148.             printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
  149.             if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
  150.                 pokoje[1] = 0;
  151.                 ledr |= LED1;
  152.                 flaga_zgas2 = 0;
  153.             }
  154.         }
  155.         if (pokoje[1] == 0) {
  156.             if ((ledg && LED1) && (flaga_zgas == 0)) {
  157.                 czas = alt_nticks();
  158.                 flaga_zgas = 1;
  159.             }
  160.             printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
  161.             if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
  162.                 ledg = ledg & ~LED1;
  163.                 flaga_zgas = 0;
  164.             }
  165.         }
  166.         alt_up_character_lcd_set_cursor_pos(uchwytLCD, 0, 1);
  167.         char liczba[3];
  168.         snprintf(liczba, 3, "%d", pokoje[1]);
  169.         alt_up_character_lcd_string(uchwytLCD, liczba);
  170.         IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
  171.         IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
  172.         OSTimeDlyHMSM(0, 0, 0, 10);
  173.     }
  174. }
  175.  
  176. void pokoj2(void* pdata) {
  177.     printf("Task2");
  178.     int s1 = 0;
  179.     int s2 = 0;
  180.     int s5 = 0;
  181.     int sw = 0;
  182.     int *in;
  183.     INT8U err;
  184.     int czas;
  185.     int flaga_zgas = 0;
  186.     int czas2;
  187.     int flaga_zgas2 = 0;
  188.     while (1) {
  189.         in = OSMboxPend(SWBox1, 0, &err);
  190.         sw = *in;
  191.         if (!(sw & SW1) && !(sw & SW2) && !(sw & SW5)) {
  192.             OSTimeDlyHMSM(0, 0, 0, 10);
  193.             s1 = 0;
  194.             s2 = 0;
  195.             s5 = 0;
  196.             continue;
  197.         }
  198.         INT8U err;
  199.         int kierunek = sw & SW17;
  200.         if (sw & SW1 && s1 == 0) {
  201.             s1 = 1;
  202.             if (kierunek != 0) {
  203.                 pokoje[2]++;
  204.             } else {
  205.                 pokoje[2]--;
  206.             }
  207.         }
  208.         if (!(sw & SW1)) {
  209.             s1 = 0;
  210.         }
  211.  
  212.         if (sw & SW2 && s2 == 0) {
  213.             s2 = 1;
  214.             if (kierunek != 0) {
  215.                 pokoje[2]--;
  216.             } else {
  217.                 if (pokoje[1] > 0) {
  218.                     pokoje[2]++;
  219.                 }
  220.             }
  221.         }
  222.         if (!(sw & SW2)) {
  223.             s2 = 0;
  224.         }
  225.  
  226.         if (sw & SW5 && s5 == 0) {
  227.             s5 = 1;
  228.             if (kierunek != 0) {
  229.                 pokoje[2]--;
  230.             } else {
  231.                 if (pokoje[3] > 0) {
  232.                     pokoje[2]++;
  233.                 }
  234.             }
  235.         }
  236.         if (!(sw & SW5)) {
  237.             s5 = 0;
  238.         }
  239.  
  240.         if (pokoje[2] > 0) {
  241.             ledg |= LED2;
  242.         }
  243.         if (pokoje[2] < 0) {
  244.             if (flaga_zgas2 == 0) {
  245.                 czas2 = alt_nticks();
  246.                 flaga_zgas2 = 1;
  247.             }
  248.             printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
  249.             if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
  250.                 pokoje[2] = 0;
  251.                 ledr |= LED2;
  252.                 flaga_zgas2 = 0;
  253.             }
  254.         }
  255.         if (pokoje[2] <= 0) {
  256.             if ((ledg && LED2) && (flaga_zgas == 0)) {
  257.                 czas = alt_nticks();
  258.                 flaga_zgas = 1;
  259.             }
  260.             printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
  261.             if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
  262.                 ledg = ledg & ~LED2;
  263.                 flaga_zgas = 0;
  264.             }
  265.         }
  266.         printf("osoby: %d", pokoje[2]);
  267.         alt_up_character_lcd_set_cursor_pos(uchwytLCD, 3, 1);
  268.         char liczba[3];
  269.         snprintf(liczba, 3, "%d", pokoje[2]);
  270.         alt_up_character_lcd_string(uchwytLCD, liczba);
  271.         IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
  272.         IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
  273.         OSTimeDlyHMSM(0, 0, 0, 10);
  274.     }
  275. }
  276.  
  277. void pokoj3(void* pdata) {
  278.     printf("Task3");
  279.     int s5 = 0;
  280.     int s6 = 0;
  281.     int sw = 0;
  282.     int *in;
  283.     INT8U err;
  284.     int czas;
  285.     int flaga_zgas = 0;
  286.     int czas2;
  287.     int flaga_zgas2 = 0;
  288.     while (1) {
  289.         in = OSMboxPend(SWBox1, 0, &err);
  290.         sw = *in;
  291.         if (!(sw & SW5) && !(sw & SW6) ) {
  292.             OSTimeDlyHMSM(0, 0, 0, 10);
  293.             s5 = 0;
  294.             s6 = 0;
  295.             continue;
  296.         }
  297.  
  298.         int kierunek = sw & SW17;
  299.         if (sw & SW5 && s5 == 0) {
  300.             s5 = 1;
  301.             if (kierunek != 0) {
  302.                 if(pokoje[2]>0){
  303.                     pokoje[3]++;
  304.                 }
  305.             } else {
  306.                 pokoje[3]--;
  307.             }
  308.         }
  309.         if (!(sw & SW5)) {
  310.             s5 = 0;
  311.         }
  312.  
  313.         if (sw & SW6 && s6 == 0) {
  314.             s6 = 1;
  315.             if (kierunek != 0) {
  316.                 pokoje[3]--;
  317.             } else {
  318.                 if(pokoje[5]>0){
  319.                     pokoje[3]++;
  320.                 }
  321.             }
  322.         }
  323.         if (!(sw & SW6)) {
  324.             s6 = 0;
  325.         }
  326.  
  327.         if (pokoje[3] > 0) {
  328.             ledg |= LED3;
  329.         }
  330.         if (pokoje[3] < 0) {
  331.             if (flaga_zgas2 == 0) {
  332.                 czas2 = alt_nticks();
  333.                 flaga_zgas2 = 1;
  334.             }
  335.             printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
  336.             if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
  337.                 pokoje[3] = 0;
  338.                 ledr |= LED3;
  339.                 flaga_zgas2 = 0;
  340.             }
  341.         }
  342.         if (pokoje[3] == 0) {
  343.             if ((ledg && LED3) && (flaga_zgas == 0)) {
  344.                 czas = alt_nticks();
  345.                 flaga_zgas = 1;
  346.             }
  347.             printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
  348.             if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
  349.                 ledg = ledg & ~LED3;
  350.                 flaga_zgas = 0;
  351.             }
  352.         }
  353.         alt_up_character_lcd_set_cursor_pos(uchwytLCD, 6, 1);
  354.         char liczba[3];
  355.         snprintf(liczba, 3, "%d", pokoje[3]);
  356.         alt_up_character_lcd_string(uchwytLCD, liczba);
  357.         IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
  358.         IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
  359.         OSTimeDlyHMSM(0, 0, 0, 10);
  360.     }
  361. }
  362.  
  363. void pokoj4(void* pdata) {
  364.     printf("Task4");
  365.     int s3 = 0;
  366.     int s4 = 0;
  367.     int sw = 0;
  368.     int *in;
  369.     INT8U err;
  370.     int czas;
  371.     int flaga_zgas = 0;
  372.     int czas2;
  373.     int flaga_zgas2 = 0;
  374.     while (1) {
  375.         in = OSMboxPend(SWBox1, 0, &err);
  376.         sw = *in;
  377.         if (!(sw & SW3) && !(sw & SW4) ) {
  378.             OSTimeDlyHMSM(0, 0, 0, 10);
  379.             s3 = 0;
  380.             s4 = 0;
  381.             continue;
  382.         }
  383.  
  384.         int kierunek = sw & SW17;
  385.         if (sw & SW3 && s3 == 0) {
  386.             s3 = 1;
  387.             if (kierunek != 0) {
  388.                 if(pokoje[1]>0){
  389.                     pokoje[4]++;
  390.                 }
  391.             } else {
  392.                 pokoje[4]--;
  393.             }
  394.         }
  395.         if (!(sw & SW3)) {
  396.             s3 = 0;
  397.         }
  398.  
  399.         if (sw & SW4 && s4 == 0) {
  400.             s4 = 1;
  401.             if (kierunek != 0) {
  402.                 pokoje[4]--;
  403.             } else {
  404.                 pokoje[4]++;
  405.             }
  406.         }
  407.         if (!(sw & SW4)) {
  408.             s4 = 0;
  409.         }
  410.  
  411.         if (pokoje[4] > 0) {
  412.             ledg |= LED4;
  413.         }
  414.         if (pokoje[4] < 0) {
  415.             if (flaga_zgas2 == 0) {
  416.                 czas2 = alt_nticks();
  417.                 flaga_zgas2 = 1;
  418.             }
  419.             printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
  420.             if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
  421.                 pokoje[4] = 0;
  422.                 ledr |= LED4;
  423.                 flaga_zgas2 = 0;
  424.             }
  425.         }
  426.         if (pokoje[4] == 0) {
  427.             if ((ledg && LED4) && (flaga_zgas == 0)) {
  428.                 czas = alt_nticks();
  429.                 flaga_zgas = 1;
  430.             }
  431.             printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
  432.             if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
  433.                 ledg = ledg & ~LED4;
  434.                 flaga_zgas = 0;
  435.             }
  436.         }
  437.         printf("pokoj4: %d", pokoje[4]);
  438.         alt_up_character_lcd_set_cursor_pos(uchwytLCD, 9, 1);
  439.         char liczba[3];
  440.         snprintf(liczba, 3, "%d", pokoje[4]);
  441.         alt_up_character_lcd_string(uchwytLCD, liczba);
  442.         IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
  443.         IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
  444.         OSTimeDlyHMSM(0, 0, 0, 10);
  445.     }
  446. }
  447.  
  448. void pokoj5(void* pdata) {
  449.     printf("Task4");
  450.     int s6 = 0;
  451.     int s7 = 0;
  452.     int sw = 0;
  453.     int *in;
  454.     INT8U err;
  455.     int czas;
  456.     int flaga_zgas = 0;
  457.     int czas2;
  458.     int flaga_zgas2 = 0;
  459.     while (1) {
  460.         in = OSMboxPend(SWBox1, 0, &err);
  461.         sw = *in;
  462.         if (!(sw & SW6) && !(sw & SW7) ) {
  463.             OSTimeDlyHMSM(0, 0, 0, 10);
  464.             s6 = 0;
  465.             s7 = 0;
  466.             continue;
  467.         }
  468.  
  469.         int kierunek = sw & SW17;
  470.         if (sw & SW6 && s6 == 0) {
  471.             s6 = 1;
  472.             if (kierunek != 0) {
  473.                 if(pokoje[3]>0){
  474.                     pokoje[5]++;
  475.                 }
  476.             } else {
  477.                 pokoje[5]--;
  478.             }
  479.         }
  480.         if (!(sw & SW6)) {
  481.             s6 = 0;
  482.         }
  483.  
  484.         if (sw & SW7 && s7 == 0) {
  485.             s7 = 1;
  486.             if (kierunek != 0) {
  487.                 pokoje[5]--;
  488.             } else {
  489.                 pokoje[5]++;
  490.             }
  491.         }
  492.         if (!(sw & SW7)) {
  493.             s7 = 0;
  494.         }
  495.  
  496.         if (pokoje[5] > 0) {
  497.             ledg |= LED5;
  498.         }
  499.         if (pokoje[5] < 0) {
  500.             if (flaga_zgas2 == 0) {
  501.                 czas2 = alt_nticks();
  502.                 flaga_zgas2 = 1;
  503.             }
  504.             printf("czas %d %d", alt_nticks() - czas2, alt_ticks_per_second());
  505.             if (alt_nticks() - czas2 >= 1 * alt_ticks_per_second()) {
  506.                 pokoje[5] = 0;
  507.                 ledr |= LED5;
  508.                 flaga_zgas2 = 0;
  509.             }
  510.         }
  511.         if (pokoje[5] == 0) {
  512.             if ((ledg && LED5) && (flaga_zgas == 0)) {
  513.                 czas = alt_nticks();
  514.                 flaga_zgas = 1;
  515.             }
  516.             printf("czas %d %d", alt_nticks() - czas, alt_ticks_per_second());
  517.             if (alt_nticks() - czas >= 3 * alt_ticks_per_second()) {
  518.                 ledg = ledg & ~LED5;
  519.                 flaga_zgas = 0;
  520.             }
  521.         }
  522.         printf("pokoj4: %d", pokoje[5]);
  523.         alt_up_character_lcd_set_cursor_pos(uchwytLCD, 12, 1);
  524.         char liczba[3];
  525.         snprintf(liczba, 3, "%d", pokoje[5]);
  526.         alt_up_character_lcd_string(uchwytLCD, liczba);
  527.         IOWR(PARALLEL_PORT_1_BASE, 0, ledg);
  528.         IOWR(PARALLEL_PORT_2_BASE, 0, ledr);
  529.         OSTimeDlyHMSM(0, 0, 0, 10);
  530.     }
  531. }
  532.  
  533. /* The main function creates two task and starts multi-tasking */
  534. int main(void) {
  535.     flag = 1;
  536.  
  537.     IOWR(PARALLEL_PORT_1_BASE, 0, 0);
  538.     IOWR(PARALLEL_PORT_2_BASE, 0, 0);
  539.  
  540.     uchwytLCD = alt_up_character_lcd_open_dev("/dev/character_lcd_0");
  541.     alt_up_character_lcd_init(uchwytLCD);
  542.     alt_up_character_lcd_set_cursor_pos(uchwytLCD, 0, 0);
  543.     alt_up_character_lcd_string(uchwytLCD, "P1 P2 P3 P4 P5");
  544.     SWBox1 = OSMboxCreate((void*) 0);
  545.  
  546.     OSTaskCreateExt(pokoj1, NULL, (void *) &pokoj1_stk[TASK_STACKSIZE - 1], 1,
  547.             1, pokoj1_stk, TASK_STACKSIZE, NULL, 0);
  548.  
  549.     OSTaskCreateExt(pokoj2, NULL, (void *) &pokoj2_stk[TASK_STACKSIZE - 1], 2,
  550.             2, pokoj2_stk, TASK_STACKSIZE, NULL, 0);
  551.  
  552.     OSTaskCreateExt(pokoj3, NULL, (void *) &pokoj3_stk[TASK_STACKSIZE - 1], 3,
  553.             3, pokoj3_stk, TASK_STACKSIZE, NULL, 0);
  554.  
  555.     OSTaskCreateExt(pokoj4, NULL, (void *) &pokoj4_stk[TASK_STACKSIZE - 1], 4,
  556.             4, pokoj4_stk, TASK_STACKSIZE, NULL, 0);
  557.  
  558.     OSTaskCreateExt(pokoj5, NULL, (void *) &pokoj5_stk[TASK_STACKSIZE - 1], 5,
  559.                 5, pokoj5_stk, TASK_STACKSIZE, NULL, 0);
  560.  
  561.     OSStart();
  562.     return 0;
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement