Advertisement
Guest User

REDY LAB2

a guest
Oct 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.10 KB | None | 0 0
  1. /*
  2.  * "Hello World" example.
  3.  *
  4.  * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
  5.  * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
  6.  * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
  7.  * device in your system's hardware.
  8.  * The memory footprint of this hosted application is ~69 kbytes by default
  9.  * using the standard reference design.
  10.  *
  11.  * For a reduced footprint version of this template, and an explanation of how
  12.  * to reduce the memory footprint for a given application, see the
  13.  * "small_hello_world" template.
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <io.h>
  19. #include <system.h>
  20.  
  21.  #define  SEGA 0x00001
  22.  #define  SEGB 0x00002
  23.  #define  SEGC 0x00004
  24.  #define  SEGD 0x00008
  25.  #define  SEGE 0x00010
  26.  #define  SEGF 0x00020
  27.  #define  SEGG 0x00040
  28.  
  29. int main()
  30. {
  31.   printf("Hello from Nios II!\n");
  32.  
  33.     int sw = 0; // stan sliderow
  34.     int S[5] = {0}; // pokoje licznik osob
  35.     int Sb[5] = {0}; // pokoje stan swiatla 1-zaplone 0-zgaszone
  36.     int light = 0; // przechowuje stan ledow
  37.     short state = 0; // przechowuje pushbuttony PB nr.4 stan 1- z mniej do wiekszego pokoju (wchodzenie) 0- z wieksz do mniej(wychodzenie)
  38.     //int CD[10] = {0}; // wejscia
  39.     int swFlag = 0; // flaga sprawdza, czy switch jest wlaczony czy nie
  40.     int errFlag = 0; // sprawdza czy jest wiecej niz 1 slider wlacozny
  41.     int errLED = 0;
  42.     int HEXDsp = 0; // przechowuje HEX Display
  43.     int osoby[5] = {0}; // tablica przechowuje il.osob -> HEDDisplay
  44.     int i;  // index
  45.  
  46.     while(1){
  47.         if(state == IORD(PUSHBUTTON_BASE, 0)){
  48.             state = 0;  // wychodzenie
  49.         }
  50.         else
  51.             state = 1;  // wchodzenie
  52.  
  53.  
  54.         sw = IORD(SW_SLIDERS_BASE, 0);
  55.  
  56.         if(swFlag == 0){
  57.             switch (sw) {
  58.                 case 0:
  59.                     break;
  60.                 case 1:
  61.                      printf("Drzwi 1\n");
  62.                      if(state)
  63.                          S[4]++;
  64.                      else if(S[4]<1){
  65.                          errFlag = 1;
  66.                          printf("Error Drzwi 1\n");
  67.                      }
  68.                      else
  69.                          S[4]--;
  70.                      swFlag = 1;
  71.                      break;
  72.                 case 2:
  73.                      printf("Drzwi 2\n");
  74.                      if(state)
  75.                          S[0]++;
  76.                      else if(S[0] < 1){
  77.                          errFlag = 1;
  78.                          printf("Error Drzwi 2\n");
  79.                      }
  80.                      else
  81.                          S[0]--;
  82.                      swFlag = 1;
  83.                      break;
  84.                 case 4:
  85.                      printf("Drzwi 3\n");
  86.                      if(state)
  87.                          S[2]++;
  88.                      else if(S[2] < 1){
  89.                          errFlag = 1;
  90.                          printf("Error Drzwi 3\n");
  91.                      }
  92.                      else
  93.                          S[2]--;
  94.                      swFlag = 1;
  95.                      break;
  96.                 case 8:
  97.                      printf("Drzwi 4\n");
  98.                      if(state){ // s0 -> s2
  99.                          if(S[0] < 1){
  100.                             errFlag = 1;
  101.                             printf("Error Drzwi 4\n");
  102.                          }
  103.                          else{
  104.                              S[0]--;
  105.                              S[2]++;
  106.                          }
  107.                      }
  108.                      else{  //s2 -> s0
  109.                          if(S[2] < 1){
  110.                             errFlag = 1;
  111.                             printf("Error Drzwi 4\n");
  112.                          }
  113.                          else{
  114.                              S[2]--;
  115.                              S[0]++;
  116.                          }
  117.                      }
  118.                      swFlag = 1;
  119.                      break;
  120.                 case 16:
  121.                      printf("Drzwi 5\n");
  122.                      if(state){ // s0 -> s4
  123.                          if(S[0] < 1){
  124.                             errFlag = 1;
  125.                             printf("Error Drzwi 5\n");
  126.                          }
  127.                          else{
  128.                              S[0]--;
  129.                              S[4]++;
  130.                          }
  131.                      }
  132.                      else{  //s4 -> s0
  133.                          if(S[4] < 1){
  134.                             errFlag = 1;
  135.                             printf("Error Drzwi 5\n");
  136.                          }
  137.                          else{
  138.                              S[4]--;
  139.                              S[0]++;
  140.                          }
  141.                      }
  142.                      swFlag = 1;
  143.                      break;
  144.                 case 32:
  145.                      printf("Drzwi 6\n");
  146.                      if(state){ // s3 -> s4
  147.                          if(S[3] < 1){
  148.                             errFlag = 1;
  149.                             printf("Error Drzwi 6\n");
  150.                          }
  151.                          else{
  152.                              S[3]--;
  153.                              S[4]++;
  154.                          }
  155.                      }
  156.                      else{  //s4 -> s3
  157.                          if(S[4] < 1){
  158.                             errFlag = 1;
  159.                             printf("Error Drzwi 6\n");
  160.                          }
  161.                          else{
  162.                              S[4]--;
  163.                              S[3]++;
  164.                          }
  165.                      }
  166.                      swFlag = 1;
  167.                      break;
  168.                 case 64:
  169.                      printf("Drzwi 7\n");
  170.                      if(state)
  171.                          S[3]++;
  172.                      else if(S[3] < 1){
  173.                          errFlag = 1;
  174.                          printf("Error Drzwi 7\n");
  175.                      }
  176.                      else
  177.                          S[3]--;
  178.                      swFlag = 1;
  179.                      break;
  180.                 case 128:
  181.                      printf("Drzwi 8\n");
  182.                      if(state)
  183.                          S[7]++;
  184.                      else if(S[7] < 1){
  185.                          errFlag = 1;
  186.                          printf("Error Drzwi 8\n");
  187.                      }
  188.                      else
  189.                          S[7]--;
  190.                      swFlag = 1;
  191.                      break;
  192.                 case 256:
  193.                      printf("Drzwi 9\n");
  194.                      if(state)
  195.                          S[8]++;
  196.                      else if(S[8] < 1){
  197.                          errFlag = 1;
  198.                          printf("Error Drzwi 9\n");
  199.                      }
  200.                      else
  201.                          S[8]--;
  202.                      swFlag = 1;
  203.                      break;
  204.                 case 512:
  205.                      printf("Drzwi 10\n");
  206.                      if(state){ // s1-> s3
  207.                          if(S[1] < 1){
  208.                             errFlag = 1;
  209.                             printf("Error Drzwi 6\n");
  210.                          }
  211.                          else{
  212.                              S[1]--;
  213.                              S[3]++;
  214.                          }
  215.                      }
  216.                      else{  //s3 -> s1
  217.                          if(S[3] < 1){
  218.                             errFlag = 1;
  219.                             printf("Error Drzwi 6\n");
  220.                          }
  221.                          else{
  222.                              S[3]--;
  223.                              S[1]++;
  224.                          }
  225.                      }
  226.                      swFlag = 1;
  227.                      break;
  228.  
  229.                 default:
  230.                     printf("Error 2 slidery wlaczone");
  231.                     errFlag = 1;
  232.                     swFlag = 1;
  233.                     break;
  234.             }
  235.         }
  236.         else if(sw == 0){   // slidery sa na OFF
  237.             swFlag = 0;
  238.             if(errFlag){
  239.                 errFlag = 0;
  240.                 //S[] = 0 // dopsiac petle for
  241.             }
  242.         }
  243.  
  244. //----------- ilosc osob na swiatlo
  245.             if(S[0] > 0)
  246.                 Sb[0] = 1;
  247.             else
  248.                 Sb[0] = 0;
  249.             if(S[1] > 0)
  250.                 Sb[1] = 2;
  251.             else
  252.                 Sb[1] = 0;
  253.             if(S[2] > 0)
  254.                 Sb[2] = 4;
  255.             else
  256.                 Sb[2] = 0;
  257.             if(S[3] > 0)
  258.                 Sb[3] = 8;
  259.             else
  260.                 Sb[3] = 0;
  261.             if(S[4] > 0)
  262.                 Sb[4] = 16;
  263.             else
  264.                 Sb[4] = 0;
  265.             if(errFlag)
  266.                 errLED = 0b1000000000;  //512?
  267.             else
  268.                 errLED = 0;
  269.  
  270. //------------------- zapalanie LEDow
  271.             light = errLED + Sb[0] + Sb[1] + Sb[2] + Sb[3] +Sb[4];
  272.             //printf("%d\n", light);
  273.             IOWR(LEDS_BASE,0,light);
  274.  
  275.          }
  276.  
  277. //------------------- HEXY
  278.     if(errFlag = 0){
  279.         for(i=0; i< 5; i++){
  280.             switch(S[i]){
  281.             case 0:
  282.                 osoby[i] = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF;
  283.                 break;
  284.             case 1:
  285.                 osoby[i] = SEGB | SEGC;
  286.                 break;
  287.             case 2:
  288.                 osoby[i] = SEGA | SEGB | SEGD | SEGE | SEGG;
  289.                 break;
  290.             case 3:
  291.                 osoby[i] = SEGA | SEGB | SEGC | SEGD | SEGG;
  292.                 break;
  293.             case 4:
  294.                 osoby[i] = SEGB | SEGC | SEGF | SEGG;
  295.                 break;
  296.             case 5:
  297.                 osoby[i] = SEGA | SEGF | SEGG | SEGC | SEGD;
  298.                 break;
  299.             case 6:
  300.                 osoby[i] = SEGA | SEGC | SEGD | SEGE | SEGF | SEGG;
  301.                 break;
  302.             case 7:
  303.                 osoby[i] = SEGA | SEGB | SEGC;
  304.                 break;
  305.             case 8:
  306.                 osoby[i] = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG;
  307.                 break;
  308.             case 9:
  309.                 osoby[i] = SEGA | SEGB | SEGC | SEGD | SEGF | SEGG;
  310.                 break;
  311.             default:
  312.                 osoby[i] = SEGG;    // jezeli >9 to wyswietla -
  313.                 break;
  314.             }
  315.         }
  316.     }
  317.     else{   // ERROR
  318.         osoby[4] = SEGA | SEGD | SEGE | SEGF | SEGG;    // E
  319.         osoby[3] = SEGE | SEGG; // r
  320.         osoby[2] = SEGE | SEGG; // r
  321.         osoby[1] = SEGC | SEGD | SEGE | SEGG; // o
  322.         osoby[0] = SEGE | SEGG; // r
  323.     }
  324.  
  325.     HEXDsp = osoby[0] | (osoby[1] << 7) | (osoby[2] << 14) | (osoby[3] << 21) | (osoby[4] << 28);
  326.  
  327.  
  328.     IOWR(HEX_BASE,0,HEXDsp);
  329.     return 0;
  330.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement