Sunday, October 17, 2010

2.1.7 Blinking LEDs sequentially on 2 Ports [using If,else]

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.1 Bytewise [all port pins-8bits]

First we see how to control port pins bytewise.

  2.1.7 Blinking LEDs sequentially on 2 Ports [using If,else]

This program teaches how to distinguish the input values and send them to their respective output ports using if…else control structure. Here zero to 255 numbers are thrown using for loop continuously. The if…else control structure catches them : if it is below or equal to 125 [first half of 255-0 to 125] sends it to Port 2; otherwise sends value [second half of 255-126 to 255] to Port 3.

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_217.C and add it to above created project as a source file. It is time to add code to Pgm_217.C, so type below shown code in it.


Code explanation:
Line 10: Initializing Port 2 & Port 3 to 0x00.
Line 11: This statement creates infinite loop.
Line 14: This for loop throws 0 to 255 numbers infinitely.
Line 16: This if statement checks catched number, if it is less than or equal to125 sends it to Port 2.
Line 21: This else code block will be executed if catched number is greater than 125 and sends catched number to Port 3.

Below is the flow chart diagram of program PGM_217.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 sending 0 to 255 numbers in binary form to Port 2 and Port 3 continuously. So we want to watch the Port 2 & Port 3 pins while running this program for changing values. To get Port 2 & Port 3 tab click Peripherals->I/O Ports->Port 2 and Peripherals->I/O Ports->Port 2 menus.

Run the program by pressing F5 or clicking Debug->Run. You can see the changing values from 0 to 125 on Port 2 tab and 126 to 255 on Port 3 continuously in binary form. If you generate HEX file of this program, burn it into your target chip’s memory using suitable uploader and connecting 8 LEDs to Port 2 & 8 LEDs to Port 3 you can watch LEDs blinking in binary form. 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.1.7 Blinking LEDs sequentially on 2 Ports [using If,else]>


...till next post bye-bye & take care.

No comments:

Post a Comment