78
Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/ [email protected] Chapter 3 Interfacing to a microprocessor ECE 3120

Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Dr. Mohamed Mahmoud

http://iweb.tntech.edu/mmahmoud/[email protected]

Chapter 3

Interfacing to a microprocessor

ECE 3120

Page 2: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Outline

3.1 Basic Concepts of Parallel I/O Ports

3.2 Interfacing with Simple I/O Devices

3.3 Advanced Parallel I/O Devices

Page 3: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Output port

Output portInput port

Input port

Input port

3 - 1

- Input and output (I/O) ports connect I/O devices to the microcontroller

- Examples of output devices: 7 segments, LEDs, LCDs,.. etc

- Examples of input devices: switches, pushbuttons, keypad, .. etc

- Using output ports, data (zeroes and ones) flows from microcontroller to output device

- Using input ports, data flows from input devices to the microcontroller

Page 4: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- I/O ports are connected to the data, address, and control buses

- Output ports read from data bus

- Input ports write to the data bus

- Similar to the memory locations, each port has a unique address

- The address on the address bus can enable only one port

How the microprocessor can read from/write to I/O ports

3 - 2

Page 5: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

The HCS12 I/O ports

Available in H family devices onlyOur board’s has D family microcontroller

3 - 3

Page 6: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Port APort B

Port B Port HPort H Port EPort E

Port SPort P

Port P

Port T

Port T

JK

K

Port PAD

M J

3 - 4

Page 7: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- Each I/O port has a set of pins and several registers to support its operation.

- Registers of I/O ports have unique addresses

- Memory instructions can be used with I/O registers

- Each port has two main registers

1. Data Direction Register configures the ports as input or output

2. Data Register by which data is exchanged with peripheral

1. Data Direction Register:

- Can configure each bit individually as input or output.

- To configure a pin for output, write a ‘1’ to the associated bit in the data direction register.

- To configure a pin for input, write a ‘0’ to the associated bit in the data direction register.

3 - 5

Page 8: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- The addresses of the data direction registers are defined in mc9s12dg256.inc with using EQU directives.

- Example: DDRA equ $2

- Name is formed by adding the letters “DDR” as the prefix to the port name. For example, DDRA, DDRB, and DDRT.

movb #$FF,DDRA ; configure Port A for output

movb #0,DDRB ; configure Port B for input

movb #$AA,DDRB ; $AA = %1010 1010 configured Port A odd pins for output, and even pins for input

movb #$0F,DDRB ; configure the first 4 bits in Port B asoutput an the last 4 bits as input

bset DDRA,$81 ; $81 = %1000 0001 configure Port A pin 7 and 1 for output

3 - 6

Page 9: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

0

1

Outside worldMicrocontroller

Simple 2-bit port

Tri state Buffer

0

1

=

Output = input

1

0

=

3 - 7

Page 10: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

2. I/O Data Register:

- When inputting data from input port, the user reads from the port data register.

- When outputting data to an output port, the user writes to the port data register.

- Each I/O data register is assigned to an address. For example, Port A data register is assigned to address 0

- The addresses of the data registers are defined in mc9s12dg256.inc

- Using names to access the I/O registers can improve program readability and reduce the chance of errors.

- The name of port data register is formed by adding letters “PT” or “PORT” as the prefix to the port name. For example, PORTA, PORTB, and PTT. See slide 3-3

3 - 8

Page 11: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

movb #$35,0 ;output $35 to Port A

PORTA equ 0 ; defined in mc9s12dg256.inc

movb #$35, PORTA ; output $35 to Port A

staa PORTB ; output the contents of A to port B

movb #$67,PORTB ; output the value $67 to Port B

movb PORTB,ibuf ; read the contents of Port B and save themat the memory location represented by ibuf

ldaa PORTA ; accumulator A = port B input

3 - 9

Notice: all the instructions used for memory locations can be used with ports

Equal

Page 12: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Outline

3.1 Basic Concepts of Parallel I/O Ports

3.2 Interfacing with Simple I/O Devices

3.3 Advanced Parallel I/O Devices

Page 13: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 10

1- Interfacing with LEDs

LED Circuit in the Draong12-Plus Demo Board

F ig u re 4 .1 6 C ircu it co n n e c tio n fo r E x a m p le 4 .1 2

P B 3

P B 2

P B 1

P B 0

H C S1 2

P B 7

P B 6

P B 5

P B 4

1.5 K

P J1

When Port J pin 1 (PJ1) is low, the LEDs are enabled to light

When PJ1 is high, the LEDs are disabled

When LEDs are enabled, a LED is on when its bit on port B has one

7 segment displays and the LEDs are connected to port B.

When using the 7 segment display, PJ1 is used to disable the LEDs.

Page 14: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 11

Page 15: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

A flashing LED

- A LED can flash by repeatedly pulling its pin to high for a certain amount of time and then pulling it to low for some time.

Example: Write a program to repeatedly turn a LED on for 1 second and turn it off for 1 second.

Steps

1. Configure the pin 0 of port B (PB0) and pin 1 of port J (PJ1) for output2. Disable the 7 segments 3. Pull the PJ1 pin to low to enable the LEDs4. Pull the PB0 pin to high5. Wait for 1 Sec 6- Pull PB0 pin to low7. Wait for 1 Sec 8. Go to step 4 3 - 121 sec

1 sec

The LED pin

Page 16: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc’

org $1500lds #$2500

bset DDRJ,$02 ;configure PJ1 pin for outputbclr PTJ,$02 ;enable LEDs to light

movb #$FF,DDRP ; disable 7 segments that are connected

movb #$0F,PTP ; ‘’ ‘’ ‘’bset DDRB,#%00000001 ;configure PB0 pin for output

forever: bset PORTB,#%00000001 ; pull PB0 pin to high

ldy #1000 ; wait for 1000 ms = 1 secjsr Delay_yms ; “

bclr PORTB,#%00000001 ; pull PB0 pin to low

ldy #1000 ; wait for 1000 ms = 1 secjsr Delay_yms ; “

bra forever3 - 13

Page 17: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To make this delay, we need 1ms/41.67 ns E-clocks = 24000 E-clocks. One way to do that is a loop for 1000 times for a sequence of code that needs 24 E-cycles

Delay_yms: ; subroutine to make a delay of Y ms

pshx

outerloop: ldx #1000

innerloop: psha ; 2 E cyclespula ; 3 E cyclespsha ; 2 E cyclespula ; 3 E cyclespsha ; 2 E cyclespula ; 3 E cyclespsha ; 2 E cyclespula ; 3 E cyclesnop ; 1 E cyclenop ; 1 E cyclenop ; 1 E cyclenop ; 1 E cycle

dbne x,innerloop

dbne y,outerloop

pulx

rts

24 E-clocks 24,0000 E-clocks= 1 ms delay

Y x 1 ms delay

3 - 14

Page 18: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Generating a square signal

- A square wave can be generated by repeatedly pulling a pin to high for certain amount of time and then pulling it to low for some time.

Example: Write a program to generate a 1-kHz periodic square wave from the PT5 pin.

Steps

1. Configure the PT5 pin for output

2. Pull the PT5 pin to high

3. Wait for 0.5 ms

4- Pull PT5 pin to low

5. Wait for 0.5ms

6. Go to step 23 - 15

0.5 ms

0.5 ms

Page 19: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc’

org $1500lds #$2500

bset DDRT,#%00100000 ;configure PT5 pin for output

Forever: bset PTT,#%00100000 ; pull PT5 pin to high

ldy #10 ; wait for 0.5 msjsr delay_50Yus ; “

bclr PTT, #%00100000 ; pull PT5 pin to low

ldy #10 ; wait for 0.5 msjsr delay_50Yus ; “

bra forever

delay_50Yus is a subroutine that makes a delay equal to Y x 50usY is the content of register Y

Can you write the code of delay_50Yus? 3 - 16

Page 20: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Square waveforms at all pins of port B with different frequencies

Pin 0: 10KHzPin 1: 5KHzPin 2: 2.5 KHzPin 3: 1.25 KHz-------

0 10101010

0 01100110

0 00011110

0 000 00001

0 00000000

0 00000000

0 00000000

0 00000000

Pin 0Pin 7

3 - 17

1- PortB = 0 2- Wait 50us3- Increment PortB4- Loop to 2

At pin 0: duration of 0 and 1 is 50usAt pin 1: duration of 0 and 1 is double of those of pin 0 = 100usAt pin 2: duration of 0 and 1 is double of those of pin 1 = 200us

----------------------------------------------------------

50us50us50us

--------

Page 21: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc‘

org $1500lds #$2500

movb #%11111111,DDRB ;configure port B as output

movb #$FF,DDRP ; disable 7 segments that are connected

movb #$0F,PTP ; ‘’ ‘’ ‘’bset DDRJ,$02 ;configure PJ1 pin for outputbclr PTJ,$02 ;enable LEDs to lightclra

forever:

staa PORTB

ldy #1 ; wait for 50 usjsr delay_50Yus ; ‘’ ‘’ incabra forever

3 - 18

Page 22: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- To turn on the LEDs one at a time, we output the values %1000 0000, %0100 0000, ……………… %0000 0001, which are equivalent to $80, $40, …,$01.

- Only one bit is 1 and the LED that corresponds to this pin lights

Steps

1- Configure port B and pin 1 or port J as output, disable 7 segments 2- Store the values $80, $40, …., $01, $02,… ,$40 in a table. Use

register X to point to the starting address of the table. 3- PJ1 pin is low to enable the LEDs 4- Output the value pointed by X to port B and increment X5- Wait a second6- If X points to the end of the table, let X point to the beginning of

the table7- Go to step 2 3 - 19

Write a program to drive the LEDs so that one LED lights at a time from top to bottom and then from bottom to top with each LED lighted for half a second.

Page 23: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

org $1000lpcnt ds.b 1led_tab dc.b $80,$40,$20,$10,$08,$04,$02,$01

dc.b $02,$04,$08,$10,$20,$40

org $1500lds #$2500movb #$FF,DDRB ;configure port B for outputbset DDRJ,$02 ;configure PJ1 pin for outputbclr PTJ,$02 ;enable LEDs to lightmovb #$FF,DDRP ; disable 7 segments that are connecetdmovb #$0F,PTP ; ‘’

forever: movb #14,lpcnt ;initialize LED pattern countldx #led_tab ;Use X as the pointer to LED pattern table

led_lp: movb 0,x,PORTB ;turn on one LEDinxldy #1000 ; wait for half a secondjsr Delay_yms; "dec lpcnt ; reach the end of the table yet?bne led_lp

