-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
107 lines (99 loc) · 2.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
int direction=0;
int state=0;
ISR(TIM0_COMPA_vect)
{
if((PINB & (1<<PB0)) == 0 && direction==0){
//button pressed , forward mode
switch (state){
case 0:
PORTB= (0<<PB4) | (0<<PB3) | (0<<PB2) | (1<<PB1);
state++;
break;
case 1:
PORTB= (0<<PB4) | (0<<PB3) | (1<<PB2) | (1<<PB1);
state++;
break;
case 2:
PORTB= (0<<PB4) | (0<<PB3) | (1<<PB2) | (0<<PB1);
state++;
break;
case 3:
PORTB= (0<<PB4) | (1<<PB3) | (1<<PB2) | (0<<PB1);
state++;
break;
case 4:
PORTB= (0<<PB4) | (1<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 5:
PORTB= (1<<PB4) | (1<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 6:
PORTB= (1<<PB4) | (0<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 7:
PORTB= (1<<PB4) | (0<<PB3) | (0<<PB2) | (1<<PB1);
state=0;
break;
}
}
else if((PINB & (1<<PB0)) == 0 && direction==1){
//button pressed , reverse mode
switch (state){
case 7:
PORTB= (0<<PB4) | (0<<PB3) | (0<<PB2) | (1<<PB1);
state=0;
break;
case 6:
PORTB= (0<<PB4) | (0<<PB3) | (1<<PB2) | (1<<PB1);
state++;
break;
case 5:
PORTB= (0<<PB4) | (0<<PB3) | (1<<PB2) | (0<<PB1);
state++;
break;
case 4:
PORTB= (0<<PB4) | (1<<PB3) | (1<<PB2) | (0<<PB1);
state++;
break;
case 3:
PORTB= (0<<PB4) | (1<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 2:
PORTB= (1<<PB4) | (1<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 1:
PORTB= (1<<PB4) | (0<<PB3) | (0<<PB2) | (0<<PB1);
state++;
break;
case 0:
PORTB= (1<<PB4) | (0<<PB3) | (0<<PB2) | (1<<PB1);
state++;
break;
}
}
}
int main (void){
//setup io pins
PORTB= (0<<PB4) | (0<<PB3) | (0<<PB2) |(0<<PB1) | (1<<PB0);
DDRB= (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (0<<DDB0);
//setup timer and interrupt
OCR0A|=0xA;
TCCR0A|=(1<<COM0A1) | (0<<COM0A0);
TCCR0B|=(0<<CS02) | (0<<CS01) | (1<<CS00);
TIMSK0|=(1<<OCIE0A);
//check button state at startup for direction.
if((PINB & (1<<PB0)) == 0){
direction=1; // reverse mode
}
sei(); // Enable Global Interrupts
while (1){}
}