chapter 3 interfacing to a microprocessor
play

Chapter 3 Interfacing to a microprocessor ECE 3120 Dr. Mohamed - PowerPoint PPT Presentation

Chapter 3 Interfacing to a microprocessor ECE 3120 Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/ mmahmoud@tntech.edu Outline 3 .1 Basic Concepts of Parallel I / O Ports 3.2 Interfacing with Simple I/ O Devices 3.3 Advanced


  1. Chapter 3 Interfacing to a microprocessor ECE 3120 Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/ mmahmoud@tntech.edu

  2. 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

  3. Input port Output port Input port Output port Input port - 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 3 - 1 - Using input ports, data flows from input devices to the microcontroller

  4. How the microprocessor can read from/ write to I/ O ports - 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 - Sim ilar to the m em ory locations, each port has a unique address - The address on the address bus can enable only one port 3 - 2

  5. The HCS12 I/O ports Available in H family devices only Our board’s has D family microcontroller 3 - 3

  6. Port S M J Port P Port P K Port PAD Port T Port T K J Port A Port B Port B Port H Port H Port E 3 - 4 Port E

  7. - Each I/ O port has a set of pins and several registers to support its operation. - Registers of I/ O ports have unique addresses - Mem ory instructions can be used w ith 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

  8. - 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 as output an the last 4 bits as input bset DDRA,$81 ; $81 = % 1000 0001 configure Port A pin 7 and 1 for output 3 - 6

  9. Simple 2-bit port 0 Tri state Buffer 1 0 = 1 1 = Outside world Output = input Microcontroller 0 3 - 7

  10. 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

  11. movb # $35,0 ; output $35 to Port A PORTA equ 0 ; defined in mc9s12dg256.inc Equal 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 them at the memory location represented by ibuf ldaa PORTA ; accumulator A = port B input Notice : all the instructions used for m em ory locations can be used w ith ports 3 - 9

  12. Outline 3.1 Basic Concepts of Parallel I/ O Ports 3 .2 I nterfacing w ith Sim ple I / O Devices 3.3 Advanced Parallel I/ O Devices

  13. 1- Interfacing with LEDs LED Circuit in the Draong12-Plus Demo Board H C S1 2 1.5 K  P B 7 P B 6 7 segment displays and P B 5 the LEDs are connected to P B 4 port B. P B 3 When using the 7 segment P B 2 display, PJ1 is used to P B 1 disable the LEDs. P B 0 P J1 F ig ure 4 .1 6 C ircuit co nnection fo r Ex am ple 4 .1 2 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 3 - 10

  14. 3 - 11

  15. 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 output 2. Disable the 7 segments 3. Pull the PJ1 pin to low to enable the LEDs 4. Pull the PB0 pin to high 5. Wait for 1 Sec 1 sec 6- Pull PB0 pin to low The LED pin 7. Wait for 1 Sec 1 sec 8. Go to step 4 3 - 12

  16. include 'mc9s12dg256.inc’ org $1500 lds #$2500 bset DDRJ,$02 ;configure PJ1 pin for output bclr 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 sec jsr Delay_yms ; “ bclr PORTB,#%00000001 ; pull PB0 pin to low ldy #1000 ; wait for 1000 ms = 1 sec jsr Delay_yms ; “ bra forever 3 - 13

  17. ; 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 cycles pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles 24,0000 E-clocks 24 E-clocks psha ; 2 E cycles = 1 ms delay pula ; 3 E cycles nop ; 1 E cycle nop ; 1 E cycle nop ; 1 E cycle nop ; 1 E cycle dbne x,innerloop dbne y,outerloop Y x 1 ms delay pulx rts 3 - 14

  18. 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 0.5 ms 3. Wait for 0.5 ms 4- Pull PT5 pin to low 5. Wait for 0.5ms 0.5 ms 6. Go to step 2 3 - 15

  19. include 'mc9s12dg256.inc’ org $1500 lds #$2500 bset DDRT,#%00100000 ;configure PT5 pin for output Forever: bset PTT,#%00100000 ; pull PT5 pin to high ldy #10 ; wait for 0.5 ms jsr delay_50Yus ; “ bclr PTT, #%00100000 ; pull PT5 pin to low ldy #10 ; wait for 0.5 ms jsr delay_50Yus ; “ bra forever delay_50Yus is a subroutine that makes a delay equal to Y x 50us Y is the content of register Y 3 - 16 Can you write the code of delay_50Yus?

  20. Square waveforms at all pins of port B with different frequencies Pin 0: 10KHz Pin 1: 5KHz Pin 0 Pin 7 Pin 2: 2.5 KHz 0 0 0 0 0 0 0 0 50us Pin 3: 1.25 KHz 0 0 0 0 0 0 0 1 ---- 50us 0 0 0 0 0 0 1 0 --- 50us 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 -- 0 0 0 0 0 1 0 1 1- PortB = 0 -- 0 0 0 0 0 1 1 0 2- Wait 50us -- 0 0 0 0 0 1 1 1 -- 3- Increment PortB 0 0 0 0 1 0 0 0 4- Loop to 2 At pin 0: duration of 0 and 1 is 50us At pin 1: duration of 0 and 1 is double of those of pin 0 = 100us At pin 2: duration of 0 and 1 is double of those of pin 1 = 200us ----------------------------- ----------------------------- 3 - 17

  21. include 'mc9s12dg256.inc‘ org $1500 lds #$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 output bclr PTJ,$02 ;enable LEDs to light clra forever: staa PORTB ldy #1 ; wait for 50 us jsr delay_50Yus ; ‘’ ‘’ inca bra forever 3 - 18

  22. 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. - 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 X 5- Wait a second 6- If X points to the end of the table, let X point to the beginning of the table 7- Go to step 2 3 - 19

Recommend


More recommend