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
30 * 0x15 Distance forward1 MSB (read only)
31 * 0x16 Distance forward1 LSB (read only)
32 * 0x17 Distance backward MSB (read only)
33 * 0x18 Distance backward LSB (read only)
34 * 0x19 Distance forward2 MSB (read only)
35 * 0x1A Distance forward2 LSB (read only)
41 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
42 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
43 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
45 static volatile uint8_t ireg=0;
46 static volatile uint8_t bootloader=0;
47 static volatile uint16_t dist_left=0;
48 static volatile uint16_t dist_right=0;
49 static volatile uint16_t dist_forward1=0;
50 static volatile uint16_t dist_forward2=0;
51 static volatile uint16_t dist_backward=0;
52 static volatile uint8_t start_dist_fwd1=0;
53 static volatile uint8_t start_dist_fwd2=0;
54 static volatile uint8_t start_dist_bwd=0;
55 static volatile uint16_t voltage=0;
56 static volatile uint8_t pind_pre=0;
60 static int16_t tmp16=0;
64 case TW_SR_SLA_ACK: // start write
68 case TW_SR_DATA_ACK: // write
70 case 0x00: // register select
73 if (ireg == 0x05) start_dist_fwd1=1;
74 if (ireg == 0x07) start_dist_bwd=1;
75 if (ireg == 0x0b) start_dist_fwd2=1;
77 ireg--; // because we do ireg++ below
80 case 0xff: // bootloader
85 if (ireg < 0xff) ireg++;
87 case TW_ST_SLA_ACK: // start read
88 case TW_ST_DATA_ACK: // read
90 case 0x01: // Distance left MSB
95 case 0x02: // Distance right LSB
99 case 0x03: // Distance right MSB
104 case 0x04: // Distance right LSB
108 case 0x05: // Distance forward1 MSB
109 tmp16 = dist_forward1;
113 case 0x06: // Distance forward1 LSB
117 case 0x07: // Distance backward MSB
118 tmp16 = dist_backward;
122 case 0x08: // Distance backward LSB
126 case 0x09: // Voltage MSB
131 case 0x0A: // Voltage LSB
135 case 0x0B: // Distance forward2 MSB
136 tmp16 = dist_forward2;
140 case 0x0C: // Distance forward2 LSB
144 case 0x15: // Distance forward1 MSB
145 tmp16 = dist_forward1;
149 case 0x16: // Distance forward1 LSB
153 case 0x17: // Distance backward MSB
154 tmp16 = dist_backward;
158 case 0x18: // Distance backward LSB
162 case 0x19: // Distance forward2 MSB
163 tmp16 = dist_forward2;
167 case 0x1A: // Distance forward2 LSB
186 uint16_t ReadChannel(uint8_t mux) {
190 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
191 // setzen auf 8 (1) und ADC aktivieren (1)
193 ADMUX = mux; // Kanal waehlen
196 /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
197 also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
198 ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
199 while ( ADCSRA & (1<<ADSC) ) {
200 // auf Abschluss der Konvertierung warten
202 result = ADCW; // ADCW muss einmal gelesen werden,
203 // sonst wird Ergebnis der nächsten Wandlung
206 /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
210 ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
211 while ( ADCSRA & (1<<ADSC) ) {
212 // auf Abschluss der Konvertierung warten
214 result += ADCW; // Wandlungsergebnisse aufaddieren
217 ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
219 result /= 5; // Summe durch 5 teilen = arithm. Mittelwert
225 static unsigned short get_distance(uint8_t i) {
226 return ReadChannel(i);
230 static unsigned short get_voltage(void) {
231 return ReadChannel(2)*1.46;
236 static uint16_t t_start=0;
237 uint16_t t_now = TCNT1;
240 if (bit_is_set(PIND, 2)) { // high level
244 t_diff = t_now - t_start;
245 dist_forward1 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
246 // disable this interrupt
247 EIMSK &= ~(1 << INT0);
253 static uint16_t t_start=0;
254 uint16_t t_now = TCNT1;
257 if (bit_is_set(PIND, 3)) { // high level
261 t_diff = t_now - t_start;
262 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
263 // disable this interrupt
264 EIMSK &= ~(1 << INT1);
270 uint8_t pind_cur = PIND;
272 if ((pind_cur ^ pind_pre) & (1<<4)) { // PCINT20
273 static uint16_t t_start=0;
274 uint16_t t_now = TCNT1;
277 if (bit_is_set(pind_cur, 4)) { // high level
281 t_diff = t_now - t_start;
282 dist_forward2 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
283 // disable this interrupt
284 PCMSK2 &= ~(1 << PCINT20);
301 // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
303 TCCR1B = (1 << CS12);
305 // External Interrupts
306 EICRA = (1 << ISC10) | (1 << ISC00);
307 PCICR = (1 << PCIE2);
309 printf_P(PSTR("\r\nStart\r\n"));
311 set_sleep_mode(SLEEP_MODE_IDLE);
315 case 0x01: // ir left
316 dist_left = get_distance(0);
318 case 0x03: // ir right
319 dist_right = get_distance(1);
321 case 0x09: // voltage
322 voltage = get_voltage();
324 case 0xff: // Magic reg that starts the bootloader
325 if (bootloader == 0xa5) {
327 // write mark to first area in eeprom
328 eeprom_write_byte((uint8_t*)0, 123);
330 // Use watchdog to restart
331 wdt_enable(WDTO_15MS);
336 if (start_dist_fwd1) {
345 // wait for interrupt
346 EIFR &= (1 << INTF0); // clear old interrupt before enabling
347 EIMSK |= (1 << INT0);
349 if (start_dist_bwd) {
358 // wait for interrupt
359 EIFR &= (1 << INTF1); // clear old interrupt before enabling
360 EIMSK |= (1 << INT1);
362 if (start_dist_fwd2) {
372 // wait for interrupt
374 PCMSK2 |= (1 << PCINT20);