Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. volatile uint16_t TMP36_VoutADCReading = 0U;
  2.  
  3. void TMP36_Init( void )
  4. {
  5. // Set the ADC prescaler to 128 (i.e., 16MHz/128 = 250kHz)
  6. ADCSRA |= ( 1 << ADPS2 ) | ( 1 << ADPS1 ) | ( 1 << ADPS0 );
  7.  
  8. // Set the voltage reference from AVcc (i.e., 5V).
  9. // ADMUX |= ( 1 << REFS0 );
  10.  
  11. // Turn on the ADC.
  12. ADCSRA |= ( 1 << ADEN );
  13.  
  14. // Do the initial conversion (i.e., the slowest conversion)
  15. // to ensure that everything is up and running.
  16. ADCSRA |= ( 1 << ADSC );
  17. }
  18.  
  19. void TMP36_ADCRead( uint8_t channel )
  20. {
  21. // Clear the previously read channel.
  22. ADCSRA &= 0xf0;
  23.  
  24. // Select the ADC channel to be read.
  25. ADMUX |= channel;
  26.  
  27. // Start a new conversion. By default, this conversion will
  28. // be performed in single conversion mode.
  29. ADCSRA |= ( 1 << ADSC );
  30.  
  31. // Wait until the conversion is complete.
  32. while( ADCSRA & ( 1 << ADSC ) );
  33.  
  34. // Obtain the ADC reading from Vout.
  35. TMP36_VoutADCReading = ADC;
  36. }
  37.  
  38. int main( void )
  39. {
  40. // Disable all interrupts for the time being.
  41. cli();
  42.  
  43. // Initialize the TMP36GZ temperature sensor.
  44. TMP36_Init();
  45.  
  46. // Enable all interrupts.
  47. sei();
  48.  
  49. while( 1 )
  50. {
  51. // Compute the distance.
  52. uint8_t channel = 1;
  53. TMP36_ADCRead( channel ); // At this point, TMP36_VoutADCReading = 1023
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement