Presentation is loading. Please wait.

Presentation is loading. Please wait.

EET 2261 Unit 7 I/O Pins and Ports

Similar presentations


Presentation on theme: "EET 2261 Unit 7 I/O Pins and Ports"— Presentation transcript:

1 EET 2261 Unit 7 I/O Pins and Ports
Read Almy, Chapters 12 – 15. Homework #7 and Lab #7 due next week. Quiz next week. Take Quiz 6.

2 General-Purpose Input/Output Ports
A general-purpose I/O port is a group of pins (typically 8) that can be connected to generic digital input or output devices. Example of a generic digital input device: a switch that switches between 0 V and +5 V. Example of a generic digital output device: an LED. “Generic” means that these devices don’t have specialized synchronization or timing requirements, as some digital devices do.

3 Number and Names of Bits
Input/Output Ports on the HCS12 Our HCS12 chip has ten general-purpose digital I/O ports: Port Number and Names of Bits Port A 8 bits: PA0 to PA7 Port B 8 bits: PB0 to PB7 Port E 8 bits: PE0 to PE7 (PE0 and PE1 are input only) Port H 8 bits: PH0 to PH7 Port J 4 bits: PJ0, PJ1, PJ6, PJ7 Port K 7 bits: PK0 to PK5, PK7 Port M 8 bits: PM0 to PM7 Port P 8 bits: PP0 to PP7 Port S 8 bits: PS0 to PS7 Port T 8 bits: PT0 to PT7

4 Multiplexed I/O Pins To keep the pin count to a reasonable number, most of the pins for the general-purpose I/O ports can also serve as inputs or outputs to specific modules on the HCS12. You must choose how you want to use the pins. Example: If you want to use the chip’s Enhanced Capture Timer module, then you can’t use Port T for general-purpose I/O. Note that the names on the Dragon12 board’s female header are abbreviated to show only one of the pin’s functions.

5 Multiplexed I/O Pins (Continued)
To see which functions are shared by a single pin, see block diagram on page 6 of textbook or page 23 of Device User Guide. Also see pin diagram on page 52 of Device User Guide. Note that the names on the Dragon12 board’s female header are abbreviated to show only one of the pin’s functions.

6 Special-Function Registers
The HCS12 has hundreds of special- function registers, some of which play a role in using the general-purpose I/O ports. Most of these registers are either: Data registers, which transfer data from place to place. Control registers, which control various aspects of the chip’s operation. Status registers, which hold status information about events that have occurred.

7 How to Access the Special-Function Registers
In the HCS12’s memory map (page 26 of Device User Guide), addresses from $0000 to $03FF are assigned to the special- function registers. When you execute an LDAA or STAA instruction to one of these addresses, you’re not reading or writing to memory; instead, you’re reading or writing to a special- function register.

8 List of Special-Function Registers
Pages in the Device User Guide list all of the special-function registers and their addresses. Example: the special-function register named PORTA is assigned address $0000. So to copy data from Accumulator A to PORTA, you would use STAA $0000.

9 Special-Function Registers and the Ports
Each I/O port has at least two special- function registers: A data register (such as PORTA or PORTB) that holds data traveling in or out through the port. A control register called a data-direction register (such as DDRA or DDRB) that controls whether the port’s pins are configured as inputs or as outputs.

10 Data Direction Registers
Each bit in a DDR configures the corresponding pin of the port as an input (if the bit = 0) or as an output (if the bit = 1). Example: Suppose that on Port A we want to use pins 0 to 3 as input pins, and we want to use pins 4 to 7 as output pins. Then we must write the value % (or $F0) to DDRA: LDAA #$F0 ;0-3 in, 4-7 out STAA $0002 ;DDRA is at $0002

11 Example: Using Port A for Output
Suppose we want to send % out on Port A. We must: Use DDRA to configure all of Port A’s pins as output pins. Send % to PORTA. This code will do the job: LDAA #$FF ;outputs STAA $0002 ;DDRA is at $ LDAA #$5B ;data to be sent STAA $0000 ;PORTA is at $0000

12 Example: Using Port A for Input
Suppose we want to read in a byte from Port A. We must: Use DDRA to configure all of Port A’s pins as input pins. (May not be needed, since by default ports are configured as inputs.) Load an accumulator from PORTA. This code will do the job: LDAA #$00 ;inputs STAA $0002 ;DDRA is at $ LDAA $0000 ;load from PORTA Warn not to confuse Port A and Port B with Accumulator A and Accumulator B.

13 Using Labels for the Addresses
Programs are easier to read and maintain if we use EQU directives to define labels for commonly used numbers. Example: Instead of this: LDAA #$ STAA $ LDAA $0000 We can do this: PORTA EQU $0000 DDRA EQU $ LDAA #$ STAA DDRA LDAA PORTA Remind that labels must start in the first column.

14 Using Labels for the Addresses
Including the other lines that we use in all our programs, here’s a complete program to read in a byte from Port A: ABSENTRY Entry ORG $2000 PORTA EQU $0000 DDRA EQU $0002 Entry: LDAA #$00 STAA DDRA LDAA PORTA BRA * END Have them enter and assemble it. See what happens if you get rid of the EQU directives.

15 Include Files When you create a project, CodeWarrior automatically creates two files named derivative.inc and mc9s12dg256.inc. (The second file’s name depends on the device you select in the New Project Wizard.) These are called include files, which is why the names end in .inc.

16 What’s in These Include Files?
These files contain EQU directives that define thousands of commonly used labels such as PORTA, DDRA, and so on. You should not edit these files.

17 How to Use These Include Files
The following assembler directive tells CodeWarrior to add everything from those two include files into your program INCLUDE 'derivative.inc' This will save you from having to type your own EQU directives for PORTA, DDRA, and all of the other special-function registers.

18 Earlier Example, But this Time Using the Include Files
Now we can use PORTA and DDRA in our program without typing EQU statements that define those labels. INCLUDE 'derivative.inc' ABSENTRY Entry ORG $2000 Entry: LDAA #$00 STAA DDRA LDAA PORTA BRA * END Have them enter it, assemble it, and start debugger. Note that Source window in debugger now contains more than 8000 lines! How many of those lines are HCS12 instructions?

19 Name of Data Direction Register
Names of the Port Registers and Bits CAUTION! Note variations: Port Name of Data Register Name of Data Direction Register Names of Bits Port A PORTA DDRA PA0 to PA7 Port B PORTB DDRB PB0 to PB7 Port E PORTE DDRE PE0 to PE7 Port H PTH DDRH PH0 to PH7 Port J PTJ DDRJ PJ0, PJ1, PJ6, PJ7 Port K PORTK DDRK PK0 to PK5, PK7 Port M PTM DDRM PM0 to PM7 Port P PTP DDRP PP0 to PP7 Port S PTS DDRS PS0 to PS7 Port T PTT DDRT PT0 to PT7

20 Switches on the Dragon12 Trainer
On our trainer board, Port H is wired to eight DIP switches. If we want to use these switches, we must configure Port H for input. (See Schematic Diagram 4.) We’ll use these switches and LEDs to get the hang of programming I/O.

21 LEDs on the Dragon12 Trainer
On our trainer board, Port B is wired to eight LEDs. If we want to use these LEDs, we must configure Port B for output. We must also configure PJ1 as an output and set it equal to 0. (See p. 24 of Dragon12 manual and Schematic Diagram 4.) We’ll use these switches and LEDs to get the hang of programming I/O.

22 Speaker on the Dragon12 Trainer
On our trainer board, bit 5 of Port T is wired to a speaker (buzzer). See p. 28 of Dragon12 manual and Schematic Diagram 1. To make a noise on the speaker, you must configure bit PT5 for output, and then cause this output pin to alternate rapidly between HIGH and LOW. The pitch of the sound depends on how quickly you alternate it.

23 Review: BCLR and BSET We’ve used BCLR and BSET to manipulate single bits in memory. We can also use BCLR and BSET to manipulate single bits of an output port. (Recall that I/O ports “look” just like memory locations to the HCS12’s CPU.)

24 Using BSET with an I/O Port: Example
Example: Suppose we want to set bit 3 of Port E. Here’s how to do it: BSET $0008, % Better yet, use a label instead of a number for the address: BSET PORTE, %

25 Review: BRCLR and BRSET
We’ve used BRCLR and BRSET to branch based on bits in memory. We can also use BRCLR and BRSET to branch based on bits of an input port.

26 Using BRSET with an I/O Port: Example
Example: Suppose we want to branch if bit 3 of Port E is a 1 (in other words, if bit 3 of Port E is set). Here’s how to do it: BRSET $0008, % , GoHere Better yet, use a label instead of a number for the address: BRSET PORTE, % , GoHere

27 Some Electrical Considerations
Unused input pins must not be allowed to float. An output pin’s current consumption is roughly proportional to the rate (frequency) at which the pin’s values changes. Example: If an output pin is being used to make an LED blink on and off, a blink rate of 10 times per second consumes roughly twice as much current as a rate of 5 times per second. See Chapter 13 for more detail on this and the next several slides.

28 Connecting a Mechanical Switch
Two important considerations when connecting a mechanical switch to an input pin: The pin must never be allowed to float. A common way to achieve this is to use the HCS12’s internal pull-up resistors. The switch may need to be debounced, either in hardware or in your program.

29 Pull-Up Resistors A pull-up resistor is a resistor with one end connected to a HIGH voltage level and the other end connected to a point in a digital circuit. The resistor’s purpose is to pull up the voltage at that point to a HIGH level when it would otherwise be in a float condition (not HIGH or LOW).

30 Pull-Up Resistor (Continued)
Good explanation at

31 Pull-Up Resistors on the Dragon12 Board’s DIP Switches
The Dragon12’s eight DIP switches are connected to pull-up resistors (and pull-down resistors) on the board. See Schematic Diagram 4.

32 Pull-Up Resistors on the Red Trainer’s Data Switches
The red trainer’s eight data switches are also connected to pull-up resistors. (Schematic diagram from trainer’s user manual.)

33 Internal Pull-Up Resistors on the HCS12
The HCS12, like many microcontrollers, has an internal pull-up resistor on each I/O pin. These pull-up resistors are disabled by default, but the programmer can enable them. Very handy for when you’re connecting an HCS12 input pin to a switch that’s not already connected to a pull-up resistor. Next slide shows the hardware associated with a single pin on Port A.

34 Hardware Associated with a Single I/O Pin on Port A
See page 143 of text. This figure applies to Ports A, B, E, and K. The other ports are more complicated. See figure on page 141 of text.

35 Activating the Internal Pull-Up Resistors: Ports A, B, E, K
How do we activate the HCS12’s internal pull-up resistors? By using bits in special function registers. For Ports A, B, E, and K, use the PUCR register. See page 24 of the MEBI Block User Guide.

36 Activating the Internal Pull-Up Resistors: Ports H, J, M, P, S, T
For these ports, use the PERx and PPSx registers. See page 37 of the Port Integration Module Block User Guide.

37 Review: Connecting a Mechanical Switch
Two important considerations when connecting a mechanical switch to an input pin: The pin must never be allowed to float. A common way to achieve this is to use the HCS12’s internal pull-up resistors. The switch may need to be debounced, either in hardware or in your program.

38 Contact Bounce Although it’s not obvious to us in everyday life, most mechanical switches suffer from contact bounce. When you flip the switch lever, the contacts inside the switch repeatedly bounce open and closed for a short time ( 50 ms). This can lead to problems in digital circuits, as shown in the following figures from your Digital Electronics textbook.

39 Figure 11.37 Switch used as a clock input to a toggle flip-flop.

40 Figure 11.38 Waveform at point Cp for Figure 11–37 assuming Q starts HIGH.

41 Ways to Solve Contact Bounce
There are several standard ways to debounce a switch. In hardware, we can use a latch or flip-flop, or an RC circuit with a Schmitt-trigger gate. See Section 114 of your Digital Electronics textbook (by Kleitz) for details. As noted on page 136 of our textbook, debouncing can also be done in your program by checking the input at well-spaced time intervals.

42 No Debounce Circuit on the Dragon12 Board’s DIP Switches
The Dragon12 does not include any hardware to debounce the DIP switches.

43 Debounce Circuit on Our Red Trainer’s Pulse Switches
The red trainer includes internal hardware to debounce the pulse switches.

44 No Debounce Circuit on Red Trainer’s Data Switches
The trainer does not include any internal hardware to debounce the data switches.

45 Some Timing Considerations
Un


Download ppt "EET 2261 Unit 7 I/O Pins and Ports"

Similar presentations


Ads by Google