*****************************************************
*Oprindelig kode    :port B = kontrol, port C = data*
*(PIO PortC skulle da konfigureres til output)	    *
*						    *
*Vores version	    :port A = kontrol, port B = data*
*						    *
*Kontrolben					    *
*(de brugte databit på portA er alle output only)   *
*Port A3 = E					    *
*Port A4 = R/W					    *
*Port A5 = RS					    *
*						    *
*Databen (PortB er output only)			    *
*Port B0-7 = Data				    *
*(forbindes til respektiv DB0-7 på LCD)		    *
*****************************************************

************************************************************************
*_Ib_Refer__________________________________________Rev:23.nov.1999____*
*_Rettet_af_Rasmus_Maagaard_________________________Rev:10.apr.2001____*
* Routiner til interface af et alfanumerisk display.                   *
* Som display anvendes et 2x16 liniers display (HD44780A)              *
* sammenkobling imellem HC11 og LM016L:                                *
* ben1 = GND                                                           *
* ben2 = +5V                                                           *
* ben3 = kontrast (spaendingsdeling 10K imellem + og GND)               *
* ben4 = RS  - RegisterSelect(0) [PB5]                                 *
* ben5 = R/W - Read(1)/Write(0) [PB6]                                  *
* ben6 = E   - LCD-Enable(1) Disable(0) [PB7]                          *
* ben7 = DataBit0 [PC0]                                                *
* ben8 = DataBit1 [PC1]                                                *
* ben9 = DataBit2 [PC2]                                                *
* ben10= DataBit3 [PC3]                                                *
* ben11= DataBit4 [PC4]                                                *
* ben12= DataBit5 [PC5]                                                *
* ben13= DataBit6 [PC6]                                                *
* ben14= DataBit7 [PC7]                                                *
************************************************************************
regbase equ     $1000                   ; registerblok start
portb   equ     $04                     ; port B
portc   equ     $03                     ; port C
DDRC    equ     $07                     ; data direction port C
alle    equ     $FF                     ; alle bit
bit5    equ     %00100000               ; maske for bit 5, RS
bit6    equ     %01000000               ; maske for bit 6, RW
bit7    equ     %10000000               ; maske for bit 7, E
LCDlin1 equ     $00                     ; linie 1 position 1
;LCDlin2 equ     $C0                     ; linie 2 position 1
LCDlin2 equ     $40
LCDclr  equ     $01                     ; clear display, cursor home
LCDhome equ     $02                     ; cursor home, DDram u‘ndret
LCDinc  equ     $06                     ; cursor increment mode
LCDdec  equ     $04                     ; cursor decrement mode
LCDon   equ     $0C                     ; display on
LCDoff  equ     $08                     ; display off
LCDleft equ     $18                     ; skift display til venstre
LCDrght equ     $1C                     ; skift display til h›jre
CURon   equ     $0E                     ; display on, cursor on
CURoff  equ     $0C                     ; display on, cursor off
CURblk  equ     $0F                     ; display on, cursor blink
CURleft equ     $10                     ; flyt cursor til venstre
CURrght equ     $14                     ; flyt cursor til h›jre
LCDCG   equ     $40                     ; CGRAM start adresse
;LCDfn   equ     %00011100 ;denne linie goer at led bliver taendt
LCDfn   equ     %00111000               ; LCD function (ops‘tning)

                                        ; 8 databit; 2 linier og
                                        ; font = 5x7 dots

        org     $f800
        ldx     #regbase
        jsr     LCDinit

        ldaa    #%01111110
        staa    $0000
        staa    $0002
        jsr     mem2LCD
loop    bra     loop

******* i det f›lgende forventes regbase at ligge i X ******************
LCDinit
        bset    DDRC,x alle             ; port C = udgange
        bclr    portb,x %11100000       ; register-mode/Write-mode
        ldaa    #LCDfn                  ; l‘g ops‘tning i acc.A
        jsr     LCDreg                  ; og send den til LCD
        jsr     wait5ms                 ; vendt 5 ms
        jsr     LCDreg                  ; og send den til LCD
        jsr     wait5ms                 ; vendt 5 ms
        jsr     LCDreg                  ; og send den til LCD
        jsr     wait5ms                 ; vendt 5 ms
        ldaa    #LCDon                  ; LCD on og cursor off
        jsr     LCDreg                  ; send komando til LCD
        ldaa    #LCDclr                 ; clear display
        jsr     LCDreg                  ; send komando til LCD
        ldaa    #LCDinc                 ; cursor increment mode
        jsr     LCDreg                  ; send komando til LCD
        rts                             ; returner
************************************************************************
LCDreg
        jsr     LCDchk                  ; check LCD busy-flag
        bclr    portb,x bit5            ; g† i register-mode
        bra     LCDsend                 ; send data
************************************************************************
LCDdata jsr     LCDchk                  ; check LCD busy-flag 
        bset    portb,x bit5            ; g† i ASCII-mode 
LCDsend
        bset    DDRC,x  alle            ; port C = udgang
        bclr    portb,x bit6            ; g† i write-mode
        staa    portc,x                 ; send data til port C 
        bra     LCDen                   ; enable-puls til LCD
************************************************************************
LCDchk
        bclr    DDRC,x  alle            ; port C = indgang
        bclr    portb,x bit5            ; g† i register-mode   
        bset    portb,x bit6            ; g† i read-mode    
        bset    portb,x bit7            ; enable LCD
        nop                             ; pause (en E-clock)
        brset   portc,x bit7 *          ; vent til busy-flag=lavt
        bclr    portb,x bit7            ; disable LCD
        rts                             ; returner fra sub.
************************************************************************
LCDen   bset    portb,x bit7            ; enable LCD 
        nop                             ; (puls p† mindst 500ns)
        bclr    portb,x bit7            ; disable LCD    
        rts                             ; returner fra sub.
*****************************************************************>>cycles
wait5ms pshx                            ; skub x-reg. ud p† stakken  4
        ldx     #612                    ; l‘g 612 i X-reg.           3
w5ms1   nop                             ; gennemf›r                  2!
        nop                             ; loopet (8,138 us)          2!
        dex                             ; 612 gange                  3!
        bne     w5ms1                   ; 8,138us x 612 = 4,98 ms    3!6120
        pulx                            ; hent x-reg. fra stakken    5
        rts                             ;                    ialt    6132
************************************************************************
mem2LCD ldy     #screen                 ; l‘g start-adr. i Y
        ldab    #16                      ; l‘g linie counter i acc.B
        ldaa    #LCDlin1                ; g† til linie 1 pos 1
        jsr     LCDreg                  ; send kommando til LCD
        jsr     lin2LCD                 ; send en linie data til LCD
        ldab    #16                      ; l‘g linie counter i acc.B
        ldaa    #LCDlin2                ; g† til linie 2 pos 1
        jsr     LCDreg                  ; send kommando til LCD 
lin2LCD ldaa    0,y                     ; hent data fra memory
        jsr     LCDdata                 ; send data til LCD
        iny                             ; increment Y
        decb                            ; decrement acc.B
        bne     lin2LCD                 ; er linien skrevet? (acc.B=0)
        rts                             ; returner fra sub.
************************************************************************
        org     $0000                   ; start-adr.
screen  rmb     32                      ; reserver 32 bytes til LCD

