• No results found

Interfacing the HC(S)12 microcontroller to a Hitachi/Optrex LCD Display Panel

N/A
N/A
Protected

Academic year: 2020

Share "Interfacing the HC(S)12 microcontroller to a Hitachi/Optrex LCD Display Panel"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

Interfacing the HC(S)12 microcontroller to a

Interfacing the HC(S)12 microcontroller to a

Hitachi/Optrex LCD Display Panel

Hitachi/Optrex LCD Display Panel

The LCD display panel contains its own microcontroller (Hitachi

The LCD display panel contains its own microcontroller (Hitachi

HD44780) that communicates with an external microcontroller over a

HD44780) that communicates with an external microcontroller over a

relatively simple interface and also controls the LCD display driver IC.

relatively simple interface and also controls the LCD display driver IC.

This LCD panel receives 8-bit binary commands and/or ASCII data

This LCD panel receives 8-bit binary commands and/or ASCII data

characters from the external microcontroller (our CSM12C32 module).

characters from the external microcontroller (our CSM12C32 module).

Communication between the external microcontroller and the

Communication between the external microcontroller and the

HD44780 LCD panel microcontroller requires eight or four data lines

HD44780 LCD panel microcontroller requires eight or four data lines

(in 4-bit mode, the 8-bit data characters and commands are sent as

(in 4-bit mode, the 8-bit data characters and commands are sent as

two back-to-back 4-bit nybbles), and two control lines: a data enable

two back-to-back 4-bit nybbles), and two control lines: a data enable

clock line (E), and a Command/Data Register Select (RS) line.

clock line (E), and a Command/Data Register Select (RS) line.

When 8 data lines are used, communication is faster, but requires four

When 8 data lines are used, communication is faster, but requires four

more external microcontroller I/O pins.

more external microcontroller I/O pins.

The number of pins required at the external microcontroller may be

The number of pins required at the external microcontroller may be

reduced to three pins using an SPI port between the LCD panel and

reduced to three pins using an SPI port between the LCD panel and

the microcontroller!

(2)

Optrex and Hitachi LCD Panel Display

(3)
(4)

LCD Panel Pin Description

LCD Panel Pin Description

DB7:0 – Data Bus. 8-bit data bus for both display commands AND

DB7:0 – Data Bus. 8-bit data bus for both display commands AND

display ASCII data. 4-bit mode transfers occur as two

display ASCII data. 4-bit mode transfers occur as two

back-to-back 4-bit transfers on DB7:4 (DB3:0 are not connected.)

back 4-bit transfers on DB7:4 (DB3:0 are not connected.)

RS – Register Select. Set to 0 to indicate a command on Data Bus

RS – Register Select. Set to 0 to indicate a command on Data Bus

and 1 to indicate ASCII data on Data Bus.

and 1 to indicate ASCII data on Data Bus.

RW-Read/Write. Set to 0 for write to data bus and 1 for read from

RW-Read/Write. Set to 0 for write to data bus and 1 for read from

data bus.

data bus.

We shall strap this pin to 0 V, so that it is permanently

We shall strap this pin to 0 V, so that it is permanently

enabled for WRITE to data bus mode, in order to simplify our

enabled for WRITE to data bus mode, in order to simplify our

interface.

interface.

E – Enable (data clock). This pin must rise at least 140 ns after RS

E – Enable (data clock). This pin must rise at least 140 ns after RS

and R/W have been set up, and it must fall at least 195 ns after

and R/W have been set up, and it must fall at least 195 ns after

DB7:0 has been set up. Furthermore, DB7:0 must be held at least

DB7:0 has been set up. Furthermore, DB7:0 must be held at least

(5)
(6)

The setting of RS and R/W allows us to:

The setting of RS and R/W allows us to:

(1) write data to Display RAM (DR)

(1) write data to Display RAM (DR)

(2) Read data

(2) Read data

from DR

from DR

(3) Write commands to the display

(3) Write commands to the display

(4) read the busy flag and current value of the address counter

(4) read the busy flag and current value of the address counter

(which controls position of character being written to DR.)

(which controls position of character being written to DR.)

We shall tie R/W low so that we will only write commands and ASCII data to the LCD panel

(7)
(8)
(9)

Note: to minimize the complexity of our interface (at the

Note: to minimize the complexity of our interface (at the

expense of slowing data transfer to the LCD panel), we shall

expense of slowing data transfer to the LCD panel), we shall

use 4-bit mode, and we shall NOT check the LCD panel’s “Busy

use 4-bit mode, and we shall NOT check the LCD panel’s “Busy

Flag”. Instead, we shall allow plenty of extra time for the data

Flag”. Instead, we shall allow plenty of extra time for the data

transfer to complete and for the “Busy Flag” to be set.

(10)
(11)
(12)
(13)
(14)

xdef Entry

xdef Entry ;Entry point must be available to debugger;Entry point must be available to debugger xref

xref LCD_MESSAGELCD_MESSAGE ;These routines are in another file;These routines are in another file xref

xref LCD_INITLCD_INIT, PLL_INIT , PLL_INIT xref

xref __SEG_END_SSTACK ;High end of STACK area in RAM__SEG_END_SSTACK ;High end of STACK area in RAM include 'mc9s12c128.inc'

include 'mc9s12c128.inc'

CodeSec: SECTION ;relocatable code section

CodeSec: SECTION ;relocatable code section

Entry:

Entry: lds #__SEG_END_SSTACKlds #__SEG_END_SSTACK

jsr PLL_INITjsr PLL_INIT ;Set Bus Clock to 24 MHz;Set Bus Clock to 24 MHz

jsr LCD_INIT jsr LCD_INIT ;Init LCD Display;Init LCD Display

ldx #msg1ldx #msg1

ldab #0ldab #0 ;Display Data RAM address 0 = start of 1 ;Display Data RAM address 0 = start of 1stst line line

jsr LCD_MESSAGEjsr LCD_MESSAGE

ldab #$40 ;Display Data RAM address $40 = start of 2nd lineldab #$40 ;Display Data RAM address $40 = start of 2nd line

ldx #msg2ldx #msg2

jsr LCD_MESSAGEjsr LCD_MESSAGE wthr:

wthr: bra wthrbra wthr

ConstSec: SECTION ;relocatable constant section

ConstSec: SECTION ;relocatable constant section

msg1:

msg1: dc.b "hello there", 0dc.b "hello there", 0 ;ASCII null-terminated string;ASCII null-terminated string

Main.asm – Main program that calls the LCD display routine

Main.asm – Main program that calls the LCD display routine

(15)

;***************************************************** ;***************************************************** ;Type:

;Type: M68HCs12 Assembly Program for Code WarriorM68HCs12 Assembly Program for Code Warrior ;Program Name:

;Program Name: lcd4bit_displaylcd4bit_display ;Written By:

;Written By: Jianjian Song & Keith HooverJianjian Song & Keith Hoover ;Date:

;Date: October 5 2007October 5 2007

;Purpose: 4-Bit Mode LCD Panel interfacing via E, RS, DB7-4 ;Purpose: 4-Bit Mode LCD Panel interfacing via E, RS, DB7-4 ;Display Panel Connections

;Display Panel Connections

;1--Vss(0V), 2--Vcc(5V), 3--Vee (0V), 4--RS = PT2, 5--R/W = 0V, ;1--Vss(0V), 2--Vcc(5V), 3--Vee (0V), 4--RS = PT2, 5--R/W = 0V, ;6--E clock = PT3,11--DB4 = PT4,12--DB5 = PT5,13--DB6 = PT6 ;6--E clock = PT3,11--DB4 = PT4,12--DB5 = PT5,13--DB6 = PT6 ;14--DB7 = PT7.

;14--DB7 = PT7.

;**************************************************** ;****************************************************

xdef

xdef LCD_INIT ;MAKE THE FOLLOWING LCD_INIT ;MAKE THE FOLLOWING ;AVAILABLE TO OTHER FILE ;AVAILABLE TO OTHER FILE xdef

