Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #define F_CPU 16000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. // PORT DEFINES
  5. #define LED_Direction DDRC     
  6. #define LED_PORT PORTC         
  7. #define DIGIT_Direction DDRB  
  8. #define DIGIT_PORT PORTB       
  9. #define BUTTON_Direction DDRA  
  10. #define BUTTON_Vcc PORTA
  11. #define BUTTON_PORT PINA       
  12. #define STATE_NONE_PRESSED 0x00
  13. #define STATE_SOME_PRESSED 0xff
  14. int main(void){ // setting ports as inputs and output
  15.     LED_Direction |= 0xff;     
  16.     LED_PORT = 0xff;
  17.     DIGIT_Direction |= 0xff;
  18.     DIGIT_PORT = 0xff;
  19.     BUTTON_Direction |= 0x00;  
  20.     BUTTON_Vcc = 0xff;
  21.     BUTTON_PORT = 0xff;
  22.  
  23.     char symbols[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; // 0-9 number to 7segment display translation table
  24.     char yes[] = {0x91, 0x86,0x92,0xff}; // 'yes' in 7seg
  25.     char no[] = {0xC8, 0xC0,0xff,0xff}; // 'no' in 7seg
  26.     char pin[]={0,0,0,0}; // output values: 'yes', 'no', 'no vote', 'invalid vote (yes and no)'
  27.     char votes[7] = {}; //votes of every button (1=yes, 2=no, 3=invalid, 0=no-vote)
  28.     char buttons = 0x00;
  29.     char button_state = STATE_NONE_PRESSED;
  30.     char key[8];
  31.    
  32.     for(int i=0; i<8; i++){
  33.         key[i] = ~(1<<i);
  34.     }
  35.     int state = 0;
  36.     int allow = 0;
  37.     int yess= 0;
  38.     int nos = 0;
  39.     while(1) { //output clear
  40.         pin[0] = 0;
  41.         pin[1] = 0;
  42.         pin[2] = 0;
  43.         pin[3] = 0;
  44.         for(int j = 6; j >= 0 ; j--){  //count votes
  45.             if(votes[j] == 0){
  46.                 pin[2]++;
  47.             }
  48.             if(votes[j] == 1){
  49.                 pin[0]++;
  50.             }
  51.             if(votes[j] == 2){
  52.                 pin[1]++;
  53.             }
  54.             if(votes[j] == 3){
  55.                 pin[3]++;
  56.             }
  57.         } //output in 7segment
  58.         if(state < 2){ //if voting in pending
  59.             for(int i=0;i<4;i++){  //for every digit
  60.                 if(state == 0){ //state 0 = voting for yes
  61.                     if(i < 3){
  62.                     LED_PORT = yes[i];
  63.                     }else{
  64.                     LED_PORT = symbols[yess];
  65.                     }
  66.                 }
  67.                 if(state == 1){ //state 1 = voting for no
  68.                     if(i < 3){
  69.                         LED_PORT = no[i];
  70.                         }else{
  71.                         LED_PORT = symbols[nos];
  72.                     }
  73.                 }
  74.                 DIGIT_PORT = ~(0x01 << i); //shift 1bit to output on another digit
  75.                 _delay_ms(4);
  76.             }
  77.         }else{
  78.             for(int i=0;i<4;i++){ //if state 2 = end of voting
  79.                 if(state == 2){
  80.                     LED_PORT = symbols[pin[i]];
  81.                 }
  82.                     DIGIT_PORT = ~(0x01 << i);
  83.                     _delay_ms(4);
  84.                 }
  85.         }  //voting
  86.         buttons = BUTTON_PORT;
  87.         if(button_state == STATE_NONE_PRESSED){
  88.             if(buttons != 0xff){
  89.                 button_state = STATE_SOME_PRESSED;
  90.                 if(state < 2){
  91.                     for(int i = 6; i >= 0 ; i--){
  92.                         if(state){ // if state != 0  (voting for no)
  93.                             if(buttons == key[i]){  //if key 1-6 pressed -> vote
  94.                                 if(votes[i] > 1){
  95.                                     votes[i] -= 2;
  96.                                     nos--;
  97.                                 } else {
  98.                                     votes[i] += 2;
  99.                                     nos++;
  100.                                 }
  101.                                 allow = 1;
  102.                             }
  103.                             if(buttons == key[7] && allow == 1){ //if key 7 pressed -> change state
  104.                                 state = 2;
  105.                             }
  106.                         }else{ //if state == 0 (voting for yes)
  107.                             if(buttons == key[i]){  //if key 1-6 pressed -> vote
  108.                                 if(votes[i] == 1){
  109.                                     votes[i] = 0;
  110.                                     yess--;
  111.                                 } else{
  112.                                     votes[i] = 1;
  113.                                     yess++;
  114.                                 }
  115.                             }
  116.                             if(buttons == key[7]){ //if key 7 pressed -> change state
  117.                                 state = 1;
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         if(button_state == STATE_SOME_PRESSED){ // Wait for buttons to stop being pressed
  125.             if(buttons == 0xff){
  126.                 button_state = STATE_NONE_PRESSED;
  127.             }
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement