Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * TC1047 temperature sensor
  3.  *
  4.  * Using ADC for reading sensor value.
  5.  *
  6.  * Author: Erik Larsson
  7.  * Date: 2006-12-19
  8.  *
  9.  */
  10.  
  11. /*-----------------------------------------------
  12.  * Includes
  13.  * ---------------------------------------------*/
  14. #include <avr/io.h>
  15. #include <stdio.h>
  16.  
  17. #include <tc1047.h>
  18.  
  19.  
  20. /*----------------------------------------------
  21.  * Functions
  22.  * --------------------------------------------*/
  23.  
  24. /*
  25.  * Initiate ADC for reading temperature from sensor.
  26.  */
  27. void adcTemperatureInit()
  28. {
  29. #if PDIP
  30.     /* Enable ADC0 (for PDIP package) */
  31.     ADMUX &= ~((1<<MUX0)|(1<<MUX1)|(1<<MUX2)|(1<<MUX3));
  32. #else
  33.     /* Enable ADC7 (for TQFP package) */
  34.     ADMUX |= (1<<MUX0)|(1<<MUX1)|(1<<MUX2);
  35.     ADMUX &= ~(1<<MUX3);
  36. #endif
  37.  
  38.     /* Enable AVcc as Voltage Reference */
  39.     ADMUX |= (1<<REFS0);
  40.     ADMUX &= ~(1<<REFS1);
  41.  
  42.     /* Right adjust the result */
  43.     ADMUX &= ~(1<<ADLAR);
  44.  
  45.     /* Wake up ADC and enable it */
  46.     PRR &= ~(1<<PRADC);
  47.     ADCSRA |= (1<<ADEN);
  48.  
  49.     /* Make the first conversion (takes 25 ADC clock cycles) and throw away */
  50.     getTC1047temperature();
  51. }
  52.  
  53. /*
  54.  * Start reading and return the temperature value.
  55.  */
  56. uint32_t getTC1047temperature()
  57. {
  58.     uint32_t temperatureData;
  59.  
  60.     /* Start measurement (takes 13 ADC clock cycles) */
  61.     ADCSRA |= (1<<ADSC);
  62.  
  63.     while( ADCSRA & (1<<ADSC) ){ /* Wait for conversion to complete */ }
  64.  
  65.     /* Get the result, convert and return */
  66.     temperatureData = ADCW;
  67.  
  68.     temperatureData = temperatureData * VREF;
  69.     temperatureData = (temperatureData * 100 / 1024) - 50;
  70.  
  71.     return temperatureData;
  72. }
  73.