lcdh44780m8counter06262026.asm

;; david vajda
;; 06/14/2026
;; m8/lcd hd44780 ... to m8/t counter... up...

.include "m8def.inc"

.equ HD44780WAITLOOPLONGCOUNT1 = $21
.equ HD44780WAITLOOPLONGCOUNT2 = $C9
.equ HD44780WAITLOOPSHORTCOUNT = 42

.org 0x0000
rjmp RESET
.org OVF1addr
rjmp OVF1addr

RESET:

ldi r16, HIGH (RAMEND)
out SPH, r16
ldi r16, LOW (RAMEND)
out SPL, r16
ldi r16, 0xff
out DDRB, r16

rcall hd44780init
rcall hd44780cleardisplay
ldi ZH, HIGH (2*str)
ldi ZL, LOW (2*str)
l1:
lpm r16, Z+
rcall hd44780echo
subi r16, 0x00
brne l1

str: .db "https://www.vajda-breadboard.de", 10, 13, 0x00, 0x00

hd44780setup:
sbi PORTB, 5
nop
nop
nop
cbi PORTB, 5
ret

hd44780waitlooplong:
push r16
push r17
ldi r16, HD44780WAITLOOPLONGCOUNT1
hd44780waitlooplongl1:
ldi r17, HD44780WAITLOOPLONGCOUNT2
hd44780waitlooplongl2:
dec r17
brne hd44780waitlooplongl2
dec r16
brne hd44780waitlooplongl1
pop r17
pop r16
ret


hd44780waitloopshort:
push r16
ldi r16, HD44780WAITLOOPSHORTCOUNT
hd44780waitloopshortl1:
dec r16
brne hd44780waitloopshortl1
pop r16
ret

hd44780init:
;; $3, $3, $3, $2, $20
ldi r16, 50 ;; weiss ich auswendig so lange am anfang warten
hd44780initl1:
rcall hd44780waitlooplong
dec r16
brne hd44780initl1
ldi r16, $3
out PORTB, r16
rcall hd44780setup
rcall hd44780waitlooplong
rcall hd44780setup
rcall hd44780waitlooplong
rcall hd44780setup
rcall hd44780waitlooplong
ldi r16, $2
rcall hd44780setup
rcall hd44780waitlooplong
ldi r16, $20
rcall hd44780setup
rcall hd44780waitlooplong

;; weiss auswendig
;; 0b0000 0001 clear display
;; 0b0000 001x cursor home
;; 0b0000 01is entry mode
;; 0b0000 1dbc display on, blink, cursor on
;; 0b0001
;; hier folgt etwas zu konfiguration ist wichtig...
;; damit das interface auch 4 bit modus macht...


;;; rein kopiert
           ldi r16, 0b00101000        ; 4Bit / 2 Zeilen / 5x8
           rcall hd44780cmd
           ldi r16, 0b00001100        ; Display ein / Cursor aus / kein Blinken
           rcall hd44780cmd
           ldi r16, 0b00000110        ; Cursor inkrementieren / kein Scrollen
           rcall hd44780cmd
           ret
ret

hd44780cmd:
mov r17, r16
swap r17
andi r17, 0b00001111
out PORTB, r17
rcall hd44780setup
mov r17, r16
andi r17, 0b00001111
out PORTB, r17
rcall hd44780setup
rcall hd44780waitloopshort
ret

hd44780echo:
mov r17, r16
swap r17
andi r17, 0b00001111
ori r17, 0x10
out PORTB, r17
rcall hd44780setup
mov r17, r16
andi r17, 0b00001111
ori r17, 0x10
out PORTB, r17
rcall hd44780setup
rcall hd44780waitloopshort
ret

hd44780cleardisplay:
ldi r16, 0b00000001
rcall hd44780cmd
ret

;;; divide command
;;; max per 16 0b00001111
;;; reg mit 8 Bit

;;; test ldi r16, DIVIDEND
div:
push r16
push r17
push r18
push r19
ldi r16, DIVIDEND
ldi r17, DIVISOR

lsl r17
lsl r17
lsl r17
lsl r17

ldi r19, 0b10000000
div001:
cp r16, r17
brlt div002
sub r16, r17
or r18, r19
div002:
lsr r17
lsr r19
brne div001
mov r20, r18
...

ret

byte2bcdstr: