Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/interrupt.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #define RED1 PORTA0
- #define YEL1 PORTA1
- #define GRN1 PORTA3
- #define RED2 PORTA4
- #define YEL2 PORTA5
- #define GRN2 PORTA6
- #define SW1 PINA7
- #define SW2 PINB0
- int main(void) {
- DDRA = ((1<<DDA3) | (1<<DDA6) | (1<<DDA4) | (1<<DDA0) | (1<<DDA1) | (1<<DDA5)); // Set up Port A pin 3 and 6 mode to output (green LEDs)
- DDRA &= ~(1<<SW1); // Set input direction for push button 1
- DDRB &= ~(1<<SW2); // Set input direction for push button 2
- PUEA |= (1<<SW1); // Set internal pullup for push button 1
- PUEB |= (1<<SW2); // Set internal pullup for push button 2
- PORTA |= 1<<SW1; // Set input high for push button 1 (probably not needed)
- PORTB |= 1<<SW2; // Set input high for push button 2 (probably not needed)
- PORTA &= ~((1<<GRN1) | (1<<GRN2));
- volatile int counter_state = 0;
- PORTA &= ~((1<<GRN1) | (1<<GRN2) | (1<<YEL1) | (1<<YEL2) | (1<<RED1) | (1<<RED2));
- while(1) {
- if(!(PINA & (1<<SW1))) {
- counter_state = (counter_state + 1) % 64;
- }
- if(!(PINB & (1<<SW2))) {
- counter_state = counter_state == 0 ? 63 : counter_state - 1;
- }
- if(counter_state & 1) {
- PORTA |= (1<<RED2);
- } else {
- PORTA &= ~(1<<RED2);
- }
- if(counter_state & 2) {
- PORTA |= (1<<YEL2);
- } else {
- PORTA &= ~(1<<YEL2);
- }
- if(counter_state & 4) {
- PORTA |= (1<<GRN2);
- } else {
- PORTA &= ~(1<<GRN2);
- }
- if(counter_state & 8) {
- PORTA |= (1<<RED1);
- } else {
- PORTA &= ~(1<<RED1);
- }
- if(counter_state & 16) {
- PORTA |= (1<<YEL1);
- } else {
- PORTA &= ~(1<<YEL1);
- }
- if(counter_state & 32) {
- PORTA |= (1<<GRN1);
- } else {
- PORTA &= ~(1<<GRN1);
- }
- _delay_ms(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement