Wednesday, June 9, 2010

Session 1 - Blink LED - A Convention

As convention, we can start with a program which blinks LED connected to the processor. The program below does this job. The LEDs should be connected in such a way that when a High Voltage (5V) is connected to one terminal, it should turn on.  It will be very simple to use a development board like STK500 for simulating the program.  (If you are using STK500, its circuit arrangement is different.  It will flash the LED when a zero is fed.)


Program - 1

This simple program can turn on 8 LEDs connected to 8 pins of PORTA of the processor.  The pin names are PA0, PA1,.... PA7.  When you burn this program into the processor, the LEDs will be turned on.  There are a lot of things to be understood behind the working of this program.  We can go to it step by step.

#include...:
Here the statement #include.. is what is called the 'Header file'.  As its name indicates, it is the head of this program.  We include this head in the starting of program.  Because it contains informations about the 'commands' which we use in program.  In that header file, instructions are provided to the computer (or specifically Compiler) about the things-to-do when a command is executed.  There are lot of header files are available.  These are also called as Library files.  Depending on the variety of commands we use in the program, more header files are to be included.  Because all commands cannot be defined in a single header file.  In this program only one header file is enough and that is the one we included here - io.h. io stands for input-output.

int main()
Every C program contains this statement.  The execution of commands will start from this 'main' block.  What we write under this statement will be executed line by line when the program runs.  The two brackets - () can be called as function.  So here we will call it as Main function (main()).  The main() is isolated from other part of the program by providing braces ({}).

DDRB=0b11111111;
Now we are getting into the core of this program.  In this statement, a number is shown as '0b11111111'.  The first two digits - 0b, only indicates that the number following it is in binary format.  If you need to write the number in hexadecimal you can use the prefix '0x'.  If you need to use decimal itself, no need of any prefix.  Usually in programs we follow the hexadecimal format.  It is mainly because the processing of Atmega processors are done in Hex format.  So if we are using hexadecimal, we will feel that we are working very close to the processor.  So this code can be rewritten as:
DDRB=0xFF;
Now what does this DDRB mean?  DDR means the Data Direction Register.  That is the name given by Atmel.  We are going to look on it from our way.  Our aim is to make the processor communicable with the outside world.  For that purpose a lot of pins are provided in it.  What can we do with these pins?  It is to be noted that all logics are realized, through the two states of these pins.  The 'Zero' State and the 'One' State.
When we use an Air conditioner, we adjust the temperature by simply pressing desired switches.  When we press one switch, it will pass a one/zero to the desired pin of th we wante microcontroller.  The program is coded in it such that if a one/zero is received in that particular pin, its meaning is to change the temperature.  For that purpose the processor change the state of another pin which is connected to the temperature control unit of the Air conditioner.  This is what is actually happening in the base level.
In this example of Air conditioner, you can notice one difference in the type of signal.  When we press switch to change the temperature, a signal is given TO the processor.  When processor identifies that it is the signal to change the temperature, we receive signal FROM the processor.  So there are two functions for the pins.  One is to receive input from the outside world and the other is to give output to the outside world.  That is a pin can be used in two ways depending on its direction of flow of signal.  For that purpose we use the DDR - The 'Data Direction Register'.

In Atmega16/32, there are 32 input/output pins are available.  These are divided as PORTA, PORTB, PORTC and PORTD.  In our program the code is written as DDRB.  It represents the pins of PORTB.  The value in this register (Register is a memory space.  Here it contains 8 bits) determines which pins in PORTB are to be used as input and which are to be as outputs.  In this case the value of DDRB is declared as 0b11111111 (8 Bits).  It means all the pins (PORTB has 8 pins) are used as output.  For making these pins input, the value 0b00000000 can be used.  We can make some pins as inputs and some as outputs in a single PORT by adjusting this value.  1 defines an output pin and 0 defines an input pin.

We made all the pins as output because we are connecting LEDs to these pins and our aim is to turn on these LEDs.  So it is an output from the processor.

PORTB=0b11111111;
We have defined the direction of pins as outputs.  Now it is time to think about what values those pins should actually give.  As per the circuit, we need a HIGH (1) output to turn the LED on.  For that purpose the command PORTB=0b11111111 is used.  Here the value 1 will provide a HIGH (about 5V) output in that pin.  So the code here says the processor to make all the pins to HIGH Value.  So when the processor gets turned on, all the pins in PORTB will provide a 5V output.

That's it!  A working program is ready now!  Now a little bit more.  Beginners may feel a main difference between the programs in embedded C and normal C and it is, we do not have a start and stop in embedded.  The program is expected to be working whenever the processor is ON.  But in the case of normal computer programs, we are expected a start and a stop of the program.

Note:  The program can be simulated using the software AVR Studio which can be downloaded from the site of Atmel for free.  The complete details about installing tools, using AVR Studio for creating, compiling, debugging program etc. can be downloaded by clicking here.  It gives a step by step and complete information needed to work with the softwares.

4 comments:

  1. Next session is expected to be posted on or before 25th June.

    ReplyDelete
  2. Da its interesting..some points from my side.
    Syntax of function declaration is

    return_type function_name (parameters)

    so return type specifies whether a function returns a value or not..in the case of the main function its too odd to return a value..
    hence we usually use return type as void, like
    void main (void)

    in case you declare it with any other data type like int then the compiler will expect a return value.. in those cases better add a return 0;
    to the end of the program..Like
    #include

    int main(void){
    DDRB=0xFF;
    PORTB=0xFF;
    return 0;
    }

    or like

    #include

    void main(void){
    DDRB=0xFF;
    PORTB=0xFF;
    }
    also a kunj sujjustn..da try to include a program fully.then step by step explain it in the same post..my led is still ON..nt blinking yet.. Nice effort

    ReplyDelete
  3. I didn't forget about the return type. But avoided it purposefully to avoid complexity. And also the program is full. If you burn that to your processor, it will work. Right?

    ReplyDelete
  4. sure it works..nt blinking.ki ki
    Nice effort man

    ReplyDelete