5 #include <avr/interrupt.h>
7 #include <util/delay.h>
11 * I2C Register Map (8 Bit)
12 * 0x00 Register select
13 * 0x01 Distance left MSB
14 * 0x02 Distance left LSB
15 * 0x03 Distance right MSB
16 * 0x04 Distance right LSB
17 * 0x05 Distance forward MSB
18 * 0x06 Distance forward LSB
19 * 0x07 Distance backward MSB
20 * 0x08 Distance backward LSB
26 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
27 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
28 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
30 static volatile uint8_t ireg=0;
31 static volatile uint8_t bootloader=0;
32 static volatile uint16_t dist_left=0;
33 static volatile uint16_t dist_right=0;
34 static volatile uint16_t dist_forward=0;
35 static volatile uint16_t dist_backward=0;
36 static volatile uint8_t start_dist_fwd=0;
37 static volatile uint8_t start_dist_bwd=0;
41 static int16_t tmp16=0;
45 case 0x60: // start write
51 case 0x00: // register select
54 if (ireg == 0x05) start_dist_fwd=1;
55 if (ireg == 0x07) start_dist_bwd=1;
57 ireg--; // because we do ireg++ below
60 case 0xff: // bootloader
67 case 0xA8: // start read
70 case 0x01: // Distance left MSB
75 case 0x02: // Distance right LSB
79 case 0x03: // Distance right MSB
84 case 0x04: // Distance right LSB
88 case 0x05: // Distance forward MSB
93 case 0x06: // Distance forward LSB
97 case 0x07: // Distance backward MSB
98 tmp16 = dist_backward;
102 case 0x08: // Distance backward LSB
118 uint16_t ReadChannel(uint8_t mux) {
122 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
123 // setzen auf 8 (1) und ADC aktivieren (1)
125 ADMUX = mux; // Kanal waehlen
126 ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen
128 /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
129 also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
130 ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
131 while ( ADCSRA & (1<<ADSC) ) {
132 // auf Abschluss der Konvertierung warten
134 result = ADCW; // ADCW muss einmal gelesen werden,
135 // sonst wird Ergebnis der nächsten Wandlung
138 /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
142 ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
143 while ( ADCSRA & (1<<ADSC) ) {
144 // auf Abschluss der Konvertierung warten
146 result += ADCW; // Wandlungsergebnisse aufaddieren
149 ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
151 result /= 5; // Summe durch 5 teilen = arithm. Mittelwert
157 static unsigned short get_distance(uint8_t i) {
158 return ReadChannel(i);
163 static uint16_t t_start=0;
164 uint16_t t_now = TCNT1;
167 if (bit_is_set(PIND, 2)) { // high level
171 t_diff = t_now - t_start;
172 dist_forward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
173 // disable this interrupt
174 EIMSK |= (1 << INT0);
180 static uint16_t t_start=0;
181 uint16_t t_now = TCNT1;
184 if (bit_is_set(PIND, 3)) { // high level
188 t_diff = t_now - t_start;
189 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
190 // disable this interrupt
191 EIMSK |= (1 << INT1);
205 // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
207 TCCR1B = (1 << CS12);
209 // External Interrupts
210 EICRA = (1 << ISC10) | (1 << ISC00);
212 printf("\r\nStart\r\n");
214 set_sleep_mode(SLEEP_MODE_IDLE);
218 case 0x01: // ir left
219 dist_left = get_distance(0);
221 case 0x03: // ir right
222 dist_right = get_distance(1);
224 case 0xff: // Magic reg that starts the bootloader
225 if (bootloader == 0xa5) {
228 void (*start)(void) = (void*)0x1800;
235 if (start_dist_fwd) {
244 // wait for interrupt
245 EIMSK |= (1 << INT0);
247 if (start_dist_bwd) {
256 // wait for interrupt
257 EIMSK |= (1 << INT1);