I'm using a proprietary 8051 board to learn assembly programming. I'm currently working on an LCD 'Hello World' program. Here's the code.
lcd_cmd equ 0800h ;Write COMMAND reg address 0800h
lcd_st equ 0801h ;Read STATUS reg address 0801h
lcd_wr equ 0802h ;Write DATA reg address 0802h
lcd_rd equ 0803h ;Read DATA reg address 0803h
ORG 08100h
hello:
mov P2, #(lcd_cmd SHR 8) ;load P2 with high address
mov R0, #(lcd_cmd AND 255) ;load R0 with command reg addr
mov R7, #03h ;set LCD position, line=1, char=3
mov dptr, #mesg1 ;point to mesg1
acall wr_string ;write mesg1 to LCD
mov R7, #41h ;set LCD position, line= 2, char=1
mov dptr, #mesg2 ;point to mesg2
acall wr_string ;write mesg2 to LCD
stop: ajmp stop ;soft halt
wr_string:
acall lcd_busy ;wait until LCD not busy
mov a, R7 ;get LCD position
orl a, #080h ;msb set for LCD RAM address
movx @R0, a ;write lcd_cmd to set line & char
nxt_char:
acall lcd_busy ;wait until LCD not busy
clr a
movc a, @a+dptr
inc dptr ;point to next byte in string
jz str_end ;if 0 then end of string
mov R1, #(lcd_wr AND 255) ;Load R1 with wr_data address
movx @R1, a ;Write char to LCD
sjmp nxt_char ;get next char in string
str_end: ret
lcd_busy:
mov R1, #(lcd_st AND 255) ;Load R1 with status address
movx a, @R1 ;read LCD status
jb acc.7, lcd_busy ;keep checking until busy bit clear
ret
mesg1: db "Hello ",0
mesg2: db "World ",0
END
Everything works fine. However, I'm having trouble outputting a variable to the LCD. Replacing #mesg1 with a hex value (ascii to keep things simple) simply brings up scrambled characters on the screen. So does calling a subroutine which simply increments a value every time, so I'm unsure what format the data should be when it's moved into the dptr.
Anything stupid I've missed?
Thanks!
dptr
contains theaddress
of the text to display. So, if you replace#mesg1
with something likeyou are outputting the (random) contents from memory at address 0x45, which explains the scrambled characters you see.
In order to output some decimal value, you need to convert it to an ascii string first, then you can use your existing
wr_string
routine to get it printed. See http://www.electro-tech-online.com/microcontrollers/14371-hex-decimal-then-ascii.html for a code sample (i,j,k contains the result string which you still need to zero-terminate for your wr_string routine).The following code shows a similar routine. Note that
wr_string
needs to be modified to read the data from XDATA, not from code memory (movx a, @dptr
instead ofclr a
/movc a, @a+dptr
):On your code, dptr contains the address of your code memory which has the string you want to output. So, if you change #mesg1 to some hex value, like this:
LCD will try to write the ascii value which is on this hex address. And you don't know what it contains. In order to output variables on LCD (just like registers values), you should be attempted to:
1 - You can't store variable data on program memory with DB instruction. It doesn't work. You should write your variable value on internal or external data memory. For example, you can't do this:
In above example, LCD will only output MY STRING, independently the value of R1
2 - You need to convert the value of your variable (which will be on binary, decimal or hexadecimal) to ASCII. Just for 0 to 9, this is a very easy task. Just add to your variable the hexadecimal value 30H. So, for example:
Thus, an option is to split your variable into individual decimal numbers, and then convert to ascii every one. The code here do this, and stores each ascii value on variables i, j, k.
What I used to, is to use two functions: one for read strings from code memory, which was previously stored by DB instruction, and another to read variables values from internal memory:
The above code should output on LCD the string HELLO WORLD, followed by R1 value.