240 likes | 377 Views
EET 2261 Unit 5 I/O Pins and Ports. Read Almy , Sections 12 – 15. Homework #5 and Lab #5 due next week. Quiz next week. General-Purpose Input/Output Ports.
E N D
EET 2261 Unit 5I/O Pins and Ports • Read Almy, Sections 12 – 15. • Homework #5 and Lab #5 due next week. • Quiz next week.
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.
Input/Output Ports on the HCS12 • Our HCS12 chip has ten general-purpose digital I/O ports:
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. • 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.
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.
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.
List of Special-Function Registers • Pages 27-49 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 to from Accumulator A to PORTA, you would use STAA $0000.
Special-Function Registers and the Ports • For each I/O port, we have 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 registers (such as DDRA and DDRB) that controls whether the port’s pins are configured as inputs or as outputs.
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 0to 3 as input pins, and we want to use pins 4 to 7 as output pins. Then we must write the value %11110000 (or $F0) to DDRA: LDAA #$F0 ;0-3 in, 4-7 out STAA $0002 ;DDRA is at $0002
Example: Using Port A for Output • Suppose we want to send %01011011 out on Port A. We must: • Use DDRA to configure all of Port A’s pins as output pins. • Send %01011011 to PORTA. • This code will do the job: LDAA #$FF ;outputsSTAA $0002 ;DDRA is at $0002 LDAA #$5B;data to be sent STAA $0000;PORTA is at $0000
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 ;inputsSTAA $0002 ;DDRA is at $0002 LDAA $0000;load from PORTA
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 #$00STAA $0002 LDAA $0000 • We can do this:PORTA EQU $0000DDRA EQU $0002 LDAA #$00STAA DDRA LDAA PORTA
Using Labels for the Addresses • Adding 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
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.
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.
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.
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
Names of the Port Registers and Bits • CAUTION! Note variations:
Switches and LEDs on the Dragon12 Trainer • On our trainer board: • Port H is wired to eight DIP switches • Port B is wired to eight LEDs • If we want to use these switches and LEDs, we must configure Port H for input and Port B for output. • Also, to use the LEDs, we must configure PJ1 as an output and set it equal to 0. (See p. 24 of Dragon12 manual and Schematic Diagram 4.)
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.)
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, %00001000 • Better yet, we’d use a label instead of a number for the address:BSET PORTE, %00001000
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.
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.
Using BRSET with an I/O Port: Example • Example: Suppose we want to branch if bit 3 of Port E is a 1 (set). • Here’s how to do it:BRSET $0008, %00001000, GoHere • Better yet, we’d use a label instead of a number for the address:BRSET PORTE, %00001000, GoHere