Open Demo project.mcu8051ide in Windows C:\Program Files (x86)MCU 8051 IDE\demo. Linux has this same demo code in /usr/share/mcu8051ide/demo.
Go to the last of the ten code samples LCD.asm. We are in editor Read Only mode so we cannot change code. The demo uses an already compiled .hex file.
Run the simulator in the MCU8051IDE program. Hover over the buttons and read the controls. Try starting and stopping, running, pausing and stepping. Animate program lets us see inside the processor but it runs slower.
Run program means running the program, animate adds visualization. Changing code means stopping the simulator and recompiling.
Performance PenaltyGenerating these images takes processor power. It will slow your computer down. Tick box if you do not want to see again.
The simulator is not real time, look in the bottom corner of the simulator at the Time value. Using this code on a real circuit needs you to add delays.
Full SimulationTo really see what is happening we add simulated hardware. This display simulates the HD44780 LCD display that works with stored dot matrix images of ASCII characters.
Green and red show the voltages going to our display. Look closely and you will see the display has port and bit settings to select device pins.
Virtual Hardware MenuThis is great! We can add virtual hardware circuits from our Virtual HW menu. Our simulated hardware circuits can be saved and moved to other computers.
We have a ready made virtual hardware component in the folder with our sample code. Menu Virtual HW->Open-> navigate to C:\Program Files (x86)MCU 8051 IDE\demo and open LCD.vhc.
Click ON/OFF, leave ON. Click the wrench configuration icon. Try all the features. Window always on top can be convenient for our project. Expect errors if you try to save changes to this read-only file.
If you have an 8051 and programmer device you are almost ready to upload. Look at all the sample code and you see disclaimers about delays. Our code samples have eliminated delay loops needed to interface with the real world.
Look at the code, pin 3.0 is RS, 3.1 is RW and 3.2 is En signal to our display. 8 pins of PortP1 are wired to pins D0 to D7 of the screen. These pins are all on the same side of an 8051 chip. Reset restarts program in flash.
We can change the code to use other ports and pins. Then we need to change the wiring, too. Imagine switching the wires to Port0 and Port2.
An Arduino project using this display LCD Devboard describes the functions of the RS register select, RW ready/wait and En enable leads..
What the code doesLet's watch. The simulator shows us what's going on inside. Watch a millisecond go by. Remember, pins on P1 and P3 are carrying current.
Little electric voltages are sent to display pins RS, RW and En on IO port P3 to control the display and send characters to print. In a loop the processor pushes bytes out of IO port P1.
HD44780 displays already contain image maps to show when told to print ASCII characters (American Standard Code for Information Interchange).
Assembly CodeAssembly code is a series of direct instructions to the processor. This demo starts at organizing flash data from address zero. At this level bits are turned on and off.
We assign names RS, RW and E to our control pins. Since P3 is the 8 pin IO port we are working on bits 0 to 2 of the byte. D is an entire byte and it is register P1.
cmd macroTalking to the display module is a repetitive action and this code uses a macro as a routine to toggle the E and D values of the display. Where you see cmd in our source code the assembler program uses the 4 instruction lines.
string:The character array string: is stored in the memory of the 8051 chip. \0 makes the last letter 0b00000000 the null character. You can see this string stored in the .hex file and the flash of the running simulator.
start:The program begins a loop at label start: and the data byte D going to our display will be 0. RW will be 0. We make a data pointer DPTR load the starting address of our array #string.
main: loop. main: begins by clearing the display and setting cursor position. The program does this once for each loop. First command is to clr RS to zero.
cmd is our macro, from earlier. The byte values you see are pushed out through P1.
print: loopA character array string: is stored in the memory of the 8051 chip. The bytes are ASCII values for whatever string we use in our demo. The assembler program knows ASCII, too. Look hard and you can find hex values for our text in the hex and bin files.
Inside and Outside LoopsThis should explain everything. Watch how main: begins with initialization of the screen, clearing, setting cursor position. Commands are executed consecutively until the print: loop where our ASCII array character string is printed, one letter at a time.
A is our current character in the array string:. DPTR points to our current character. DPTR is incremented to step through the string.
cjne means compare jump if not equal, when A has a value of 0 it is the null value, end of the string.
sjmp main sends the program back to main: in a simple jump. Closing the big loop.
LCD.asm Assembly CodeWe will use the simulator to see how this code actually works in detail. An array of ASCII characters is pushed into a display module. A reset of the display and repeat.
; Very basic demonstration example for HD44780 simulator
; * Click on "Virtual HW" in the main menu,
; * choose "Open",
; * change filter to VH Component,
; * open "LCD.vhc",
; * press F2,
; * press F6,
; * enjoy ... :-)
; * press F2 to end.
;
; Note: simulated delays are skipped in this example.
;
org 0
jmp start
RS bit P3.0
RW bit P3.1
E bit P3.2
D equ P1
cmd macro cmd_code
setb E
mov D, cmd_code
clr E
endm
string: db 'Welcome to MCU 8051 IDE!\0'
start: mov D, #0
clr RW
mov DPTR, #string
main: clr RS
cmd #00000001b ; Clear display
cmd #00000010b ; Cursor home
cmd #00000110b ; Entry mode set
cmd #00001111b ; Display ON/OFF control
cmd #00011110b ; Cursor/display shift
cmd #00111100b ; Function set
cmd #10000001b ; Set DDRAM address
; Print the string ...
setb RS
mov R0, #0
print: mov A, R0
inc R0
movc A, @A+DPTR
cmd A
cjne A, #0, print
sjmp main
end
Check Project FolderEach of the demos has more than one file. Some you can open with notepad and edit.
The assembly code file .asm is ordinary text. We can edit it with notepad. The assembler program has created a hex file file that we can upload to a chip and run.
Error Messages and MoreIn project SDCC C Language Demo for mcu8051ide we go much deeper into the program.
Comments
Please log in or sign up to comment.