Rev 1455 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1455 | acid | 1 | |
2 | #define F_CPU 8000000UL |
||
3 | |||
4 | #include <string.h> |
||
5 | #include <avr/interrupt.h> |
||
6 | #include <avr/eeprom.h> |
||
7 | #include <util/delay.h> |
||
8 | #include "servoboard.h" |
||
9 | #include "twislave.h" |
||
10 | |||
11 | |||
12 | #define SERVODDR DDRB |
||
13 | #define SERVOPORT PORTB |
||
14 | #define SERVO1 PB0 |
||
15 | #define SERVO2 PB1 |
||
16 | #define SERVO3 PB2 |
||
17 | #define SERVO4 PB3 |
||
18 | #define SERVO5 PB4 |
||
19 | #define SERVO6 PB5 |
||
20 | |||
21 | |||
22 | uint8_t pwm_neutral_programming_mode = 0; |
||
23 | uint8_t eeprom_neutral_pos[6] EEMEM = { 127, 127, 127, 127, 127, 127 }; |
||
24 | uint16_t eeprom_pwm_limit[6] EEMEM = { 0, 0, 0, 0, 0, 0 }; |
||
25 | uint8_t pwm_signal[6] = { 0, 0, 0, 0, 0, 0 }; |
||
26 | uint8_t pwm_counter = 0; |
||
27 | uint8_t pwm_neutral_position[6] = { 127, 127, 127, 127, 127, 127 }; |
||
28 | uint8_t pwm_position[6] = { 127, 127, 127, 127, 127, 127 }; |
||
29 | uint16_t pwm_limit[6]; |
||
30 | |||
31 | |||
32 | |||
33 | void pwm_init() { |
||
34 | |||
35 | SERVODDR = 0xff; |
||
36 | |||
37 | set_pwm(); |
||
38 | |||
39 | TCCR1B = (1 << CS11); |
||
40 | TCCR0 = (1 << CS00); |
||
41 | |||
42 | } |
||
43 | |||
44 | void set_pwm() { |
||
45 | |||
46 | for(uint8_t n = 0; n < 6; n++) { |
||
47 | pwm_signal[n] = pwm_position[n] - 127 + pwm_neutral_position[n]; |
||
48 | if (pwm_limit[n] != 0) { |
||
49 | uint8_t l, h; |
||
50 | l = pwm_limit[n] & 0xff; |
||
51 | h = pwm_limit[n] >> 8; |
||
52 | if (pwm_signal[n] < l) { |
||
53 | pwm_signal[n] = l; |
||
54 | } |
||
55 | if (pwm_signal[n] > h) { |
||
56 | pwm_signal[n] = h; |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | } |
||
62 | |||
63 | void set_pwm_neutral() { |
||
64 | |||
65 | for(uint8_t i = 0; i < 6; i++) { |
||
66 | pwm_position[i] = 127; |
||
67 | } |
||
68 | set_pwm(); |
||
69 | |||
70 | } |
||
71 | |||
72 | void eeprom_init() { |
||
73 | |||
74 | /* |
||
75 | eeprom_busy_wait(); |
||
76 | for(uint8_t i = 0; i < 6; i++) { |
||
77 | pwm_neutral_position[i] = eeprom_read_byte(&eeprom_neutral_pos[i]); |
||
78 | pwm_limit[i] = eeprom_read_byte(&eeprom_pwm_limit[i]); |
||
79 | } |
||
80 | */ |
||
81 | |||
82 | pwm_neutral_position[0] = 0b10101000; |
||
83 | pwm_neutral_position[1] = 0b01101101; |
||
84 | pwm_neutral_position[2] = 0b10000000; |
||
85 | pwm_neutral_position[3] = 0b10010101; |
||
86 | pwm_neutral_position[4] = 0; |
||
87 | pwm_neutral_position[5] = 127; |
||
88 | |||
89 | pwm_limit[0] = 0; |
||
90 | pwm_limit[1] = 0; |
||
91 | pwm_limit[2] = 0; |
||
92 | pwm_limit[3] = 0b1011001101110101; |
||
93 | pwm_limit[4] = 0; |
||
94 | pwm_limit[5] = 0; |
||
95 | |||
96 | } |
||
97 | |||
98 | void eeprom_write() { |
||
99 | |||
100 | cli(); |
||
101 | for(uint8_t i = 0; i < 6; i++) { |
||
102 | eeprom_write_byte(&eeprom_neutral_pos[i], pwm_position[i]); |
||
103 | eeprom_write_byte(&eeprom_pwm_limit[i], pwm_limit[i]); |
||
104 | } |
||
105 | sei(); |
||
106 | |||
107 | } |
||
108 | |||
109 | void delay(int ms) { |
||
110 | |||
111 | while(ms--) { |
||
112 | _delay_ms(1); |
||
113 | } |
||
114 | |||
115 | } |
||
116 | |||
117 | |||
118 | int main(void) { |
||
119 | |||
1456 | acid | 120 | #define PROGRAMMINGMODE 0 |
121 | #if PROGRAMMINGMODE |
||
1455 | acid | 122 | uint8_t current_servo = 0; |
123 | uint8_t keyrate = 0; |
||
1456 | acid | 124 | #endif |
1455 | acid | 125 | |
1456 | acid | 126 | DDRD = 0x80; |
127 | PORTD = 0x80; |
||
1455 | acid | 128 | |
129 | DDRC &= ~0x0f; |
||
130 | PORTC |= 0x0f; |
||
131 | |||
132 | cli(); |
||
133 | eeprom_init(); |
||
134 | InitIC2_Slave(0x82); |
||
135 | pwm_init(); |
||
1456 | acid | 136 | set_pwm_neutral(); |
1455 | acid | 137 | sei(); |
138 | |||
139 | #if PROGRAMMINGMODE |
||
1456 | acid | 140 | DDRD = 0xff; |
141 | PORTD = 0xff; |
||
1455 | acid | 142 | if (!(PINC & 0x01)) { |
143 | pwm_neutral_programming_mode = 1; |
||
144 | PORTD = ~(1 << current_servo); |
||
145 | delay(1000); |
||
146 | } |
||
147 | #endif |
||
148 | |||
149 | TCNT1 = 0; |
||
150 | while(1) { |
||
151 | |||
152 | cli(); |
||
153 | SERVOPORT = 0xff; |
||
154 | while(TCNT1 < 300) ; |
||
155 | |||
156 | for(uint8_t i = 0; i < 255; i++) { |
||
157 | |||
158 | TCNT0 = 0; |
||
159 | |||
160 | if (i == pwm_signal[0]) { |
||
161 | SERVOPORT &= ~(1<<SERVO1); |
||
162 | } |
||
163 | if (i == pwm_signal[1]) { |
||
164 | SERVOPORT &= ~(1<<SERVO2); |
||
165 | } |
||
166 | if (i == pwm_signal[2]) { |
||
167 | SERVOPORT &= ~(1<<SERVO3); |
||
168 | } |
||
169 | if (i == pwm_signal[3]) { |
||
170 | SERVOPORT &= ~(1<<SERVO4); |
||
171 | } |
||
172 | if (i == pwm_signal[4]) { |
||
173 | SERVOPORT &= ~(1<<SERVO5); |
||
174 | } |
||
175 | if (i == pwm_signal[5]) { |
||
176 | SERVOPORT &= ~(1<<SERVO6); |
||
177 | } |
||
178 | |||
179 | while (TCNT0 < 60) ; |
||
180 | |||
181 | } |
||
182 | sei(); |
||
183 | |||
184 | SERVOPORT = 0; |
||
185 | |||
186 | set_pwm(); |
||
187 | |||
188 | while(TCNT1 < 20000) ; |
||
189 | TCNT1 = 0; |
||
190 | |||
191 | #if PROGRAMMINGMODE |
||
192 | |||
193 | if (pwm_neutral_programming_mode) { |
||
194 | |||
195 | if (keyrate == 0) { |
||
196 | |||
197 | if (!(PINC & 0x01)) { |
||
198 | if (pwm_position[current_servo] < 255) { |
||
199 | pwm_position[current_servo]++; |
||
200 | keyrate = 5; |
||
201 | set_pwm(); |
||
202 | } |
||
203 | } |
||
204 | if (!(PINC & 0x02)) { |
||
205 | if (pwm_position[current_servo] > 0) { |
||
206 | pwm_position[current_servo]--; |
||
207 | keyrate = 5; |
||
208 | set_pwm(); |
||
209 | } |
||
210 | } |
||
211 | if (!(PINC & 0x04)) { |
||
212 | current_servo++; |
||
213 | current_servo %= 6; |
||
214 | PORTD = ~(1 << current_servo); |
||
215 | keyrate = 25; |
||
216 | } |
||
217 | if (!(PINC & 0x08)) { |
||
218 | //keyrate = 50; |
||
219 | PORTD = ~pwm_position[current_servo]; |
||
220 | } |
||
221 | |||
222 | } else { |
||
223 | keyrate--; |
||
224 | } |
||
225 | |||
226 | } |
||
227 | #endif |
||
228 | |||
229 | } |
||
230 | |||
231 | return 0; |
||
232 | } |
||
233 |