Saturday, October 30, 2010

2.2.2 Single LED Blinking [using define]

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!


2. Manipulating Port Pins

This section teaches you how to manipulate port pins bytewise and bitwise.

2.2 Bitwise [single bit]

Now we see how to control port pins bitwise.

2.2.2   Single LED Blinking [using define]


This program blinks single LED and thus shows how to manipulate single bit of microcontroller chip. Here define directive is used to define the status of LED, instead of 0 and 1 numbers as in previous program PGM_221.C. This improves the readability of the program and is the advantage find in embedded C language.

Start KEIL V4.01 and close projects, if any are open. Now start new project with Device -> Generic->8051 [all variants] for general 8051 family target chip. Add a new text file, name it Pgm_222.C and add it to above created project as a source file. It is time to add code to Pgm_222.C, so type below shown code in it.

Code explanation:
Line 04 & 05: Defining High & Low signals as ON & OFF respectively.
Line 07: Declaring single bit [0th bit] of Port 2 using sbit key word.
Line 13: Initializing Port 2 to 0x00.
Line 15: This statement creates infinite loop.
Line 15: Making LED connected to Port 2 pin 0 HIGH.
Line 16: 250 MilliSecond Delay.
Line 17: Making LED connected to Port 2 pin 0 LOW.
Line 18: 250 MilliSecond Delay.

Below is the flow chart diagram of program PGM_222.C.

After typing the code, press F7 or click Project->Build Target for building the source code. If there are no typical errors file will build successively and shows zero errors and zero warnings.
Now enter into debug mode by pressing Ctrl+F5 or click Debug->Start/Stop Debug Session menu. Our program is making/toggling LED connected to Port 2-pin 0 continuously by sending 0 & 1 repeatedly. So we want to watch the Port 2 0th pin while running this program for changing values. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu.

Run the program by pressing F5 or clicking Debug->Run. You can see the Port 2 0th pin toggles continuously. If you generate HEX file of this program, burn it into your target chip’s memory using suitable uploader and connecting an LED to 0th pin of Port 2 you can watch LED blinking. To stop running the program press red ‘x’ mark icon or click Debug->Stop.

To get back the compile mode press Ctrl+F5 or click Debug->Start/Stop Debug Session menu.   

<End of 2.2.2 Single LED Blinking [using define]>
...till next post bye-bye & take care.