Monday, October 11, 2010

2.1.1 Blinking LEDs sequentially

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 bytewisely.
 
2.1.1 Blinking LEDs sequentially

The first program you are going to write is showing 0 to 255 numbers, in binary form on output port P2. 

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


Code explanation: The program uses for loop structure [line 08] to send numbers 0 to 255 [0 to FFxh] to Port 2. The variable used [line 07] to send is of type unsigned char as it needs one byte memory only [where as int needs 2 bytes]. Inside for loop z, a unsigned char variable is initialized to zero [line 08]. The ‘z<=255’ instruction checks whether z is less than or equal to 255 or not. If it is then zero is send to Port 2 [line 10] and increments z. Now z is 1 and after validation is send to Port 2. Again z is incremented, and above steps is repeated. When z value reaches 256, validation goes wrong, as it is not less than or equal to 255 [z is greater than 255] and hence for loop terminates [line 11]. Since there are no other codes to executes [line 12], compiler reinitializes the for loop [line 08] and continues the same thing repeatedly.

Below is the flow chart diagram of program PGM_211.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 continuously. 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 text.

Run the program by pressing F5 or clicking Debug->Run. You can see the changing values from 0 to 255 on Port 2 tab 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 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.1 Blinking LEDs sequentially >

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

No comments:

Post a Comment