xdef LCD_DTALCD_DTA

xdef

xdef LCD_ADDRESSLCD_ADDRESS

xdef

xdef LCD_MESSAGELCD_MESSAGE

include 'mc9s12c128.inc' include 'mc9s12c128.inc'

;Initialize the LCD panel hardware interface ;Initialize the LCD panel hardware interface

LCD_DATA: EQU PTT LCD_DATA: EQU PTT LCD_CTRL: EQU PTT LCD_CTRL: EQU PTT

LCD_DATA_DIRECTION EQU DDRT LCD_DATA_DIRECTION EQU DDRT LCD_CTRL_DIRECTION EQU DDRT LCD_CTRL_DIRECTION EQU DDRT DATA_OUTPUT EQU %11110000 DATA_OUTPUT EQU %11110000

lcd_subroutines.asm” file (to be linked with

lcd_subroutines.asm” file (to be linked with

main.asm)

(16)

CTRL_OUTPUT EQU %00001100

CTRL_OUTPUT EQU %00001100

E:

E: EQUEQU %00001000 ;E = MASK TO ACCESS PT3 = LCD CONTROL LINE E%00001000 ;E = MASK TO ACCESS PT3 = LCD CONTROL LINE E RS:

RS: EQUEQU %00000100 ;RS = MASK TO ACCESS PT2 = LCD CONTROL LINE RS%00000100 ;RS = MASK TO ACCESS PT2 = LCD CONTROL LINE RS DataSec: SECTION

DataSec: SECTION

TIME:

TIME: DSDS 22 ;delay time variable;delay time variable CodeSec: SECTION

CodeSec: SECTION

;*************************************************************

;*************************************************************

;LCD_INIT subroutine

;LCD_INIT subroutine

;Initializes LCD Display according to manufacturer's directions

;Initializes LCD Display according to manufacturer's directions

;************************************************************

;************************************************************

LCD_INIT:

LCD_INIT:

BSET LCD_DATA_DIRECTION,DATA_OUTPUT ;MAKE PT7:4 DRIVE LCD DB7:4 DATA

BSET LCD_DATA_DIRECTION,DATA_OUTPUT ;MAKE PT7:4 DRIVE LCD DB7:4 DATA

INPUTS

INPUTS

BSET LCD_CTRL_DIRECTION,CTRL_OUTPUT ;MAKE PT2 & PT3 DRIVE LCD RS

BSET LCD_CTRL_DIRECTION,CTRL_OUTPUT ;MAKE PT2 & PT3 DRIVE LCD RS

;AND E CONTROL LINES;AND E CONTROL LINES BCLR

BCLR LCD_CTRL,E LCD_CTRL,E ; Set E to 0; Set E to 0 BCLR

BCLR LCD_CTRL,RSLCD_CTRL,RS ; Set RS=0 to select instruction entry mode; Set RS=0 to select instruction entry mode BCLR

BCLR LCD_DATA,DATA_OUTPUT ;Set 4-bit data output to 0LCD_DATA,DATA_OUTPUT ;Set 4-bit data output to 0

; 1st wait for 20 milliseconds (minimum of 15 ms)

; 1st wait for 20 milliseconds (minimum of 15 ms)

lcd_subroutines.asm, continued

(17)

; Note: the LCD display always powers up in 8-bit transfer mode, but even though all 8

; Note: the LCD display always powers up in 8-bit transfer mode, but even though all 8

; bits are transferred into the display module when E falls, the bottom 4 bits of each

; bits are transferred into the display module when E falls, the bottom 4 bits of each

; byte are IGNORED when an INIT command is sent, so it is OK that the bottom 4 bits of the

; byte are IGNORED when an INIT command is sent, so it is OK that the bottom 4 bits of the

; LCD panel data bus (DB3:0) are not connected to anything!

; LCD panel data bus (DB3:0) are not connected to anything!

; send the 8-bit mode INIT command 3 times in a row (as recommended by manufacturer)

; send the 8-bit mode INIT command 3 times in a row (as recommended by manufacturer)

; Here is the first INIT command

; Here is the first INIT command

LDAB #$30

LDAB #$30

JSR SEND4bitsJSR SEND4bits

; wait for 10 ms ( minimum of 4.1 ms) before sending next INIT command.

; wait for 10 ms ( minimum of 4.1 ms) before sending next INIT command.

MOVW

MOVW #200, TIME#200, TIME JSR

JSR VAR_DELAYVAR_DELAY ; send second INIT command

; send second INIT command

LDAB #$30

LDAB #$30

JSR SEND4bits; 3rd wait for 1 millisecond (minimum of 0.1 ms)JSR SEND4bits; 3rd wait for 1 millisecond (minimum of 0.1 ms) MOVW

MOVW #20, TIME#20, TIME

lcd_subroutines.asm, continued

(18)

JSR

JSR VAR_DELAYVAR_DELAY

LDAB #$30 LDAB #$30

JSR SEND4bitsJSR SEND4bits ; Send third INIT command; Send third INIT command

MOVWMOVW #20, TIME#20, TIME

JSR

JSR VAR_DELAYVAR_DELAY

; Now send a fourth init command so that it changes the data transfer mode ; Now send a fourth init command so that it changes the data transfer mode ; from 8-bits all at once to the 4-bit transfer mode, in which an 8-bit

; from 8-bits all at once to the 4-bit transfer mode, in which an 8-bit ; byte is transferred over the most-significant 4 bits of the display panel ; byte is transferred over the most-significant 4 bits of the display panel ; data bus (DB7:4)by two back-to-back calls to SEND4bits

; data bus (DB7:4)by two back-to-back calls to SEND4bits LDAB #$20

LDAB #$20

JSR SEND4bitsJSR SEND4bits

MOVWMOVW #20, TIME#20, TIME

JSR

JSR VAR_DELAYVAR_DELAY

; This command sets data transfer mode to 4-bit mode, so now a full 8-bits are transferred ; This command sets data transfer mode to 4-bit mode, so now a full 8-bits are transferred ; by two back-to-back calls to SEND4bits

; by two back-to-back calls to SEND4bits

LDAB #$28LDAB #$28

JSR SENDBYTE ; Function Set Command Format: 0 0 1 DL N F * *JSR SENDBYTE ; Function Set Command Format: 0 0 1 DL N F * * ; We just sent: 0 0 1 0 1 0 0 0

; We just sent: 0 0 1 0 1 0 0 0 ; DL=0 => 4-bit mode,

; DL=0 => 4-bit mode, ; N=1 => 1/8 duty cycle ; N=1 => 1/8 duty cycle ; F=0 => 5 X 7 dot font ; F=0 => 5 X 7 dot font LDAB #$08

LDAB #$08

JSR SENDBYTE ;JSR SENDBYTE ; Display OFF command Display OFF command

lcd_subroutines.asm, continued

(19)

LDAB #$06 LDAB #$06

JSR SENDBYTE ;Entry mode set command format: 0 0 0 0 0 1 I/D SJSR SENDBYTE ;Entry mode set command format: 0 0 0 0 0 1 I/D S

;I/D = 1 => Increment display addr ptr.;I/D = 1 => Increment display addr ptr.

;S = 0 => Do not shift (scroll) display ;S = 0 => Do not shift (scroll) display LDAB #$01

LDAB #$01

JSR SENDBYTE;JSR SENDBYTE; ;Clear display and return home ;Clear display and return home LDAB #$0F

LDAB #$0F

JSR SENDBYTEJSR SENDBYTE ; Display ON command ; Display ON command

RTSRTS

;******************************************************** ;********************************************************

lcd_subroutines.asm, continued

(20)

;*******************************************************************

;*******************************************************************

; Subroutine SEND4bits drives 4-bit data out on upper 4 bits of LCD data port

; Subroutine SEND4bits drives 4-bit data out on upper 4 bits of LCD data port

; then, after > 1 us delay, raises the E line, then, after > 1 us delay

; then, after > 1 us delay, raises the E line, then, after > 1 us delay

; lowers the E line to complete the write cycle, then, after > 1 us delay

; lowers the E line to complete the write cycle, then, after > 1 us delay

; returns.

; returns.

;******************************************************************* ;******************************************************************* SEND4bits: SEND4bits: PSHBPSHB

ANDB #$F0ANDB #$F0

LDAA LCD_DATA ; Load high 4 bits of accum B into LCD_DATA (PTT), butLDAA LCD_DATA ; Load high 4 bits of accum B into LCD_DATA (PTT), but

ANDA #$0F ; leave the bottom 4 bits untouched (since E and RS areANDA #$0F ; leave the bottom 4 bits untouched (since E and RS are

STAA LCD_DATA ; in the bottom 4 bits!)STAA LCD_DATA ; in the bottom 4 bits!)