bra forever ; start from beginning 3 - 20

Page 24: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- Seven-segment displays are mainly used to display decimal digits and a small set of letters.

- In dragon board, segments a-g are connected to the pins PB0 to PB6- The microcontroller must send an appropriate value to Port B in order

to display a certain value.

2- Interfacing with Seven-Segment Displays

3 - 21

Page 25: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to make one 7-segment display count from 0 to 9 and repeats. The display should change every one second.

The circuit of the dragon board

Steps:-1- Make a table having a to g segments’ values for the digits 0, 1, 2, ..92- Enable only one display.3- X points at the beginning of the table4- Output the value pointed by X to port B 5- Wait one second6- If X points to the last element, go to 3 else go to 4

3 - 22

Display #3 is enabled if PP3 is 0

Page 26: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

Org $1000DispTab dc.b $3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F

org $1500lds #$2000movb #$FF,DDRB ; Configure port B as output portmovb #$0F,DDRP ; configure PP0 to PP3 as output pinsmovb #$FE,PTP ; enable the display #0bset DDRJ,$02 ;configure PJ1 pin for outputbset PTJ,$02 ;disable the LEDs

forever: ldx #DispTab ;set X to point to the display table

loopi: movb 0,x,PORTB ; output segment patterninx

ldy #1000jsr Delay_yms; wait for 1 second

cpx #DispTab+10 ; reach the end of the table?bne loopi

bra forever 3 - 23

Page 27: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- A time-multiplexing technique is used to display multiple digits simultaneously.

- Repeatedly, enable one display for a short period of time and then disable this display and enable the next one. Port B = the number you wanna display on the enabled display.

- Within a second, each display is lighted in turn many times. Due to the persistence of vision, all digits appear to be lighted at the same time.

Displaying Multiple Seven-Segment Displays

3 - 24

Only one bit in PP0 –PP3 is zero to enable one display at a time

Displays trick us

Page 28: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 – 25

- For Port P, to enable one display at a time, only one bit should be zero and the other bits should be ones.

- To enable a display, its cathode should be 0.

- To display 3 on the display #3

movb #$FF,DDRB ; configure Port B for output

movb #$0F,DDRP ; configure P0-P3 for output

movb #%0000 0111,PTP ; enable display #3

movb #$4F,PORTB ; send out the segment pattern of 7

Page 29: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to display 3456 on the four seven-segment displays in the dragon board

Steps:-

1- Define a table with the values that should be outputted at ports B and P

2- Set X points at the address of the first element in the table

3- Output the byte pointed by X to Port B then increment X

4- Output the byte pointed by X to Port P then increment X

5- Wait 1ms

6- If X points at the last location go to 2 else go to 33 - 26

Page 30: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 27

include 'mc9s12dg256.inc'

Org $1000DispTab dc.b $4F,$0E

dc.b $66,$0Ddc.b $6D,$0Bdc.b $7D,$07

org $1500Start: lds #$2000

movb #$FF,DDRBmovb #$0F,DDRPbset DDRJ,$02 ;configure PJ1 pin for outputbset PTJ,$02 ;disable the LEDs

forever: ldx #DispTab ;set X to point to the display tableloopi: movb 0,x,PORTB ; output segment pattern

inxmovb 0,x,PTP ;output display select inxldy #1jsr Delay_yms ; wait for 1 mscpx #DispTab+8 ; reach the end of the table?bne loopi

bra forever

Page 31: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- In Dragon board there is a buzzer connected to pin 5 in port T

- A sound can be generated by creating a digital waveform in the audible range and using it to drive the buzzer.

- By alternating the frequency of the generated waveform between two different values, a two tone siren can be generated

- The duration of the siren tone is variable

- The siren would sound more urgent if the tone duration were shorter

3- Interfacing with Buzzer

Example Write a program to generate a two-tone siren that alternates between 250Hz and 500 Hz with each tone lasting for half of a second.

3 - 28

Page 32: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

org $1500lds #$2500bset DDRT,#%00100000; configure PT5 pin for output

forever:

ldx #250 ;0.5 second should have 250 cycles from 500 Hz signaltone1: bset PTT,#%00100000 ; pull PT5 pin to high

ldy #1jsr Delay_ymsbclr PTT,#%00100000ldy #1jsr Delay_ymsdbne x,tone1

ldx #125 ;0.5 second should have 125 cycles from 250Hz signaltone2: bset PTT,#%00100000

ldy #2jsr Delay_ymsbclr PTT,#%00100000ldy #2jsr Delay_ymsdbne x,tone2bra forever 3 - 29

1 ms

1 ms

500 Hz signal

2 ms

2 ms

250Hz signal

Page 33: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

4- Interfacing with DIP switches and push buttons

- In the Dragon board, The four push buttons are connected to PH0 – PH3 while the eight dip switches are connected to PH0 - PH7

1- The port bit is 1 if the push button is released, and 0 if pressed.2- The port bit is 1 if the DIP switch is open, and 0 if closed.

A port bit is zero if DIP switch or push button is connected. No way to know which one caused the zero.

one push button and one dip switch.

3 - 30

Page 34: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- How to read the status of the switches

movb #0,DDRH ; configure port H for inputldaa PTH ; read into accumulator A

- Check the individual bits of A to know the status of the switches.

- A switch is closed if the corresponding bit is 0

Steps:1. Configure port B (LEDs’ port) and pin1 in port J (to enable LEDs) for

output. Enable the LEDs and disable the 7 segments.2- Configure port H (switches’ port) for input.3. Read switches of port H, and complement the number to make bit 1

corresponds to connected switch and thus turns a LED on4. Send the number to the LEDs at port B5. Go to 3

Example: Write a program which continuously monitors the dip switches on the Dragon12 and echo the switches on the LEDs. A LED is on if the corresponding switch is connected.

3 - 31

Page 35: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

org $1500

movb #$FF,DDRB ;configure port B for outputbset DDRJ,$02 ;configure PJ1 pin for outputbclr PTJ,$02 ;enable LEDs to lightmovb #0,DDRH ; configure port A for input

movb #$FF,DDRP ; disable 7 segments that are connected

movb #$0F,PTP ; ‘’

forever: ldaa PTH ; read switches into accumulator Acoma ; bit =1 when a switch is pressed so

that a LED is on when switch is connectedstaa PORTB

bra forever ; start from beginning

In next slide, we update the siren program explained in slide 3-29 to use DIP switch 0 as on/off switch. If the switch is on, the Siren works and it does not work if the switch is off.

3 - 32

Page 36: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

org $1500lds #$2500bset DDRT,#%00100000; configure PT5 pin for outputmovb #0,DDRH ; configure port H for input

forever: brclr PTH,#%00000001,forever ; wait here if bit0 = 0

ldx #250 ;0.5 second should have 250 cycles from 500 Hz signaltone1: bset PTT,#%00100000 ; pull PT5 pin to high

ldy #1jsr Delay_ymsbclr PTT,#%00100000ldy #1jsr Delay_ymsdbne x,tone1ldx #125 ;0.5 second should have 125 cycles from 250Hz signal

tone2: bset PTT,#%00100000ldy #2jsr Delay_ymsbclr PTT,#%00100000ldy #2jsr Delay_ymsdbne x,tone2bra forever 3 - 33

Using on/off DIP switch

Page 37: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 34

Using on/off push button switch

Modify the siren program explained in slide 3-29 to use push button#0 as on/off switch.

- When the button is pressed, the port bit becomes 0 and when the button is released, the port bit becomes 1.

- When a DIP switch is connected there is a continuous zero on the corresponding bit, but when a push button is pressed, there will be a zero pulse for some time

- We need a variable to determine whether the operation should be on or off

- The variable alternates between on and off each time the switch is pressed.

Pressed Release

Page 38: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

include 'mc9s12dg256.inc'

Org $1000Onoff dc.b 0 ; Onoff = FF on, Onoff = 00 off

org $1500lds #$2500bset DDRT,#%00100000; configure PT5 pin for outputmovb #0,DDRH ; configure port A for input

forever: brset PTH,#%00000001,start ; go to start if no press

;switch is pressed ldaa Onoff ;complement onoffcomastaa Onoff

HH: brclr PTH,#%00000001,HH

; Debouncing code should be here

start: ldab Onoffcmpb #0beq forever ; do not run the siren

; buzzer should work 3 - 35

Page 39: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

ldx #250 ;0.5 second should have 250 cycles from 500 Hz signaltone1: bset PTT,#%00100000 ; pull PT5 pin to high

ldy #1jsr Delay_ymsbclr PTT,#%00100000ldy #1jsr Delay_ymsdbne x,tone1

ldx #125 ;0.5 second should have 125 cycles from 250Hz signaltone2: bset PTT,#%00100000

ldy #2jsr Delay_ymsbclr PTT,#%00100000ldy #2jsr Delay_ymsdbne x,tone2bra forever

3 - 36

Page 40: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

A common problem in mechanical switches

When a switch is asserted, we expect a signal like this:

However, the actual signal is:

Pressed Release

Switch bouncing

For a short period of time, the switch signal is bouncing.

5 to 20 ms 5 to 20 ms

3 - 37

Ideal switch

Real switch

Page 41: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Why there is bouncing? because switch contacts do not come to rest immediately

Solution: De-bouncing

1- H/W solutions

2- Software solutions

3 - 38

Problem: Although it is one press, the microcontroller may consider each low voltage as one press.

Humans cannot press a switch several time in 20 ms, but because the microcontroller is so fast, it can read several low voltages in 20 ms

Page 42: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

2- H/W solutions

Several methods.

A simple solution: using a capacitor

- When the switch is closed, Vout =0

- Because of bouncing the capacitor charges for a short time making a small voltage on the capacitor.

- This small voltage will be interrupted as logic 0

3 - 39

Page 43: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

2- Software solutions

5 to 20 ms 5 to 20 ms

When the port pin is 0, wait for around 20ms to bypass the bouncing.

ldy #20jsr Delay_yms

When the port pin is 1, wait for around 20ms to bypass the bouncing.

ldy #20jsr Delay_yms

Wait until the switch is released.

HH: brclr PTH,#%00000001,HH

To solve the bouncing problem in the program of slide 3-35, add these instruction before start label. 3 - 40

Page 44: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to turn on LED number 0 and turn off the other LEDs initially. Each time a push button number 3 is pressed, the LED is off an the next one on left is on. Always only one LED is on and each time the switch is pushed, the on LED shifts to left

Org $1000lds #$2500movb #$FF,DDRB ; port b is outputbset DDRJ,#$02 ;configure PJ1 pin for outputbclr PTJ,#$02 ;enable LEDs to lightmovb #%00000000,DDRH ; port H is input - push buttonsmovb #%00000001,PORTB ; first LED is on

movb #$FF,DDRP ; disable 7 segments that are connectedmovb #$0F,PTP ; ‘’

LOOP: brset PTH,#%00001000,LOOP ; loop as long a the switch is not pressed

ldy #25jsr Delay_yms

HH: brclr PTH,#%00001000,HH ldy #25jsr Delay_yms

3 - 41

Page 45: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to increment the number on two 7-segment displays when a push button is pressed and decrement the number when another push button is pressed

1- Read the switches 2- If increment button is pressed, increment the display3- If decrement button is pressed, decrement the display

3 - 42

lsl PORTBbne SKIP ; go to SKIP if PORTB does not have 0movb #%00000001,PORTB ; reinitialize to 01h

SKIP: bra LOOP

Page 46: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to display a number 7 on display 3 and each time a button is pushed the number moves to the next display

1- Port B = $07 to display 72- Port P = 0000 0001 to enable display 33- If the switch is not pressed, does nothing4- If the switch is pressed, shift left for port P to enable next display, If P = 00010000, then P = 0000 0001

3 - 43

Write a program to continuously read the value on switches and display it on 7 segment displays as decimal number. Max number should be 255

1- Read the switches, and complement the number.2- Convert the binary number to decimal

2.1 divide the number by 10 then digit 1 = remainder2.2 divide the quotient by 10 then digit 2 = remainder2.3 divide the quotient by 10 then digit 3 = remainder

3- display the three digits

Page 47: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Outline

3.1 Basic Concepts of Parallel I/O Ports

3.2 Interfacing with Simple I/O Devices

3.3 Advanced Parallel I/O Devices

Page 48: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- Array of switches.- When a switch is pressed, it connects two conductors one row and one column

Interfacing the HCS12 to a Keypad

The keypad’s connection in the Dragon board

PA3

PA2

PA1PA0

PA4

PA5

PA6

PA7

Input pins

Output pins

* 0 #

3 - 44

Page 49: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

PA3

PA2

PA1PA0

PA4

PA5

PA6

PA7* 0 #

1

1

1

1

00

00

no key is pressed

1- PA4 ~ PA7 = 11112- Read PA0 ~ PA33- If PA0 ~ PA3 = 0000, no key is pressed, else a key is pressed

1- Is any key pressed?

3 - 45

K2: MOVB #%11110000,PORTA LDAA PORTAANDA #%00001111CMPA #$00 BEQ K2

Page 50: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

2- What are the row and column numbers of the pressed key?

Assume key 9 is connected

1- Test if there is a key connected on row 0

(1) PA7 ~ P4 = 0001

Put 1 at the row you want to test

(2) Check PA0 to PA3

If all of them are zeros, then there isno key pressed in row 0

because if a key is pressed, it would connect the one at PA4 to one pin in PA0~PA3.

Move to the next row to check if a key is pressed in this row3 - 46

PA3PA2PA1PA0

PA4

PA5

PA6

PA7

0

000

1

0

0

0 * 0 #

Page 51: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

;;; Test Row 0LDAA #%00010000 ;MAKE HIGH ROW0 THE REST GROUNDEDSTAA PORTA ;LDAA PORTA ;READ PORTAANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;IS INPUT ZERO?BNE R0 ;IF COLUMS NOT ZERO THEN BUTTON IS IN ROW 0

PA3PA2PA1PA0

PA4

PA5

PA6

PA7

0

000

0

1

0

0 * 0 #

2- Test if there is a key connected on row 1

Similar to what we have done on row 0

No key is pressed on row 1

Move to the next row to check if a key is pressed

3 - 47

Page 52: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3- Test if there is a key connected on row 2

(1) PA7 ~ P4 = 0010

Put 1 at the row you want to test

(2) Check PA0 to PA3

Since PA0 to PA3 do not equal to 0000, there is a key connected in row 2

PA3PA2PA1PA0

PA4

PA5

PA6

PA7

0

100

0

0

1

0 * 0 #

Giving that PA6 is connected to PA2, we can know that switch 9 is pressed

3 - 48

;; test Row 2 LDAA #%01000000STAA PORTALDAA PORTAANDA #%00001111 CMPA #$00 BNE R2

Done

This branch will be taken

Page 53: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3- Finding the ASCII code of the pressed key

KCODE0 DC.B '123A' KCODE1 DC.B '456B' KCODE2 DC.B '789C' KCODE3 DC.B '*0#D'

1- Define a table with the keys’ ASCIIs

2- Using the pressed key’s row number and column number, get the pressed key’s ASCII

If row = 0, X = KCODE0If row = 1, X = KCODE1If row = 2, X = KCODE2If row = 3, X = KCODE3

R2: LDX #KCODE2 ; since the pressed key is on row 2 ; X = the beginning address of ROW2

BRA FIND ;GO FIND COLUMN

3 - 49

The pressed Key’s ASCII = X + column number

X has the beginning address of the row’s ASCIIS

Page 54: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

SHIFT:LSRA ;LOGICAL SHIFT RIGHT PORTABCS MATCH ;IF CARY SET COLUMN IS FOUNDINX ;IF CARY NOT CLEAR INCREMENT XBRA SHIFT ;SHIFT RIGHT UNTIL CARY IS CLEAR.

MATCH: ; X point at the address of key’s ASCII

LDAA 0,X ;LOAD ASCII FROM look up table3 - 50

A = PORTA SHIFT: Shift A to right

If carry = 1 got to MATCH

Elseincrement X

End IfGo to SHIFT

MATCH:// X = the address of the ASCII of the pressed key

Page 55: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 51

Write a subroutine called Keypad to read a character from the keypad and return the ASCII of the character in register A

keypad:; Read one character from keypad and returns it in A MOVB #$F0,DDRA ;Configure PA0-PA4 input as and PA5-PA7 outputPSHYPSHB

;; 1- If the user presses and holds a key down, this must be one press K1: MOVB #%11110000,PORTA ;SET ROWS HIGH

LDAA PORTA ;CAPTURE PORT AANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;BNE K1 ;IF COLUMS IS ZERO NO BUTTON PRESSED

;DO NOT MOVE ON UNTILL NO BUTTON IS PRESSED

;; 2- Check if a switch is pressedK2: ;

LDY 15JSR Delay_yms ; wait 15 msLDAA PORTA ;ANDA #%00001111 ;CMPA #$00 ;IF COLS !=0 THEN A BUTTON IS PRESSEDBEQ K2 ;IF NO BUTTON PRESSED KEEP CHECKING

Page 56: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 52

;FOR DEBOUNCING, WAIT 15ms AND CHECK AGAINLDY 15JSR Delay_ymsLDAA PORTA ;READ PORT AANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;CHECK FOR PRESS AFTER DEBOUNCEBEQ K2 ;IF NO PRESS AFTER DEBOUNCE GO BACK

; 3- A SWITCH IS PRESSED – FIND THE ROWOVER1:

;;; Test Row 0LDAA #%00010000 ;MAKE HIGH ROW0 THE REST GROUNDEDSTAA PORTA ;LDAB #$08 ;SET COUNT TO PROVIDE SHORT DELAY FOR STABILITY

;AFTER CHANGING THE PORT A OUTPUTP1: DECB ;DECREMENT COUNT

BNE P1 ;IF COUNT NOT ZERO KEEP DECREMENTINGLDAA PORTA ;READ PORTAANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;IS INPUT ZERO?BNE R0 ;IF COLUMS NOT ZERO THEN BUTTON IS IN ROW 0

Page 57: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 – 53

;; test Row 1LDAA #%00100000 ;IF ZERO THEN BUTTON NOT IN ROW0STAA PORTA ;TURN ON ROW 1 TURN OFF ALL OTHERSLDAB #$08 ;SHORT DELAY TO STABALIZE AFTER CHANGING THE PORT A

OUTPUTP2: DECB ;DECREMENT COUNT

BNE P2 ;IF COUNT NOT ZERO KEEP DECREMENTINGLDAA PORTA ;READ PORT AANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;CHECK FOR KEY PRESSBNE R1 ;IF PRESSED KEY IS IN ROW1

;; test Row 2 LDAA #%01000000 ;IF ZERO BUTTON NOT IN ROW1STAA PORTA ;TURN ON ROW2 ALL OTHERS OFFLDAB #$08 ;SHORT DELAY TO STABALIZE PORTA

P3: ;DECB ;DECREMENT COUNTBNE P3 ;DELAY LOOP LDAA PORTA ;READ PORTAANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;CHECK FOR PRESSBNE R2 ;IF FOUND KEY IS IN ROW2

Page 58: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 54

;;;;; test Row 3LDAA #%10000000 ;IF ZERO MOVE TO ROW3STAA PORTA ;TURN ON ROW3 ALL OTHERS OFFLDAB #$08 ;SHORT DELAY TO STABALIZE OUTPUT

P4: ;DECB ;DECREMENT DELAYBNE P4 ;DELAY LOOPLDAA PORTA ;READ PORT AANDA #%00001111 ;MASK OUT ROWSCMPA #$00 ;CHECK FOR PRESSBNE R3 ;IF FOUND KEY IN ROW3BRA K2 ;IF ROW NOT FOUND GO BACK TO START

;; ---------------------------------------------------------

R0: LDX #KCODE0 ;LOAD PONTER TO ROW0 ARRAYBRA FIND ;GO FIND COLUMN

R1: LDX #KCODE1 ;LOAD POINTER TO ROW1 ARRAYBRA FIND ;GO FIND COUMN

R2: LDX #KCODE2 ;LOAD PINTER TO ROW2BRA FIND ;GO FIND COLUMN

R3: LDX #KCODE3 ;LOAD POINTER TO ROW3BRA FIND ;GO FIND COLUMN

Page 59: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

FIND: ;We knew the row number. Now we need to know the column number to get the key ASCII

ANDA #%00001111 ;MASK OUT ROWS A = 0000 0001 or A = 0000 0010 or A = 0000 0100 or A = 0000 1000

SHIFT: ;LSRA ;LOGICAL SHIFT RIGHT PORTABCS MATCH ;IF CARY SET COLUM IS FOUNDINX ;IF CARY NOT CLEAR INCREMENT POINTER TO

ROW ARRAYBRA SHIFT ;SHIFT RIGHT UNTIL CARY IS CLEAR.

MATCH: ; X point at the address of key’s ASCIILDAA 0,X ;LOAD ASCII FROM look up table

PULBPULYrts ; end of keypad routine

; look up tableKCODE0 DC.B '123A' KCODE1 DC.B '456B' KCODE2 DC.B '789C' KCODE3 DC.B '*0#D' 3 - 55

Do not forget to includeDelay_yms subroutine inyour program. It is usedin Keypad subroutine

Page 60: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Using Keypad subroutine, write a program that reads a key from the keypad and display its ASCII on the LEDs

ABSENTRY Entry INCLUDE 'mc9s12dp256.inc'

ORG $2000Entry:

LDS #$2500 ;Initialize Stack

MOVB #$F0,DDRA ; configure PA0 – PA3 for input and PA4 – PA7 for output; ======== For LEDs =============

MOVB #$FF,DDRB ;MAKE PORTB OUTPUTMOVB #$02,DDRJ ;ENABLE LED ARRAY ON PORTB OUTPUTMOVB #$00,PTJ ;ENABLE LED ARRAY ON PORTB OUTPUTMOVB #$00,PORTB ;INITIALIZE PORT B

; ======== For Seven segments ============= MOVB #$0F,DDRP ;MOVB #$0F,PTP ;TURN OFF 7SEG LED

yy: BSR keypad ; returns the key’s ASCII in register ASTAA PORTB ;PUT ASCII TO PORTB BRA yy ;BACK TO START 3 - 56

Page 61: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

- In Dragon board, the LCD is connected to port K as shown in the figure

- RS pin is connected to PK0. RS = 0, write a command (clear, turn off, etc.)RS = 1, write data

- E is the enable. It is connected to PK1

- 4 bit from the data (or command) are connected to PK2 ~ PK5 3 - 57

Interfacing the HCS12 to a LCD

Page 62: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 58

- However, data and commands are 8 bits. We write to the LCD twice: 4 bits each time

- The Dragon board LCD has two lines and 16 character per line.

- The LCD has a RAM and the data you want to display should be written to the RAM.

- The address of the data RAM should be set up using a command before writing to the LCD.

Page 63: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

The procedure for writing a byte to the LCD data register1- Output the most significant 4 bits to PK2~PK5. 2- RS = high. // tell LCD you send data2- E = high and wait for some time to make sure that the internal operation is complete.4- E = low5- Repeat these procedures to send the least significant 4 bytes

Write a subroutine called PUTCLCD, to display the character in register A in the LCD

PUTCLCD: PSHA ;save a copy of the data in the stackANDA #$F0 ; Clear the lower 4 bits – output the upper 4 bits first LSRA ;match the upper 4 bits with the LCDLSRA ; two shifts because the 4 bits should start from bit 2STAA PORTKBSET PORTK,#$01 ;RS=1 tell LCD you send data BSET PORTK,#$02 ;EN = highNOP ; 3 NOP to extend the duration of ENNOPNOP 3 - 59

Page 64: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

BCLR PORTK,#$02 ;EN = low

PULA ; repeat previous instructions to output the other 4 bitsANDA #$0FLSLALSLASTAA PORTKBSET PORTK,#$01 ;RS=1 – we write dataBSET PORTK,#$02 ;EN = highNOPNOPNOPBCLR PORTK,#$02 ;E = low to complete the write cycleLDY #$01 ;delay is needed until the LCD does all the internalJSR Delay_yms ; operationsRTS

- Where the character will be written? As long as we do not specify, it will be next to the last character.

- Later we will discuss that we can specify the location of the character to be written

LDAA #’H’ JSR PUTCLCD

3 - 60

To display a character, put its ASCII in register A and call this subroutine.

Page 65: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a subroutine called CMD2LCD, to send a command in register A to the LCD

CMD2LCD : PSHA ;save a copy of the command in the stackANDA #$F0 ; Clear the lower 4 bits – output the upper 4 bits first LSRA ; match the upper 4 bits with the LCDLSRA ; two shifts because the 4 bits should start from bit 2STAA PORTKBCLR PORTK,#$01 ;RS select LCD instruction register

BSET PORTK,#$02 ;EN = high, 4bits of the command are sent with RS and EN NOP ; 3 NOP to extend the duration of ENNOPNOP

The procedure for writing a command1- Write the most significant 4 bits to PK2~PK5 2- RS = low.3- E = high and wait for some time to make sure that the internal operation is complete.4- E = low5- Repeat these procedures to send the least significant 4 bytes

3 - 61

Page 66: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

BCLR PORTK,#$02 ;EN = low

PULA ; repeat previous instructions to output the other 4 bitsANDA #$0FLSLALSLASTAA PORTKBCLR PORTK,#$01 ;RS select LCD data registerBSET PORTK,#$02 ;EN = highNOPNOPNOPBCLR PORTK,#$02 ;E = low to complete the write cycleLDY #$01 ;delay is needed until the LCD does all the internalJSR Delay_yms ; operationsRTS

When you want to send a command to the LCD, put the command in register A and call this subroutine.

Easy3 - 62

Page 67: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Commands

3 - 63

Page 68: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 64

Page 69: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Commands;CLEAR DISPLAY, CURSOR AT THE BEGINNING, WRITE FROM THE ;BEGINNINGLDAA #$1JSR CMD2LCD

; Return cursor to home position (left hand side) without ; changing the content; when you write, you overwrite the LCD contentsLDAA #$2JSR CMD2LCD

; I/D (bit 1) bit is 0 - after each character you write, the cursor is decremented (move to left)LDAA #%00000100JSR CMD2LCD

; I/D (bit 1) bit is 1 - after each character you write, the cursor is incremented (move to right)LDAA #%00000110JSR CMD2LCD 3 - 65

Page 70: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

; C bit = 0, cursor is off – it can write but without showing the cursor LDAA #%00001100JSR CMD2LCD

; C bit = 1, cursor is onLDAA #%00001110JSR CMD2LCD

;B bit = 0 cursor blinking is off LDAA #%00001110JSR CMD2LCD

; B bit = 1 cursor blinking is on LDAA #%00001111JSR CMD2LCD

; D bit = 0, turn off display – can write but without showing the characters LDAA #%00001000JSR CMD2LCD

3 - 66

Page 71: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

; S/C =0 (bit3) to move cursor - R/L (bit2)= 0 to left, when you write, it writes to the cursor locationLDAA #%00010000JSR CMD2LCD

; S/C =0 (bit3) to move cursor - R/L (bit2)= 1 to right, when you write, it writes to the cursor locationLDAA #%00010100JSR CMD2LCD

; S/C =1 (bit3) to shift the values on the display - R/L (bit2)= 0 to left, when you write, it writes to the cursor location, can be used in moving the displayLDAA #%00011000JSR CMD2LCD

3 - 67

; D bit = 1 turn onLDAA #%00001100JSR CMD2LCD

Page 72: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

How to write in a certain location

3 - 68

; S/C =1 (bit3) to shift the values on the display - R/L (bit2)= 1 to right, when you write, it writes to the cursor location, can be used in moving the display LDAA #%00011100JSR CMD2LCD

1 B6 0 0 B3 B2 B1 B0

B6 = 0 first lineB6 = 1 second line B3 B2 B1 B0 a location in the row

;--- write zero at digit 16 (1111) in line 0 because bit 6 =0LDAA #%10001111JSR CMD2LCD ;Set the starting address to display information

; to digit 16 line 0LDAA #$30JSR PUTCLCD ; write 0

Page 73: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 69

; Put the cursor at the beginning of line 2LDAA #$C0 ; #%11000000JSR CMD2LCD

The cursor moves to the address

;-- write zero at digit 16 (1111) in line 1 because bit 6 =1LDAA #%11001111JSR CMD2LCD ;set the address to digit 16 line 1LDAA #$30JSR PUTCLCD ; write 0

; Put the cursor at the beginning of line 1LDAA #$80 ; #%10000000JSR CMD2LCD

Page 74: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

CONFIGLCD:

MOVB #$FF,DDRK; configure port K for output

LDY #$10JSR Delay_yms

LDAA #$28 ; set 4 bit data LCD - two line display - 5x8 fontJSR CMD2LCD

LDAA #$0E ;turn on display, turn on cursor , turn off blinkingJSR CMD2LCD

LDAA #$01 ; ; clear display screen and return to home positionJSR CMD2LCD

LDAA #$06 ;move cursor to right (entry mode set instruction) JSR CMD2LCD

LDY #$02JSR Delay_ymsRTS

The LCD should be configured before it is used. The following subroutine can configure the LCD.

3 - 70

Page 75: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Write a program to display the following on the LCD:-

ECE 3120 is easyWe like it!

ABSENTRY EntryINCLUDE 'mc9s12dp256.inc'

ORG $1000MSG1 dc.b "ECE 3120 is easy",0MSG2 dc.b "We like it!",0

ORG $2000 Entry:

LDS #$4000 ; Initialize stack

JSR CONFIGLCD ; configure the LCD

LDX #MSG1 ; display message 1JSR PUTSLCD

LDAA #$c0 ; new lineJSR CMD2LCD

LDX #MSG2 ; display message 2 JSR PUTSLCD

AGAIN: BRA AGAIN 3 - 71

Page 76: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

PUTSLCD: ; this subroutine displays the characters ;starting from address pointed by X until it find 0

PSHANEXT: LDAA 1,X+

BEQ DONEJSR PUTCLCD BRA NEXT

DONE: PULArts

Do not forget to add the following subroutines:-CMD2LCD, PUTCLCD, CONFIGLCD, and Delay_yms

3 - 72

Page 77: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

3 - 73

Summary of important subroutines

Keypad Return the ASCII code of a pressed key in register ANeeds Delay_yms subroutine

CONFIGLCD Configure the LCD. It should be called once before using the LCD. Needs Delay_yms and CMD2LCDsubroutines.

CMD2LCD Send a command to the LCD. The command should be in A. Needs Delay_yms

PUTCLCD Send a character to the LCD. The ASCII of the character should be in A. Needs Delay_yms

Displays a string on the LCD Starting address of the string is in register X The string should end by 0Needs Delay_yms and PUTCLCD subroutines

PUTSLCD

Delay_yms Make a delay for Y ms. Y is the content of register Y.

Page 78: Chapter 3 Interfacing to a microprocessormmahmoud/teaching_files/undergrad/E… · ; Make a delay for 1ms, given that E-clock = 24MHz. Each E-clock time interval is 41.67 ns ; To

Questions

Mohamed Mahmoud