8051液晶的'Hello World' - 可变取代DB [英] 8051 LCD 'Hello World' - replacing DB with variable
问题描述
我使用的是专有的8051板学习汇编编程。我目前工作的一个LCD的'Hello World程序上。这里的code。
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
一切工作正常。然而,我无法输出到LCD的变量。更换#mesg1用十六进制值(ASCII让事情变得简单)简单地调出屏幕上的乱码。所以不调用子程序这只是增加每次价值,所以我不能确定时,它的移动到DPTR应该是什么格式的数据。
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?
谢谢!
推荐答案
DPTR
包含文本的地址
显示。所以,如果你替换#mesg1
的东西,如
dptr
contains the address
of the text to display. So, if you replace #mesg1
with something like
mov dptr, #045h
你在地址×45,这也解释了您看到乱码输出从内存(随机)内容。
you are outputting the (random) contents from memory at address 0x45, which explains the scrambled characters you see.
为了输出部分十进制值,则需要先将其转换成ASCII字符串,那么你可以使用现有的 wr_string
程序来把它打印出来。见的http://www.electro-tech-online.com/microcontrollers/14371-hex-decimal-then-ascii.html为code样品(I,J,K包含结果字符串,你仍然需要为您的日常wr_string零终止)。
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).
以下code显示了类似的程序。
需要注意的是 wr_string
需要修改读取XDATA数据,而不是从code内存( MOVX A,@dptr
而不是 CLR A
/ MOVC A,@ A + DPTR
)
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 of clr a
/ movc a, @a+dptr
):
ORG 08100h
hello:
mov r7, #42 ; value to convert
mov dptr, #buffer ; destination buffer
acall str2ascii ; convert value
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, #buffer ; point to buffer
acall wr_string ; write buffer to LCD
...
str2ascii:
; Converts a one byte decimal value into its ASCII string representation.
; Result is prepended with leading zeroes.
; 0 becomes "000"
; 42 becomes "042"
; 255 becomes "255"
;
; @param r7 Input value to convert (1 byte, 0 .. 255)
; @param dptr Destination buffer, at 4 bytes (3 digits plus \0)
;
mov a, r7
mov b, #100
div ab ; leftmost digit in a
add a,#30h ; convert to ASCII
movx @dptr, a
inc dptr
mov a,b ; get reminder
mov b,#10
div ab ; middle digit in a, rightmost digit in b
add a,#30h ; convert to ASCII
movx @dptr, a
inc dptr
mov a,b
add a,#30h ; convert to ASCII
movx @dptr,a
inc dptr
mov a,#0
movx @dptr, a ; terminate string
ret
xseg
buffer: ds 17 ; one LCD line plus terminating \0
end
这篇关于8051液晶的'Hello World' - 可变取代DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!