• No se han encontrado resultados

Mallas espaciales de doble capa octaédricas

Figure 6.5. A read only input port allows the software to sense external digital signals.

Checkpoint 6.1: What happens if the software writes to an input port like Figure 6.5?

While an input device usually just involves the software reading the port, an output port can participate in both the read and write cycles very much like a regular memory. Figure 6.6 describes a readable output port. A write cycle to the port address will affect the values on the output pins. In particular, the microcontroller places information on the data bus and that information is clocked into the D flip-flops.

Since it is a readable output, a read cycle access from the port address returns the current values existing on the port pins. There are no output-only ports on the LM4F/TM4C family of microcontrollers.

Figure 6.6. A readable output port allows the software to generate external digital signals.

Checkpoint 6.2: What happens if the software reads from an output port like Figure 6.6?

To make the microcontroller more marketable, most ports can be software-specified to be either inputs or outputs. Microcontrollers use the concept of a direction register to determine whether a pin is an input (direction register bit is 0) or an output (direction register bit is 1), as shown in Figure 6.7. We define an initialization ritual as a program executed during start up that initializes hardware and software. If the ritual software makes direction bit zero, the port behaves like a simple input, and if it makes the direction bit one, it becomes a readable output port. Each digital port pin has a direction bit.

This means some pins on a port may be inputs while others are outputs. The digital port pins on most microcontrollers are bidirectional, operating similar to Figure 6.7.

Figure 6.7. A bidirectional port can be configured as a read-only input port or a readable output port.

Common Error: Many program errors can be traced to confusion between I/O ports and regular memory.

For example, you should not write to an input port, and sometimes we cannot read from an output port.

6.3. I/O Programming and the Direction Register

On most embedded microcontrollers, the I/O ports are memory mapped. This means the software can access an input/output port simply by reading from or writing to the appropriate address. It is important to realize that even though I/O operations “look” like reads and writes to memory variables, the I/O ports often DO NOT act like memory. For example, some bits are read-only, some are write-only, some can only be cleared, others can only be set, and some bits cannot be modified. To make our software more readable we include symbolic definitions for the I/O ports. We set the direction register (e.g., GPIO_PORTF_DIR_R) to specify which pins are input and which are output. Individual port pins can be general purpose I/O (GPIO) or have an alternate function. We will set bits in the alternate function register (e.g., GPIO_PORTF_AFSEL_R) when we wish to activate the alternate functions listed in Table 6.1. For each I/O pin we wish to use whether GPIO or alternate function we must enable the digital circuits by setting the bit in the enable register (e.g., GPIO_PORTF_DEN_R). Typically, we write to the direction and alternate function registers once during the initialization phase. We use the data register (e.g., GPIO_PORTF_DATA_R) to perform input/output on the port. Conversely, we read and write the data register multiple times to perform input and output respectively during the running phase. Table 6.2 shows some of the parallel port registers for the LM4F120/TM4C123. The only differences among the Stellaris and Tiva families are the number of ports and available pins in each port.

Address 7 6 5 4 3 2 1 0 Name

$400F.E108 -- -- GPIOF GPIOE GPIOD GPIOC GPIOB GPIOA SYSCTL_RCGC2_R

$4000.43FC DATA DATA DATA DATA DATA DATA DATA DATA GPIO_PORTA_DATA_R

$4000.4400 DIR DIR DIR DIR DIR DIR DIR DIR GPIO_PORTA_DIR_R

$4000.4420 SEL SEL SEL SEL SEL SEL SEL SEL GPIO_PORTA_AFSEL_R

$4000.4510 PUE PUE PUE PUE PUE PUE PUE PUE GPIO_PORTA_PUR_R

$4000.451C DEN DEN DEN DEN DEN DEN DEN DEN GPIO_PORTA_DEN_R

$4000.4524 1 1 1 1 1 1 1 1 GPIO_PORTA_CR_R

$4000.4528 0 0 0 0 0 0 0 0 GPIO_PORTA_AMSEL_R

$4000.53FC DATA DATA DATA DATA DATA DATA DATA DATA GPIO_PORTB_DATA_R

$4000.5400 DIR DIR DIR DIR DIR DIR DIR DIR GPIO_PORTB_DIR_R

$4000.5420 SEL SEL SEL SEL SEL SEL SEL SEL GPIO_PORTB_AFSEL_R

$4000.5510 PUE PUE PUE PUE PUE PUE PUE PUE GPIO_PORTB_PUR_R

$4000.551C DEN DEN DEN DEN DEN DEN DEN DEN GPIO_PORTB_DEN_R

$4000.5524 1 1 1 1 1 1 1 1 GPIO_PORTB_CR_R

$4000.6528 AMSEL AMSEL AMSEL AMSEL JTAG JTAG JTAG JTAG GPIO_PORTC_AMSEL_R

$4000.73FC DATA DATA DATA DATA DATA DATA DATA DATA GPIO_PORTD_DATA_R

$4000.6520 LOCK (write 0x4C4F434B to unlock, other locks) (reads 1 if locked, 0 if unlocked) GPIO_PORTC_LOCK_R

$4000.7520 LOCK (write 0x4C4F434B to unlock, other locks) (reads 1 if locked, 0 if unlocked) GPIO_PORTD_LOCK_R

$4002.5520 LOCK (write 0x4C4F434B to unlock, other locks) (reads 1 if locked, 0 if unlocked) GPIO_PORTF_LOCK_R

Table 6.2 Some TM4C123 parallel ports. Each register is 32 bits wide. For PMCx bits, see Table 6.1. JTAG means do not use these pins and do not change any of these bits.

To initialize an I/O port for general use we perform seven steps. Steps two through four are needed only for the LM4F/TM4C microcontrollers. First, we activate the clock for the port. Second, we unlock the port; unlocking is needed only for pins PC3-0, PD7, PF0 on the LM4F and TM4C. Third, we disable the analog function of the pin, because we will be using the pin for digital I/O. Fourth, we clear bits in the PCTL (Table 6.1) to select regular digital function. Fifth, we set its direction register. Sixth, we clear bits in the alternate function register, and lastly, we enable the digital port. We need to add a short delay between activating the clock and accessing the port registers. The direction register specifies bit for bit whether the corresponding pins are input or output. A DIR bit of 0 means input and 1 means output.

Common Error: You will get a bus fault if you access a port without enabling its clock.

In this first example we will make PF4 and PF0 input, and we will make PF3 PF2 and PF1 output, as shown in Program 6.1. To use a port we first must activate its clock in the SYSCTL_RCGC2_R register.

The second step is to unlock the port, by writing a special value to the LOCK register, followed by setting bits in the CR register. Only PC3-0, PD7, and PF0 on the TM4C need to be unlocked. All the

other bits on the TM4C are always unlocked. The third step is to disable the analog functionality, by clearing bits in the AMSEL register. The fourth step is to select GPIO functionality, by clearing bits in the PCTL register, as described in Table 6.1. The fifth step is to specify whether the pin is an input or an output by clearing or setting bits in the DIR register. Because we are using the pins as regular digital I/O, the sixth step is to clear the corresponding bits in the AFSEL register. The last step is to enable the corresponding I/O pins by writing ones to the DEN register. To run this example on the LaunchPad, we also set bits in the PUR register for the two switch inputs (Figure 6.3) to have an internal pull-up resistor.

When the software reads from location 0x400253FC, the bottom 8 bits are returned with the current values on Port F. The top 24 bits are returned zero. As shown in Figure 6.7, when reading an I/O port, the input pins show the current digital state, and the output pins show the value last written to the port.

The function PortF_Input will read from the five input pins, and return a value depending on the current status of the inputs. As shown in Figure 6.7, when writing to an I/O port, the input pins are not affected, and the output pins are changed to the value written to the port. The function PortF_Output will write new values to the three output pins. The #include will define symbolic names for all the I/O ports for that microcontroller. The header file tm4c123ge6pm.h can be found in the inc folder.

#include "tm4c123ge6pm.h"

unsigned long In; // input from PF4

unsigned long Out; // output to PF2 (blue LED) //Function Prototypes

void PortF_Init(void);

// 3. Subroutines Section

// MAIN: Mandatory for a C Program to be executable

int main(void){ // initialize PF0 and PF4 and make them inputs PortF_Init(); // make PF3-1 out (PF3-1 built-in LEDs)

while(1){

In = GPIO_PORTF_DATA_R&0x10; // read PF4 into Sw1 In = In>>2; // shift into position PF2

Out = GPIO_PORTF_DATA_R;

// Subroutine to initialize port F pins for input and output // PF4 is input SW1 and PF2 is output Blue LED

// Inputs: None // Outputs: None // Notes: ...

void PortF_Init(void){ volatile unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay = SYSCTL_RCGC2_R; // allow time for clock to start GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0

// only PF0 needs to be unlocked, other bits can't be locked GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF

GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0 GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out

GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0 GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4 GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0 }

Program 6.1. A set of functions using PF4,PF0 as inputs and PF3-1 as outputs (C6_InputOutputxxx.zip).

Checkpoint 6.3: Does the entire port need to be defined as input or output, or can some pins be input

Documento similar