ORAB LCD_DATAORAB LCD_DATA

STAB LCD_DATA ;DRIVE 4-BIT DATA OUT ON UPPER 4 BITS OF DATA PORTSTAB LCD_DATA ;DRIVE 4-BIT DATA OUT ON UPPER 4 BITS OF DATA PORT

JSR WT5USJSR WT5US

BSET LCD_CTRL,E

BSET LCD_CTRL,E ; RAISE E LINE; RAISE E LINE JSR WT5US

JSR WT5US ; LET IT STAY HIGH FOR > 1 US ; LET IT STAY HIGH FOR > 1 US BCLR

BCLR LCD_CTRL,E LCD_CTRL,E ; LOWER IT; LOWER IT MOVW

MOVW #2, TIME #2, TIME ; Wait 1 us for BF flag; Wait 1 us for BF flag JSR

JSR VAR_DELAYVAR_DELAY

lcd_subroutines.asm, continued

(21)

; Subroutine WT5US waits approximately 5 us, then returns

; Subroutine WT5US waits approximately 5 us, then returns

;********************************************************

;********************************************************

WT5US:

WT5US:

PSHY ;WAIT ABOUT 5 USPSHY ;WAIT ABOUT 5 US

LDY #10LDY #10 WTHERE:

WTHERE:

DBNE Y,WTHEREDBNE Y,WTHERE PULYPULY RTSRTS ;**************************************************************** ;****************************************************************

; Subroutine: VAR_DELAY

; Subroutine: VAR_DELAY

; Delays for a period of time equal to Tdelay = TIME*50 microseconds

; Delays for a period of time equal to Tdelay = TIME*50 microseconds

; This assumes a 24MHz bus clock, and that the RAM location

; This assumes a 24MHz bus clock, and that the RAM location

;

; "TIME" is a input variable that must be loaded prior to calling VAR_DELAY"TIME" is a input variable that must be loaded prior to calling VAR_DELAY ;

; Note: The inner loop delay time = (4*300+4)*(1/24MHz)=50 microsecondsNote: The inner loop delay time = (4*300+4)*(1/24MHz)=50 microseconds ;****************************************************************

;****************************************************************

LOOPS EQU 2400

LOOPS EQU 2400

VAR_DELAY:

VAR_DELAY:

PSHX

PSHX ; 2 cycles; 2 cycles PSHY

PSHY ; 2 cyles; 2 cyles LDY TIME

LDY TIME ;3 cycles;3 cycles LP1:

LP1: LDXLDX #LOOPS#LOOPS ; 2 cycle; 2 cycle LP2:

LP2: DEXDEX ; 1 cycle; 1 cycle BNE

BNE LP2LP2 ; 3 cycles; 3 cycles DEY

DEY ; 1 cycles; 1 cycles BNE

(22)

;********************************* ;********************************* ; SUBROUTINE: SENDBYTE

; SUBROUTINE: SENDBYTE

; Writes a byte in Accumulator B to LCD panel in "4-bit transfer" mode ; Writes a byte in Accumulator B to LCD panel in "4-bit transfer" mode ; via two back-to-back 4-bit writes over bits DB7:4)

; via two back-to-back 4-bit writes over bits DB7:4) ;********************************* ;********************************* SENDBYTE: SENDBYTE: PSHB PSHB

