Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- typedef enum {A,B,C,D, screen} states_t;
- states_t stato = A;
- volatile uint8_t portbhistory = 0xFF;
- uint16_t tasto0 =0;
- uint16_t tasto1 =0;
- uint16_t tasto2 =0;
- uint16_t tasto3 =0;
- uint16_t arrivo[4]={9,9,9,9};
- uint16_t indice=0;
- uint16_t i=0;
- int main (void) {
- DDRD &= ~((1 << DDD4) | (1 << DDD5) | (1 << DDD6)|(1 << DDD2)|(1 << DDD3));
- DDRD &= ~(1 << DDD2);
- DDRD &= ~(1 << DDD3);
- PORTD=0xff;
- DDRC=0xff;
- PCICR |= (1 << PCIE2);
- PCMSK2 |= (1 << PCINT18) | (1 << PCINT19) | (1 << PCINT20)| (1 << PCINT21)| (1 << PCINT22) |(1<<PCINT23);
- TCCR1B |= (1 << WGM12); // Set the Timer Mode to CTC (Mode 4)
- // Set the value that you want to count to // [ (clock_speed / Prescaler_value) * Desired_interval_in_Seconds ] - 1 // [ (16000000 / 1024) * 0.2 ] -1 = 19953
- OCR1A = 1953;
- // Set interrupt on compare match
- TCCR1B |= (1 << CS12) | (1 << CS10); // starts the timer, set prescaler to 1024
- TIMSK1 |= (1 << OCIE1A);
- sei();
- while (1) {
- switch (stato) {
- case A : PORTC=0;
- tasto0=0;
- tasto1=0;
- tasto2=0;
- tasto3=0;
- arrivo[0]=9;
- arrivo[1]=9;
- arrivo[2]=9;
- arrivo[3]=9;
- indice=0;
- break;
- case B:
- PORTC|=(1<<PORTC4);
- if(tasto0==1){ PORTC|=(1<<PORTC0); if (arrivo[0]!=0 & arrivo[1]!=0 & arrivo[2]!=0 & arrivo[3]!=0)
- {arrivo[indice]=0; indice++;
- }
- }
- if(tasto1==1){ PORTC|=(1<<PORTC1); if (arrivo[0]!=1 & arrivo[1]!=1 & arrivo[2]!=1 & arrivo[3]!=1)
- {arrivo[indice]=1; indice++;
- }}
- if(tasto2==1){ PORTC|=(1<<PORTC2); if (arrivo[0]!=2 & arrivo[1]!=2 & arrivo[2]!=2 & arrivo[3]!=2)
- {arrivo[indice]=2; indice++;
- }}
- if(tasto3==1){ PORTC|=(1<<PORTC3); if (arrivo[0]!=3 & arrivo[1]!=3 & arrivo[2]!=3 & arrivo[3]!=3)
- {arrivo[indice]=3; indice++;
- } }
- if (tasto0==1 & tasto1==1 & tasto2==1 & tasto3==1)
- {
- stato=C;
- }
- if(i==80){ stato=A;}
- break;
- case C: PORTC=0Xf;
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC |=(1<<arrivo[1]);
- PORTC &= ~(1<<arrivo[2]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC |=(1<<arrivo[1]);
- PORTC |=(1<<arrivo[2]);
- PORTC &= ~(1<<arrivo[3]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC |=(1<<arrivo[1]);
- PORTC &= ~(1<<arrivo[2]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC &= ~(1<<arrivo[1]);
- i=0;
- while(i!=1){}
- PORTC &= ~(1<<arrivo[0]);
- i=0;
- while(i!=1){}
- PORTC |=(1<<arrivo[0]);
- PORTC |=(1<<arrivo[1]);
- PORTC |=(1<<arrivo[2]);
- PORTC |=(1<<arrivo[3]);
- break;
- case D:
- break;
- }
- }
- }
- ISR(TIMER1_COMPA_vect)
- {i++;
- }
- ISR(PCINT2_vect)
- {
- uint8_t changedbits;
- changedbits = PIND ^ portbhistory;
- portbhistory = PIND;
- if(changedbits & (1 << PIND2))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND2)) == 1 )
- { /* LOW to HIGH pin change */ }
- else {tasto0=1;}
- }
- if(changedbits & (1 << PIND3))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND3)) == 1 )
- { /* LOW to HIGH pin change */ }
- else{tasto1=1;}
- }
- if(changedbits & (1 << PIND5))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND5)) == 1 )
- { /* LOW to HIGH pin change */ }
- else
- { tasto3=1; }
- }
- if(changedbits & (1 << PIND4))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND4)) == 1 )
- { /* LOW to HIGH pin change */ }
- else
- { tasto2=1; }
- }
- if(changedbits & (1 << PIND6))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND6)) == 1 )
- { /* LOW to HIGH pin change */ }
- else
- { stato=B; i=0;}
- }
- if(changedbits & (1 << PIND7))
- {
- /* PCINT0 changed */
- if( (PIND & (1 << PIND7)) == 1 )
- { /* LOW to HIGH pin change */ }
- else
- { stato=A; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement