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)
70 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
71 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
72 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
74 static volatile uint8_t ireg=0;
75 static volatile uint8_t bootloader=0;
76 static volatile uint16_t dist_left=0;
77 static volatile uint16_t dist_right=0;
78 static volatile uint16_t dist_forward1=0;
79 static volatile uint16_t dist_forward2=0;
80 static volatile uint16_t dist_backward=0;
81 static volatile uint8_t start_dist_fwd1=0;
82 static volatile uint8_t start_dist_fwd2=0;
83 static volatile uint8_t start_dist_bwd=0;
84 static volatile uint16_t voltage=0;
85 static volatile uint16_t current=0;
86 static volatile uint8_t pind_pre=0;
90 static int16_t tmp16=0;
94 case TW_SR_SLA_ACK: // start write
98 case TW_SR_DATA_ACK: // write
100 case 0x00: // register select
103 if (ireg == 0x05) start_dist_fwd1=1;
104 if (ireg == 0x07) start_dist_bwd=1;
105 if (ireg == 0x0b) start_dist_fwd2=1;
107 ireg--; // because we do ireg++ below
110 case 0xff: // bootloader
115 if (ireg < 0xff) ireg++;
117 case TW_ST_SLA_ACK: // start read
118 case TW_ST_DATA_ACK: // read
120 case 0x01: // Distance left MSB
125 case 0x02: // Distance right LSB
129 case 0x03: // Distance right MSB
134 case 0x04: // Distance right LSB
138 case 0x05: // Distance forward1 MSB
139 tmp16 = dist_forward1;
143 case 0x06: // Distance forward1 LSB
147 case 0x07: // Distance backward MSB
148 tmp16 = dist_backward;
152 case 0x08: // Distance backward LSB
156 case 0x09: // Voltage MSB
161 case 0x0A: // Voltage LSB
165 case 0x0B: // Distance forward2 MSB
166 tmp16 = dist_forward2;
170 case 0x0C: // Distance forward2 LSB
174 case 0x0D: // Current MSB
179 case 0x0E: // Current LSB
183 case 0x15: // Distance forward1 MSB
184 tmp16 = dist_forward1;
188 case 0x16: // Distance forward1 LSB
192 case 0x17: // Distance backward MSB
193 tmp16 = dist_backward;
197 case 0x18: // Distance backward LSB
201 case 0x19: // Distance forward2 MSB
202 tmp16 = dist_forward2;
206 case 0x1A: // Distance forward2 LSB
225 uint16_t ReadChannel(uint8_t mux) {
229 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
230 // setzen auf 8 (1) und ADC aktivieren (1)
232 ADMUX = mux; // Kanal waehlen
235 /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
236 also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
237 ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
238 while ( ADCSRA & (1<<ADSC) ) {
239 // auf Abschluss der Konvertierung warten
241 result = ADCW; // ADCW muss einmal gelesen werden,
242 // sonst wird Ergebnis der nächsten Wandlung
245 /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
249 ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
250 while ( ADCSRA & (1<<ADSC) ) {
251 // auf Abschluss der Konvertierung warten
253 result += ADCW; // Wandlungsergebnisse aufaddieren
256 ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
258 result /= 5; // Summe durch 5 teilen = arithm. Mittelwert
264 static unsigned short get_distance(uint8_t i) {
265 return ReadChannel(i);
269 static unsigned short get_voltage(void) {
270 return ReadChannel(2)*1.46;
276 * sensitivity: 133mV/A
278 static unsigned short get_current(void) {
279 double volt = ReadChannel(3)*5.0171; // mV
280 return (volt-517.78)/0.12656; // mA
285 static uint16_t t_start=0;
286 uint16_t t_now = TCNT1;
289 if (bit_is_set(PIND, 2)) { // high level
293 t_diff = t_now - t_start;
294 dist_forward1 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
295 // disable this interrupt
296 EIMSK &= ~(1 << INT0);
302 static uint16_t t_start=0;
303 uint16_t t_now = TCNT1;
306 if (bit_is_set(PIND, 3)) { // high level
310 t_diff = t_now - t_start;
311 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
312 // disable this interrupt
313 EIMSK &= ~(1 << INT1);
319 uint8_t pind_cur = PIND;
321 if ((pind_cur ^ pind_pre) & (1<<4)) { // PCINT20
322 static uint16_t t_start=0;
323 uint16_t t_now = TCNT1;
326 if (bit_is_set(pind_cur, 4)) { // high level
330 t_diff = t_now - t_start;
331 dist_forward2 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
332 // disable this interrupt
333 PCMSK2 &= ~(1 << PCINT20);
350 // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
352 TCCR1B = (1 << CS12);
354 // External Interrupts
355 EICRA = (1 << ISC10) | (1 << ISC00);
356 PCICR = (1 << PCIE2);
358 printf_P(PSTR("\r\nStart\r\n"));
360 set_sleep_mode(SLEEP_MODE_IDLE);
364 case 0x01: // ir left
365 dist_left = get_distance(0);
367 case 0x03: // ir right
368 dist_right = get_distance(1);
370 case 0x09: // voltage
371 voltage = get_voltage();
373 case 0x0d: // current
374 current = get_current();
376 case 0xff: // Magic reg that starts the bootloader
377 if (bootloader == 0xa5) {
379 // write mark to first area in eeprom
380 eeprom_write_byte((uint8_t*)0, 123);
382 // Use watchdog to restart
383 wdt_enable(WDTO_15MS);
388 if (start_dist_fwd1) {
397 // wait for interrupt
398 EIFR &= (1 << INTF0); // clear old interrupt before enabling
399 EIMSK |= (1 << INT0);
401 if (start_dist_bwd) {
410 // wait for interrupt
411 EIFR &= (1 << INTF1); // clear old interrupt before enabling
412 EIMSK |= (1 << INT1);
414 if (start_dist_fwd2) {
424 // wait for interrupt
426 PCMSK2 |= (1 << PCINT20);