Changeset 576 for trunk/electronics/avr
- Timestamp:
- 10/16/09 04:06:37 (2 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/electronics/avr/i2c_irboard/i2c_slave/twi_slave.c
r575 r576 14 14 15 15 uint16_t ADCIN(void); 16 16 17 uint8_t EEMEM slaveaddr = 0x26; 17 18 uint16_t EEMEM EE_m = 6787; … … 19 20 uint8_t EEMEM EE_k = 4; 20 21 21 uint8_t distance(uint16_t k, uint8_t m, uint8_t b) { 22 uint8_t distance(uint16_t k, uint8_t m, uint8_t b) 23 { 22 24 uint16_t analog = ADCIN(); 23 25 uint16_t distance = (m / (analog - b)) - k; … … 31 33 void adc_init(void) 32 34 { 33 // enable ADC 34 ADCSRA = _BV(ADEN); 35 36 // set prescalers 1 and 2, 125KHz ADC clock 37 ADCSRA |= _BV(ADPS1); 38 ADCSRA |= _BV(ADPS2); 39 40 // use internal 2.56v ref, no external bypass cap (pin needed for I2C) 41 ADMUX = _BV(REFS2) | _BV(REFS1); 42 // use ADC2, pb4, pin 3, no gain 43 ADMUX |= _BV(MUX1); 35 // enable adc, 62.5KHz ADC clock 36 ADCSRA = _BV(ADEN) 37 | _BV(ADPS0) 38 | _BV(ADPS1) 39 | _BV(ADPS2); 44 40 } 45 41 … … 48 44 { 49 45 uint8_t twi_in, twi_out; 50 sei();51 46 adc_init(); 47 sei(); // enable interrupts, needed for twi 52 48 53 49 // read in stored fitting constants for IR sensor … … 65 61 twi_out = 1; 66 62 twi_in = usiTwiReceiveByte(); 67 switch (twi_in)68 {63 switch (twi_in) 64 { 69 65 case RAW_ADC: 70 66 twi_out = ADCIN() > 2; … … 93 89 k = usiTwiReceiveByte(); 94 90 eeprom_write_byte(&EE_k, k); 91 break; 95 92 default: 96 93 twi_out = ERROR; 97 94 break; 98 } 99 95 } 100 96 usiTwiTransmitByte(twi_out); 101 97 } 102 98 asm volatile ("NOP" ::); 103 104 99 } 105 100 } 106 101 107 108 102 uint16_t ADCIN(void) 109 103 { 104 uint16_t value; 110 105 // enable single shot ADC conversion 106 107 // use internal 2.56v ref, no external bypass cap (pin needed for I2C) 108 ADMUX = _BV(REFS2) 109 | _BV(REFS1) 110 | _BV(MUX1); // use ADC2, pb4, pin 3, no gain 111 111 112 ADCSRA |= _BV(ADSC); 112 113 113 while ( ADCSRA & ( 1 << ADSC ) ); 114 while ( ADCSRA & ( 1 << ADSC ) ); // wait for conversion to complete 114 115 116 value = ADCL; 117 value += ADCH << 8; 115 118 return ADCH; 116 119 }

