Thursday, December 06, 2012

EMBEDDED SYSTEMS TUTORIAL 5- GPIO IN AVR



Hiii………friend this is next tutorial on AVR microcontroller, I know it took some time because I was trying to make it crystal clear for you all. In this tutorial we are going to discuss about basic GPIO( general purpose intput output) in AVRs.

All the interfacing of input and outputs of an AVR MCU is performed through PORTs. Before starting anything take a look of ATMEGA 16.





Here you can see there are 32 I/O pins grouped as A,B,C,D with 8 pins in a group. These groups are known as PORTs. You can see there are some special functions written in bracket  in front of these I/O pins, these are are the special function which our AVR MCU can perform with these specific pins other than I/O. we will discuss about all these functions in upcoming tutorials.


REGISTERS


All the processes in a MCU are performed through the help of registers. Register is a 8 bit location in the RAM. Here there are total 32x8 bit registers in ATMEGA16. Though the size of register is 8 bit that’s why it is called an 8 bit MCU.


HOW TO CONFIGURE INPUT /OUTPUT PROCESS IN MCU

Each PORT contain three type of registers.

DDRX- set weather a PORT is input or output.
PORTX- setting the value of output PORT.
PINX- reading the value of input PORT

DDRX( Data Direction Register)

With the help of this register we can make a port input PORT or output PORT.
To make a pin input or output we set its corresponding bit as

Set bit 1 to make it a output pin
Set bit 0 to make it a input pin.

So if we want to make all the pins of PORT A as input pins and all the pins of PORT B as output pins ,in our code we will write


DDRA=0x00;                          0x for hexadecimal
DDRB=0x FF;

OR

DDRA=0b00000000;                 0b for binary
DDRB=0b11111111;

Similarily if we want to make only first four pins of PORT C as input and last four as output, we will write.

DDRC=0XF0;
OR
DDRC=0b11110000;

PORTX (PORTX DATA REGISTER)

This register is used to set the value of a PORT, now a pin can be input or output let see both the cases

OUTPUT PIN

A pin be set to HIGH OUTPUT +5V  by setting it to 1, and can be set to LOW OUTPUT OV by setting it to 0.

For example we want high output on PC0,PC1 pins of PORTC and low output on rest of the pins of PORT C, then we can configure it as

PORTC=0x02;
OR
PORTC=0b00000011;

No the desired output sequence ia available on PORTC.

PINX (   Data read register)

This register is used to read the value of a PORT, If a pin is set as input then corresponding pin can be read as

0 for low input
1 for high output.

Now suppose PORT C is input port and we have connected a switch or sensor at PC0, then we can read the value of PC0 with the help of PINX register. You can read the state of key/switch by simply reading the PINC. It will be ‘1’ when  pressed and ‘0’ if  un pressed. 



CODING

...
if(PINC & 0b00000001)
{
 //Switch is pressed
 ...
}
else
{
 //Switch is not pressed
 ...
}
...
  
NOTE- all the input output functions needs the header file <avr/io.h> it must be added at the starting of the program.

In case you are not getting this code , I will suggest you to go through basics of C first. Download some tutorials from web or simply get any book on basics of c/c++.



Now its time to  use all this stuff in a code so we are going to implement a simple input output project in which a led will not  glow as output when switch will be pressed as input.
Here we are connecting the LED at PB0 and a push button at PC0. When we push the switch LED does not  glow otherwise it glows.

The source code is given below just compile this code get hex file and program your MCU with this hex file.


/*
 * IO_PROJECT.c
 *
 * Created: 10/16/2012 3:07:51 AM
 *  Author: ABHILASH
 */


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

int main(void)


{
 
                DDRB=0xFF;    // define PORT B as output PORT
                DDRC=0x00;   // define PORT C as input port
                  int s1=0;  // initializing and defining s1=0, acquire switch as s1
                while(1)      // create infinite loop
                {  
                                s1=PINC&0b00000001;  // acquire switch status connected at PC0
                                if(s1==0b00000001)   // check if switch is pressed
                                {
                                                PORTB=0b00000000;  // turn of led by setting  PB0 at low output
                                }
                                else
                                {
                                                PORTB=0b00000001;   // turn on LED by setting PB0 as high output
                                }
                                }
}



Now implement this circuit on the breadboard and you will we able to see the desired results…



Result will be like this.



.Switch pressed LED  not glowing.



 Switch not pressed LED glowing






some times there occurs the problem of switch debouncing, we will discuss about that in any upcoming tutorial.

I  hope this article was beneficial for beginners. in next tutorial we will discuss about some other basics of AVR. If you have any doubt, question or critical comment please post here.

THNX...:)

Wednesday, December 05, 2012

BASIC ELECTRONICS PROJECT4- SIMPLE ELECTRONIC CODE LOCK



Hiii….friends this time we ae going to implement another 555 timer IC project. With this project we can operate the operation of locking or unlocking it is just a dummy project it does not lock or unlock any thing it just operates some electronic action when switches of correct number are pressed.



CIRCUIT DIAGRAM



Here we are using six switches (  you can add more) in which 3 are CORRECT SWITCH and 3 are WRONG SWITCHES only right combination of COORECT SWITCHES unlocks the circuit.
In the circuit 555 timer IC is used in bistable mode , which can be reset or triggered by applying a low input on the corresponding pin. The CORRECT SWITCHES are connected to trigger pin( PIN2) when all the switches are pressed trigger pin receives a low input and the LED glows which means the circuit is unlocked. But in case any WRONG SWITCH connected in parallel  and connected to reset pin(PIN4) is pressed the circuit gets reset and LED does not blink means circuit is not unlocked so a correct switch combination can only unlock the circuit.  In this circuit the circuit unlocks only when S1, S2, S3 are pressed and unlocks if any of S4,S6,S5  is pressed .For operating any AC appliance u can use relay also.





COMPONENT REQUIRED



R1-R2         RESISTORS(100K)

R3               RESISTOR(1M)

R4               RESISTOR(330R)

C1,C2         CAPACITOR(1nF)

S1-S6          SPST SWITCHES

D1               LED( YOUR FAVOURITE COLOUR..;))

U1               IC555




I hope you found it helpful, in case of any doubt, question or critical view just leave a comment.

THNX…J

BASIC ELECTRONICS PROJECT3- TRAFFIC LIGHT PROJECT



Hiiii….. friends , this time I am posting  a basic electronicS project of TRAFFIC LIGHT, its really interesting and easy to implement. With this project we can make a dummy traffic light system, flashing red, yellow and green LEDs in a desired sequence and timing.


CIRCUIT DIAGRAM




Here a 555 timer IC is used to generate clock pulses and its output is driving another IC 4017, which is 1-10 counter IC, every time 4017 receives a clock one of its ten output goes high we are using these outputs in flashing our LEDs in a proper sequence and timing. A variable resistor is also provided to adjust the flashing time of LEDs, with the help of this variable resistor we can adjust our LED flashing time from several seconds to some minutes. You can easily implement it on any general purpose PCB or even on a breadboard.

COMPONENTS REQUIRED

D1-D9, D13             DIODES(1N4007)

R1,R4,R5                  RESISTOR (330R)

R2,R3                              RESISTOR (10K)

D10, D11, D12         LEDs( RED, YELLOW, GREEN)

RV1                            VARIABLE RESISTOR (1M)

U1                               IC NE555

U2                              IC 4017

C1                                CAPACITOR (10Uf)



I hope you found it helpful, in case of any doubt, question or critical view just leave a comment.

THNX…:)

Tuesday, December 04, 2012

BASIC ELECTRONICS PROJECT2- TONE GENERATION CIRCUIT USING NE555



Hiiii…..friends now its time for another basic electronic project for beginners. This time I  am going to show you a simple tone generator circuit using NE 555 IC. In this project we will see how we can generate a pleasant melody by using some basic electronic componentS.


CIRCUIT DIAGRAM



In this circuit NE555 IC is used in astable mode(free running mode). In this circuit a variable resistor is also provide by which we can control the pitch of tone generated and we can get multiple tones by this single circuit. The capacitor is provided to reduce the noise interference and getting a smooth pitch sound on adjusting.
Want more detail about NE555 just go through the DATASHEET OF 555 TIMER IC


I hope this project was an interesting one, in case of any doubt leave a comment.


THNX..:)

Monday, December 03, 2012

BASIC ELECTRONICS PROJECT 1- 7 SEGMENT DISPLAY COUNTER / INCOMING PERSON COUNTER






Hiii…….friends this time I am here with a very interesting projects for those who have just started with electronics. With this project we can display 0-9 digits on the 7 segment and we can count the no of persons entering in the room.

Circuit diagram


Here a counter IC 7490 is  used along with BCD to 7 segment decoder IC 7447. The function of 7490 is to provide the counting value and decoder IC is decoding that value to 7 segment output . Every time when you will push the switch the clock of IC7490 will be triggered and as a result count will increase by 1 . Hence with this simple project we can display numbers 0 to 9 on seven segment display just by pressing switch. It’s a very easy circuit and you can easily implement it on a general purpose PCB.


NOW ITS TIME TO MAKE USE OF THIS CIRCUIT IN COUNTING THE NUMBER OF PERSONS ENTERING IN THE ROOM...... LETS SEE HOW….??

So  instead of using switch we will use LDR here , we will place LDR at the entry door in such a manner that the shadow of the entering person should fall directly on LDR , when shadow will fall on LDR clock of IC 7490 will be triggered and as a result the count will increase by one. Circuit wilh some small changes  is as shown. Variable resistor is used to adjust the resistance to synchronize the light intensity for better results.



COMPONENT REQUIRED


U1                  IC7490

U2                  IC 7447

R1-R7            RESISTORS 330R x7

SEVEN SEGMENT DISPLAY( common anode)

Push button

POWER SUPPLY ( 5 to9 V)

LDR and VARIABLE RESISTOR



I hope this was a interesting project and u can use it as your school project or mini project as well.  In case of any doubt, question or critical view just leave a comment.

         THNX…J