Search This Blog

Wednesday, November 2, 2011

LCD instruction in detail


INSTRUCTIONS
RS
R/W
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
DESCRIPTION
Clear display
0
0
0
0
0
0
0
0
0
1
Clear entire display
Return Home
0
0
0
0
0
0
0
0
1
-
Returns display to be shifted to original position
Entry mode set
0
0
0
0
0
0
0
1
1/D
S
Sets cursor move direction and specifies shift of display. Performed during data write and read
Display On/Off control
0
0
0
0
0
0
1
D
C
B
D – sets on off entire display.
C – cursor on off
B – blink of cursor position character
Cursor or display shift
0
0
0
0
0
1
S/C
R/L
-
-
Moves cursor and shifts display without changing DD RAM contents.
Function set
0
0
0
0
1
DL
N
F
-
-
DL – sets interface data length
L – number of display lines
F – character font
Set CG RAM Address
0
0
0
1

AGC




Sets CG RAM address. CG RAM data is sent and received after this setting
Set DD RAM Address
0
0
1


ADD




Sets DD RAM address. DD RAM address is sent and received after this setting
Read Busy Flag and address
0
1
BF


AC




Reads Busy Flag BF indicating internal operation is being performed and reads address counter contents
Write data CG RAM or DD RAM
1
0
WRITE DATA
Writes data into DD or CG RAM
Read data CG RAM or DD RAM
1
1
READ DATA
Reads data from DD or CG RAM

Tuesday, October 18, 2011

Simple LCD interfacing program with 8051 microcontroller

so we have three control pins to take care of.
so lets program using these 3 control pins.
; P2.0 EQU RS
; P2.1 EQU RW
; P2.2 EQU E
ORG 0000H    ;
MOV A, #38H    ; INITIATE LCD
ACALL COMMWRT    ;
ACALL DELAY    ;

MOV A, #0EH    ; DISPLAY ON CURSOR ON
ACALL COMMWRT    ;
ACALL DELAY    ;

MOV A, #01H    ; CLEAR LCD
ACALL COMMWRT    ;
ACALL DELAY    ;

MOV A, #84H    ; CURSOR AT LINE 1 POSITION 4
ACALL COMMWRT    ;
ACALL DELAY    ;

MOV A, #'A'    ; SEND ASCII DATA
ACALL DATAWRT    ;
ACALL DELAY    ;

AGAIN :
SJMP AGAIN    ;

COMMWRT:
MOV P1, A    ;
CLR P2.0    ; RS = 0 FOR COMMAND REGISTER
CLR P2.1    ; R/W = 0 FOR WRITE
SETB P2.2    ; E = 1 FOR HIGH PULSE
ACALL DELAY    ;
CLR P2.2    ; E = 0 FOR LOW PULSE
RET

DATAWRT:
MOV P1, A    ;
SETB P2.0    ; RS = 1 FOR DATA REGISTER
CLR P2.1    ; R/W = 0 FOR WRITE
SETB P2.2    ; E = 1 FOR HIGH PULSE
ACALL DELAY    ;
CLR P2.2    ; E = 0 FOR LOW PULSE
RET

DELAY :
MOV R3, #50H ;
BACK: 
MOV R4, #255H ;
HERE:
DJNZ R4, HERE ;
DJNZ R3, BACK ;
RET
END

Terms used in LCD


Terms used in LCD
display data RAM
DD RAM
display data RAM
CG RAM
character generator RAM
ACC
CG RAM address
ADD
DD RAM address corresponding to cursor address
AC
address counter used for both CG and DD
1/D = 1 for increment
1/D = 0 for decrement
S = 1 accompanies display shift

S/C = 1 display shift
S/C = 0 for cursor shift
R/L = 1 shift to right
R/L = 0 shift to left
DL = 1 8 bits
DL = 0 4 bits
F = 1 5x10 dots
F = 0 5x7 dots
BF = 1 Internal operation
BF = 0 can accept instruction

Programming LCD (basics)

Few Points for programming LCD.
>> Rs register select pin.
If RS = 0 command code register is selected or opened allowing user to send commands.
If RS = 1 data register is selected or opened allowing user to send data.

>> As LCD runs on a lower clock speed therefore proper DELAY should be provided after every command write and data write.
Usually a 255D delay is more than enough.

>> Enable pin. E. when data is supplied to data pins a high to low pulse must be applied to pin in order to latch data to

LCD pin. This must be a minimum of 450nS wide.

Steps to send command and data.
38h initialize LCD
0Eh display on cursor on.
01h clear LCD
06h shift cursor right
84h position the cursor.
now send data.

for LCD program see the next post

Friday, September 23, 2011

List of LCD instructions(ultra short)

Clear display
Return home
Entry mode set
Display On/off control
Cursor or Display shift
Function set
Set CG RAM address( CG = Character generator)
Set DD RAM address( DD = Display Data)
Read busy flag(BF) and address
Write data CG or DD RAM
Read data CG or DD RAM.

For further details of LCD stay connected.

MOVX dest-byte, source-byte

It moves external memory.
No flags are afffected.
This transfers data between external memory and register A.
this allows us to access external memory connected to the controller.
The address of external memory access can be either 8 bit or 16 bit.

For 16 bit
use DPTR.
like
MOVX A, @DPTR    ;

moves data from external memory stored at address DPTR to accumulator.
External data is moved into the CPU.

MOVX @DPTR, A    ;

moves data from Aaccumulator to address stored at DPTR.
data is moved from CPU to external memory.

For 8 bit
use R0 or R1 register.
like

MOVX A, @Ri ; WHERE i IS 0 OR 1

moves data from external memory stored at address Ri to accumulator.
External data is moved into the CPU.

MOVX @Ri, A ; WHERE i IS 0 OR 1

moves data from Aaccumulator to address stored at Ri.
data is moved from CPU to external memory.

The 16 bit is used for accessing external memory whereas 8 bit is used for accessing external I/O ports.

MOVC A, @A+DPTR

It moves code byte.
No flags are affected
This moves a byte of data located in program code ROM into Accumulator A.
This allows to create a lookup table of sorts in code space and read them into CPU.
The address of the desired byte in code space is formed by adding A + DPTR

Example:

ORG 0000
MOV DPTR, #200H    ;
LOOP : CLR A    ;
MOVC A,@A+DPTR    ; MOVE DATA @A+DPTR IN A
JZ EXIT        ;
MOV P1, A    ;
INC DPTR    ;
SJMP LOOP    ;
EXIT :
-----
-----
ORG 200H
DB 'A','B','C',0;
END

another example can be finding square or square root of numbers.
This is strictly used for on chip ROM. for off chip memory we use MOVX.


MOVC A, @A+PC

It moves code byte.
No flags are affected
Same as above instruction inly difference is that the PC is used instead of DPTR.

Example:

MOV A, R3    ;
INC A        ;
MOVC A, @A+PC    ;
RET
DB 0,1,4,9,16,25;

The following should be noted:

The program counter, which is pointing to the RET is taken for addition.
Always remember that always the next code value is taken in PC before adding to Accumulator A.
 This method is preferrable over "MOVC A, @A+DPTR" when we do not want to divide the program code space into 2 separate

areas of code and data. As a result we save considerable amount of space on chip

Differentiate between ADDC and SUBB

ADDC
Does addition with carry
changes OV, AC, CY flags
This does A = A + byte + CY
used in multibyte addition
example
addition of 3456H and 2345H

CLR C        ;CY =0
MOV A, #56H    ;
ADDC A, #45H    ;
MOV R7, A    ; STORE LOWER BYTE IN R7
MOV A, #34H    ;
ADDC A, #23H    ; UPPER BYTE

Result = 3456 + 2345 = 579B

Addressing modes
Immediate:         ADDC A, #data
Register:          ADDC A, Rn
Direct:            ADDC A, 30H
Register-Indirect:     ADDC A, @R0
            ADDC A, @R1

SUBB
Does subtraction with borrow
Changes OV, Ac, CY flags
This subtracts source byte and carry flag from accumulator and puts result in accumulator.
The steps for subtraction:
Take 2's complement
Add to A
Invert carry.

CY
dest>source 0 result is positive
dest=source 0 result is 0
dest<source 1 result is negative in 2's complement

No SUB instruction in 8051
A = A - byte - CY
example:
MOV A, #45H
CLR C
SUBB A, #23H
Result 22H

Addressing modes
Immediate:         SUBB A, #data
Register:          SUBB A, Rn
Direct:            SUBB A, 30H
Register-Indirect:     SUBB A, @R0
            SUBB A, @R1