Thursday, January 6, 2011

2.2.9 Relay Controlled Device ON/OFF program [2-bit manipulation]

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.9   Relay Controlled Device ON/OFF program [2-bit manipulation]


This little bit complex program allows user to control three output port connected relays using two switches. This program is basically a mini-project and to understand it fully please have closer look on source code and flow chart diagram. 

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_229.C and add it to above created project as a source file. It is time to add code to Pgm_229.C, so type below shown code in it.
 
Code explanation:
Line 06, 07 & 08: Declaring the two keys KEY1, KEY2 & a buzzer respectively.
Line 09, 10 & 11: Declaring the three realys RL1, RL2 & RL3 respectively.
Line 19: Initializing Port 2 as input port as it is connected with keys.
Line 20: Initializing Port 3 as output port as it is connected with keys.
Line 21: This statement creates infinite loop.
Line 23: The variable z is stored with Port 2 value, i.e., KEY press value.
Line 24: The input port Port2 values are masked with 0x03 to get last two digits which represent the key press.
Line 25: This switch() control structure makes relays ON or OFF with respective to z value.

Below is the flow chart diagram of program PGM_229.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 two input pins and four output pins in port2 & port3. So we want to watch the Port 2 & Port 3 pins while running this program for changing values. To get Port 2 tab click Peripherals->I/O Ports->Port 2 menu and for Port 3 tab click Peripherals->I/O Ports->Port 3 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 3 bits are all zero. Now press Reset KEY1 button on ToolBox. Port 3 bits changes to 0000 0100 [that means RELAY2 is ON] and Buzzer turns ON.  Next press Reset KEY2 button on ToolBox. Port 3 bits changes to 0000 1100 [that means RELAY2 & RELAY 3 are ON] and Buzzer remains ON. Now press Set KEY1 button on ToolBox. Port 3 bits changes to 0000 1110 [that means RELAY1, RELAY2 & RELAY3 are ON] and Buzzer remains ON. Lasttly press Set KEY2 button on ToolBox. Port 3 bits will get reset to 0000 0000 and buzzer turns OFF. 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.9 Relay Controlled Device ON/OFF program [2-bit manipulation]>
 
...till next post bye-bye & take care.

No comments:

Post a Comment