Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- volatile uint16_t TMP36_VoutADCReading = 0U;
- void TMP36_Init( void )
- {
- // Set the ADC prescaler to 128 (i.e., 16MHz/128 = 250kHz)
- ADCSRA |= ( 1 << ADPS2 ) | ( 1 << ADPS1 ) | ( 1 << ADPS0 );
- // Set the voltage reference from AVcc (i.e., 5V).
- // ADMUX |= ( 1 << REFS0 );
- // Turn on the ADC.
- ADCSRA |= ( 1 << ADEN );
- // Do the initial conversion (i.e., the slowest conversion)
- // to ensure that everything is up and running.
- ADCSRA |= ( 1 << ADSC );
- }
- void TMP36_ADCRead( uint8_t channel )
- {
- // Clear the previously read channel.
- ADCSRA &= 0xf0;
- // Select the ADC channel to be read.
- ADMUX |= channel;
- // Start a new conversion. By default, this conversion will
- // be performed in single conversion mode.
- ADCSRA |= ( 1 << ADSC );
- // Wait until the conversion is complete.
- while( ADCSRA & ( 1 << ADSC ) );
- // Obtain the ADC reading from Vout.
- TMP36_VoutADCReading = ADC;
- }
- int main( void )
- {
- // Disable all interrupts for the time being.
- cli();
- // Initialize the TMP36GZ temperature sensor.
- TMP36_Init();
- // Enable all interrupts.
- sei();
- while( 1 )
- {
- // Compute the distance.
- uint8_t channel = 1;
- TMP36_ADCRead( channel ); // At this point, TMP36_VoutADCReading = 1023
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement