Debian Codegraph @

microhope/microhope-doc/prog/motor.c

raw
1 /*
2 Stepper motor coils are connected to the collectors of 4 transistors
3 whose bases are connected to PA0 to PA3.
4 */
5
6 #include <avr/io.h>
7
8
9 void delay (uint16_t k) // Delay is (k * 2) usec
10 {
11 volatile uint16_t x = k;
12 while (x) --x;
13 }
14
15
16 void rotateMotor (int nsteps, int dir)
17 {
18 static uint8_t pos = 0, seq[4] = { 12, 6, 3, 9 };
19 int i;
20
21 for (i = 0; i < nsteps; ++i)
22 {
23 if (dir)
24 if (pos == 3)
25 pos = 0;
26 else
27 ++pos;
28 else if (pos == 0)
29 pos = 3;
30 else
31 --pos;
32
33 PORTA = seq[pos];
34 delay (3000);
35 }
36 }
37
38 int main (void)
39 {
40 DDRA = 15; // Data Direction Register for PORT A
41
42 for(;;)
43 {
44 rotateMotor(100,0);
45 rotateMotor(100,1);
46 }
47
48 return 0;
49 }