Z80-Assembler Release 1.8 Thu Sep 23 15:01:46 2021 Page 1 Source file: lcdtestmpf1.asm Title: LOC OBJECT CODE LINE STMT SOURCE CODE 1 1 ;******************** 2 2 ; Some simple test code to write commands and data to a 3 3 ; character LCD display (tested with a 2x16 display) 4 4 ; Based on https://bread80.com/2020/07/01/connecting-an-lcd-to-a-z80-with-two-glue-chips/ 5 5 ; https://github.com/Bread80/z80-character-lcd 6 6 ; 7 7 ; Tested with the z80asm assembler 8 8 ; fjkraan@electrickery.nl, 2021-09-23 9 9 ;******************** 10 10 11 11 ; Ports used 00c8 12 12 lcdCtrl equ 0C8h ;Port addresses. Change as needed. 00c9 13 13 lcdData equ lcdCtrl + 1 14 14 15 15 ; write 0001 16 16 lcd_cls equ 01h ; Clear display 0002 17 17 lcd_home equ 02h ; Return home 0004 18 18 lcd_entMd equ 04h ; Entry mode set (cursor move direction and display shift) 0001 19 19 lcd_entSh equ 01h ; Entry mode display shift bit 0002 20 20 lcd_entCd equ 02h ; Entry mode display cursor move diection bit 0008 21 21 lcd_donMod equ 08h ; Display on/off control (display, cursor, blink) 0004 22 22 lcd_donD equ 04h ; Display on/off control display bit 0002 23 23 lcd_donC equ 02h ; Display on/off control cursor bit 0001 24 24 lcd_donB equ 01h ; Display on/off control cursor blink bit 0010 25 25 lcd_cdShft equ 10h ; Cursor or display shift 0008 26 26 lcd_cdC equ 08h ; Cursor or display shift S/C (display / cursor) 0004 27 27 lcd_cdR equ 04h ; Cursor or display shift R/L (right / left) 28 28 003f 29 29 lcd_set8bit equ 3fh ; 8-bit port, 2-line display 000f 30 30 lcd_curOn equ 0fh ; Turn cursors on (lcd_donMod + lcd_donD + lcd_donC + lcd_donB) 31 31 0040 32 32 lcd_setcgradd equ 40h ; Set CGRAM address (bit 0-5 contain address) 0080 33 33 lcd_setdramad equ 80h ; Set DDRAM address (bit 0-6 contain address) 34 34 0040 35 35 lcd_2ndrow equ 40h ; Default address 1st char, 2nd row 36 36 37 37 ; read 0080 38 38 lcd_statM equ 80h ; Bit 7 contains busy flag 004f 39 39 lcd_addr equ 4Fh ; Bit 0-6 contain address counter 40 40 41 41 org 2000h 42 42 43 43 ;Initialisation 2000 3e 3f 44 44 ld a,lcd_set8bit 2002 cd 48 20 45 45 call lcdSendCmd 46 46 2005 3e 0f 47 47 ld a,lcd_curOn 2007 cd 48 20 48 48 call lcdSendCmd 49 49 200a 3e 01 50 50 ld a,lcd_cls 200c cd 48 20 51 51 call lcdSendCmd 52 52 53 53 ;Send a single character 200f 3e 3e 54 54 ld a,'>' 2011 cd 50 20 55 55 call lcdSendData 56 56 57 57 ;Send a string 2014 21 2b 20 58 58 ld hl,hello_world 2017 cd 5a 20 59 59 call lcdSendAsc 60 60 Z80-Assembler Release 1.8 Thu Sep 23 15:01:46 2021 Page 2 Source file: lcdtestmpf1.asm Title: LOC OBJECT CODE LINE STMT SOURCE CODE 61 61 ;2nd line 201a 3e c0 62 62 ld a, lcd_setdramad + lcd_2ndrow 201c cd 48 20 63 63 call lcdSendCmd 64 64 ; 201f 21 3a 20 65 65 ld hl,mpf1 2022 cd 5a 20 66 66 call lcdSendAsc 67 67 2025 3e 02 68 68 ld a, lcd_home 2027 cd 48 20 69 69 call lcdSendCmd 70 70 202a 76 71 71 halt 72 72 73 73 hello_world: 202b 20 48 65 6c 74 74 DEFB ' Hello, world!', 0 202f 6c 6f 2c 20 74 75 2033 77 6f 72 6c 74 76 2037 64 21 00 74 77 75 78 76 79 mpf1: 203a 20 20 20 66 77 80 DEFB ' from MPF-I', 0 203e 72 6f 6d 20 77 81 2042 4d 50 46 2d 77 82 2046 49 00 77 83 78 84 79 85 ;****************** 80 86 ;Send a command byte to the LCD 81 87 ;Entry: A= command byte 82 88 ;Exit: All preserved 83 89 ;****************** 84 90 lcdSendCmd: 2048 c5 85 91 push bc ; Preserve 2049 cd 6d 20 86 92 call lcdWait 87 93 204c ed 79 88 94 out (c), a ; Send command 204e c1 89 95 pop bc ; Restore 204f c9 90 96 ret 91 97 92 98 ;****************** 93 99 ;Send a data byte to the LCD 94 100 ;Entry: A= data byte 95 101 ;Exit: All preserved 96 102 ;****************** 97 103 lcdSendData: 2050 c5 98 104 push bc ; Preserve 2051 cd 6d 20 99 105 call lcdWait 100 106 2054 0e c9 101 107 ld c, lcdData ; Data port 2056 ed 79 102 108 out (c), a ; Send data 2058 c1 103 109 pop bc ; Restore 2059 c9 104 110 ret 105 111 106 112 ;****************** 107 113 ;Send an asciiz string to the LCD 108 114 ;Entry: HL=address of string 109 115 ;Exit: HL=address of ending zero of the string. All others preserved 110 116 ;****************** 111 117 lcdSendAsc: 205a f5 112 118 push af 205b c5 113 119 push bc ; Preserve 114 120 lcdAscC Z80-Assembler Release 1.8 Thu Sep 23 15:01:46 2021 Page 3 Source file: lcdtestmpf1.asm Title: LOC OBJECT CODE LINE STMT SOURCE CODE 205c cd 6d 20 115 121 call lcdWait 116 122 205f 7e 117 123 ld a,(hl) ; Get character 2060 a7 118 124 and a ; Is it zero? 2061 28 07 119 125 jr z,lcdAscD ; If so, we're done 120 126 2063 0e c9 121 127 ld c,lcdData ; Data port 2065 ed 79 122 128 out (c),a ; Send data 2067 23 123 129 inc hl ; Next char 2068 18 f2 124 130 jr lcdAscC 125 131 126 132 lcdAscD: 206a c1 127 133 pop bc ; Restore 206b f1 128 134 pop af 206c c9 129 135 ret 130 136 131 137 ; Wait for the busy flag (BF) to be cleared. This hangs with no display detected. 132 138 lcdWait: ; Destroys bc 206d 0e c8 133 139 ld c, lcdCtrl ; Command port 134 140 lcdWaitL: 206f ed 40 135 141 in b, (c) ; Read status byte 2071 cb 20 136 142 sla b ; Shift busy bit (7) into carry flag 2073 38 fa 137 143 jr c, lcdWaitL ; While busy 2075 c9 138 144 ret