5 #include <avr/interrupt.h>
7 #include <util/delay.h>
9 #include <avr/eeprom.h>
11 #include <avr/pgmspace.h>
15 * I2C Register Map (8 Bit)
16 * 0x00 Register select
17 * 0x01 Distance left MSB
18 * 0x02 Distance left LSB
19 * 0x03 Distance right MSB
20 * 0x04 Distance right LSB
21 * 0x05 Distance forward1 MSB
22 * 0x06 Distance forward1 LSB
23 * 0x07 Distance backward MSB
24 * 0x08 Distance backward LSB
27 * 0x0B Distance forward2 MSB
28 * 0x0C Distance forward2 LSB
32 * 0x15 Distance forward1 MSB (read only)
33 * 0x16 Distance forward1 LSB (read only)
34 * 0x17 Distance backward MSB (read only)
35 * 0x18 Distance backward LSB (read only)
36 * 0x19 Distance forward2 MSB (read only)
37 * 0x1A Distance forward2 LSB (read only)
43 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
44 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
45 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
47 static volatile uint8_t ireg=0;
48 static volatile uint8_t bootloader=0;
49 static volatile uint16_t dist_left=0;
50 static volatile uint16_t dist_right=0;
51 static volatile uint16_t dist_forward1=0;
52 static volatile uint16_t dist_forward2=0;
53 static volatile uint16_t dist_backward=0;
54 static volatile uint8_t start_dist_fwd1=0;
55 static volatile uint8_t start_dist_fwd2=0;
56 static volatile uint8_t start_dist_bwd=0;
57 static volatile uint16_t voltage=0;
58 static volatile uint16_t current=0;
59 static volatile uint8_t pind_pre=0;
63 static int16_t tmp16=0;
67 case TW_SR_SLA_ACK: // start write
71 case TW_SR_DATA_ACK: // write
73 case 0x00: // register select
76 if (ireg == 0x05) start_dist_fwd1=1;
77 if (ireg == 0x07) start_dist_bwd=1;
78 if (ireg == 0x0b) start_dist_fwd2=1;
80 ireg--; // because we do ireg++ below
83 case 0xff: // bootloader
88 if (ireg < 0xff) ireg++;
90 case TW_ST_SLA_ACK: // start read
91 case TW_ST_DATA_ACK: // read
93 case 0x01: // Distance left MSB
98 case 0x02: // Distance right LSB
102 case 0x03: // Distance right MSB
107 case 0x04: // Distance right LSB
111 case 0x05: // Distance forward1 MSB
112 tmp16 = dist_forward1;
116 case 0x06: // Distance forward1 LSB
120 case 0x07: // Distance backward MSB
121 tmp16 = dist_backward;
125 case 0x08: // Distance backward LSB
129 case 0x09: // Voltage MSB
134 case 0x0A: // Voltage LSB
138 case 0x0B: // Distance forward2 MSB
139 tmp16 = dist_forward2;
143 case 0x0C: // Distance forward2 LSB
147 case 0x0D: // Current MSB
152 case 0x0E: // Current LSB
156 case 0x15: // Distance forward1 MSB
157 tmp16 = dist_forward1;
161 case 0x16: // Distance forward1 LSB
165 case 0x17: // Distance backward MSB
166 tmp16 = dist_backward;
170 case 0x18: // Distance backward LSB
174 case 0x19: // Distance forward2 MSB
175 tmp16 = dist_forward2;
179 case 0x1A: // Distance forward2 LSB
198 uint16_t ReadChannel(uint8_t mux) {
202 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
203 // setzen auf 8 (1) und ADC aktivieren (1)
205 ADMUX = mux; // Kanal waehlen
208 /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
209 also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
210 ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
211 while ( ADCSRA & (1<<ADSC) ) {
212 // auf Abschluss der Konvertierung warten
214 result = ADCW; // ADCW muss einmal gelesen werden,
215 // sonst wird Ergebnis der nächsten Wandlung
218 /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
222 ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
223 while ( ADCSRA & (1<<ADSC) ) {
224 // auf Abschluss der Konvertierung warten
226 result += ADCW; // Wandlungsergebnisse aufaddieren
229 ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
231 result /= 5; // Summe durch 5 teilen = arithm. Mittelwert
237 static unsigned short get_distance(uint8_t i) {
238 return ReadChannel(i);
242 static unsigned short get_voltage(void) {
243 return ReadChannel(2)*1.46;
249 * sensitivity: 133mV/A
251 static unsigned short get_current(void) {
252 double volt = ReadChannel(3)*5.0171; // mV
253 return (volt-517.78)/0.12656; // mA
258 static uint16_t t_start=0;
259 uint16_t t_now = TCNT1;
262 if (bit_is_set(PIND, 2)) { // high level
266 t_diff = t_now - t_start;
267 dist_forward1 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
268 // disable this interrupt
269 EIMSK &= ~(1 << INT0);
275 static uint16_t t_start=0;
276 uint16_t t_now = TCNT1;
279 if (bit_is_set(PIND, 3)) { // high level
283 t_diff = t_now - t_start;
284 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
285 // disable this interrupt
286 EIMSK &= ~(1 << INT1);
292 uint8_t pind_cur = PIND;
294 if ((pind_cur ^ pind_pre) & (1<<4)) { // PCINT20
295 static uint16_t t_start=0;
296 uint16_t t_now = TCNT1;
299 if (bit_is_set(pind_cur, 4)) { // high level
303 t_diff = t_now - t_start;
304 dist_forward2 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
305 // disable this interrupt
306 PCMSK2 &= ~(1 << PCINT20);
323 // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
325 TCCR1B = (1 << CS12);
327 // External Interrupts
328 EICRA = (1 << ISC10) | (1 << ISC00);
329 PCICR = (1 << PCIE2);
331 printf_P(PSTR("\r\nStart\r\n"));
333 set_sleep_mode(SLEEP_MODE_IDLE);
337 case 0x01: // ir left
338 dist_left = get_distance(0);
340 case 0x03: // ir right
341 dist_right = get_distance(1);
343 case 0x09: // voltage
344 voltage = get_voltage();
346 case 0x0d: // current
347 current = get_current();
349 case 0xff: // Magic reg that starts the bootloader
350 if (bootloader == 0xa5) {
352 // write mark to first area in eeprom
353 eeprom_write_byte((uint8_t*)0, 123);
355 // Use watchdog to restart
356 wdt_enable(WDTO_15MS);
361 if (start_dist_fwd1) {
370 // wait for interrupt
371 EIFR &= (1 << INTF0); // clear old interrupt before enabling
372 EIMSK |= (1 << INT0);
374 if (start_dist_bwd) {
383 // wait for interrupt
384 EIFR &= (1 << INTF1); // clear old interrupt before enabling
385 EIMSK |= (1 << INT1);
387 if (start_dist_fwd2) {
397 // wait for interrupt
399 PCMSK2 |= (1 << PCINT20);