Thursday 12 September 2013

Turn on indication when Temperature goes higher then 50 degree. using ATMEGA 13 Microcontroller

/*=======================================================

ADC with blinking of LED on PB0 pin
Created by : Gaurav Kumar Garg

if temp goes higher then 50 degree then it glow red led if temp goes below 50 degree then
it glow green led. lm35 temp ic is use for temp sensor. which give 10 mv for every incresing
of temp 1 degree. so for 50 degree temp output of lm35 is 0.5 volt. and output of adc is
102. (see below calculation)

in this programme for 0.7 sec (700 msec) yello led goes glow which is connected to PB0 pin
 and for  0.8 (800 msec) yellow led goes off. so it take ADC Sample when only Yellow LED Glow

in this programme if LED is on then it take 10 sample of ADC Value. then calculate avg
value of adc. if value goes higher then 102 then it glow red led.

in this programme i am using 10 bit resolution ADC. and 5 volt internal referance voltage

so step size = Vref/(2^10)
= 5/1024
= 0.00488

Dout = Vin/step size

where Vin is input analog voltage to ADC

for eg Vin = 0.5 volt

Dout = 0.5/(0.0048)
Dout  = 102 (approx )


Oscillator Freq = 9.6 MHz
prescaler for timer is 1024 so clk freq for timer is  9.6MHz/1024  = 9375 Hz
so every tick of timer generate 106.6 u sec of delay.

so for generating delay of 10 msec we load value in OCR Register 0x5E (94 in decimal)

94x106.6 usec =  10 msec

 ========================================================
*/



#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

unsigned int i,avg=0,sum=0;
volatile unsigned int temp=0;
int ReadADC();
int val=0;

ISR (TIM0_COMPA_vect)
{
OCR0A = 0X5E;
++temp;
}

int main (void)
{
 DDRB = 0X07;

 TCCR0A=0X02; // CTC MODE  operation  WGM2=0, WGM1=1, WGM0=0,

 TIMSK0 |= (1<<OCIE0A); // TIMER CTC interrupt enable
 OCR0A = 0X5E;
 sei();                      // Enable ADC interrupt
              // timer should click 94 for delay of 10 msec (1 click=106.6 usec)
 TCCR0B |= (1<<CS02) | (1<<CS00);  // Set prescaler clk/1024

 ADMUX |= (0 << REFS0); // VCC as Reference (5 Volt )
 ADMUX |= (1 << MUX1) | (0 << MUX0); // ADC2 PB.4
 ADCSRA |= (1 << ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC  
 
 while (1)
 {
   
   On_Off();
 
 }

   return 1;  
}


int ReadADC()
{
   
      ADMUX |= (0 << REFS0);   // VCC as Reference
      ADMUX = 0X02;              // ADC2 PB.4
      ADCSRA |= (1 << ADSC); // Start Converstion

      while((ADCSRA & 0x40) !=0); //wait for conv complete

      return ADC;
}
void On_Off()
{

if (temp < 70)
{
PORTB |= (1<<PB0);

for (i=0;i<10;i++)
    {
    val = ReadADC();
_delay_ms(2);
    sum=sum+val;
    }
avg = sum/10;

if (avg > 102)
    {
      PORTB |= (1 << PB1);
    PORTB &= ~(1 << PB2);
     }
    else
    {
      PORTB |= (1 << PB2);
  PORTB &= ~(1<<PB1);
    }
avg = 0;
sum = 0;
}
else if (( temp>70 ) && (temp <150))
{
PORTB &= ~(1<<PB0);

}
else if(temp > 150)
{
temp=0;
}
}

/*  When Temp is Below 50 degree


When Temp is Below 50 degree Green Led Glow



When Temp is above 50 degree
When Temp is above 50 degree (Red Led Glow)


No comments:

Post a Comment