Microcontroller4

Microcontroller4 - Peripheral Devices • We have learned to write/run assembly programs on HCS12 CPU • We now want to learn to involve I/O

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2/16/2011 Peripheral Devices • We have learned to write/run assembly programs on HCS12 CPU. • We now want to learn to involve I/O devices: – – – – – – Parallel I/O Ports and Devices (Chapters 4 and 7) Switches LEDs 7-Segments LCDs Keypads … • I/O devices are also called peripheral devices. • They are outside the microcontroller chip and exchange data with it. • We can communicate with peripheral devices using I/O Ports. A.H. Mohsenian-Rad (U of T) Networking and Distributed Systems 1 Dr. Hamed Mohsenian-Rad HCS12 Parallel Ports and Pins ECE 3362: Microcontrollers – Spring 2011 Port Name Number of Pins 4-4 Pin Name PE7~PE0 8 PH7~PH0 4 PJ7~PJ0 K 7 PK4~PK0 M 8 PM7~PM0 P 8 PP7~PP0 S 8 PS3~PS0 T Dr. Hamed Mohsenian-Rad 8 J 4-3 PB7~PB0 H Texas Tech University PA7~PA0 8 E Port A 8 B ECE 3362: Microcontrollers – Spring 2011 Texas Tech University • Number of pins available in some parallel port. A Dr. Hamed Mohsenian-Rad 4-2 HCS12 Parallel Ports and Pins • Each port is available for communication through multiple pins. Port B Texas Tech University 8 PT7~PT0 ECE 3362: Microcontrollers – Spring 2011 1 2/16/2011 Interface Chip I/O Port Registers • A chip to synchronize data transfer between the CPU and I/O devices. • To perform input and output, the CPU sends data to or from a register (with a two-byte address) instead of dealing with I/O pins: Microcontroller • It allocates certain memory addresses into the I/O pins. • Example: To send $2B to port A we put it on address $0000. movb #$2B, 0 • Data transfer can be proceeded bit-by-bit or in multiple bits (parallel). Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-5 I/O Port Registers DDRx, where x is the port name. 4-6 $00 $01 $02 $03 Examples: DDRA, DDRB, DDRH • In general, this is how the direction is determined: ; I/O port A 8-bit data register ; I/O port B 8-bit data register ; I/O portA 8-bit data direction reg ; I/O portB 8-bit data direction reg Bit b in register DDRx #include "MC9S12DP256BEQU.inc“ movb #$2B, PORTA ECE 3362: Microcontrollers – Spring 2011 output 0 • The above terms are already defined in “MC9S12DP256BEQU.inc”. Bit b in register x 1 Dr. Hamed Mohsenian-Rad Texas Tech University • Each I/O Port has a data direction register: • Instead, we can use EQU directive to make readable instructions: equ equ equ equ ECE 3362: Microcontrollers – Spring 2011 I/O Port Direction Configuration • However, it is difficult to read a program that uses these addresses. PORTA: PORTB: DDRA: DDRB: Dr. Hamed Mohsenian-Rad input • Q: How can we set all bits of port A for output? Texas Tech University 4-7 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-8 2 2/16/2011 Programming I/O Devices • Interfacing with DIP Switches Examples that we will consider: • A switch is probably the simplest input device we can find. DIP Switches LEDs 7-Segments • A set of eight switches are organized as a dual inline package (DIP). LCD Display Keypad • For each device, we need to know how the pins are assigned. • This will depend on the board being used, not the microcontroller. • We focus on Dragon-12 boards. Different versions can be different. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-9 Interfacing with DIP Switches Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-12 • Q: What does this program do? #include "MC9S12DP256BEQU.inc" org $1000 • Q: Fill the gap in the following code to read the DIP switches: movb ldab ----, DDRH PTH 4-10 Interfacing with DIP Switches • The switches are connected to Port H, pins PTH7~PTH0. movb load Texas Tech University ; set port H for input ; read port H in accumulator A TOP: #$00, DDRH #%00001000 ; ; ldaa cba bne end PTH ; ; ; TOP • Q: Does it make sense to write on a switch? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-11 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 3 2/16/2011 Interfacing with DIP Switches Interfacing with LEDs • Q: What does this program do? org movb ldaa PIN1: PIN2: PIN3: ENDP: $1000 #$00, DDRH PTH cmpa bne … bra #%00000001 PIN2 cmpa bne … bra #%00000010 PIN3 cmpa bne … bra • There are 8 LEDs near the DIP switch. #%00000100 Default ; set Port H for input Anode Cathode • To turn on an LED we should put 1 on Anode an 0 on Cathode. ; can be any code ENDP • Each LED’s Anode is connected to each Port B’s pin: 8 LEDs / 8 Pins. ; can be any code ENDP • All LEDs’ Cathodes are connected to Pin 1 of Port J: PTJ1. ; can be any code ENDP … Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-13 Interfacing with LEDs Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-14 Interfacing with LEDs • The following code turns on the second and fourth LEDs: • Q: What does the following program do? #include "MC9S12DP256BEQU.inc" org $1000 #include "MC9S12DP256BEQU.inc" org $1000 movb movb #$FF, DDRB #$FF, DDRJ bclr PTJ, $02 #%00001010, PORTB ; input ; output ; output ; output bset bclr ; turn on the two LEDs #$00, DDRH #$FF, DDRB #$FF, DDRJ #$FF, DDRP PTP, $0F PTJ, $02 ; disables 7-segs / will see more soon ; enable LEDs by clearing PTJ1 ldaa staa bra PTH PORTB TOP ; ; ; ; set bit PTJ1 to 0. movb movb movb movb movb ; set Port B for output ; set Port J for output TOP: • Q: What if you want to turn on the first and third LEDs? end Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-15 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-16 4 2/16/2011 Interfacing with LEDs Interfacing with LEDs • Q: What does the following program do? • Q: What does the following program do? (1 or 2) #include "MC9S12DP256BEQU.inc“ 1 TOP: 4 $1000 #$FF, DDRB #$FF, DDRJ #$FF, DDRP ; set Port B for output ; set Port J for output ; set port P for output bclr bset PTJ, $02 PTP, $0F ; set J1 = 0 / enable LEDs ; set P0, P1, P2, P3 = 1 / disable 7-segs #1 ; [A] 1 ; clear flag C staa jsr rola bra PORTB mydelay200ms ; send [A] to Port B ; delay ; rotate [A] left TOP … 2 org movb movb movb ldaa clc 1 ; includes the code for subroutine mydelay200ms Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 3 Texas Tech University 3 4-17 $1500 #$FF, DDRB #$FF, DDRJ #$FF, DDRP PTJ, $02 PTP, $0F #1 staa bcc pshc neg pulc PORTB CONT LEFT pulc rola bra 1 ; test [i] - $00 ; branch if minus ; shift rotate left TOP pulc rora bra TOP … org dc.b dc.b dc.b $1000 $00, $80, $C0, $E0, $F0, $F8, $FC, $FE, $FF, $FE $FC, $F8, $FC, $FE, $FC, $F8, $F0, $E0, $F0, $F8 $FC, $F8, $F0, $E0, $C0, $E0, $E0, $C0, $80, $80 org lds movb movb movb bclr bset $1500 #$3000 #$FF, DDRB #$FF, DDRJ #$FF, DDRP PTJ, $02 PTP, $0F ; set Port B for output ; set Port J for output ; set port P for output ; enable LEDs ; disable 7-seg ; includes the code for subroutine mydelay200ms ; shift rotate right • The selected pattern is saved in hex in Step 1. • Q: Why do we need to restore CCR in TOP loop in this program? Dr. Hamed Mohsenian-Rad 4-18 • We can also show an array of selected patterns on LEDS (1 of 2): 2 RIGHT Texas Tech University Interfacing with LEDs mydelay200ms i RIGHT ; branch if C = 0 ; push c 4 ; negate ; pull c ECE 3362: Microcontrollers – Spring 2011 LED_PATTERN pshc tst bmi ; set Port B for output ; set Port J for output ; set port P for output ; enable LEDs ; disable 7-seg ; [A] 1 ; clear Flag C i Dr. Hamed Mohsenian-Rad • Q: What does the following program do? (2 or 2) jsr TOP: org movb movb movb bclr bset ldaa clc 2 Interfacing with LEDs CONT #include "MC9S12DP256BEQU.inc“ org $1000 i dc.b 1 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-19 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-20 5 2/16/2011 Interfacing with LEDs Interfacing with LEDs • We can also show an array of selected patterns on LEDS (2 of 2): ldx • Also try this new pattern: #LED_PATTERN LED_PATTERN LOOP jsr 4 mydelay50ms ldaa staa inx cpx blt $1000 $00, $81, $42, $24, $18, $18, $99, $5A, $3C, $3C $BD, $7E, $7E, $FF, $FF, $7E, $7E, $BD, $3C, $3C $5A, $99, $18, $18, $24, $42, $81, $00, $00, $00 0, X PORTB ldx bra org org dc.b dc.b dc.b #LED_PATTERN LOOP 3 #LED_PATTERN+30 LOOP • The rest of the program is the same as the last two slides. • You can create many other nice patterns in a similar way. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-21 Interfacing with 7-Segment Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-22 Interfacing with 7-Segment • There are 4 seven-segment displays. • Anodes are connected to Port B. Segments: PB0 to PB6. Dot: PB7. • They are used to display only a few digits. • Cathodes are connected to Port P. • Each segment is an LED. Pins PP0 to PP3. Each anode is connected to a separate pin. • The following instructions turn on all LEDs in all 7-Segments: All cathodes are connected to a common pin. bset bclr movb movb • HCS12 pins are connected to 7-Segment through a buffer chip. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-23 Dr. Hamed Mohsenian-Rad DDRP, #$0F PTP, $0F #$FF, DDRB #$FF, PORTB ; set PP0~PP3 for output ; set PP0~PP3 to 0 / Enable Cathodes ; set Port B for output ; turn on all segments ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-24 6 2/16/2011 Interfacing with 7-Segment Interfacing with 7-Segment • We can use the following table to put decimal numbers on 7-segments. Decimal Digit Segments abcdefg 0 1 1 1 1 110 $3F 1 0 1 1 0 000 $06 2 1 1 0 1 101 $5B 3 1 1 1 1 001 $4F 4 0 1 1 0 011 $66 5 1 0 1 1 011 $6D 6 1 0 1 1 111 $7D 7 1 1 1 0 000 $07 8 1 1 1 1 111 $7F 9 1 1 1 1 011 • Since Port B is connected to all 7-Segments in parallel, displaying different digits on different 7-segments is tricky! $6F Dr. Hamed Mohsenian-Rad Corresponding Hex Number • Example, if we send $66 to Port B and $04 to Port P, you will see: 0 1 1 0 0 1 1 0 1 1 0 0 1 1 Port B 0 PTP0 = 0 0 1 0 Port P • Q: How can we display ‘1234’ on the 7-segments? ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-25 Interfacing with 7-Segment Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 1 $06 2 3 4 1 ms $5B %0000 1101 1 ms $4F %0000 1011 1 ms %0000 0111 1 $1000 movb movb movb #$FF, DDRB #$FF, DDRJ #$FF, DDRP ; set Port B for output ; set Port J for output ; set port P for output bset Wait Time %0000 1110 $66 4-28 #include "MC9S12DP256BEQU.inc" org Port P Texas Tech University • Here is the code (1 of 2): • We will turn on each digit for a few msecs. Port B 4-26 Interfacing with 7-Segment • We should take advantage of persistence of vision! Step Texas Tech University PTJ, $02 ; disable LEDs LOOP: 1 ms … 2 bra • Human eyes would see all digits together. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-27 Dr. Hamed Mohsenian-Rad LOOP … ; includes the code for subroutine mydelay200ms ECE 3362: Microcontrollers – Spring 2011 7 2/16/2011 Interfacing with 7-Segment Interfacing with 7-Segment • Here is the code (2 of 2): LOOP: movb movb #%00001110, PTP #$06, PORTB jsr mydelay1ms movb movb #%00001101, PTP #$5B, PORTB jsr mydelay1ms movb movb #%00001011, PTP #$4F, PORTB jsr mydelay1ms movb movb #%00000111, PTP #$66, PORTB jsr mydelay1ms bra • If we replace mydelay1ms with mydelay100ms then you can better see how the program is working. Step 1 LOOP Dr. Hamed Mohsenian-Rad Step 2 • The delay should be selected short enough to avoid feeling the rotation of the turned on 7-segments. Step 3 Step 4 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-29 Interfacing with 7-Segment Dr. Hamed Mohsenian-Rad Texas Tech University 4-30 ASCII to 7-Segment • Let us write a subroutine to convert numbers from ASCII to 7-seg. Digit ASCII $30 $3F 1 $31 $32 $5B 3 $33 $4F 4 $34 $66 5 $35 $36 $7D 7 $37 $07 8 $38 $7F 9 $39 $6F $1000 $3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F $33 $6D 6 org dc.b dc.b $06 2 • Here is the code: 7-Segment 0 1 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 ECE 3362: Microcontrollers – Spring 2011 sevseg digit org $1500 2 ldaa suba digit #$30 ; [A] ; [A] digit [A] - $30 3 ldx ldaa #sevseg A, X ; [X] ; [A] sevseg [A+X] swi end Texas Tech University 4-31 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-32 8 2/16/2011 ASCII to 7-Segment ASCII to 7-Segment • Q: What if we want to make it a subroutine? • Here is the code (1 of 2): ascii_sevseg • There is one 8-bit incoming parameter for ASCII value and one 16-bit return parameter for the pointer to the address for the result. 1 SP SP + 0 SP + 1 SP + 2 SP + 3 SP + 4 SP + 5 SP + 6 SP + 7 SP + 8 SP + 9 Dr. Hamed Mohsenian-Rad C DH DL XH XL RTNH RTNL as_ascii as_p_resH as_p_resL Q: How about Y? 2 Registers 3 Incoming Parameter Returning Parameter ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-33 5 as_p_res, SP 0, X suba ldx ldaa #$30 #sevseg A, X Dr. Hamed Mohsenian-Rad ; X [as_p_res+SP] ; [A] [0+X] digit result ; restore the registers pulx puld pulc dc.b as_ascii, SP ECE 3362: Microcontrollers – Spring 2011 org dc.b dc.b $1000 $34 0 4-34 Q: What will be the value of result? org #$2000 1 movw movb #result, 2, -SP digit, 1, -SP ; push: a hole for the result ; push: digit 2 $3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F $1500 lds jsr leas ascii_sevseg 3, SP ; call the subroutine ; release allocated memory bra ENDP: • Q: Why did we write part 4 this way? ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-35 Dr. Hamed Mohsenian-Rad ENDP … Dr. Hamed Mohsenian-Rad Texas Tech University • Here is a program to call subroutine ascii_sevseg: rts sevseg ; store the registers pshc pshd pshx ASCII to 7-Segment • Here is the code (2 of 2): ldx staa ; offset to as_ascii ; offset to as_res 7 8 ldaa Return Address ASCII to 7-Segment 4 as_ascii equ as_p_res equ ; subroutine will be defined here. end ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-36 9 2/16/2011 Example Example • Write a program to read a number in Terminal Window and display it on the first seven segment. • Here is the code (2 of 2): LOOP: 1 result 2 org dc.b $1500 #$2000 #$FF, DDRB #$FF, DDRJ #$FF, DDRP PTJ, $02 Dr. Hamed Mohsenian-Rad ; get a character #result, 2, -SP ; push: a hole for the result ; push the character entered ascii_sevseg 3, SP ; call the subroutine ; release allocated memory movb movb #%00000111, PTP result, PORTB ; enable first 7-segment bra LOOP ; subroutines will be defined here. 3 $1000 0 org lds movb movb movb bset [getchar, pcr] jsr leas #include "MC9S12DP256BEQU.inc" #include "DEBUG12V4EQU.inc" jsr movw pshb • Here is the code (1 of 2): 4 ; setup the stack ; set Port B for output ; set Port J for output ; set port P for output ; disable LEDs ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-37 Example Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 • Q: What does this program do (2 of 3)? #include "MC9S12DP256BEQU.inc" TOP: org lds $1000 #$2000 bset bset bset DDRB, DDRP, DDRJ, ldaa ldab #%10000000 #%00000111 PTJ, $02 3 ; program space ; control stack pointer clc rola bcc oraa …. …. ; A will keep track of pattern ; B will keep track of segments bset 2 ; port B is set to outputs ; port P low nibble to outputs ; port J is set to outputs WRITELED #$01 ; clear carry ; rotate A left one bit ; branch if C = 0 (7/8th of time) ; otherwise, A0 = 1 ; disable the LEDs #$FF #$0F #$FF WRITELED: ; we will take care of changing B. stab staa PTP PORTB ; Enable LEDs according to B ; [A] --> Port B (LEDs) ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-39 Dr. Hamed Mohsenian-Rad mydelay200ms TOP … • Q: How do we need to update A and B? jsr bra 4 Dr. Hamed Mohsenian-Rad 4-38 Example • Q: What does this program do (1 of 3)? 1 Texas Tech University ; write the delay subroutine here ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-40 10 2/16/2011 Example Interfacing with LCD • Q: What does this program do (3 of 3)? [switch/case structure] • Each dragon-12 board has one Liquid Crystal Display (LCD). Seg1: cmpb bne ldab bra #%00001110 Seg2 #%00001101 WRITELED ; check if 1st segment ; if not, try 2nd one ; update B to enable 2nd seg only Seg2: cmpb bne ldab bra #%00001101 Seg3 #%00001011 WRITELED ; check if 2nd segment ; if not, try 3rd one ; update B to enable 3rd seg only Seg3: cmpb bne ldab bra #%00001011 Seg4 #%00000111 WRITELED ; check if 3nd segment ; if not, try 4th one ; update B to enable 4th seg only • It is an HD44780 module which includes an LCD controller. Seg4: ldab bra #%00001110 WRITELED ; update B to enable 1st seg only • LCDs can display characters and graphics. 3 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-41 Interfacing with LCD Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-42 Interfacing with LCD • HCS12 / HD44780 LCD module interfacing on Dragon-12 boards: • HCS12 / HD44780 LCD module interfacing on Dragon-12 boards: • The HD44780 pins involved: RS, E, and DB4...DB7. • RS pin is used to select one of the two HD44780 registers to write on. • They are connected to Port K of the HCS12 microcontroller. • E pin is used to enable writing the HD44780 registers. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-43 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-44 11 2/16/2011 Interfacing with LCD Interfacing with LCD • HCS12 / HD44780 LCD module interfacing on Dragon-12 boards: • HD44780 LCD module has two 8-bit registers: Instruction Register (IR) Data Register (DR) • Send commands to IR to control the operation of HD44780 LCD. • Send data to DR to display them on the HD44780 LCD screen. • DB4…DB7 are four pins that are used for sending data and instructions into the HD44780 LCD module in 4-bit chunks. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-45 Interfacing with LCD Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-46 Interfacing with LCD • By setting the RS pin we determine which register we want to write on: • By setting the RS pin we determine which register we want to write on: RS = 0 Instruction Register (IR) RS = 0 Instruction Register (IR) RS = 1 Data Register (DR) RS = 1 Data Register (DR) • The cycle to send commands to the instruction register: Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University • The cycle to send data to the data register: 4-47 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-48 12 2/16/2011 Interfacing with LCD Interfacing with LCD • Timing Parameters: • Recall that data and instructions should be sent in 4-bit chunks. • However, all data and instructions have 8-bits. They are very small • Therefore, we should break each byte into two parts: We send the higher 4 bits first. • Note: we need to give a small delay before pulling E to low. Then we send the lower 4 bits immediately after that. • However, we should also wait for about 50 µsec such that the command is executed by the LCD before sending the next command. First Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-49 Sending Commands to LCD Instruction Register Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 cmd2lcd cmd2lcd psha ; save the 8-bit command in stack bclr bset • Note that each 4-bit chunk needs to be sent over PK5~PK2. 1 PORTK, #%00000001 PORTK, #%00000010 anda lsra lsra anda oraa staa #%11110000 nop nop bclr Texas Tech University 4-50 • Here is the code (1 of 3): • We place the command in accumulator A before calling cmd2LCD. ECE 3362: Microcontrollers – Spring 2011 Texas Tech University Sending Commands to LCD Instruction Register • We want to write a subroutine cmd2LCD to send a command to IR. Dr. Hamed Mohsenian-Rad Second 4-51 Dr. Hamed Mohsenian-Rad #%11111110 #%00000010 PORTK ; set RS = PK0 = 0 ; set E = PK1 = 1 ; clear the lower 4 bits ; place the command chunk at PK5~PK2 ; " " " ; keep RS = 0 ; keep E = 1 ; send the command, along with RS, E ; wait for a while ; “ PORTK, #%00000010 ; Set E = PK1 = 0 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-52 13 2/16/2011 Sending Commands to LCD Instruction Register Sending Commands to LCD Instruction Register • Here is the code (2 of 3): pula pula • Here is the code (3 of 3): ; load the 8-bit command from stack bclr bset 2 PORTK, #%00000001 PORTK, #%00000010 anda lsla lsla anda oraa staa #%00001111 nop nop bclr Dr. Hamed Mohsenian-Rad #%11111110 #%00000010 PORTK ; set RS = PK0 = 0 ; set E = PK1 = 1 ; add delay to let the command be executed • Q: Why do we add the delay here inside the subroutine? [Why not going back to the main program and then wait?] ; wait for a while ; “ ; Set E = PK1 = 0 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-53 Sending Data to LCD Data Register Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 4-54 • Here is the code (1 of 3): data2lcd data2lcd psha • We place the command in accumulator A before calling cmd2LCD. 1 PORTK, #%00000001 PORTK, #%00000010 anda lsra lsra oraa oraa staa #%11110000 nop nop bclr Texas Tech University ; save the 8-bit command in stack bset bset • Note that each 4-bit chunk needs to be sent over PK5~PK2. ECE 3362: Microcontrollers – Spring 2011 Texas Tech University Sending Data to LCD Data Register • We want to write a subroutine data2LCD to send a command to DR. Dr. Hamed Mohsenian-Rad delay50us rts ; clear the higher 4 bits ; place the command chunk at PK5~PK2 ; " " " ; keep RS = 0 ; keep E = 1 ; send the command, along with RS, E PORTK, #%00000010 jsr 3 4-55 Dr. Hamed Mohsenian-Rad #%00000001 #%00000010 PORTK ; set RS = PK0 = 1 ; set E = PK1 = 1 ; clear the lower 4 bits ; place the command chunk at PK5~PK2 ; " " " ; keep RS = 1 ; keep E = 1 ; send the command, along with RS, E ; wait for a while ; “ PORTK, #%00000010 ; Set E = PK1 = 0 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-56 14 2/16/2011 Sending Data to LCD Data Register Sending Data to LCD Data Register • Here is the code (2 of 3): pula pula • Here is the code (3 of 3): ; load the 8-bit command from stack bset bset 2 PORTK, #%00000001 PORTK, #%00000010 anda lsla lsla oraa oraa staa #%00001111 #%00000001 #%00000010 PORTK nop nop jsr 3 ; set RS = PK0 = 1 ; set E = PK1 = 1 delay50us ; add delay to let the command be executed rts ; clear the higher 4 bits ; place the command chunk at PK5~PK2 ; " " " ; keep RS = 1 ; keep E = 1 ; send the command, along with RS, E ; wait for a while ; “ bclr PORTK, #%00000010 Dr. Hamed Mohsenian-Rad ; Set E = PK1 = 0 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-57 List of HD44780 LCD Commands Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-58 List of HD44780 LCD Commands • What kind of 8-bit commands can we send to the Instruction Register? • Cursor Home Code to Be Sent to IR: • Clear Display Code to Be Sent to IR: Description: Description: [It does not clear the screen] 1. Clears display 2. Returns curser to the home position ldaa jsr #$01 cmd2lcd 1. Returns curser to the home position ldaa jsr #$03 cmd2lcd ; load A with “Cursor Home” command ; " ; load A with “Clear Display” command ; " Note that cursor / blinking determines the location to write the next character. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-59 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-60 15 2/16/2011 List of HD44780 LCD Commands List of HD44780 LCD Commands • Entry Mode Set • Display On/Off Control Code to Be Sent to IR: Code to Be Sent to IR: Description: Description: 1. Sets I/D and S bits. I/D Bit: Controls the incrementing (I/D = 1) or decrementing (I/D = 0) of cursor. 1. Sets D, C, and B bits. D Bit: Display On / Off D = 1: Display On D = 0: Display Off C Bit: Cursor On / Off C = 1: Cursor On C = 0: Cursor Off B Bit: Blinking On / Off B = 1: Blinking On B = 0: Blinking Off I/D = 1: Cursor moves to the right. I/D = 0: Cursor moves to the left. S Bit: Controls whether the cursor should shift (S = 0) or not (S = 1). Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-61 List of HD44780 LCD Commands Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-62 List of HD44780 LCD Commands • Examples: • Cursor or Display Shift Code to Be Sent to IR: ldaa jsr #%00001011 cmd2lcd ldaa jsr #%00001110 cmd2lcd ldaa jsr #%00001111 cmd2lcd Description: Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 1. Shifts curser to the right or left without writing anything. 2. Shifting the screen to the right or left. Texas Tech University 4-63 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-64 16 2/16/2011 List of HD44780 LCD Commands LCD Initialization • Function Set • We define a subroutine initlcd to initialize the LCD parameters: initlcd ; configure port K direction for output N Bit: Number of Display Lines N = 1: Two Lines F = 1: 5 x 10 Fonts F = 0: 5 x 8 Fonts ; Display On/Off Control ; " #%00000110 cmd2lcd ; Entry Mode Set ; " #%00000001 cmd2lcd ; Clear Display ; " delay100ms ; wait for display to turn on N = 0: One Line F Bit: Font #%00001111 cmd2lcd ldaa jsr DL = 0: 4-bit chunks ; Function Set ; " jsr DL = 1: 8-bit chunks ; not necessary #%00101000 cmd2lcd ldaa jsr DL Bit: Interface Data Length delay100ms ldaa jsr 1. Selecting interface data length, number of display lines and font. #$FF, DDRK ldaa jsr Description: movb jsr Code to Be Sent to IR: rts Note: For the boards that we have should always set DL = 0. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-65 LCD Initialization Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-66 Showing Characters on LCD Display • Once initialization is done, we can send characters to LCD. org lds $1000 #$2000 jsr initlcd ldaa jsr • Q: What if we do not want the cursor to blink? #$54 data2lcd • Q: What if we want the cursor to shift to right instead of left? ; ASCII for ‘T’ ; send the character to LCD swi • Q: What if we want to have one line only? … ; place the subroutines here • Q: How can we write string “Texas Tech” on the LCD? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-67 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-68 17 2/16/2011 Showing Characters on LCD Display Showing Characters on LCD Display • Similarly, we can also display a NULL-ended string on LCD: org MyUniv dc.b $1000 "Texas Tech", 0 org lds DONE: $2000 #$3000 jsr LOOP: • Q: How can we write a program to read characters from the keyboard in Terminal Window and show them on LCD? initlcd ldx ldaa beq pshx jsr pulx inx bra #MyUniv 0, X DONE data2lcd • The program needs two parts: Reading characters from terminal window in a loop. Sending each character to LCD. ; send the character to LCD LOOP swi Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-69 Showing Characters on LCD Display initlcd jsr tfr jsr bra org lds [getchar, pcr] B, A data2lcd LOOP ECE 3362: Microcontrollers – Spring 2011 4-70 $2000 #$3000 jsr LOOP: initlcd jsr cmpb beq tfr jsr bra [getchar, pcr] #8 BACKSP B, A data2lcd LOOP BACKSP: ldaa jsr bra • Q: What if you press Backspace? Dr. Hamed Mohsenian-Rad Texas Tech University • We can change the program to include Backspace: $2000 #$3000 jsr LOOP: ECE 3362: Microcontrollers – Spring 2011 Showing Characters on LCD Display • Q: W hat does this program do? org lds Dr. Hamed Mohsenian-Rad Texas Tech University 4-71 Dr. Hamed Mohsenian-Rad #%00010000 cmd2lcd LOOP ; check if Backspace is entered ; shift curser to the left ; " ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-72 18 2/16/2011 Showing Characters on LCD Display Showing Characters on LCD Display • Q: What if you replace the last three lines with this code? BACKSP: ldaa jsr #%00010000 cmd2lcd ; shift urser to the left ; " ldaa jsr #32 data2lcd ; Insert Space ; " ldaa jsr #%00010000 cmd2lcd ; shift urser to the left ; " bra • Q: What if you want to include Enter to start a new page? LOOP • Change the loop to: LOOP: jsr pshb cmpb beq pulb cmpb beq tfr jsr bra [getchar, pcr] #8 BACKSP #13 ENTER B, A data2lcd LOOP • Also add: ENTER: • Q: Does it look nicer on LCD this time? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-73 Showing Characters on LCD Display ldaa jsr bra Dr. Hamed Mohsenian-Rad #%00000001 cmd2lcd LOOP ; clear the display ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-74 Showing Characters on LCD Display • Next, we want to write a program to: • Here is the program (1 of 4): org ds.b $1600 $50 org nop lds ldy $1650 Show the strings on LCD as they are being typed. #$1750 #StringList Save the strings in a array in a particular memory location. jsr initlcd StringList Get several strings from the users through terminal window. • We store the strings in memory starting from address $1600. • We use register Y to point to the memory to write the next character. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-75 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-76 19 2/16/2011 Showing Characters on LCD Display Showing Characters on LCD Display • Here is the program (2 of 4): BACKSP: pshy jsr puly #8 BACKSP cmpb beq #13 ENTER stab tfr pshy jsr puly 0, Y B, A iny bra Dr. Hamed Mohsenian-Rad ldaa jsr #%00010000 cmd2lcd ; shift urser to the left ; " ldaa jsr #32 data2lcd ; ldaa jsr #%00010000 cmd2lcd ; shift urser to the left ; " puly dey ; place the character in memory bra data2lcd ; Insert Space " ; move backward in memory LOOP ; move to the next address LOOP ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-77 Showing Characters on LCD Display Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-78 Showing Strings on LCD Display • Here is the program (4 of 4): ENTER: pshy [getchar, pcr] pshb cmpb beq pulb LOOP: • Here is the program (3 of 4): • We want to write a program to show and browse a list: pshy Faculty List ldaa jsr #%00000001 cmd2lcd ; clear the display movb #0, 0, Y ; enter NULL to indicate the ; end of the string 1. Hamed M. Rad 2. Brian Nutter iny bra 3. Ranadip Pal Press Backspace to Scroll UP ; move to the next address LOOP 4. Changzhi Li 5. Mohammad Saed Press Enter to Scroll Down 6. Donald Lie 7. Tanja Karp 8. Hamed S. Sarraf 9. Sunanda Mitra • The strings will be separated by NULL. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 10. Mary Baker Texas Tech University 4-79 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-80 20 2/16/2011 Showing Strings on LCD Display Showing Strings on LCD Display • Here is the program (1 of 3): • Here is the program (2 of 3): ldx LOOP2: • We allocate 16 characters (fixed) for each name. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-81 Showing Strings on LCD Display Dr. Hamed Mohsenian-Rad B, X data2lcd ; Show FacultyList Item ; " #16 ; check if reached end of the string ; " LOOP2 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-82 • If the string is long, we can show it by shifting the display. pshx jsr pulx BACKSP #0 pshx pshb ldaa jsr pulb pulx ; Initialize LCD ; " ; " Showing Strings on LCD Display • Here is the program (3of 3): ENTER Dr. Hamed Mohsenian-Rad initlcd ble $1500 #$2000 LOOP1: #FacultyList ldab $1000 "1. Hamed M. Rad " "2. Brian Nutter " "3. Ranadip Pal " "4. Changzhi Li " "5. Mohammad Saed" "6. Donald Lie " "7. Tanja Karp " "8. Hamed Sarraf " "9. Sunanda Mitra" "10. Mary Baker “ pshx jsr pulx incb cmpb org dc.b dc.b dc.b dc.b dc.b dc.b dc.b dc.b dc.b dc.b org lds FacultyList ; Get a character from Terminal Window ; " ; " [getchar, pcr] cmpb bne cpx bge leax bra #13 BACKSP #FacultyList+9*16 LOOP1 16, X LOOP1 ; Check if the character is Enter cmpb bne cpx ble leax bra #8 LOOP1 #FacultyList LOOP1 -16, X LOOP1 ; Check if the character is Backspace • We can do this by using “Cursor or Display Shift” command. ; Check if we reached the end of the list ; X = X + 16 ; Check if we reached the beginning of the list ; X = X - 16 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-83 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-84 21 2/16/2011 Showing Strings on LCD Display Showing Strings on LCD Display • Here is the program (1 of 2): • Here is the program (1 of 2): $2100 #$2200 initlcd #MyLongString ldaa beq 0, X NEXT pshx jsr pulx 1 $2010 "Texas Tech is located in Lubbock, TX.", 0 jsr ldx LOOP: org dc.b org lds MyLongString data2lcd NEXT: Dr. Hamed Mohsenian-Rad [getchar, pcr] #8 Backspace cmpb beq #13 Enter bra NEXT Backspace: ldaa jsr bra #%00011100 cmd2lcd NEXT Enter: ldaa jsr bra #%00011000 cmd2lcd NEXT ; send the character to LCD inx bra jsr cmpb beq LOOP ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-85 Showing Strings on LCD Display Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-86 Defining New Characters • HD44780 LCD module has two Data RAMs and one Data ROM. • Q: What if we want to write a text editor? Display Data RAM (DDRAM) • The user would enter the characters on a large screen. Character Generator RAM (CGRAM) Character Generator ROM (CGROM) • He/she can browse to see different parts of the screen. • DDRAM stores display data in 8-bit character codes (e.g., ASCII). • Once the user is done, he/she can save the text as a .txt file. TE XA S DDRAM Content: $54, $54, $58, $41, $53. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-87 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-88 22 2/16/2011 Defining New Characters Defining New Characters • CGROM stores the bit-patterns for standard character codes. 1 2 34 • CGRAM stores the bit-pattern for a few customized character codes. 5 1 1 2 4 5 5 6 6 7 7 8 8 • This is the bit-pattern corresponding to character code $54. Dr. Hamed Mohsenian-Rad 5 3 4 34 2 3 2 1 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University • CGRAM Content: $1F, $15, $04, $0E, $04, $04, $04, $00. 4-89 Defining New Characters Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-90 Defining New Characters • CGRAM has space to store bit-patterns for eight 5 x 8 characters. • CGRAM has space to store the bit-pattern for eight 5 x 8 characters. $0 $8 $0 $8 $1 $9 $1 $9 $2 $A $2 $A $3 $B $3 $B $4 $C $4 $C $5 $D $5 $D $6 $E $6 $E $7 $F $7 $F • The bit patterns are stored from address $00 up to address $0F. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University • The character codes for Double T logo and Sigma are $00 and $01. 4-91 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-92 23 2/16/2011 Defining New Characters Defining New Characters • We cannot change the content of CGROM. • Set CGRAM Address Code to Be Sent to IR: • But we can change the content of CGRAM using data2lcd. Description: 1. Initiate the address where you want to start writing on CGRAM. • When using data2lcd you send data to either DDRAM or CGRAM. • Default is DDRAM. ldaa jsr #$54 data2lcd • Example: $8 ldaa jsr • But we can change it using two special commands to be sent to IR. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-93 Defining New Characters ECE 3362: Microcontrollers – Spring 2011 4-94 • Here is the code to define and display Double T logo (1 of 3): org DoubleT dc.b Code to Be Sent to IR: org lds 1. Initiate the address where you want to start writing on DDRAM. [This address is actually the address for cursor.] $3000 $1F, $15, $04, $0E, $04, $04, $04, $00 $1000 #$2000 • Example: jsr initlcd ldaa jsr #%01000000 cmd2lcd ; set CGRAM address to zero ; " $00 = Top Left of the Screen ldaa jsr Dr. Hamed Mohsenian-Rad Texas Tech University Defining New Characters • Set DDRAM Address Description: Dr. Hamed Mohsenian-Rad #%01001000 cmd2lcd #%10000000 cmd2lcd ECE 3362: Microcontrollers – Spring 2011 • The LCD is now ready to receive bit-pattern for character code $00. Texas Tech University 4-95 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-96 24 2/16/2011 Defining New Characters Defining New Characters • Here is the code to define and display Double T logo (2 of 3): ldx LOOP: pshx ldaa jsr pulx inx cpx blo • Here is the code to define and display Double T logo (3 of 3): #DoubleT ldaa jsr #%10000000 cmd2lcd ; set DDRAM address to zero ; " 0, X data2lcd ldaa jsr #$54 data2lcd ; first display T ; ldaa jsr #0 data2lcd ; then display Double T ; “ #$3008 LOOP • Now that we have the bit-pattern for character code $00, we can go ahead and use this character just like other characters. • We go through array Double T and send all 8 lines of the bit-pattern. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-97 LCD Graphics Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-98 LCD Graphics • We can show simple graphic features by defining new characters. • We also use character codes $02 and $03 to show a smiley face! $0 %00000111 %00011100 $10 %00000111 $1 %00001000 %00000010 $11 %00001000 %00000010 $2 %00010010 %00001001 $12 %00010010 %00001001 $3 %00010000 %00000001 $13 %00010000 %00000001 $4 %00010001 %00010001 $14 %00010010 %00001001 $5 %00010010 %00001001 $15 %00010001 %00010001 $6 %00001000 %00000010 $16 %00001000 %00000010 $7 %00000111 %00011100 $17 %00000111 %00011100 • We use character codes $00 and $01 to show a sad face! Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University %00011100 • We need to define bit-patterns for four character codes… 4-99 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-100 25 2/16/2011 LCD Graphics LCD Graphics • Here is the program (1 of 3): • Here is the program (2 of 3): org dc.b dc.b dc.b dc.b $3000 $07, $08, $12, $10, $11, $12, $08, $07 $1c, $02, $09, $01, $11, $09, $02, $1c $07, $08, $12, $10, $12, $11, $08, $07 $1c, $02, $09, $01, $09, $11, $02, $1c org lds $1000 #$2000 jsr #%01000000 cmd2lcd Dr. Hamed Mohsenian-Rad ldx LOOP: ; set CGRAM address to zero ; " ECE 3362: Microcontrollers – Spring 2011 Texas Tech University pshx ldaa jsr pulx inx cpx blo 0, X data2lcd #$3020 LOOP • Q: How about Sad2, Smiley1, and Smiley2 bit-patterns? 4-101 LCD Graphics Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-102 LCD Graphics • Here is the program (3 of 3): • You can use the following table to create some nice graphics. ldaa jsr #%10000000 cmd2lcd ldaa jsr #0 data2lcd ; Show Sad1 ; " ldaa jsr #1 data2lcd ; Show Sad2 ; " ldaa jsr #32 data2lcd ; Insert a Space ; " ldaa jsr #2 data2lcd ; Show Smiley1 ; " ldaa jsr Dr. Hamed Mohsenian-Rad #Sad1 initlcd ldaa jsr Sad1 Sad2 Smiley1 Smiley2 #3 data2lcd ; Show Smiley2 ; " ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-103 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-104 26 2/16/2011 Keypad Keypad • A keyboard/keypad is arranged as an array of switches. • A 16-key keypad can be interfaced to one of the HCS12 parallel ports. • A keypad usually consists of 12 to 24 keys. • A keypad is adequate for many applications: Calculator Entering Password … • HCS12 should scan the keypad to find which key is pressed. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-105 Keypad Scanning Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-106 Keypad Scanning • The lower four pins (PA3~PA0) are configured for output. • If no key is pressed, we always read 0000 on upper four pins. • The upper four pins (PA7~PA4) are configured for input. • Let us assume that we set PA0 = 1 while PA1 = PA2 = PA3 = 0. • The upper pins are grounded through some resisters. • Q: What does that mean if you read PA4 = 1 and PA5 = PA6 = PA7 = 0? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-107 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-108 27 2/16/2011 Keypad Scanning Keypad Scanning • Next, assume that we set PA2 = 1 while PA0 = PA1 = PA3 = 0. • Following the same approach, we can check all keys. • Q: What does that mean if you read PA5 = 1 and PA4 = PA6 = PA7 = 0? PA0 PA1 PA2 PA3 1 0 0 0 Selected Keys 0 1 0 0 2, 5, 8, and 0 0 0 1 0 3, 6, 9, and # 0 0 0 1 A, B, C, and D 1, 4, 7, and * • Algorithm to scan the keypad: Every time set one column to 1 and check which row turns to 1. • Q: What does that mean if you read PA7 = 1 and PA4 = PA5 = PA6 = 0? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-109 Keypad Scanning KEYS COLS ROWS ECE 3362: Microcontrollers – Spring 2011 $1000 '1', '2', '3', 'A‘ '4', '5', '6', 'B‘ '7', '8', '9', 'C‘ '*', '0', '#', 'D‘ dc.b dc.b $01, $02, $04, $08 $10, $20, $40, $80 ORG 4-110 #%00001111,DDRA • Here is the code for simple keypad scanning (2 of 4): ; Set PA0 = 1 and Test Inputs ; " ; " ; " 0, X PORTA READROWS ; Set PA1 = 1 and Test Inputs ; " ; " ; " 0, X PORTA READROWS ; Set PA2 = 1 and Test Inputs ; " ; " ; " inx ldaa staa jsr ; Lower bits: Output ; Upper bits: Input #COLS 0, X PORTA READROWS inx ldaa staa jsr ; Columns ; Rows ldx ldaa staa jsr inx ldaa staa jsr TOP: ; The Keys ; " ; " ; " $1500 movb 0, X PORTA READROWS ; Set PA3 = 1 and Test Inputs ; " ; " ; " bra Dr. Hamed Mohsenian-Rad Texas Tech University Keypad Scanning • Here is the code for simple keypad scanning (1 of 4): org dc.b dc.b dc.b dc.b Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-111 Dr. Hamed Mohsenian-Rad TOP ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-112 28 2/16/2011 Keypad Scanning Keypad Scanning • Here is the code for simple keypad scanning (3 of 4): READROWS: ldy ldaa anda bne #ROWS PORTA 0, Y ShowChar • Here is the code for simple keypad scanning (4 of 4): ; Check if PA4 = 1 ; " ; " ; " ShowChar PORTA 0, Y ShowChar iny ldaa anda bne PORTA 0, Y ShowChar ; Check if PA6 = 0 ; " ; " ; " iny ldaa anda bne PORTA 0, Y ShowChar ; Check if PA7 = 0 ; " ; " ; " KEYS ( X × 4 + Y ) • Example: If X = 2 and Y = 1, then KEYS(9) = ‘8’. ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-113 Keypad Scanning Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-114 Bouncing Problem • Next, we display the pressed key on Terminal Window: ShowChar ; Y will point to the row ; X will point to the column • Using KEYS array and values of X and Y we can find the exact key: rts Dr. Hamed Mohsenian-Rad -ROWS, Y -COLS, X swi rts ; Check if PA5 = 1 ; " ; " iny ldaa anda bne leay leax • Mechanical switches are most popular for keyboards. leay leax -ROWS, Y -COLS, X ; Y will point to the row ; X will point to the column • Mechanical switches have a problem called contact bounce. ldd emul tfr aba #4 ; B = row x 4 ; " ; A = column ; A = row x 4 + column • Problem: Closing a mechanical switch generates a series of pulses. ldx ldab ldaa #KEYS A, X #0 ; D = KEYS( row x 4 + column ) ; " ; " jsr [putchar, pcr] ; Display the key on Terminal Wind. X, A • Reason: The switch contacts are not released immediately. swi rts • Q: What happens if we remove swi at the end of ShowChar? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-115 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-116 29 2/16/2011 Debouncing Debouncing • The solution to bouncing problem is debouncing. • This can be achieved by replacing the current ShowChar with: Wait for about 10 ms. Check if the same key is still pressed. ; Check if the key is still pressed ; " ; " -ROWS, Y -COLS, X #4 ldx ldab ldaa jsr Check which key is pressed. ; Some Delay PORTA 0, Y READROWS ldd emul tfr aba • We count such continuous close signal as a single key pressing. mydelay150ms leay leax • We recognize that a switch is closed if it remains closed for about 10 ms. jsr ldaa anda beq ShowChar #KEYS A, X #0 [putchar, pcr] X, A rts Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-117 Showing the Numbers on LCD … ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-118 Get a String from KeyPad • A program to show the numbers on LCD: ShowChar Dr. Hamed Mohsenian-Rad • We want to write a program to receive a string from keypad. ; same as last slide ldx ldaa #KEYS A, X jsr data2lcd • The number should be displayed on LCD while being entered. rts • We treat # as ‘Enter’ key. • The entered string will be stored in variable KeyPadStr: KeyPadStr StrPointer • We also need to call initlcd at the beginning of the program. ds.b dc.b 20 0 ; To store the entered password ; Index pointer to PASSWRD characters • Setting up the stack will be needed too. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-119 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-120 30 2/16/2011 Get a String from KeyPad Pin Assignment • We should rewrite ShowChar subroutine as follows: ShowChar … • Q: Why not connect each switch directly to one pin to avoid scanning? ; same as before ldx ldaa 1 CONT: #KEYS A, X ; load ASCII in A cmpa bne swi #'#' CONT ; Check if ‘#’ is entered ; " psha jsr pula 2 data2lcd ldx ldab staa inc #KeyPadStr StrPointer B, X StrPointer 1 2 3 4 5 6 7 8 9 10 ; Place the character in PASSWRD ; " ; " ; Move POINTER ahead • Because we can significantly reduce the number of pins needed. rts Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-121 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-122 Pin Assignment • Also note that some boards may have a different pin assignment. • Q: What is the key scanning process for this pin assignment? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 4-123 31 ...
View Full Document

This document was uploaded on 10/27/2011 for the course EE 3362 at Texas Tech.

Ask a homework question - tutors are online