Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- // PORT DEFINES
- #define LED_Direction DDRC
- #define LED_PORT PORTC
- #define DIGIT_Direction DDRB
- #define DIGIT_PORT PORTB
- #define BUTTON_Direction DDRA
- #define BUTTON_Vcc PORTA
- #define BUTTON_PORT PINA
- #define STATE_NONE_PRESSED 0x00
- #define STATE_SOME_PRESSED 0xff
- int main(void){ // setting ports as inputs and output
- LED_Direction |= 0xff;
- LED_PORT = 0xff;
- DIGIT_Direction |= 0xff;
- DIGIT_PORT = 0xff;
- BUTTON_Direction |= 0x00;
- BUTTON_Vcc = 0xff;
- BUTTON_PORT = 0xff;
- char symbols[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; // 0-9 number to 7segment display translation table
- char yes[] = {0x91, 0x86,0x92,0xff}; // 'yes' in 7seg
- char no[] = {0xC8, 0xC0,0xff,0xff}; // 'no' in 7seg
- char pin[]={0,0,0,0}; // output values: 'yes', 'no', 'no vote', 'invalid vote (yes and no)'
- char votes[7] = {}; //votes of every button (1=yes, 2=no, 3=invalid, 0=no-vote)
- char buttons = 0x00;
- char button_state = STATE_NONE_PRESSED;
- char key[8];
- for(int i=0; i<8; i++){
- key[i] = ~(1<<i);
- }
- int state = 0;
- int allow = 0;
- int yess= 0;
- int nos = 0;
- while(1) { //output clear
- pin[0] = 0;
- pin[1] = 0;
- pin[2] = 0;
- pin[3] = 0;
- for(int j = 6; j >= 0 ; j--){ //count votes
- if(votes[j] == 0){
- pin[2]++;
- }
- if(votes[j] == 1){
- pin[0]++;
- }
- if(votes[j] == 2){
- pin[1]++;
- }
- if(votes[j] == 3){
- pin[3]++;
- }
- } //output in 7segment
- if(state < 2){ //if voting in pending
- for(int i=0;i<4;i++){ //for every digit
- if(state == 0){ //state 0 = voting for yes
- if(i < 3){
- LED_PORT = yes[i];
- }else{
- LED_PORT = symbols[yess];
- }
- }
- if(state == 1){ //state 1 = voting for no
- if(i < 3){
- LED_PORT = no[i];
- }else{
- LED_PORT = symbols[nos];
- }
- }
- DIGIT_PORT = ~(0x01 << i); //shift 1bit to output on another digit
- _delay_ms(4);
- }
- }else{
- for(int i=0;i<4;i++){ //if state 2 = end of voting
- if(state == 2){
- LED_PORT = symbols[pin[i]];
- }
- DIGIT_PORT = ~(0x01 << i);
- _delay_ms(4);
- }
- } //voting
- buttons = BUTTON_PORT;
- if(button_state == STATE_NONE_PRESSED){
- if(buttons != 0xff){
- button_state = STATE_SOME_PRESSED;
- if(state < 2){
- for(int i = 6; i >= 0 ; i--){
- if(state){ // if state != 0 (voting for no)
- if(buttons == key[i]){ //if key 1-6 pressed -> vote
- if(votes[i] > 1){
- votes[i] -= 2;
- nos--;
- } else {
- votes[i] += 2;
- nos++;
- }
- allow = 1;
- }
- if(buttons == key[7] && allow == 1){ //if key 7 pressed -> change state
- state = 2;
- }
- }else{ //if state == 0 (voting for yes)
- if(buttons == key[i]){ //if key 1-6 pressed -> vote
- if(votes[i] == 1){
- votes[i] = 0;
- yess--;
- } else{
- votes[i] = 1;
- yess++;
- }
- }
- if(buttons == key[7]){ //if key 7 pressed -> change state
- state = 1;
- }
- }
- }
- }
- }
- }
- if(button_state == STATE_SOME_PRESSED){ // Wait for buttons to stop being pressed
- if(buttons == 0xff){
- button_state = STATE_NONE_PRESSED;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement