From aac3c16b2987b426f3bdc3ea8f5111a45e66b776 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sat, 28 Jan 2023 15:29:58 +0100 Subject: [PATCH] Started working on LCD module. --- avr/lcd.asm | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ avr/main.asm | 10 ++-- 2 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 avr/lcd.asm diff --git a/avr/lcd.asm b/avr/lcd.asm new file mode 100644 index 0000000..3febb52 --- /dev/null +++ b/avr/lcd.asm @@ -0,0 +1,149 @@ + +; *************************************************************************** +; defines + +.equ LCD_CONTROLBYTE_DC_MASK = 0x40 +.equ LCD_CONTROLBYTE_CO_MASK = 0x80 + +.equ LCD_TWI_ADDRESS = 0x3c + +.equ LCD_WIDTH = 128 +.equ LCD_HEIGHT = 64 + + + +; *************************************************************************** +; data + +.dseg + +lcdDataBegin: +lcdDataEnd: + + + +; *************************************************************************** +; code + +.cseg + + + + +LCD_Init: + sbi DDRA, PORTA2 ; debug + sbi PORTA, PORTA2 ; debug (off) + + ldi zl, LOW(lcdInitCommandsBegin) + ldi zh, HIGH(lcdInitCommandsBegin) + ldi r16, (lcdInitCommandsEnd-lcdInitCommandsBegin)*2 + rcall lcdWriteCommandsFromFlash + + ldi r16, 0 + rcall LCD_Fill + + sec + ret + + + + +; - R16: number of bytes to send +; - Z=pointer to data +lcdWriteCommandsFromFlash: + rcall twiStart + lsl zl + rol zh + mov r20, r16 ; number of bytes + ldi r16, (LCD_TWI_ADDRESS*2) + rcall twiSendByte + brcc lcdWriteCommandsFromFlash_error + tst r16 + brne lcdWriteCommandsFromFlash_error + +lcdWriteCommandsFromFlash_loop: + lpm r16, z+ + rcall twiSendByte + brcc lcdWriteCommandsFromFlash_error + tst r16 + brne lcdWriteCommandsFromFlash_error + dec r20 + brne lcdWriteCommandsFromFlash_loop + rcall twiStop + sbi DDRA, PORTA2 ; debug + cbi PORTA, PORTA2 ; debug (on) + sec + ret + +lcdWriteCommandsFromFlash_error: + rcall twiStop + clc + ret + + + +; - R16: data to write +LCD_Fill: + mov r20, r16 + + rcall twiStart + + ldi r16, (LCD_TWI_ADDRESS*2) + rcall twiSendByte + brcc LCD_Fill_error + tst r16 + brne LCD_Fill_error + + ldi r21, 0 ; Y +LCD_Fill_loopY: + ldi r16, 0xb0 ; set page address (data byte and next control byte follow, data byte is a command) + add r16, r21 + rcall twiSendByte + brcc LCD_Fill_error + tst r16 + brne LCD_Fill_error + ldi r16, 0x01 ; set lower column address + rcall twiSendByte + brcc LCD_Fill_error + tst r16 + brne LCD_Fill_error + + ldi r16, 0x10 ; set higher column bits + rcall twiSendByte + brcc LCD_Fill_error + tst r16 + brne LCD_Fill_error + + ldi r19, 0 +LCD_Fill_loopX: + mov r16, r20 + rcall twiSendByte + brcc LCD_Fill_error + tst r16 + brne LCD_Fill_error + inc r19 + cpi r19, LCD_WIDTH + brcs LCD_Fill_loopX + + inc r21 + cpi r21, 8 + brcs LCD_Fill_loopY + rcall twiStop + sec + ret +LCD_Fill_error: + rcall twiStop + clc + ret + + + + +lcdInitCommandsBegin: ; 28 bytes + .db 0xae, 0x00, 0x10, 0x40, 0x81, 0xcf, 0xa1, 0xc8 + .db 0xa6, 0xa8, 0x3f, 0xd3, 0x00, 0xd5, 0x80, 0xd9 + .db 0xf1, 0xda, 0x12, 0xdb, 0x40, 0x20, 0x02, 0x8d + .db 0x14, 0xa4, 0xa6, 0xaf +lcdInitCommandsEnd: + + diff --git a/avr/main.asm b/avr/main.asm index 1979a4d..cb29658 100644 --- a/avr/main.asm +++ b/avr/main.asm @@ -91,6 +91,7 @@ .include "led.asm" .include "com.asm" .include "twimaster.asm" +.include "lcd.asm" @@ -145,8 +146,8 @@ main: ldi yh, HIGH(ledA3Sram) rcall Led_SetPattern - ldi r16, 1 - sts twiMasterScanEnabled, r16 +; ldi r16, 1 +; sts twiMasterScanEnabled, r16 main_loop: rcall runModulesUntilIdle @@ -190,6 +191,7 @@ initModules: rcall Com_Init rcall TWI_Master_Init + rcall LCD_Init ret @@ -263,9 +265,7 @@ onEvery100ms: ; USED: depending on called routines onEverySecond: - sbi DDRA, PORTA2 ; debug - sbi PINA, PORTA2 ; debug (toggle) - rcall TWI_Master_ScanNext +; rcall TWI_Master_ScanNext ret