Guest User

Temperature Sensor

a guest
Oct 11th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/sleep.h>
  4. #include <avr/interrupt.h>
  5. #include <util/atomic.h>
  6.  
  7. void flash(void);
  8. inline void LED(uint8_t on);
  9. void Error(uint8_t);
  10. uint8_t getTemp(void);
  11. void sendRS232Byte(uint8_t);
  12.  
  13. //RS232 defines
  14. #define BAUD 9600
  15. #define RS232_PAUSE 1000000/BAUD
  16.  
  17. volatile uint16_t timeroverflow;
  18.  
  19.  
  20. int main(void){
  21.     //ADC init
  22.     ADMUX=(1<<REFS1)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0);
  23.     ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //ADC Freq: ~63khz
  24.     ADCSRB=0;
  25.     getTemp(); //init
  26.    
  27.     DDRB=(1<<PB4)|(1<<PB1); //Enable LED output, SDA, SCL
  28.     PORTB|=(1<<PB1);    //PB1 high
  29.    
  30.     //Timer init
  31.     TCCR1=0xF;  //Timer/16384 -> overflow ~0,5s
  32.     GTCCR=0;
  33.     TIMSK=(1<<TOIE1);
  34.    
  35.        
  36.    
  37.     LED(1);             //Start
  38.     _delay_ms(1000);
  39.     LED(0);
  40.    
  41.     timeroverflow=0;
  42.    
  43.     sei();
  44.     uint16_t temp;
  45.     while(1){
  46.         ATOMIC_BLOCK(ATOMIC_RESTORESTATE){
  47.             temp=timeroverflow;
  48.             if(temp>12){            //~10min
  49.                 timeroverflow=0;
  50.                
  51.                 sendRS232Byte(getTemp());
  52.                 flash();
  53.             }
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
  59.  
  60.  
  61. uint8_t getTemp(void){
  62.     ADCSRA|=(1<<ADSC)|(1<<ADIF);
  63.     while(ADCSRA&(1<<ADSC));
  64.     return (ADC>>2);
  65. }
  66.  
  67. inline void LED(uint8_t on){
  68.     switch (on){
  69.         case 0:
  70.             PORTB&=~(1<<PB4);
  71.             break;
  72.         default:
  73.             PORTB|=(1<<PB4);
  74.         }
  75. }
  76.  
  77.  
  78. void flash(){
  79.     LED(1);
  80.     _delay_us(50);
  81.     LED(0);
  82. }
  83.  
  84. void sendRS232Byte(uint8_t data){
  85.     DDRB|=(1<<PB1);
  86.     PORTB|=(1<<PB1);                    //Idle
  87.     _delay_us(10*RS232_PAUSE);
  88.     PORTB&=~(1<<PB1);                   //Start-Bit
  89.     for(uint8_t i=0;i<8;i++){   //8 Bits
  90.         _delay_us(RS232_PAUSE);
  91.         switch((data>>i)&1){
  92.             case 1:
  93.                 PORTB|=(1<<PB1);
  94.                 break;
  95.             case 0:
  96.                 PORTB&=~(1<<PB1);
  97.                 break;
  98.         }
  99.     }
  100.     _delay_us(RS232_PAUSE);
  101.     PORTB|=(1<<PB1);                    //Stop-Bit
  102.     _delay_us(10*RS232_PAUSE);
  103. }
  104.  
  105. ISR(TIMER1_OVF_vect){
  106.     timeroverflow++;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment