|
Топ
Ранг: Минаващ
Регистриран на: Пет Окт 28, 2005 3:24 pm Мнения: 30
|
 LCD 4-bit mode
Ето ти направо решение с 37 думи - инициализация, изпращане на команда и данни.
Това го писах тогава за АТ90S2313, на 4 MHz.
Работи и на всички АТмеги и цялата серия АТ90. Направо си виж само в инициализацията къде бъркаш.
Най-често бъркат като не пращат 3 пъти тройки в началото, или не дават време на модула да се инициализира.
Хайде и се похвали като го пуснеш, защото този код вече работи в N модула.
А ако изпълниш "Welmes" фуннкцията, на долния ред ще ми изпише името на кирилица.
Успех!
;*****************************************************************************
;* LCD Driving in 4-bit Mode *
;* *
;* This program is a Driver for Character LCDs - Industrial Standart *
;* *
;* Author: *
;* Datе : 10 / 2002 *
;*****************************************************************************
;==========================================================
;-----------------------------------------------------------------------------
; LCD DEFINITIONS
;-----------------------------------------------------------------------------
.def temp= R16 ; Global Variables
.def temp1= R17
.def fine= R2 ; Pause
.equ LCD_DATA= PORTB ; LCD Data Register
; D4-D7 = PB0-PB3 (pins 12-15)
.equ LCD_RS= 4 ; Lcd Register-Select Control = PB4 - pin 16
.equ LCD_E= 5 ; Lcd Enable Control = PB5 - pin 17
.def LCD_Byte= R23 ; Lcd Byte - Command Or Data
.def SByte= R25 ; Variable For 4-Bit interface
;=============================================================================
;------------------------ L C D H A N D L I N G --------------------------
;=============================================================================
LCDINIT: ; Initialize Lcd
ldi temp , 128 ; Start Up time > 20ms
rcall DELAY
ldi LCD_Byte , 0x33 ; Set up
rcall LCDCMD
ldi LCD_Byte , 0x32 ; 4-Bit interface
rcall LCDCMD
ldi LCD_Byte , 0x28 ; 2 Lines, 5x7 dots
rcall LCDCMD
ldi LCD_Byte , 0x08 ; Display Off
rcall LCDCMD
ldi LCD_Byte , 0x01 ; Clear Screen
rcall LCDCMD
ldi LCD_Byte , 0x06 ; Entry Mode Set
rcall LCDCMD
ldi LCD_Byte , 0x0F ; Display On
rcall LCDCMD
rjmp CustomLCD ; End Initialization
;----------------------------------------------------------------------------
LCDCMD: ; Send COMMAND To Lcd
;----------------------------------------------------------------------------
; Command Code must be in LCD_Byte
ldi temp1 , 0x20 ; RS => '0' + Enable => '1'
rjmp Char
;----------------------------------------------------------------------------
LCDCHAR: ; Disply CHARACTER On Lcd
;----------------------------------------------------------------------------
; Character Code must be in LCD_Byte
ldi temp1 , 0x30 ; RS => '1' + Enable => '1'
Char: mov SByte , LCD_Byte ; LCD_Byte => SByte
Swap SByte ; SByte = D3-D0/D7-D4
andi SByte , 0x0F ; SByte = 0000/D7-D4
or SByte , temp1 ; SByte = 0 0 1 RS/D7-D4
out LCD_DATA , SByte ; Apply Data On Lcd Pins
cbi LCD_DATA , LCD_E ; Enable => '0'
ldi temp , 32 ; DELAY > 6 ms
rcall DELAY ; Required only For Initialisation
mov SByte , LCD_Byte ; LCD_Byte => SByte
andi SByte , 0x0F ; SByte = 0000/D3-D0
or SByte , temp1 ; SByte = 0 0 1 RS/D3-D0
out LCD_DATA , SByte ; Apply Data On Lcd Pins
cbi LCD_DATA , LCD_E ; Enable => '0'
ldi temp , 26 ; DELAY > 5 ms
; Time To Accept Command/Data
DELAY: ; DALAY=771 x (temp) x Tosc
dec fine ; +1
brne DELAY ; +2
dec temp ; +1
brne DELAY ; +2
ret ; End
;=============================================================================
; End LCD Control Program - total 37 words
;=============================================================================
;=============================================================================
; CUSTOMIZE LCD
;=============================================================================
CustomLCD: ; Load 8 custom characters into Lcd
ldi LCD_Byte , 0x40 ; Set Cursor at the begining of CGRAM
rcall LCDCMD
ldi ZH , high(2*LCDTable) ; Set Pointer To Lcd Table
ldi ZL , Low(2*LCDTable)
Lp: lpm ; Load Byte from Program Memory into R0
mov LCD_Byte , r0
rcall LCDCHAR ; Display Character
adiw ZL , 1 ; Set the Pointer To the Next Character
cpi Zl , Low(2*LCDTable+64) ; Tablå loaded ?
brne Lp ; NO - Go On
ret ; End
LCDTable: ; CUSTOM CHARACTERS DESCRIPTION
.db 12, 18, 20, 30, 17, 17, 30, 0 ; â = Craracter Code '00'
.db 0, 0, 15, 21, 21, 21, 21, 0 ; ò = Craracter Code '01'
.db 14, 17, 1, 14, 1, 1, 30, 0 ; Ç = Craracter Code '02'
.db 0, 0, 14, 17, 17, 30, 1, 30 ; ä = Craracter Code '03'
.db 4, 10, 10, 10, 10, 10, 31, 17 ; Ä = Craracter Code '04'
.db 0, 0, 17, 17, 31, 17, 17, 0 ; í = Craracter Code '05'
.db 0, 0, 17, 17, 15, 1, 1, 0 ; ÷ = Craracter Code '06'
.db 0, 0, 17, 19, 21, 25, 17, 0 ; è = Craracter Code '07'
;LCDTable: ; CUSTOM CHARACTERS DESCRIPTION (Cyrillic letters)
;.db 31, 17, 16, 16, 16, 16, 16, 0 ; Ã = Craracter Code '00'
;.db 4, 10, 10, 10, 10, 10, 31, 17 ; Ä = Craracter Code '01'
;.db 17, 17, 19, 21, 25, 17, 17, 0 ; È = Craracter Code '02'
;.db 7, 9, 9, 9, 9, 9, 17, 0 ; Ë = Craracter Code '03'
;.db 31, 17, 17, 17, 17, 17, 17, 0 ; Ï = Craracter Code '04'
;.db 17, 17, 17, 15, 1, 1, 1, 0 ; × = Craracter Code '05'
;.db 24, 8, 8, 14, 9, 9, 14, 0 ; Ú = Craracter Code '06'
;.db 15, 17, 17, 15, 5, 9, 17, 0 ; ß = Craracter Code '07'
;=============================================================================
; E N D CUSOMIZING - Total 43 Words
;=============================================================================
;=============================================================================
LCDMSG: ; Dislay String On Lcd, the last Byte must be '08'
; Register Z points the String is FLASH memory
;=============================================================================
lpm ; Load Byte from Program Memory into R0
mov LCD_Byte , r0
cpi LCD_Byte , 8 ; End Message ? ("08" means End String)
breq Msgend ; YES => Exit
rcall LCDCHAR ; Display Character
adiw ZL , 1 ; Set the Pointer To the Next Character
rjmp LCDMSG ; Loop
Msgend:
ret ; End String
;=============================================================================
; END of showing a string on LCD - Total 8 Words
;=============================================================================
;=============================================================================
LCDBYTE: ; Display Byte on LCD in HEX Value (00-FF)
;=============================================================================
push LCD_Byte ; Store Byte in STACK
swap LCD_Byte ; LCD_Byte = 4321/7654
rcall DisNibble ; Display High Nibble
pop LCD_Byte ; Restore Byte from STACK
DisNibble:
andi LCD_Byte , 0x0F ; LCD_Byte = 0000/xxxx
ori LCD_Byte , 0x30 ; LCD_Byte = 0011/xxxx
cpi LCD_Byte , 0x3A ; Data > 9 (A..F) ?
brlo Lcdok
subi LCD_Byte , 0xF9 ; YES => Convert Value
Lcdok: rjmp LCDCHAR ; Display Character and Exit
;=============================================================================
;=============================================================================
; Display Welcome Message
;=============================================================================
Welmes:
ldi LCD_Byte , 0x80 ; Set Cursor Position = Line 1, Pos 1
rcall LCDCMD
ldi ZH , high(2*Mess01) ; Set Message Pointer To Mess01
ldi ZL , Low(2*Mess01)
rcall LCDMSG ; Display " LCD Demo Module "
ldi LCD_Byte , 0xC0 ; Set Cursor Position = Line 2, Pos 1
rcall LCDCMD
ldi ZH , high(2*Mess02) ; Set Message Pointer To Mess02
ldi ZL , Low(2*Mess02)
rcall LCDMSG ; Display
ret
;-----------------------------------------------------------------------------
; MESSAGES
;-----------------------------------------------------------------------------
Mess01:
.db " LCD Demo Module ", 8
Mess02:
.db "A",0,1,"op:",2,3,"pa",0,"ko ",4,"e",5,6,"e",0;
.db 8
;=============================================================================
|