Tuesday, January 4, 2011

2.2.8 Buzzer ON on KEY press [other approach]

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.8   Buzzer ON on KEY press [other approach]


This program teaches how to store key press in a memory bit and later it is used to control buzzer. This once again a buzzer driving program with memory bit facility.

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_228.C and add it to above created project as a source file. It is time to add code to Pgm_228.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 08: This statement declares MEMORYBIT as bit variable .
Line 17: This statement makes KEY pin as input pin.
Line 18: This statement creates infinite loop.
Line 20: The KEY pin status is stored in MEMORYBIT variable.
Line 21: The MEMROYBIT contents are inverted and stored in MEMROYBIT only. This step is necessary as KEY Press gives logic LOW and creates confusion when using any control structure such as if.
Line 22: The if control structure checks whether [KEY is pressed] MEMROYBIT is HIGH, and if yes then calls Buzzer() function. Note that when KEY is pressed the pin voltage will be grounded and becomes LOW. Hence this status is inverted in code Line 21.
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_228.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_228. Now open PGM_228 project in KEIL V4.0 and remove the source file PGM_224.C and add this PGM_228.C file to it. Now compile the project and run the program in debug mode. For program verification steps please refer 2.2.8 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.8 Buzzer ON on KEY press [other approach]>
 
...till next post bye-bye & take care.

No comments:

Post a Comment