forked from esden/open-bldc-mk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bldc_utils.h
140 lines (125 loc) · 3.03 KB
/
bldc_utils.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Open-BLDC Firmware, Firmware for BrushLess Drive Controllers
* Copyright (C) 2009 Piotr Esden-Tempski <piotr at esden.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLDC_UTILS_H
#define BLDC_UTILS_H
inline void PWM_OFF() {
OCR1A = 0; /* set output compare 1A */
OCR1B = 0; /* set output compare 1B */
OCR2 = 0; /* set output compare 2 */
TCCR1A = 0x01; /* set WGM10 (PWM, Phase Correct, 8-bit) */
TCCR2 = 0x41; /* set WGM20 (PWM, Phase Correct) */
/* set CS20 (no prescaling) */
DDRB = 0x0E; /* set PB[123] to output */
PORTB &= ~0x0E; /* disable PB[123] */
}
inline void PWM_ON() {
/* set COM1A1, COM1B1, WGM10 */
/* (Clear OC1A/OC1B on Compare Match when up-counting. */
/* Set OC1A/OC1B on Compare Match when downcounting.) */
TCCR1A = 0xA1;
/* set WGM20, COM21, CS20 */
/* (Clear OC2 on Compare Match when up-counting. */
/* Set OC2 on Compare Match when downcounting.) */
TCCR2 = 0x61;
}
inline void PWM_A_ON() {
PWM_ON();
DDRB = 0x08;
}
inline void PWM_B_ON() {
PWM_ON();
DDRB = 0x04;
}
inline void PWM_C_ON() {
PWM_ON();
DDRB = 0x02;
}
inline void PWM_SET(uint16_t VAL) {
OCR1A = VAL;
OCR1B = VAL;
OCR2 = VAL;
}
inline void SET_A_H() {
PWM_A_ON();
}
inline void SET_B_H() {
PWM_B_ON();
}
inline void SET_C_H() {
PWM_C_ON();
}
inline void SET_A_L() {
PORTD &= ~0x30;
PORTD |= 0x08;
}
inline void SET_B_L() {
PORTD &= ~0x28;
PORTD |= 0x10;
}
inline void SET_C_L() {
PORTD &= ~0x18;
PORTD |= 0x20;
}
inline void SET_ALL_OFF() {
PORTD &= ~0x38;
PWM_OFF();
}
inline void FETS_OFF() {
PORTD &= ~0x38;
PORTB &= ~0x0E;
}
/*
* set ADMUX
*
* Using this macros we select on which phase we want to sense the
* next BEMF signal.
*/
inline void BEMF_SET_A() {
ADMUX = 0;
}
inline void BEMF_SET_B() {
ADMUX = 1;
}
inline void BEMF_SET_C() {
ADMUX = 2;
}
/* clear ACI (Analog Comparator Interrupt Flag) */
inline void CLR_BEMF() {
ACSR |= 0x10;
}
/* check ACI */
#define BEMF (ACSR & 0x10)
/* check ACO (Analog Comparator Output) */
#define BEMF_L (!(ACSR & 0x20))
#define BEMF_H ((ACSR & 0x20))
inline void ENABLE_BEMF_INT() {
ACSR |= 0x0A;
}
inline void DISABLE_BEMF_INT() {
ACSR &= ~0x0A;
}
inline void BEMF_FALLING_INT() {
ACSR &= ~0x01;
}
inline void BEMF_RISING_INT() {
ACSR |= 0x03;
}
inline void BEMF_TOGGLE_INT() {
ACSR &= ~0x03;
}
#endif /* BLDC_UTILS_H */