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
21 * 0x15 Distance forward MSB (read only)
22 * 0x16 Distance forward LSB (read only)
23 * 0x17 Distance backward MSB (read only)
24 * 0x18 Distance backward LSB (read only)
32 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
33 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
34 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
36 static volatile uint8_t ireg=0;
37 static volatile uint8_t bootloader=0;
38 static volatile uint16_t dist_left=0;
39 static volatile uint16_t dist_right=0;
40 static volatile uint16_t dist_forward=0;
41 static volatile uint16_t dist_backward=0;
42 static volatile uint8_t start_dist_fwd=0;
43 static volatile uint8_t start_dist_bwd=0;
44 static volatile uint16_t voltage=0;
48 static int16_t tmp16=0;
52 case 0x60: // start write
58 case 0x00: // register select
61 if (ireg == 0x05) start_dist_fwd=1;
62 if (ireg == 0x07) start_dist_bwd=1;
64 ireg--; // because we do ireg++ below
67 case 0xff: // bootloader
74 case 0xA8: // start read
77 case 0x01: // Distance left MSB
82 case 0x02: // Distance right LSB
86 case 0x03: // Distance right MSB
91 case 0x04: // Distance right LSB
95 case 0x05: // Distance forward MSB
100 case 0x06: // Distance forward LSB
104 case 0x07: // Distance backward MSB
105 tmp16 = dist_backward;
109 case 0x08: // Distance backward LSB
113 case 0x15: // Distance forward MSB
114 tmp16 = dist_forward;
118 case 0x16: // Distance forward LSB
122 case 0x17: // Distance backward MSB
123 tmp16 = dist_backward;
127 case 0x18: // Distance backward LSB
131 case 0x09: // Voltage MSB
136 case 0x0A: // Voltage LSB
152 uint16_t ReadChannel(uint8_t mux) {
156 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
157 // setzen auf 8 (1) und ADC aktivieren (1)
159 ADMUX = mux; // Kanal waehlen
162 /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
163 also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
164 ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
165 while ( ADCSRA & (1<<ADSC) ) {
166 // auf Abschluss der Konvertierung warten
168 result = ADCW; // ADCW muss einmal gelesen werden,
169 // sonst wird Ergebnis der nächsten Wandlung
172 /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
176 ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
177 while ( ADCSRA & (1<<ADSC) ) {
178 // auf Abschluss der Konvertierung warten
180 result += ADCW; // Wandlungsergebnisse aufaddieren
183 ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
185 result /= 5; // Summe durch 5 teilen = arithm. Mittelwert
191 static unsigned short get_distance(uint8_t i) {
192 return ReadChannel(i);
196 static unsigned short get_voltage(void) {
197 return ReadChannel(2)*1.46;
202 static uint16_t t_start=0;
203 uint16_t t_now = TCNT1;
206 if (bit_is_set(PIND, 2)) { // high level
210 t_diff = t_now - t_start;
211 dist_forward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
212 // disable this interrupt
213 EIMSK |= (1 << INT0);
219 static uint16_t t_start=0;
220 uint16_t t_now = TCNT1;
223 if (bit_is_set(PIND, 3)) { // high level
227 t_diff = t_now - t_start;
228 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
229 // disable this interrupt
230 EIMSK |= (1 << INT1);
244 // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
246 TCCR1B = (1 << CS12);
248 // External Interrupts
249 EICRA = (1 << ISC10) | (1 << ISC00);
251 printf("\r\nStart\r\n");
253 set_sleep_mode(SLEEP_MODE_IDLE);
257 case 0x01: // ir left
258 dist_left = get_distance(0);
260 case 0x03: // ir right
261 dist_right = get_distance(1);
263 case 0x09: // voltage
264 voltage = get_voltage();
266 case 0xff: // Magic reg that starts the bootloader
267 if (bootloader == 0xa5) {
270 void (*start)(void) = (void*)0x1800;
277 if (start_dist_fwd) {
286 // wait for interrupt
287 EIMSK |= (1 << INT0);
289 if (start_dist_bwd) {
298 // wait for interrupt
299 EIMSK |= (1 << INT1);