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)


Wednesday, 11 September 2013

Difference between SPI and I2C Protocols

Difference between SPI and I2C Protocols:

Major Difference Between SPI and I2C is: 

1) SPI can be full/half duplex, depending on the hardware, 3 or 4 wire and I2C is half duplex      communication....we cannt send and receive data at a time in I2c..but it possible in SPI ....SPI much    faster Than I2C.......

2)  speed is difference :
In case of i2c is 100/400kbps in 7 bit mode max 400kbps (But High speed I2C communication     protocols allow speeds upto 3.4Mbps!!) 
In case of spi speed is upto 1mbps (the higher speed of SPI is due to fact that unlike I2C, SPI interfaces to a slave device using a sperate pin called the slave select + no concept of acknowledgements which means increased Band width..) 

3) Connection wise:
 I2c require less pin then spi (SPI 3wire: 3 IOs, SPI 4 wire: 4 IOs, I2C: 2 IOs)
as spi require slave select for individual device... 

4) Noise sensitivity of i2c is high... there is chance to corrupt the r/w bit... then whole data is corrputed... but in case of spi.. chance is very less as whole word is trasmitted... However, I must say that I2C is more reliable, since the protocol supports slave feedback machanism (ACK) to detect whether was received correctly or corrupt.
5) it is better to use i2c in case of if u want to connect more device to connect. (slave addressing advantageous over SPI slave select individual pins for individual slaves) 

6) bus arbitration is possible in case of i2c... not in case of spi... (SPI does not require it, since only 1 slave is controlled by the master at any given point) 

Saturday, 31 August 2013

Weclome to Real Time Embedded System and Design

Comming Soon........!!!!!!

Under Construction...!!!!!!


Thank You for Visiting Us! Have a nice day.