Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.04 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <system.h>
  4. #include "altera_up_avalon_parallel_port.h"
  5. #include "sys/alt_irq.h"
  6. #include <sys/alt_alarm.h>
  7.  
  8. #define LED0 0x00000001
  9. #define LED1 0x00000002
  10. #define LED2 0x00000004
  11. #define LED3 0x00000008
  12. #define LED4 0x00000010
  13. #define LED5 0x00000020
  14. #define LED6 0x00000040
  15. #define LED7 0x00000080
  16. #define LED8 0x00000100
  17. #define LED9 0x00000200
  18. #define LED10 0x00000400
  19. #define LED11 0x00000800
  20. #define LED12 0x00001000
  21. #define LED13 0x00002000
  22. #define LED14 0x00004000
  23. #define LED15 0x00008000
  24. #define LED16 0x00010000
  25. #define LED17 0x00020000
  26. #define SEGA 0x01
  27. #define SEGB 0x02
  28. #define SEGC 0x04
  29. #define SEGD 0x08
  30. #define SEGE 0x10
  31. #define SEGF 0x20
  32. #define SEGG 0x40
  33.  
  34. #define SW0 0x00000001
  35. #define SW1 0x00000002
  36. #define SW2 0x00000004
  37. #define SW3 0x00000008
  38. #define SW4 0x00000010
  39. #define SW5 0x00000020
  40. #define SW6 0x00000040
  41. #define SW7 0x00000080
  42. #define SW8 0x00000100
  43. #define SW9 0x00000200
  44. #define SW10 0x00000400
  45. #define SW11 0x00000800
  46. #define SW12 0x00001000
  47. #define SW13 0x00002000
  48. #define SW14 0x00004000
  49. #define SW15 0x00008000
  50. #define SW16 0x00010000
  51. #define SW17 0x00020000
  52. #define KEY1 0x00000002
  53. #define KEY2 0x00000004
  54. #define KEY3 0x00000008
  55.  
  56. struct alt_up_dev
  57. {
  58.     alt_up_parallel_port_dev *uchwytSW;
  59.     alt_up_parallel_port_dev *uchwytLEDG;
  60.     alt_up_parallel_port_dev *uchwytLEDR;
  61.     alt_up_parallel_port_dev *uchwytHEX3;
  62.     alt_up_parallel_port_dev *uchwytHEX7;
  63. };
  64.  
  65. int NumberTo7SEG(int number)
  66. {
  67.     int cHEX = 0;
  68.  
  69.     switch (x)
  70.     {
  71.     case 0:
  72.         cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF;
  73.         break;
  74.     case 1:
  75.         cHEX = SEGB | SEGC;
  76.         break;
  77.  
  78.     case 2:
  79.         cHEX = SEGA | SEGB | SEGG | SEGE | SEGD;
  80.         break;
  81.  
  82.     case 3:
  83.         cHEX = SEGA | SEGB | SEGG | SEGC | SEGD;
  84.         break;
  85.  
  86.     case 4:
  87.         cHEX = SEGF | SEGG | SEGB | SEGC;
  88.         break;
  89.  
  90.     case 5:
  91.         cHEX = SEGA | SEGF | SEGG | SEGD | SEGC;
  92.         break;
  93.  
  94.     case 6:
  95.         cHEX = SEGA | SEGF | SEGE | SEGG | SEGD | SEGC;
  96.         break;
  97.  
  98.     case 7:
  99.         cHEX = SEGA | SEGB | SEGC;
  100.         break;
  101.  
  102.     case 8:
  103.         cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG;
  104.         break;
  105.  
  106.     // ToDo: add 9 :)
  107.  
  108.     default:
  109.         cHEX = -1;
  110.     }
  111.  
  112.     return cHEX;
  113. }
  114.  
  115. void printHEX0(int x, struct alt_up_dev *up_dev)
  116. {
  117.     int cHEX = NumberTo7SEG(x);
  118.     int error = 0;
  119.  
  120.     if (cHEX != -1)
  121.         alt_up_parallel_port_write_data(up_dev->uchwytHEX3, cHEX);
  122.     else
  123.     {
  124.         int errHEX0 = SEGE | SEGG;
  125.         int errHEX1 = SEGE | SEGG;
  126.         int errHEX2 = SEGA | SEGF | SEGG | SEGE | SEGD;
  127.         alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (errHEX2 << 16) | (errHEX1 << 8) | errHEX0);
  128.     }
  129. }
  130.  
  131. void printHEX1(int x, struct alt_up_dev *up_dev)
  132. {
  133.     int cHEX = NumberTo7SEG(x);
  134.  
  135.     if (cHEX == -1)
  136.         cHEX = 0;
  137.  
  138.     alt_up_parallel_port_write_data(up_dev->uchwytHEX7, cHEX);
  139. }
  140.  
  141. void printLEDS(int leds, struct alt_up_dev *up_dev)
  142. {
  143.  
  144.     alt_up_parallel_port_write_data(up_dev->uchwytLEDR, leds);
  145. }
  146.  
  147. void printLEDSDelay(int leds, struct alt_up_dev *up_dev)
  148. {
  149.  
  150.     const int optionOnDelay = 1;
  151.     const int optionOffDelay = 2;
  152.     const int errorOnDelay = 3;
  153.     const int errorOffDelay = 2;
  154.  
  155.     int isError = 0;
  156.     int currentAltTicks = 0;
  157.     int currentDelay = 0;
  158.  
  159.     // obecnie zaświecone ledy
  160.     int previousLEDS = alt_up_parallel_port_read_data(up_dev->uchwytLEDR);
  161.  
  162.     if (previousLEDS == leds)
  163.     {
  164.         return;
  165.     }
  166.  
  167.  
  168.     // sprawdzam czy akcja dotyczy errora, czy był zaswiecony lub dopiero ma byc
  169.     if (leds == LED17 || previousLEDS == LED17)
  170.     {
  171.         isError = 1;
  172.     }
  173.  
  174.     // light the LEDS - nowy uklad ledów (liczba wynikająca z sum logicznych ledów) jest wieksza
  175.     //            od poprzedniego => tak - zapalamy, nie - gasimy
  176.     if (leds > previousLEDS)
  177.     {
  178.         if (isError)
  179.         {
  180.             // set delay 3s
  181.             currentDelay = errorOnDelay;
  182.         }
  183.         else
  184.         {
  185.             // set delay 1s
  186.             currentDelay = optionOnDelay;
  187.         }
  188.     }
  189.     // off
  190.     else
  191.     {
  192.         if (isError)
  193.         {
  194.             // set delay 2s
  195.             currentDelay = errorOffDelay;
  196.         }
  197.         else
  198.         {
  199.             // set delay 2s
  200.             currentDelay = optionOffDelay;
  201.         }
  202.     }
  203.  
  204.     currentAltTicks = alt_nticks();
  205.     while (alt_nticks() - currentAltTicks < currentDelay * alt_ticks_per_second()){
  206.         //empty loop - waiting...
  207.     };
  208.  
  209.     alt_up_parallel_port_write_data(up_dev->uchwytLEDR, leds);
  210. }
  211.  
  212. void interrupt_handler(struct alt_up_dev *up_dev, alt_u32 id)
  213. {
  214.     int currentLEDS = 0;
  215.     int currentHEX0 = 0;
  216.     int currentHEX1 = 0;
  217.  
  218.     int currentSW = 0;
  219.     // Nie  należy używać funkcji wykorzystujących przerwania (np. printf).
  220.     // Należy pamiętać o wyzerowaniu źródła przerwania np. clear_edge_capture
  221.  
  222.     currentSW = alt_up_parallel_port_read_data(up_dev->uchwytSW);
  223.  
  224.     int tryb = currentSW & SW0;
  225.  
  226.     option1 = currentSW & SW1 + tryb;
  227.     option2 = currentSW & SW2 + tryb;
  228.     option3 = currentSW & SW3 + tryb;
  229.     option4 = currentSW & SW4 + tryb;
  230.     option5 = currentSW & SW5 + tryb;
  231.     option6 = currentSW & SW6 + tryb;
  232.     option7 = currentSW & SW7 + tryb;
  233.     option8 = currentSW & SW8 + tryb;
  234.  
  235.     if (tryb)
  236.     {
  237.         curretnLEDS = LED0;
  238.         curretnHEX0 = 1;
  239.     }
  240.     else
  241.     {
  242.         curretnLEDS = LED1;
  243.         curretnHEX0 = 2;
  244.     }
  245.  
  246.     switch (currentSW)
  247.     {
  248.         case option1:
  249.             currentLEDS = | LED2;
  250.             currentHEX1 = 1;
  251.             break;
  252.         case option2:
  253.             currentLEDS = | LED3;
  254.             currentHEX1 = 2;
  255.             break;
  256.         case option3:
  257.             currentLEDS = | LED4;
  258.             currentHEX1 = 3;
  259.             break;
  260.         case option4:
  261.             currentLEDS = | LED5;
  262.             currentHEX1 = 4;
  263.             break;
  264.         case option5:
  265.             currentLEDS = | LED5;
  266.             currentHEX1 = 5;
  267.             break;
  268.         case option6:
  269.             currentLEDS = | LED6;
  270.             currentHEX1 = 6;
  271.             break;
  272.         case option7:
  273.             currentLEDS = | LED7;
  274.             currentHEX1 = 7;
  275.             break;
  276.         case option8:
  277.             if (tryb)
  278.             {
  279.                 currentLEDS = | LED8;
  280.                 currentHEX1 = 8;
  281.             }
  282.             else
  283.             {
  284.                 //second mode have only 7 options
  285.                 currentHEX0 = -1;
  286.                 currentHEX1 = -1;
  287.                 currentLEDS = LED17;
  288.             }
  289.             break;
  290.         default:
  291.             currentHEX0 = -1;
  292.             currentHEX1 = -1;
  293.             currentLEDS = LED17;
  294.     }
  295.  
  296.     printHEX0(currentHEX0, up_dev);
  297.     printHEX1(currentHEX1, up_dev);
  298.  
  299.     //printLEDS(currentLEDS, up_dev);
  300.  
  301.     printLEDSDelay(currentLEDS, up_dev)
  302. }
  303.  
  304. int main()
  305. {
  306.     struct alt_up_dev up_dev;
  307.     alt_up_parallel_port_dev *uchwytSW = alt_up_parallel_port_open_dev("/dev/SW_SLIDERS");
  308.     alt_up_parallel_port_dev *uchwytLEDG = alt_up_parallel_port_open_dev("/dev/LEDS_GREEN");
  309.     alt_up_parallel_port_dev *uchwytLEDR = alt_up_parallel_port_open_dev("/dev/LEDS_RED");
  310.     alt_up_parallel_port_dev *uchwytHEX3 = alt_up_parallel_port_open_dev("/dev/HEX_3");
  311.     alt_up_parallel_port_dev *uchwytHEX7 = alt_up_parallel_port_open_dev("/dev/HEX_7");
  312.  
  313.     if (uchwytSW != NULL)
  314.     {
  315.         up_dev.uchwytSW = uchwytSW;
  316.     }
  317.  
  318.     if (uchwytLEDG != NULL)
  319.     {
  320.         up_dev.uchwytLEDG = uchwytLEDG;
  321.     }
  322.     if (uchwytLEDR != NULL)
  323.     {
  324.         up_dev.uchwytLEDR = uchwytLEDR;
  325.     }
  326.     if (uchwytHEX3 != NULL)
  327.     {
  328.         up_dev.uchwytHEX3 = uchwytHEX3;
  329.     }
  330.     if (uchwytHEX7 != NULL)
  331.     {
  332.         up_dev.uchwytHEX7 = uchwytHEX7;
  333.     }
  334.  
  335.     alt_irq_register(1, (void *)&up_dev, (void *)interrupt_handler);
  336.  
  337.     alt_up_parallel_port_set_interrupt_mask(uchwytSW, 0xff);
  338.  
  339.     while (true)
  340.         ;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement