Saturday 14 September 2013

Digital Lock for Electronic Gadget using ATMEGA32 Microcontroller

/*
 ========================================================================
          Digital Code Lock With Keyboard Interfacing in AVR
         
 Author    :   Gaurav Kumar Garg
                        garg.gaurav52@gmail.com
          Version Release:  06-19-2013.1
          GNU GPL Licence

          In this project row are connected to first four pin of C Port
          Coloum are connected to first four pin of D Port
 ========================================================================
*/

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

unsigned char ch1[4],ch2[4],i;

void delay(unsigned int ch)
{
_delay_ms(ch);
}

void key_init()
{
DDRC=0X0F; //Make port c as output
DDRD=0X00; //Make port D as input
DDRA=0XFF; // Just for testing purpose

}

void lcd_init()
{
DDRB=0xff; // make port b is an output port for display data on lcd
dis_cmd(0x02); // to initilize in 4 bit mode
dis_cmd(0x28);
dis_cmd(0x0c);
delay(250);
}

void lcd_cmd(unsigned char ch)
{
PORTB=ch;
PORTB|=(1<<PB0); // make EN=1
PORTB&=~(1<<PB1); //make r/w=0 for writing
PORTB&=~(1<<PB2); //make  RS =0 for command
delay(250);
PORTB&=~(1<<PB0); // high to low pulse EN = 0

}

void lcd_data(unsigned char ch)
{
PORTB=ch;
PORTB|=(1<<PB0); // make EN=1
PORTB&=~(1<<PB1); //make r/w=0 for writing
PORTB|=(1<<PB2); //make  RS =1 for data
delay(250);
PORTB&=~(1<<PB0); // high to low pulse EN = 0
}

void dis_cmd(unsigned char ch)
{
unsigned char value;
value=ch&0xf0;
lcd_cmd(value);
value=(ch<<4)&(0xf0);
lcd_cmd(value);
delay(20);
}

void dis_data(unsigned char ch)
{
unsigned char value;
value=ch&0xf0;
lcd_data(value);
value=(ch<<4)&(0xf0);
lcd_data(value);
delay(20);
}

void lcd_string(unsigned char ch[])
{
unsigned int i=0;
while (ch[i]!='\0')
{
dis_data(ch[i]);
i++;
// delay(20);
}
}

unsigned char a[4][4]={
'1', '2', '3', '4',
'5', '6', '7', '8',
'9', '0', 'A', 'B',
'C', 'D', 'E', 'F',
 };

char take_value(void)
{
unsigned char rowloc,colloc,key;
// key_init();
// lcd_init();

// while (1)
{
unsigned int i=0;
do
{
PORTC&=0X00; // Ground all row
PORTD=0X0F;
colloc=(PIND & 0X0F);
}while (colloc!=0X0F); // check until all key are released



do
{
delay(20);
colloc=(PIND & 0X0F);
}while (colloc==0x0f); //wait for key press


// Now Checking for particular row
while (1)
{
PORTC=0X0E;
colloc=PIND & 0X0F;
if (colloc!=0x0f)
{
rowloc=0;
//dis_data('0');
break;
}

PORTC=0X0D;
colloc=PIND& 0X0F;
if (colloc!=0x0f)
{
rowloc=1;
//dis_data('1');
break;
}

PORTC=0X0B;
colloc=PIND & 0X0F;
if (colloc!=0x0f)
{
rowloc=2;
// dis_data('2');
break;
}
else
{
PORTC=0x07;
colloc=PIND & 0X0F;
rowloc=3;
//dis_data('3');
break;
}
}

//dis_data('rowloc');
//dis_data('1');   //for testing purpose



// Now Detect coloum pressed
if (colloc==0x0E)
key=a[rowloc][0];

else if (colloc==0x0D)
key=a[rowloc][1];

else if (colloc==0x0B)
key=a[rowloc][2];

else
key=a[rowloc][3];

// ch1[i]=key;
// dis_data(key);

}// end of first while(1) loop

// dis_data(key);
return key;
}// end of main loop


void main()
{
unsigned char key;
key_init();
    lcd_init();

lcd_string("*Enter Password*");
dis_cmd(0xc0);

int flag=1;
for (i=0;i<4;i++)
{
// key=take_value();
ch1[i]=take_value();
key=ch1[i];
lcd_string("*");
// dis_data(key);
// dis_data();
// lcd_string(key);
}

dis_cmd (0x01);
dis_cmd (0x80);
lcd_string("Enter Again");
dis_cmd(0xC0);

for (i=0;i<4;i++)
{
ch2[i]=take_value();
lcd_string ("*");
}

for (i=0;i<4;i++)
{
if (ch1[i]!=ch2[i])
flag=0;

}

dis_cmd (0x01);
dis_cmd (0x80);
if (flag==0)
lcd_string("Wrong Password");
else
lcd_string("Lock Opening...");

if (flag!=0)
{
PORTA|=(1<<PA0); // For Glowing LED if Password enter is correct
}
}



Digital Code Lock ( It will ask you for entering password )



Verifying Password (It will ask to enter password again for verifying )


If we enter correct Password then it will Switch Device ON (Hear RED LED is using as a Device which is connected to PORT PA0 )


Thursday 12 September 2013

What is Compiler and Compilation

Compiler and Compilation :
compiler is nothing but a normal program which translates a source program written in some high-level programming language (such as C, C++, Java and etc.,) into executable files which can be understood by some CPU architecture (such as Intel, ARM, AVR, Microchip etc). For this article I have been searching a lot on net, surprisingly there is limited information available with this regard. But Wikipedia and Aho’s famous book I have used as reference.
The process we are doing to get the binary files( executable files) is nothing but “Compilation”.
What’s Cross Compilation?
Before we discuss about Cross Compilation, we should learn about what is compilation and what is compiler. Then we will go for cross compiler and cross compilation.
First we talk about something general in developing field.

Suppose if you are developing a PC application or server application, then development platform and the target platform are the same.
Here first we discuss about two terms in above line.
1. Development Platform
2. Target Platform
Before going to know about these, we need to know a tickle about the word ‘Platform’ what we used here. What I mean to say about ‘Platform’ is just nothing but the combination of CPU architecture and Operating System.
Now, come to Development Platform, it’s a machine which runs your compiler. With this, you will guess Target Platform. It’s a machine that runs your application. An embedded developer understands this very well.
Native Compiler:
As we said at first, desktop developers used to build the binaries for windows or Linux and to run those applications they will use the same machine. The compiler which is used in this case is called as “Native Compiler”.
Cross Compilation:
The process of building executable files on one machine and run them on another machine when the CPU architecture or the Operating system are different is called “Cross Compilation”.
A special program which is used to do the cross compilation process is nothing but “Cross Compiler”.  Most of the people defined it as Tool Chain.
Just take and example, we are developing the applications for a mobile phone running on some X micro controller. So, we write the program in PC and build that source code (Now a day’s most of the people trying this using Android ADK). After compiling this, we dump the code onto the target machine (our micro controller or mobile) and then we run the application. So, when we are developing the applications on one platform for running it on another platform, with host compiler getting executable files to run on the target is not possible. But with the help of cross compiler the suitable executable files to run on the target is possible.
A compiler consists of three main parts: the frontend, the middle-end, and the backend.
The front end checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end.
The middle end is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates another IR for the following backend. Most optimization efforts are focused on this part.
The back end is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on. Although most algorithms for optimization are in NP, heuristic techniques are well-developed.

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)