grzesiuwow

lab1

Oct 21st, 2019 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <system.h>
  4.  
  5. /* Przełączniki */
  6. #define  SW0 0x00000001
  7. #define  SW1 0x00000002
  8. #define  SW2 0x00000004
  9. #define  SW3 0x00000008
  10. #define  SW4 0x00000010
  11. #define  SW5 0x00000020
  12. #define  SW6 0x00000040
  13. #define  SW7 0x00000080
  14. #define  SW8 0x00000100
  15. #define  SW9 0x00000200
  16. #define  SW10 0x00000400
  17. #define  SW11 0x00000800
  18. #define  SW12 0x00001000
  19. #define  SW13 0x00002000
  20. #define  SW14 0x00004000
  21. #define  SW15 0x00008000
  22. #define  SW16 0x00010000
  23. #define  SW17 0x00020000
  24.  
  25. /* PushButtony */
  26. #define  KEY1 0x00000002
  27. #define  KEY2 0x00000004
  28. #define  KEY3 0x00000008
  29.  
  30. /* Ledy */
  31. #define  LED0 0x00000001
  32. #define  LED1 0x00000002
  33. #define  LED2 0x00000004
  34. #define  LED3 0x00000008
  35. #define  LED4 0x00000010
  36. #define  LED5 0x00000020
  37. #define  LED6 0x00000040
  38. #define  LED7 0x00000080
  39. #define  LED8 0x00000100
  40. #define  LED9 0x00000200
  41. #define  LED10 0x00000400
  42. #define  LED11 0x00000800
  43. #define  LED12 0x00001000
  44. #define  LED13 0x00002000
  45. #define  LED14 0x00004000
  46. #define  LED15 0x00008000
  47. #define  LED16 0x00010000
  48. #define  LED17 0x00020000
  49.  
  50. /* Segmenty HEX */
  51. #define  SEGA 1
  52. #define  SEGB 2
  53. #define  SEGC 4
  54. #define  SEGD 8
  55. #define  SEGE 16
  56. #define  SEGF 32
  57. #define  SEGG 64
  58.  
  59. int main() {
  60.     printf("Hello from Nios II!\n");
  61.  
  62.     /* Litery i cyfry z hex 7 segmentowych */
  63.     int HEX0 = SEGA|SEGB|SEGC|SEGD|SEGE|SEGF;
  64.     int HEX1 = SEGB|SEGC;
  65.     int HEX2 = SEGA|SEGB|SEGG|SEGE|SEGD;
  66.     int HEX3 = SEGA|SEGB|SEGC|SEGD|SEGG;
  67.     int HEX4 = SEGF|SEGG|SEGB|SEGC;
  68.     int HEX5 = SEGA|SEGF|SEGG|SEGC|SEGD;
  69.     int HEX6 = SEGA|SEGF|SEGG|SEGC|SEGD|SEGE;
  70.     int HEX7 = SEGA|SEGB|SEGC;
  71.     int HEX8 = SEGA|SEGB|SEGC|SEGD|SEGE|SEGF|SEGG;
  72.     int HEX9 = SEGA|SEGB|SEGC|SEGD|SEGF|SEGG;
  73.     int HEXE = SEGA|SEGF|SEGG|SEGE|SEGD;
  74.     int HEXr = SEGE|SEGG;
  75.  
  76.     int HEXA = SEGA|SEGB|SEGC|SEGE|SEGF|SEGG;
  77.     int HEXF = SEGA|SEGE|SEGF|SEGG;
  78.  
  79.     int HEXC = SEGA|SEGE|SEGF|SEGD;
  80.     int HEXD = SEGB|SEGC|SEGD|SEGE|SEGG;
  81.     /* Stan do sprawdzenia jaki przycisk jest wciśnięty */
  82.     int state = 0;
  83.  
  84.     /* Pętla nieskończona w której działa program */
  85.     while(1) {
  86.         /* Pobranie panelu switchów */
  87.         int switches = IORD(SW_SLIDERS_BASE,0);
  88.         /* Odczyt który slajder został użyty */
  89.         state = switches & (SW0|SW1|SW2|SW3|SW4|SW5|SW6);
  90.  
  91.         /* Robienie czegoś w zależności jaki slider został użyty */
  92.         switch(state) {
  93.             case 0:
  94.                 /* Reset wszystkich diod i segmentów, żaden slider nie został użyty*/
  95.                 /* !!!UWAGA!!!
  96.                     Jeśli jest jakiś problem z odczytywanie tych nazw LEDS_GREEN itp to używamy adresów typu 0x00000030, które można odczytać w Qsys lub z pliku system.h znajdującego się w bibliotece BSP danego projektu)
  97.                 */
  98.                 IOWR(LEDS_BASE,0,0);
  99.                 IOWR(HEX_BASE,0,0);
  100.                 break;
  101.             case 1:
  102.                 /* Zapala diode na pozycji 0 przy uzyciu slidera na pozycji 0 */
  103.                 IOWR(LEDS_BASE,0,LED0);
  104.  
  105.                 /* Instrukcja segmentów HEX:
  106.                         Pierwszy z lewej: Po prostu nazwa zdefiniowanej cyfry lub liczby HEX__
  107.                         Drugi od lewej: (HEX__ << 8)
  108.                         Trzeci z lewej: (HEX__ << 16)
  109.                         Czwarty z lewej: (HEX__ << 24)
  110.                     Na przykład:
  111.                     IOWR(HEX_BASE, (HEX3 << 24) | (HEX2 <<16) | (HEX1 <<8) |HEX0);
  112.                     Wyświetli: |0|1|2|3|
  113.                 */
  114.                 IOWR(HEX_BASE,0,HEX1);
  115.  
  116.                 int subSwitches = IORD(SW_SLIDERS_BASE,0);
  117.                 if((subSwitches & SW7) && (subSwitches & SW8)) {
  118.                     IOWR(LEDS_BASE,0,LED17);
  119.                     IOWR(HEX_BASE,0,((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  120.                 } else if(subSwitches & SW7) {
  121.                     IOWR(LEDS_BASE,0,LED7);
  122.                     IOWR(HEX_BASE,0,(HEXA<<8));
  123.                 } else if(subSwitches & SW8) {
  124.                     IOWR(LEDS_BASE,0,LED8);
  125.                     IOWR(HEX_BASE,0,(HEXF<<8));
  126.                 }
  127.  
  128.                 break;
  129.             case 2:
  130.                 IOWR(LEDS_BASE,0,LED1);
  131.                 IOWR(HEX_BASE,0,HEX2);
  132.                 break;
  133.             case 4:
  134.                 IOWR(LEDS_BASE,0,LED2);
  135.                 IOWR(HEX_BASE,0,HEX3);
  136.                 break;
  137.             case 8:
  138.                 IOWR(LEDS_BASE,0,LED3);
  139.                 IOWR(HEX_BASE,0,HEX4);
  140.  
  141.                 int subSubSwitches = IORD(SW_SLIDERS_BASE,0);
  142.                 if(((subSubSwitches & SW9) && (subSubSwitches & SW10)) ||
  143.                         ((subSubSwitches & SW9) && (subSubSwitches & SW11)) ||
  144.                         ((subSubSwitches & SW9) && (subSubSwitches & SW12)) ||
  145.                         ((subSubSwitches & SW10) && (subSubSwitches & SW11)) ||
  146.                         ((subSubSwitches & SW10) && (subSubSwitches & SW12)) ||
  147.                         ((subSubSwitches & SW11) && (subSubSwitches & SW12))) {
  148.                     IOWR(LEDS_RED_BASE,0,LED17);
  149.                     IOWR(HEX_3_BASE,0,((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  150.                 } else if(subSubSwitches & SW9) {
  151.                     IOWR(LEDS_BASE,0,LED9);
  152.                     IOWR(HEX_BASE,0,((HEXC<<16)| (HEX1 <<8)));
  153.                 } else if(subSubSwitches & SW10) {
  154.                     IOWR(LEDS_BASE,0,LED10);
  155.                     IOWR(HEX_BASE,0,((HEXC<<16)| (HEX2 <<8)));
  156.                 } else if(subSubSwitches & SW11) {
  157.                     IOWR(LEDS_BASE,0,LED11);
  158.                     IOWR(HEX_BASE,0,((HEXD<<16)| (HEX1 <<8)));
  159.                 } else if(subSubSwitches & SW12) {
  160.                     IOWR(LEDS_BASE,0,LED12);
  161.                     IOWR(HEX_BASE,0,((HEXD<<16)| (HEX2 <<8)));
  162.                 }
  163.                 break;
  164.             case 16:
  165.                 IOWR(LEDS_BASE,0,LED4);
  166.                 IOWR(HEX_BASE,0,HEX5);
  167.                 break;
  168.             case 32:
  169.                 IOWR(LEDS_BASE,0,LED5);
  170.                 IOWR(HEX_BASE,0,HEX6);
  171.                 break;
  172.             case 64:
  173.                 IOWR(LEDS_BASE,0,LED6);
  174.                 IOWR(HEX_BASE,0,HEX7);
  175.                 break;
  176.             default:
  177.                 /* Reset diod niebieskich, zapalenie ostatniej diody czerwonaj i wyświetlenie na HEX "Err"*/
  178.                 IOWR(HEX_BASE,0,((HEXE<<16) | (HEXr<<8) | HEXr));
  179.                 IOWR(LEDS_BASE,0,0);
  180.                 IOWR(LEDS_BASE,0,LED17);
  181.                 break;
  182.         }
  183.     }
  184.  
  185.     return 0;
  186. }
Add Comment
Please, Sign In to add comment