Saturday, October 16, 2010

2.1.6 Toggling Port Pins [code optimized]

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.6 Toggling Port Pins [code optimized]

This program teaches you how to send byte data to any Input/Output ports. Here 0101 0101 and 1010 1010 are sending to Port 2. That means we are toggling Port 2 pins with values 0x55 and 0xAA. This program is code optimized as compared with previous program PGM_215. Note how toggling is achieved with single instruction. Note the code and flow chart diagrams for code optimization effects that mean they are drastically reduced to less number of codes and steps.  

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


Code explanation:
Line 09: Initializing Port 2 to 0x55.
Line 10: This statement creates infinite loop as condition is always true. This is necessary as 8051 is not having any Operating System and it never exits.
Line 12: The compliment of the Port 2 value is send to Port 2 itself. That means initially 0101 0101 or 0x55 [in hex] value is inverted [~ sign] as 1010 1010 or 0xAA and sent to Port 2. For second time 0xAA is inverted as 0x55 and sent to Port 2. This repeats infinitely.
Line 13: The 250 MilliSecond Delay.

Below is the flow chart diagram of program PGM_216.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 toggling between 0x55 and 0xAA numbers in binary form to Port 2 continuously. So we want to watch the Port 2 pins while running this program for toggling 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 toggling values between 0x55 and 0xAA 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.6 Toggling Port Pins [code optimized]>

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

Friday, October 15, 2010

2.1.5 Toggling Port Pins

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.5 Toggling Port Pins

This program teaches you how to send byte data to any Input/Output ports. Here 0101 0101 and 1010 1010 are sending to Port 2. That means we are toggling Port 2 pins with values 0x55 and 0xAA. 

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



Code explanation:
Line 10: This statement creates infinite loop as its all conditions are empty. This is necessary as 8051 is not having any Operating System and it never exits.
Line 12: The 0101 0101 or 0x55 [in hex] value is sent to Port 2.
Line 13: The 250 MilliSecond Delay.
Line 14: The 1010 1010 or 0xAA [in hex] value is sent to Port 2.
Line 15: The 250 MilliSecond Delay.

Below is the flow chart diagram of program PGM_215.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 toggling 0x55 and 0xAA numbers in binary form to Port 2 continuously. So we want to watch the Port 2 pins while running this program for toggling 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 toggling values between 0x55 and 0xAA 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 toggling 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.5 Toggling Port Pins >

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

Thursday, October 14, 2010

2.1.4 Blinking LEDs sequentially with smooth delay [using Function]

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.4 Blinking LEDs sequentially with smooth delay [using Function]

The previous two programs are designed to produce rough delay and now we will design approximately millisecond delay.  This MilliSecondDelay is used to produce pause between values transmission. Here also you are going to write a program which 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_214.C and add it to above created project as a source file. It is time to add code to Pgm_214.C, so type below shown code in it.



Code explanation: The program uses for loop structure to send numbers 0 to 255 [0 to FFxh] to Port 2 [line 11]. The variable used [line 09] to send is of type unsigned char as it needs one byte memory only [where as int needs 2 bytes]. Inside for loop [line 11] z, a unsigned char variable is initialized to zero. 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 13] 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 15]. Since there are no other codes to executes [line 16], compiler reinitializes the for loop [line 11] and continues the same thing repeatedly.

Inside a for loop MSDelay() function is added [line 14] after sending Port 2 value to keep some delay between value outputs. The compiler starts executing the code from top and when it comes to line 14 it stops as it does not know what it is. So it is necessary to declare or prototype the function before its use. In line 05 delay function is prototyped, that means it is declared before main() function [line 07] execution. 

The smooth millisecond delay rDelay() function gets its delay time in milliseconds from function caller or programmer as parameters [line 14]. The for loop of line 21 repeats the code lines 22 & 23 for itime times. Here doing nothing is indicated by semicolon ‘;’ as shown on line 23. The line 22 for loop iterates for 1275 times and contributes its fixed delay to function. Since there are no further codes to execute MSDelay() function comes out. Thus this MSDelay() function produces smooth delay of programmer defined milliseconds [which differs with chip and compiler type] duration.  

Below is the flow chart diagram of program PGM_214.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 menu.

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.4 Blinking LEDs sequentially with smooth delay [using Function]>

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

Wednesday, October 13, 2010

2.1.3 Blinking LEDs sequentially with rough delay [using for]

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.3 Blinking LEDs sequentially with rough delay [using for]

The third program you are going to write is showing 0 to 255 numbers, in binary form on output port P2 with rough delay using for loop. It is mandatory for a programmer to learn to write reusable optimized delay functions. This teaches you how to write rough delay using for loop structure and put it in suitable function.

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



Code explanation: The program uses for loop structure to send numbers 0 to 255 [0 to FFxh] to Port 2 [line 11]. The variable used [line 09] to send is of type unsigned char as it needs one byte memory only [where as int needs 2 bytes]. Inside for loop [line 11]  z, a unsigned char variable is initialized to zero. 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 13] 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 15]. Since there are no other codes to executes [line 16], compiler reinitializes the for loop [line 11] and continues the same thing repeatedly.

Inside a for loop rDelay() function is added [line 14] after sending Port 2 value to keep some delay between value outputs. The compiler starts executing the code from top and when it comes to line 14 it stops as it does not know what it is. So it is necessary to declare or prototype the function before its use. In line 05 delay function is prototyped, that means it is declared before main() function [line 07] execution. 

The rough delay rDelay() function contains unsigned int i variable. Now variable used is unsigned int as we need 2 bytes of value [not single byte]. This i is initialized to zero [line 20] and checked inside the for loop structure [line 22] for validation. Here i variable is checked whether it is less than or equal to 65435 or not. If not then just increment i and check for validation. Here doing nothing is indicated by semicolon ‘;’ as shown on line 22. These steps are repeated until i becomes 65435 and for next step for loop structure breaks out. Since there are no further codes to execute rDelay() function comes out. Thus this rDelay() function produces rough delay of some milliseconds [which differs with chip and compiler type] 
 
Below is the flow chart diagram of program PGM_213.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.3 Blinking LEDs sequentially with rough delay [using for] >


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

Tuesday, October 12, 2010

2.1.2 Blinking LEDs sequentially with rough delay [using If]

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.2 Blinking LEDs sequentially with rough delay [using If]

The second program you are going to write is showing 0 to 255 numbers, in binary form on output port P2 with rough delay. It is mandatory for a programmer to learn to write reusable optimized delay functions. This teaches you how to write rough delay using if control structure and put it in suitable function. 

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

Code explanation: The program uses for loop structure to send numbers 0 to 255 [0 to FFxh] to Port 2 [line 11]. The variable used [line 09] to send is of type unsigned char as it needs one byte memory only [where as int needs 2 bytes]. Inside for loop [line 11] z, a unsigned char variable is initialized to zero. 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 13] 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. Since there are no other codes to executes, compiler reinitializes the for loop and continues the same thing repeatedly.

Inside a for loop rDelay() function is added [line 14] after sending Port 2 value to keep some delay between value outputs. The compiler starts executing the code from top and when it comes to line 14 it stops as it does not know what it is. So it is necessary to declare or prototype the function before its use. In line 05 delay function is prototyped, that means it is declared before main() function [line 07] execution. 

The rough delay rDelay() function contains unsigned int i variable. Now variable used is unsigned int as we need 2 bytes of value [not single byte]. This i is initialized to zero [line 21] and checked inside the if control structure [line 22] for validation. Here i variable is checked whether it is less than or equal to 65435 or not. If not then just increment i and check for validation. These steps are repeated until i becomes 65435 and for next step if control structure breaks out. Since there are no further codes to execute rDelay() function comes out. Thus this rDelay() function produces rough delay of some milliseconds [which differs with chip and compiler type]  

Below is the flow chart diagram of program PGM_212.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.2  Blinking LEDs sequentially with rough delay [using If] >


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