Doc Format - Lane Department of Computer Science and Electrical

advertisement
WEST VIRGINIA UNIVERSITY
Department of Computer Science and Electrical Engineering
MICROPROCESSOR SYSTEMS LABORATORY
LABORATORY #8
CpE 311
Spring 2004
Alphanumeric input and output
Introduction:
In this lab you are going to use DOS interrupts to input alphanumeric information from the keyboard
and output it to the screen. Besides, you will have to implement a driver for the Liquid Crystal Display
(LCD) and use the LCD as a secondary output device.
Please carefully study the attached datasheet for the LM2022 LCD, LCD initialization sequence
and description of the DOS interrupt calls (p.233-p.247)!
Exercise 1:
Write a program that continuously polls the keyboard and outputs the received information to the
screen. Any allowed character should be displayed immediately after it’s been pressed on the keyboard.
You do not need to store the characters in computer’s memory, just display them.
The program should react to the following keys:
- A to Z (case sensitive) should be displayed on the screen
- 0 to 9 should be displayed on the screen
- ENTER should move the cursor at the beginning of the new line
- ESC should terminate the program
- ALL OTHER KEYS SHOULD HAVE NO EFFECT AND CANNOT BE DISPLAYED
- The program should display the message “Good Bye!” before exiting
Demonstrate your work.
Exercise 2:
Complete the program given below. After being completed, this program should display the message
“HELLO WORLD!” on the LCD. Carefully read the supplied explanations, the datasheet and follow the
comments in the code. The comments in the code also contain information necessary for the
completion of the exercise and detailed assignments!
Your LCD is connected to the KAD (Keypad and Display) board through the J3 connector. Make sure
that the cable's red wire is matched to the arrow mark on the KAD board. The whole programming through
this connector is related to the 'A' I/O port of the Octagon. This means that your program can only use port
A for LCD output.
;LCD driver and example
.MODEL TINY
.CODE
ORG 100H
START:
jmp BEGIN
;skip the data and subroutines
EON
EOFF
equ 01000000b
equ 10111111b
;your comment here
;your comment here
;in the comments explain, why these particular values
;have been used
RSDATA
RSINSTR
equ 00010000b ;sets RS to 1 (data) when ORed
equ 00000000b ;does not set RS to 1 (data) when ORed
;(keeps instruction mode)
RS
db
RSINSTR
MSG
db
"Hello World!",0
;a variable that defines LCD mode
;(instructions or data)
;ASCIIZ message to be printed
PAUSE300US PROC NEAR
;This subroutine should be provided by you
;The delay time is 300us20%
PAUSE300US ENDP
PAUSE3MS PROC NEAR
;This subroutine should be provided by you
;The delay time is 3ms20%
PAUSE3MS ENDP
PAUSE20MS PROC NEAR
;This subroutine should be provided by you
;The delay time is 20ms20%
PAUSE20MS ENDP
;
;
:
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
In the utilized 4-bit configuration, all data exchange with the LCD
is performed in 4-bit chunks of data – nibbles. To send a byte, two
nibbles should be sent to the LCD.
The data exchange protocol is defined by the timing diagrams
given in the LCD datasheet. The timing diagram specifies temporal relation
between data and such signals as RS (determines if current byte contains
an instruction for the LCD or data to be displayed), R/W (read or write)
and E (enables the data exchange). The following subroutine performs the
basic data send operation – sends a nibble of data (instruction or data
as determined by RS line) to the LCD
The connections from port A to the LCD are as follows:
+-----+-----+-----+-----+-----+-----+-----+-----+
! PA7 ! PA6 ! PA5 ! PA4 ! PA3 ! PA2 ! PA1 ! PA0 !
+-----+-----+-----+-----+-----+-----+-----+-----+
!
! E ! R/W ! RS ! DB7 ! DB6 ! DB5 ! DB4 !
+-----+-----+-----+-----+-----+-----+-----+-----+
For example, any information sent to the LSB of port A (PA0) will be
sent to the bit 4 input of the LCD (DB4)
;
;
;
;
;
;
;
;
This subroutine sends a nibble to the LCD
The nibble to be sent should be in the AL
lower nibble is sent, higher nibble is lost after the call
Given the exact delay values that you obtained in exercise 2
restore the timing diagram provided by this subroutine.
Include this timing diagram in your report. Check if it fits the
timing requirements given in the LCD datasheet. Explain how such
signals as RS, R/W and E are handled.
OUTPUTNIBBLE PROC NEAR
;In the comments, explain not WHAT is done, but WHY it is done, i.e.
;and al,0FH -> perform logical AND between AL and 0FH <- WRONG ANSWER
;and al,0FH -> clear higher nibble of AL <- RIGHT ANSWER
push ax
push dx
;preserve DX (will be used for I/O)
and al, 0FH
;clear higher nibble of AL (contains RS, R/W and E)
or al,[RS]
; your comment here
mov dx,140H
; your comment here
out dx,al
; your comment here
call pause300us ; your comment here
or al,eon
; your comment here
out dx,al
; your comment here
call pause300us ; your comment here
call pause300us
and al,eoff
; your comment here
out dx,al
; your comment here
call pause3ms
; your comment here
pop dx
; your comment here
pop ax
; your comment here
ret
OUTPUTNIBBLE ENDP
;The following subroutine outputs a byte by calling OUTPUTNIBBLE twice
;The higher nibble is sent first, lower nibble – second
;The byte to be output should be in AL
;Instruction or data mode is determined by sending RSDATA or RSINSTR to RS
;variable
OUTPUTBYTE PROC NEAR
push ax
push bx
; preserve BX (BL is used for temp storage)
push cx
; preserve CX (used for shift operation)
mov bl,al
;save lower nibble of the byte in reg, BL
mov cl,4
;setup the shift for 4 bits
shr al,cl
;move AL 4 bits to the right (output higher nibble first)
call outputnibble
;send to the LCD
mov al,bl
;get the lower nibble from BL
and al,0FH
;remove higher nibble of AL
call outputnibble
;send to the LCD
pop cx
;restore CX
pop bx
;restore BX
pop ax
ret
OUTPUTBYTE ENDP
; Before the LCD can be used it should be initialized.
; The initialization procedure has certain timing
; and defines the operation parameters. Check the appendix for
; the description of the initialization procedure.
; Complete the procedure given below, comment each step and each value
; that you use
LCD_INIT PROC NEAR
push ax
mov al,0
mov dx, 140H
out dx,al
call pause20ms
;zero all output lines
;on port A
;perform I/O
;wait 20 ms
mov [rs],RSINSTR
;switch to the LCD instruction mode
mov al,00000011b
call outputnibble
;send 1st init. nibble (03)
;to the LCD
; Continue here
mov [rs], RSDATA
pop ax
ret
;switch LCD to the data mode
LCD_INIT ENDP
;LCD has functionality to start output at any location on the display
;This function will set where the next character will be displayed
;It does not check if the supplied positions are within physical limits,
;therefore the programmer should take care of that.
; AH should contain Y coordinate, AL should contain X coordinate
LCD_GOTOXY PROC NEAR
push ax
push bx
;preserve BX
mov [rs],RSINSTR
;switch to the LCD instruction mode
mov bl,0
;line 1 starts at address 0 (LCD data memory)
cmp ah,0
;check if trying to goto to line 1
jz line1
;if not line 1, then set 40H to the address
mov bl,40H
;line 2 starts at address 40h (LCD data memory)
line1: add al,bl
;determine full address
;(line address + character position)
or al, 80H
;specify goto command
call outputbyte
;send to the LCD
mov [rs],RSDATA
;switch back to the data mode
pop bx
;restore bx
pop ax
ret
;return
LCD_GOTOXY ENDP
BEGIN:
mov ax,cs
mov ds,ax
mov
mov
out
mov
mov
out
mov
mov
out
al,2
dx,143H
dx,al
al,0FFH
dx,140H
dx,al
al,3
dx,143H
dx,al
call lcd_init
; main program
; initialize data segment
;
;
;
;
;
;
;
;
;
switch to DIRECTION mode
access CONTROL register
perform I/O
all pins will be outputs
access port A
perform I/O
switch to OPERATION mode
access CONTROL register
perform I/O
;initialize LCD
;Comment the following lines
;Make sure you explain WHY each instruction is there
mov cx, 2
;your comment here
twice: lea si,msg
;your comment here
mov al, 0
;your comment here
mov ah, cl
;your comment here
dec ah
;your comment here
call lcd_gotoxy
;your comment here
outch: mov al,[si]
;your comment here
cmp al,0
;your comment here
je exit
call outputbyte
inc si
jmp outch
exit:
loop twice
mov ah, 4ch
int 21h
;your
;your
;your
;your
comment
comment
comment
comment
here
here
here
here
;your comment here
;exit to DOS
END start
.ENDS
Grading table
Weight
A Excellent
B Good
C Average
D Fair
F Poor
Demonstration
of the working
code
25%
All 4 exercises
work properly
during the
allotted time.
Only 2 out
of 4 work.
25%
Correct source
code with no
hidden errors
Everything
works in more
than one week
after the due
date or all
exercises work
with some
errors.
Working code
with errors
Exercises 1, 2
and 3 work
Source code
Partially
working code
Not working
code
Comments on
the source code
25%
Misleading
comments
Comments do
exist in the code
No
comments or
excessively
poor
comments
Documentation
and Report
25%
Clear
comments
explaining
logic structure
of the program
and meaning of
each important
instruction.
Clear,
excellent
organization,
well
documented,
and neat,
All questions
answered
correctly and
to full extent.
All 4
exercises
work
properly in
extended
time (before
the next lab
session)
Correct
source code
with
acceptable
errors
Good
comments
but not
always clear
Good
organization
but not
always clear
that writer
understands
the solution.
The answers
are correct
but contain
omissions in
the
explanations.
OK
organization,
documentation
somewhat
confused. The
answers are
more 75%
correct
Poorly
organized, hand
written, poorly
put together.
More than 50%
correct answers.
No
organization.
Less than
50% correct
answers.
Appendix
DOS provides the mechanism of software interrupts to access the functions provided by the operating
system.
To get service from DOS, the numeric index of the desired function is placed in the register AH and int
21h is generated. An interrupt call may take input parameters or return information in other registers.
Example:
Function 4Ch - "EXIT" - TERMINATE WITH RETURN CODE
Parameters: AL = return code
Return: never returns
To return to DOS use the following assembly code:
mov ah, 4CH
int 21H
Some Input/Output DOS functions
Function 01h - READ CHARACTER FROM STANDARD INPUT, WITH ECHO
Return: AL = character read
Function 02h -WRITE CHARACTER TO STANDARD OUTPUT
Parameters: DL = character to write
Return: AL = last character output
Function 08h - CHARACTER INPUT WITHOUT ECHO
Return: AL = character read from standard input
Function 09h - WRITE STRING TO STANDARD OUTPUT
Parameters: DS:DX -> '$'-terminated string
Return: AL = 24h
LCD initialization procedure in 4-bit mode
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Power ON
Wait more than 15ms
Send RS R/W DB7 DB6 DB5 DB4
0
0
0
0
1 1
Wait more than 4.1ms
Send RS R/W DB7 DB6 DB5 DB4
0
0
0
0
1 1
Wait more than 100us
Send RS R/W DB7 DB6 DB5 DB4
0
0
0
0
1 1
Send RS R/W DB7 DB6 DB5 DB4
0
0
0
0
1 0
Send “Function set” instruction. N=1, F=0
Send “Display On” instruction
Send “Clear display instruction”
Send “Entry mode set” instruction
Initialization complete
Note: Delay between consecutive instructions should exceed the execution time (see the instruction set
summary).
HD44780 instruction set
Instruction
Code
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
Clear display
0
0
0
0
0
0
0
0
0
1
Cursor home
0
0
0
0
0
0
0
0
1
*
Entry mode set
0
0
0
0
0
0
0
1
I/D
S
Display On/Off
control
0
0
0
0
0
0
1
D
C
B
Cursor/display
shift
0
0
0
0
0
1
S/C
R/L
*
*
Function set
0
0
0
0
1
DL
N
F
*
*
Set CGRAM
address
0
0
0
1
Set DDRAM
0
0
1
CGRAM address
DDRAM address
Description
Clears display and returns
cursor to the home position
(address 0).
Returns cursor to home
position (address 0). Also
returns display being shifted
to the original position.
DDRAM contents remains
unchanged.
Sets cursor move direction
(I/D), specifies to shift the
display (S). These operations
are performed during data
read/write.
Sets On/Off of all display (D),
cursor On/Off (C) and blink of
cursor position character (B).
Sets cursor-move or displayshift (S/C), shift direction
(R/L). DDRAM contents
remains unchanged.
Sets interface data length
(DL), number of display line
(N) and character font(F).
Sets the CGRAM address.
CGRAM data is sent and
received after this setting.
Sets the DDRAM address.
Execution
time**
1.64mS
1.64mS
40uS
40uS
40uS
40uS
40uS
40uS
HD44780 instruction set
Instruction
Code
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
address
Read busy-flag
and address
counter
Write to
CGRAM or
DDRAM
Read from
CGRAM or
DDRAM
BF
CGRAM / DDRAM address
1
1
0
write data
Writes data to CGRAM or
DDRAM.
40uS
1
1
read data
Reads data from CGRAM or
DDRAM.
40uS
- DDRAM = Display Data RAM.
- CGRAM = Character Generator RAM.
- DDRAM address corresponds to cursor position.
- * = Don't care.
- ** = Based on Fosc = 250KHz.
LCD. Bit names
Settings
0 = Decrement cursor
position
S 0 = No display shift
D 0 = Display off
C 0 = Cursor off
B 0 = Cursor blink off
S/C 0 = Move cursor
R/L 0 = Shift left
DL 0 = 4-bit interface
0 = 1/8 or 1/11 Duty (1
N
line)
F 0 = 5x7 dots
0 = Can accept
BF
instruction
I/D
DDRAM data is sent and
received after this setting.
Reads Busy-flag (BF)
indicating internal operation is
being performed and reads
CGRAM or DDRAM address
counter contents (depending
on previous instruction).
Execution
time**
0
Remarks:
Bit
name
Description
1 = Increment cursor
position
1 = Display shift
1 = Display on
1 = Cursor on
1 = Cursor blink on
1 = Shift display
1 = Shift right
1 = 8-bit interface
1 = 1/16 Duty (2 lines)
1 = 5x10 dots
1 = Internal operation in
progress
0uS
Download