{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Microcontroller4

Microcontroller4 - Peripheral Devices • We have learned...

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...
View Full Document

{[ snackBarMessage ]}