root / trunk / code / projects / colonet / utilities / robot_slave / motor.c @ 13
History | View | Annotate | Download (1.79 KB)
1 | 13 | emarinel | /*
|
---|---|---|---|
2 | motor.c - Contains functions necessary for activating and driving the
|
||
3 | H-bridge
|
||
4 | |||
5 | |||
6 | author: Robotics Club, Colony project
|
||
7 | |||
8 | much of this is taken from FWR's library, author: Tom Lauwers
|
||
9 | |||
10 | */
|
||
11 | |||
12 | #include "motor.h" |
||
13 | |||
14 | /*
|
||
15 | motor initialization
|
||
16 | initializes both motors
|
||
17 | |||
18 | */
|
||
19 | void motors_init( void ) { |
||
20 | |||
21 | |||
22 | // Configure counter such that we use phase correct
|
||
23 | // PWM with 8-bit resolution
|
||
24 | PORTA &= 0x0F;
|
||
25 | DDRA |= 0xF0;
|
||
26 | DDRB |= 0x60;
|
||
27 | |||
28 | //timer 1A and 1B
|
||
29 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10); |
||
30 | TCCR1B = _BV(WGM12) | _BV(CS10); |
||
31 | // TCCR1A = 0xA1;
|
||
32 | // TCCR1B = 0x04;
|
||
33 | OCR1AH=0;
|
||
34 | OCR1AL=0;
|
||
35 | OCR1BH=0;
|
||
36 | OCR1BL=0;
|
||
37 | } |
||
38 | |||
39 | // The following functions set the motor direction and speed
|
||
40 | void motor1_set(int direction, int speed) { |
||
41 | |||
42 | if(direction == 0) { |
||
43 | // turn off PWM first if switching directions
|
||
44 | if((PORTA & 0x30) != 0x10) |
||
45 | { |
||
46 | OCR1A = 0;
|
||
47 | } |
||
48 | PORTA = (PORTA & 0xCF) | 0x10; |
||
49 | // PORTD |= 0x10;
|
||
50 | // PORTD &= 0xBF;
|
||
51 | } |
||
52 | else {
|
||
53 | // turn off PWM first if switching directions
|
||
54 | if((PORTA & 0x30) != 0x20) |
||
55 | { |
||
56 | OCR1A = 0;
|
||
57 | } |
||
58 | PORTA = (PORTA & 0xCF) | 0x20; |
||
59 | // PORTD |= 0x40;
|
||
60 | // PORTD &= 0xEF;
|
||
61 | } |
||
62 | |||
63 | // Set the timer to count up to speed, an 8-bit value
|
||
64 | OCR1AL = speed; |
||
65 | |||
66 | |||
67 | } |
||
68 | |||
69 | void motor2_set(int direction, int speed) { |
||
70 | |||
71 | if(direction == 0) { |
||
72 | // PORTD |= 0x20;
|
||
73 | // PORTD &= 0x7F;
|
||
74 | // turn off PWM first if switching directions
|
||
75 | if((PORTA & 0xC0) != 0x80) |
||
76 | { |
||
77 | OCR1B = 0;
|
||
78 | } |
||
79 | |||
80 | PORTA = (PORTA & 0x3F) | 0x80; |
||
81 | } |
||
82 | else {
|
||
83 | // PORTD |= 0x80;
|
||
84 | // PORTD &= 0xDF;
|
||
85 | |||
86 | // turn off PWM first if switching directions
|
||
87 | if((PORTA & 0xC0) != 0x40) |
||
88 | { |
||
89 | OCR1B = 0;
|
||
90 | } |
||
91 | |||
92 | PORTA = (PORTA & 0x3F) | 0x40; |
||
93 | } |
||
94 | OCR1BL = speed; |
||
95 | |||
96 | } |
||
97 | |||
98 | // Just turns off both motors
|
||
99 | void motors_off( void ) { |
||
100 | OCR1AL = 0x0;
|
||
101 | OCR1BL = 0x0;
|
||
102 | } |