You are on page 1of 2

copyright = MCS Electronics www = http://www.mcselec.com email = avr@mcselec.com comment = Optimized LCD library libversion = 1.

00 date = 2 februari 2001 statement = No SOURCE code from the library may be distributed in any form statement = Of course this does not applie for the COMPILED code when you have a BASCOM-AVR license history = No known bugs. ;the default library for BASCOM can handle worst case scenarios where each pin of the LCD ;is connected to a random pin of the micro ;When we choose the data port pin in sequence, we can save some code ;This lib shows how to use the LCD in 4 bit mode ;Rs = PortB.0 ;RW = PortB.1 ; E = PortB.2 ;E2 = PortB.3 ;Db4 = PortB.4 ;Db5 = PortB.5 ;Db6 = PortB.6 ;Db7 = PortB.7

we dont use the R/W option of the LCD in this version so connect to ground optional for lcd with 2 chips the data bits must be in a nibble to save code

;This routine is called when the LCD must be initialized [_Init_LCD] .EQU LCDDDR=$17 .EQU LCDPORT=$18 _Init_LCD: *BASIC: waitms 10 Ldi R24, &B11111111 Out LCDDDR,R24 Ldi R24, &B00110000 Out LCDPORT,R24 Ldi r16,3 _Init_LCD1: SBi LCDPORT, 2 Cbi LCDPORT, 2 *BASIC: waitms 1 Dec R16 Rd = Rd - 1 Brne _Init_LCD1 Cbi LCDPORT, 4 SBi LCDPORT, 2 Cbi LCDPORT, 2 *BASIC: waitms 1 Ldi R24, 40 Rcall _Lcd_control PC = PC + k + 1 Ldi _temp1,14 Rcall _Lcd_control Ldi _temp1,6 RJmp _Lcd_control [END]

; change to another address for DDRD ($11) ; change to another address for PORTD ($12)

; all outputs ; write to Data direction ; set logic level ; write to output pins ; repeat 3 times ; E high ; E low

;LD load immediate rd,K -> rd = K ;OUT P, Rr P = Rr

;SBI P, b ;CBI P, b

Set Bit in I/O Register Clear Bit in I/O Register ;DEC Rd

I/O(P, b) = 1 I/O(P, b) = 0 Decrement

; until we are ready ;Branch if Not Equal ; DB4 must be set low now ; E high ; E low ; 4 bit mode ;RCALL ; Display on, Cursor on, Noblink ; Cursor moves right, text doesn't move ; return will come from _lcd_control K Relative Call Subroutine

;RJMP

Relative Jump

PC = PC + k + 1

;high nibble must be written first ;rw must be low for a write ;rs must be low for control, high for data ;data passed in r24 [_Write_lcd] _Write_lcd: Set ; RS flag must be made 1 Rjmp _Write_lcd_Byte _Lcd_control: Clt ; RS flag must be 0 _Write_lcd_Byte:

;SET

Set T in SREG

T=1

;CLT Clear T in SREG

T=0

Mov r25,r24 Rcall _Write_lcd_Nibble Mov R24,r25 Rr Swap R24 Rd(7..4)

; save data ; write lower nibble ; get data back ; swap nibble ;MOV ;SWAP Rd, Rr Copy Register Rd =

Rd Swap Nibbles

Rd(3..0)

_Write_lcd_nibble: Bld R24,0 ; RS flag Rd(b) = T Sbr R24,4 ; E high Rd v K Cbr R24,10 ; E2 and RW low = Rd ($FFh - K) Out LCDPORT, R24 ; write ; For high Xtals unremark next line @genus(100) ; wait for 100 uS Cbi LCDPORT,2 ; disable @genus(100) ; wait for 100 uS Ret ;RET Subroutine Return PC = STACK [END] [_CLS] $EXTERNAL _WRITE_LCD _Cls: Ldi R24,1 ; Clear display Rcall _Lcd_control *BASIC: waitms 3 Ldi R24,128 ; Select line 1 Rjmp _Lcd_control ; finish [END]

;BLD Rd, b ;SBR Rd,K

Bit load from T to Register Set Bit(s) in Register Rd =

;CBR Rd,K Clear Bit(s) in Register Rd

You might also like