Sunday, January 16, 2011

3.2 Sending Characters [2-line-using pointer]

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!


3. LCD Interface

This section teaches you how to control LCD connected to output port of a microcontroller chip.

3.2 Sending Characters [2-line-using pointer]


This program displays “Hi!” on LCD screen continuously. Here LCD is configured for two lines and both lines are used for displaying message. Note that R/W [Read/Write] pin is connected permanently to ground [in hardware] as always write operation is used in this program. When key press detected it shows different message and when no key press [default] program shows welcome               message. The pointers are used to store string messages and retrieved using while loop structure.

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

Code explanation:
Line 09 to 12: The string messages are assigned to pointer variables.
Line 21: The Switch pin is made input pin.
Line 22: The LCD is initialized.
Line 23: This statement says run this program infinite times.
Line 25: The if control structure checks whether switch is pressed, if yes executes its code block.
Line 28: This statement assigns “WELCOME 2 SYSTEM” string to transmitter pointer.
Line 29: This lcd_data() function sends above string to LCD.
Line 31: This command forces cursor to beginning of second line of LCD.
Line 32: This statement assigns “PRESS ANY KEY” string to transmitter pointer.
Line 33: This lcd_data() function sends above string to LCD.
Line 38: This statement assigns “KEYPRESS DETECTED” string to transmitter pointer.
Line 39: This lcd_data() function sends above string to LCD.
Line 41: This command forces cursor to beginning of second line of LCD.
Line 42: This statement assigns “2nd LINE MSG” string to transmitter pointer.
Line 43: This lcd_data() function sends above string to LCD.
Line 53: This lcd_init() function initializes the LCD.
Line 35: This lcd_cmd() function sends parameter commands to LCD.
Line 46: This lcd_data() function sends parameter data to LCD.
Line 57: This delay() function supplies delay between each command execution.

Below is the flow chart diagram of program PGM_32.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. The LCD is connected to Port 1. So we want to watch the Port 1 pins along with Port 2 & Port 3, while running this program for changing values. To get Port 1 tab click Peripherals->I/O Ports->Port 1 menu and repeat this for Port 2 & Port 3 also. 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 LCD port bit are showing welcome message in binary form. Now press Set SWITCH button on ToolBox. Port 1 bits shows second set of messages. To turn off these messages press Clr SWITCH 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 3.2   Sending Characters [2-line-using pointer]>


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

No comments:

Post a Comment