Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / utilities / robot_wireless_relay / motor.c @ 13

History | View | Annotate | Download (1.77 KB)

1
/*
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
#ifndef FFPP
22
        // Configure counter such that we use phase correct
23
        // PWM with 8-bit resolution
24
        DDRB |= 0x60;
25

    
26
        //timer 1A and 1B
27
        TCCR1A = 0xA1;
28
        TCCR1B = 0x04;
29
        OCR1AH=0;
30
        OCR1AL=0;
31
        OCR1BH=0;
32
        OCR1BL=0;
33
#else
34
        // Configure counter such that we use phase correct
35
        // PWM with 8-bit resolution at a frequency of
36
        // PWM is mode 1, Freq = Clock I/O/1024. 
37
        // OC1A and OC1B as pwm.
38
        OCR1A = 0x00;
39
        OCR1B = 0x00;
40
        
41
        PORTD &= 0x0F;
42
        DDRD |= 0xF0;
43
        DDRB |= 0x60;
44
        
45
        TCCR1A = 0xA1;
46
        TCCR1B = 0x05;
47
#endif
48
}
49

    
50
// The following functions set the motor direction and speed
51
void motor1_set(int direction, int speed) {
52
#ifndef FFPP
53
        if(direction == 0) {
54
                PORTD |= 0x10;
55
                PORTD &= 0xBF;
56
        }
57
        else {
58
                PORTD |= 0x40;
59
                PORTD &= 0xEF;
60
        }
61
        
62
        // Set the timer to count up to speed, an 8-bit value
63
        OCR1AL = speed;
64
#else
65

    
66
        if(direction == FORWARD) {
67
                PORTD = (PORTD & 0xCF) | 0x20;
68
        }
69
        else {
70
                PORTD = (PORTD & 0xCF) | 0x10;
71
        }
72
        
73
        // Set the timer to count up to speed, an 8-bit value
74
        OCR1A = speed;
75
#endif
76

    
77
}
78

    
79
void motor2_set(int direction, int speed) {
80
#ifndef FFPP
81
        if(direction == 0) {
82
                PORTD |= 0x20;
83
                PORTD &= 0x7F;
84
        }
85
        else {
86
                PORTD |= 0x80;
87
                PORTD &= 0xDF;
88
        }
89
        OCR1BL = speed;
90
#else
91

    
92
        if(direction != FORWARD) {
93
                PORTD = (PORTD & 0x3F) | 0x80;
94
        }
95
        else {
96
                PORTD = (PORTD & 0x3F) | 0x40;
97
        }
98
        
99
        OCR1B = speed;
100
#endif
101
}
102

    
103
// Just turns off both motors
104
void motors_off( void ) {
105
        OCR1AL = 0x0;
106
        OCR1BL = 0x0;
107
}