Wednesday, December 29, 2010

2.2.7 Intruder Detector

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.7   Intruder Detector


This program is same as PGM_226.C, only change is here instead of KEY we are using IRDetector module which is readily available in market. Hence this program acts as a mini project and alarms the user when any intruder is detected in its vicinity.

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



Code explanation:
Line 08 & 07: Declaring 0th & 1st bit of Port 2 as Buzzer & IRDectector bits respectively.
Line 16: This statement makes IRDetector pin as input pin.
Line 17: This statement creates infinite loop.
Line 18: The if control structure checks whether IRDetector pin is HIGH, and if yes i.e., intruder is detected then calls Buzzer() function. Note that when intruder is detected the IR Detector module sends HIGH signal to pin.
Line 21: This Buzzer() function generates intermittent sound when KEY is pressed.
Note: you can change the MSDelay() parameters from 25 to anything and hear different tones at buzzer output.

Below is the flow chart diagram of program PGM_227.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. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu.

Run the program by pressing F5 or clicking Debug->Run. Since this needs IRDetector’s intruder signal for simulation, you cannot simulate it fully. You can add this program file to PGM_224 project file, which is available in 2.2.4 section [or click here]. Copy the PGM_224 project folder and paste it once again and rename it as PGM_227. Now open PGM_227 project in KEIL V4.0 and remove the source file PGM_224.C and add this PGM_227.C file to it. Now compile the project and run the program in debug mode. For program verification steps please refer 2.2.4 section as they are same with slight change [instead of LED here you are driving Buzzer]. 

If you generate HEX file of this program, burn it into your target chip’s memory using suitable uploader and connecting IRDetector and Buzzer to Port 2 pins you can hear intermittent buzzer sound when intruder is detected. 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.

To get back the compile mode press Ctrl+F5 or click Debug->Start/Stop Debug Session menu.   
<End of 2.2.7 Intruder Detector >
 
...till next post bye-bye & take care.

Friday, December 17, 2010

2.2.6 LED & Buzzer ON on KEY press [intermittent buzzer ON]

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.6   LED & Buzzer ON on KEY press [intermittent  buzzer ON]


This program drives output device buzzer when KEY is pressed and thus shows how to manipulate single bit of microcontroller chip. Here Buzzer bit is configured as output pin and KEY bit as input pin. Since buzzers are frequently used in embedded systems to show the status of the system or alerting the user, this program is worth noted. In this program buzzer is generating intermittent sounds when key is pressed. It is necessary as embedded systems needs different tones for different tasks.

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_226.C and add it to above created project as a source file. It is time to add code to Pgm_226.C, so type below shown code in it.
 
Code explanation:
Line 08 & 07: Declaring 0th & 1st bit of Port 2 as Buzzer & KEY bits respectively.
Line 16: This statement makes KEY pin as input pin.
Line 17: This statement creates infinite loop.
Line 18: The if control structure checks whether KEY pin is NOT HIGH, and if yes i.e., LOW then calls Buzzer() function. Note that when KEY is pressed the pin voltage will be grounded.
Line 21: This Buzzer() function generates intermittent sound when KEY is pressed.
Note: you can change the MSDelay() parameters from 25 to anything and hear different tones at buzzer output.

Below is the flow chart diagram of program PGM_226.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. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu.

Run the program by pressing F5 or clicking Debug->Run. Since this needs user key press signal for simulation, you cannot simulate it fully. You can add this program file to PGM_224 project file, which is available in 2.2.4 section [or click here]. Copy the PGM_224 project folder and paste it once again and rename it as PGM_226. Now open PGM_226 project in KEIL V4.0 and remove the source file PGM_224.C and add this PGM_226.C file to it. Now compile the project and run the program in debug mode. For program verification steps please refer 2.2.4 section as they are same with slight change [instead of LED here you are driving Buzzer]. 

If you generate HEX file of this program, burn it into your target chip’s memory using suitable uploader and connecting switch and Buzzer to Port 2 pins you can hear intermittent buzzer sound when switch is pressed. 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.6 LED & Buzzer ON on KEY press [intermittent  buzzer ON]>
...till next post bye-bye & take care.

Sunday, December 5, 2010

2.2.5 Buzzer ON on KEY press [continuous buzzer ON]

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.5   Buzzer ON on KEY press [continuous  buzzer ON]


This program drives output device buzzer when KEY is pressed and thus shows how to manipulate single bit of microcontroller chip. Here Buzzer bit is configured as output pin and KEY bit as input pin. Since buzzers are frequently used in embedded systems to show the status of the system or alerting the user, this program is worth noted. 

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_225.C and add it to above created project as a source file. It is time to add code to Pgm_225.C, so type below shown code in it.
 
Code explanation:
Line 08 & 07: Declaring 0th & 1st bit of Port 2 as Buzzer & KEY bits respectively.
Line 15: This statement makes KEY pin as input pin.
Line 16: This statement creates infinite loop.
Line 18: The if control structure checks whether KEY pin is HIGH, and if yes makes Buzzer OFF. Note that when KEY is not pressed the pin voltage will be High.
Line 26: This else code block executes if KEY pin is LOW, or it is Pressed. Note that when key is pressed KEY pin voltage goes ground.

Below is the flow chart diagram of program PGM_225.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. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu.

Run the program by pressing F5 or clicking Debug->Run. Since this needs user key press signal for simulation, you cannot simulate it fully. You can add this program file to PGM_224 project file, which is available in 2.2.4 section [or click here]. Copy the PGM_224 project folder and paste it once again and rename it as PGM_225. Now open PGM_225 project in KEIL V4.0 and remove the source file PGM_224.C and add this PGM_225.C file to it. Now compile the project and run the program in debug mode. For program verification steps please refer 2.2.4 section as they are same with slight change [instead of LED here you are driving Buzzer].
If you generate HEX file of this program, burn it into your target chip’s memory using suitable uploader and connecting switch and Buzzer to Port 2 pins you can hear continuous buzzer sound when switch is pressed. 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.5 Buzzer ON on KEY press [continuous  buzzer ON]>
 
...till next post bye-bye & take care.

Tuesday, November 23, 2010

2.2.4 Single LED Blinking with a KEY press [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.2 Bitwise [single bit]

Now we see how to control port pins bitwise.

2.2.4   Single LED Blinking with a KEY press [code optimized]


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. Note that this is code optimized version of previous program PGM_223.C.

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



Code explanation:
Line 08 & 07: 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_224.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 LOW button on ToolBox. Port 2 KEY bit changes to low and LED becomes HIGH or ON. To turn off the LED press KEY HIGH 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.4 Single LED Blinking with a KEY press [code optimized]>
 
...till next post bye-bye & take care.

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.

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.