; send higher nibble out on DB7:4 first ; send higher nibble out on DB7:4 first

JSR SEND4bits JSR SEND4bits

MOVW #2,TIME ;Wait 100 us after first 4-bit transfer cycle MOVW #2,TIME ;Wait 100 us after first 4-bit transfer cycle JSR VAR_DELAY

JSR VAR_DELAY PULB

PULB PSHB PSHB

; rotate lower nibble up into B7:4 ; rotate lower nibble up into B7:4

ROLB ROLB ROLB ROLB ROLB ROLB ROLB ROLB

; send lower nibble out on DB7:4 last ; send lower nibble out on DB7:4 last

JSR SEND4bits JSR SEND4bits MOVW

MOVW #2,TIME#2,TIME

JSR

JSR VAR_DELAY ;Wait 100 us after second 4-bit transfer cycleVAR_DELAY ;Wait 100 us after second 4-bit transfer cycle

lcd_subroutines.asm, continued

(23)

;*********************************

;*********************************

; SUBROUTINE: LCD_ADDRESS

; SUBROUTINE: LCD_ADDRESS

; Sends an address in Accumulator B to LCD and thereby positions cursor

; Sends an address in Accumulator B to LCD and thereby positions cursor

; to any arbitrary position on display. Addr $00...$17 => 1st row of display

; to any arbitrary position on display. Addr $00...$17 => 1st row of display

; Addr $40...$57 => second row of display.

; Addr $40...$57 => second row of display.

;********************************* ;********************************* LCD_ADDRESS: LCD_ADDRESS: PSHB PSHB

BCLR LCD_CTRL, RS ; Place LCD in command mode (RS = 0)

BCLR LCD_CTRL, RS ; Place LCD in command mode (RS = 0)

ORAB #$80 ; Set MSB so the DDR address set command is sent

ORAB #$80 ; Set MSB so the DDR address set command is sent

JSR SENDBYTEJSR SENDBYTE PULB PULB RTS RTS ;********************************* ;*********************************

; SUBROUTINE: LCD_DTA

; SUBROUTINE: LCD_DTA

;

; displays 8-bit ASCII-coded character in B register displays 8-bit ASCII-coded character in B register ;********************************* ;********************************* LCD_DTA: LCD_DTA: PSHB PSHB

BSET LCD_CTRL, RS ; Place LCD in ASCII CHARACTER DISPLAY mode

BSET LCD_CTRL, RS ; Place LCD in ASCII CHARACTER DISPLAY mode

JSR SENDBYTEJSR SENDBYTE ; (RS = 1) ; (RS = 1)

(24)

;*********************************

;*********************************

; SUBROUTINE: LCD_MESSAGE(B,X)

; SUBROUTINE: LCD_MESSAGE(B,X)

; Displays an ASCII text message to LCD.

; Displays an ASCII text message to LCD.

; Accumulator B contains the address to write to

; Accumulator B contains the address to write to

; For 2 x 24 display, Line 1: 0 - 0x17, Line 2: 0x40..0x57

; For 2 x 24 display, Line 1: 0 - 0x17, Line 2: 0x40..0x57

; X register contains starting address of the ASCII-coded text message

; X register contains starting address of the ASCII-coded text message

; The text message must be terminated in an ASCII NULL ($00) character.

; The text message must be terminated in an ASCII NULL ($00) character.

;********************************* ;********************************* LCD_MESSAGE: LCD_MESSAGE: PSHBPSHB PSHX PSHX JSR

JSR LCD_ADDRESSLCD_ADDRESS ; send address to LCD; send address to LCD NEXT:

NEXT: LDABLDAB 0, X0, X ; load character; load character BEQ

BEQ DONEDONE ; exit if character is 0 (ASCII Null); exit if character is 0 (ASCII Null) JSR

JSR LCD_DTALCD_DTA ; send character in Accumulator A to LCD ; send character in Accumulator A to LCD INX

INX

BRA

BRA NEXTNEXT DONE:

DONE: PULXPULX PULB

PULB

lcd_subroutines.asm, continued

References

Related documents