Thursday, November 11, 2010

2.2.3 Single LED Blinking with a KEY press

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.3   Single LED Blinking with a KEY press


This program blinks single LED when KEY is pressed and thus shows how to manipulate single bit of microcontroller chip. Here LED bit is configured as output pin and KEY bit as input pin.
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_223.C and add it to above created project as a source file. It is time to add code to Pgm_223.C, so type below shown code in it.
 
Code explanation:
Line 08 & 09: Declaring 0th & 1st bit of Port 2 as LED & Key bits respectively.
Line 17: This statement makes KEY pin as input pin.
Line 18: This statement creates infinite loop.
Line 21: The if control structure checks whether KEY pin is LOW, and if yes makes LED pin HIGH. Note that when you press KEY the pin voltage becomes low.
Line 26: This else code block executes if KEY pin is not LOW, or it is HIGH. Note that when key is not pressed, +Vcc flows to KEY pin and thus acts as logic HIGH.

Below is the flow chart diagram of program PGM_223.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. You can burn the hex file generated by this program for hardware simulation and for software simulation we need below program.

Note that this program needs user input i.e., KEY press for software simulation. So download already compiled and crated project folder by clicking here. Open the downloaded project in KEIL V4.0 and enter into debug mode. When you enter into debug mode your debug windows looks like below screen shot.
 


To enter into debug mode press Ctrl+F5 or click Debug->Start/Stop Debug Session menu. Our program is having one input pin [P2^1] and one output pin [P2^0] in port2. So we want to watch the Port 2 pins while running this program for changing values. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu. Here we need ToolBox for inputting key press signals, and you get it by clicking View->Toll Box Window menu. Now your debug windows looks like above screen shot.

Run the program by pressing F5 or clicking Debug->Run. You can see initially Port 2 KEY bit is high and still LED is OFF. Now press KEY PRESSED button on ToolBox. Port 2 KEY bit changes to low and LED becomes HIGH or ON. To turn off the LED press KEY NOT PRESSED button from ToolBox. Thus we verified that our program will work fine with hardware interface. 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.3 Single LED Blinking with a KEY press >
 
...till next post bye-bye & take care.