Sitter här och kodar mitt assembler program för att försöka få en 2x16 hd47780 display att fungera i 4bit mode.
Jag kollade först på piclist om det fanns några lämpliga projekt, hittade detta.
Han som skrev det tycker jag var väldigt dålig på att kommentera koden så till slut skrev jag en egen. Kan ni försöka att kolla vad som kan vara fel?
Kod: Markera allt
; LCD Test Program v1.0 made by Erik Henriksson
; This program is ment to run at 20Mhz.
;
;*********************************************
; Start of program
;*********************************************
; What to do when reset
;*********************************************
Org 0x001
Goto Start
;*********************************************
; End
;*********************************************
; Alias declarations
;*********************************************
PORTA Equ 0x005
PORTB Equ 0x006
trisa Equ 0x085
trisb Equ 0x086
isr Equ 0x004
status Equ 0x003
En Equ 5
RS Equ 6
RW Equ 7
;*********************************************
; End of Alias declarations
;*********************************************
; Variable declaration
;*********************************************
;Avalible RAM in bank0: 0x0020-0x06F
;
; Delay variables
d1 Equ 0x020
d2 Equ 0x021
d3 Equ 0x022
d4 Equ 0x023
; Lcd variables
Hi_bits Equ 0x024
Lo_bits Equ 0x025
;*********************************************
; End of Variable declaration
;*********************************************
; Timer interrupt
;*********************************************
Org isr
; Do nothing
;*********************************************
; End of Timer interrupt
;*********************************************
; Subroutines
;*********************************************
Delay_5ms ; Make a delay of exactly 5ms = 25000cykles
;24993 cycles
movlw 0x86
movwf d1
movlw 0x14
movwf d2
Delay_5ms_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_5ms_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;******
Delay_160us ; Make a delay of exactly 160us = 800cykles
;793 cycles
movlw 0x9E
movwf d1
movlw 0x01
movwf d2
Delay_160us_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_160us_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;******
Enable
movlw 0x20
bsf portb, En ; pulse the LCD Enable high
nop
nop
nop
nop
nop
nop
nop
nop
bcf portb, En ; pulse the LCD Enable low
nop
nop
return
;******
Sendin4bit
; Send W to lcd trought 4bit interface
; You must set RS first if you are sending a character
clrf hi_bits
clrf lo_bits
bcf STATUS,0 ; clear C which could skrew up the bitshifts
; Load W into lo_bits and hi_bits
movwf hi_bits
movwf lo_bits
;lo_bits preparation
movlw 0x0F
andwf lo_bits, 1 ; Remove the high bits and store in lo_bits
;hi_bits preparation
movlw 0xF0
andwf hi_bits, 1 ; Remove the low bits so they dont affect the carry bit
rrf hi_bits, 1
rrf hi_bits, 1
rrf hi_bits, 1
rrf hi_bits, 1
;Send the bits
;Hi:
movfw hi_bits ; Move hi_bits to W
addwf portb ; Add the bits so you can set RS before if you want
call Enable
call delay_160us ; lcd busy delay
subwf portb
;Lo
movfw lo_bits ; Move hi_bits to W
addwf portb ; Add the bits so you can set RS before if you want
call Enable ; Set enable for 3,2us
call delay_160us ; lcd busy delay
subwf portb
;Done
return
;*********************************************
; End of Subroutines
;*********************************************
; Start
;*********************************************
Start
Clrf porta ; Init porta
Clrf portb ; Init portb
Banksel trisb ; Select bank1
Movlw 0x00 ; Data direction value, 0 = output
Movwf trisb ; Write to trisb
Movlw 0xFF
Movwf trisa
Banksel portb ; Select bank0
;*********************************************
; End of Start
;*********************************************
; Main
;*********************************************
Call delay_5ms
Call delay_5ms
Call delay_5ms
Call delay_5ms ; Wait for lcd to start
Movlw 0x03
Movwf portb
Call Enable ; Send 0x03
Call delay_5ms
Call Enable ; Send 0x03
Call delay_160us
Call Enable ; Send 0x03
Call delay_160us
Movlw 0x02
Movwf portb
Call Enable ; Send 0x02
Call delay_160us
Movlw 0x28 ; Interface length is: 4bit, 2 lines, 5x7
Call Sendin4bit
Movlw 0x10 ; Turn off the display
Call Sendin4bit
Movlw 0x01 ; Clear the display
Call Sendin4bit
Movlw 0x06 ; Shift cursor on every written byte
Call Sendin4bit
Movlw 0x0F ; Turn display on, cursor on, cursor blinking on
Call Sendin4bit
;Init done
Movlw 0x45 ; Send 'E'
Call Sendin4bit
;*********************************************
; End of Main
;*********************************************
; End of Program
;*********************************************
END