Actions

EmSys

Difference between revisions of "Switch Inputs and LED Outputs in TM4C123"

From EdWiki

m (Read SW2 and Write it on Red LED)
m (SW1 and SW2 on LaunchPad)
Line 12: Line 12:
 
* There is no pull-up resistor connected to SW1 & SW2
 
* There is no pull-up resistor connected to SW1 & SW2
 
* To use the SW1 and SW2, we need to enable the internal pull-up resistor for PF0 and PF4 pins.
 
* To use the SW1 and SW2, we need to enable the internal pull-up resistor for PF0 and PF4 pins.
 
+
{{EmSys:TM4C123GXL GPIO - Configure each pad in the port to have pull-up-pull-down-open drain#GPIO Pull-Up Select (GPIOPUR) Register}}
The '''GPIOPUR''' register is the pull-up control register. When a bit is set, a weak pull-up resistor on the corresponding GPIO signal is enabled. Setting a bit in '''GPIOPUR''' automatically clears the corresponding bit in the GPIO Pull-Down Select ('''GPIOPDR''') register. The GPIOPDR register is the pull-down control register. When a bit is set, a weak pull-down resistor on the corresponding GPIO signal is enabled. Setting a bit in '''GPIOPDR''' automatically clears the corresponding bit in the GPIO Pull-Up Select ('''GPIOPUR''') register.
+
 
+
[[image:tm4c_gpiopdr_r.png|center]]
+
<br />
+
[[image:tm4c_gpiopur_r.png|center]]
+
  
 
== Read SW1 & Display it on the Green LED ==
 
== Read SW1 & Display it on the Green LED ==

Revision as of 05:25, 4 June 2019

Switch Inputs and LED Outputs

There are four ways to interface a switch to the microcontroller as shown in the following Figure:

Interface of a switch to a microcomputer input

We can use either positive or negative logic, and we can use an external resistor or select an internal resistor. Notice the positive logic circuit with external resistor is essentially the same as the positive logic circuit with internal resistance; the difference lies with whether the pull-down resistor is connected externally as a 10 kΩ resistor or internally by setting the corresponding PDR bit during software initialization.

SW1 and SW2 on LaunchPad

SW1 and SW2 on LaunchPad
  • SW1 push-button switch is connected directly to PF0 pin.
  • SW2 push-button switch is connected directly to PF4 pin.
  • There is no pull-up resistor connected to SW1 & SW2
  • To use the SW1 and SW2, we need to enable the internal pull-up resistor for PF0 and PF4 pins.

GPIO - Configure each pad in the port to have pull-up, pull-down, or open drain

We can configure each pad in the port to have pull-up, pull-down, or open drain function through the GPIOPUR, GPIOPDR , or GPIOODR register. Slew rate may also be configured, if needed, through the GPIOSLR register.

GPIO Pull-Up Select (GPIOPUR), GPIO Pull-Down Select (GPIOPDR)

  • The GPIOPUR register is the pull-up control register.
    When a bit is set, a weak pull-up resistor on the corresponding GPIO signal is enabled.
    Setting a bit in GPIOPUR automatically clears the corresponding bit in the GPIO Pull-Down Select (GPIOPDR) register.
  • The GPIOPDR register is the pull-down control register.
    When a bit is set, a weak pull-down resistor on the corresponding GPIO signal is enabled.
    Setting a bit in GPIOPDR automatically clears the corresponding bit in the GPIO Pull-Up Select (GPIOPUR) register.

GPIO Pull-Up Select (GPIOPUR) Register

Tm4c gpiopur r.png
Bit/Field Name Description
7:0 PUE Pad Weak Pull-Up Enable
0: The corresponding pin's weak pull-up resistor is disabled.
1: The corresponding pin's weak pull-up resistor is enabled.
Setting a bit in the GPIOPDR register clears the corresponding bit in

the GPIOPUR register. The change is effective on the second clock cycle after the write if accessing GPIO via the APB memory aperture. If using AHB access, the change is effective on the next clock cycle.

31:8 Reserved Software should not rely on the value of a reserved bit.

GPIO Pull-Down Select (GPIOPDR) Register

Tm4c gpiopdr r.png
Bit/Field Name Description
7:0 PDE Pad Weak Pull-Down Enable
0: The corresponding pin's weak pull-down resistor is disabled.
1: The corresponding pin's weak pull-down resistor is enabled.
Setting a bit in the GPIOPUR register clears the corresponding bit in the GPIOPDR register. The change is effective on the second clock cycle after the write if accessing GPIO via the APB memory aperture. If using AHB access, the change is effective on the next clock cycle.
31:8 Reserved Software should not rely on the value of a reserved bit.

GPIO Open Drain Select (GPIOODR)

The GPIOODR register is the open drain control register. Setting a bit in this register enables the open-drain configuration of the corresponding GPIO pad. When open-drain mode is enabled, the corresponding bit should also be set in the GPIO Digital Enable (GPIODEN) register (see page 682). Corresponding bits in the drive strength and slew rate control registers (GPIODR2R, GPIODR4R, GPIODR8R, and GPIOSLR) can be set to achieve the desired fall times. The GPIO acts as an input if the corresponding bit in the GPIODIR register is cleared. If open drain is selected while the GPIO is configured as an input, the GPIO will remain an input and the open-drain selection has no effect until the GPIO is changed to an output.

Tm4c gpioodr r.png


Bit/Field Name Description
7:0 ODE Output Pad Open Drain Enable
0: The corresponding pin is not configured as open drain..
1: The corresponding pin is configured as open drain.
31:8 Reserved Software should not rely on the value of a reserved bit. To provide compatibility with future products, the value of a reserved bit should be preserved across a read-modify-write operation.


Read SW1 & Display it on the Green LED

To read SW1 and display it on the green LED, the following steps must be taken.

  1. Enable the clock to PortF
  2. Set the Direction register PF4 as input, and PF3 as output
  3. Enable the digital I/O feature of PortF
  4. Enable the pull up resistor option in PUR register since the switch circuit does not have pull-up resistor
  5. Read SW1 on PortF
  6. Invert the value since the switch is active low and the LED is active high
  7. Shift right the switch bit (PF4) to green LED bit(PF3) of the value
  8. Write the value to green LED of PortF
  9. Repeat steps 5 to 8

Source Code

  1. /* Read a switch and write it to the LED */
  2. /* This program reads SW1 of Tiva LaunchPad and writes the inverse of the value to the green
  3.    LED. SW1 is low when pressed (Normally High). LED is ON when high.
  4. */
  5.  
  6. #include <stdint.h>
  7. #include "inc/tm4c123gh6pm.h"
  8.  
  9. int main(void)
  10. {
  11.     unsigned int value;
  12.  
  13.     SYSCTL_RCGC2_R |= 0x00000020;;   /* enable clock to GPIOF */
  14.  
  15.     GPIO_PORTF_DIR_R = 0x08;         /* set PORTF3 pin as output (LED) pin */
  16.                                      /* and PORTF4 as input, SW1 is on PORTF4 */
  17.     GPIO_PORTF_DEN_R = 0x18;         /* set PORTF pins 4-3 as digital pins */
  18.     GPIO_PORTF_PUR_R = 0x10;         /* enable pull up for pin 4 */
  19.  
  20.     While(1) {
  21.         value = GPIO_PORTF_DATA_R;  /* read data from PORTF */
  22.         value = ~value;             /* switch is low active; LED is high active */
  23.         value = value >> 1;         /* shift it right to display on green LED */
  24.         GPIO_PORTF_DATA_R = value;  /* put it on the green LED */
  25.     }
  26. }

Read SW2 and Write it on Red LED

To read SW2 and display it on the red LED, the program is similar to Previous except extra steps needed to take care of PortF0 as described below:

  • The SW2 is connected to PortF0 pin, which is shared with NMI (non-maskable interrupt).
  • To prevent accidental write to configuration registers and thus disables NMI, the configuration register bits for PortF0 are normally locked.
  • They may be unlocked by writing a passcode value of 0x4C4F434B to the LOCK Register followed by setting bit 0 of the Commit Register (GPIOCR).

GPIOLOCK Register

The GPIOLOCK register enables write access to the GPIO Commit Register (GPIOCR). Writing 0x4C4F434B to the GPIOLOCK register unlocks the GPIOCR register. Writing any other value to the GPIOLOCK register re-enables the locked state. Reading the GPIOLOCK register returns the lock status rather than the 32-bit value that was previously written. Therefore, when write accesses are disabled, or locked, reading the GPIOLOCK register returns 0x0000.0001. When write accesses are enabled, or unlocked, reading the GPIOLOCK register returns 0x0000.0000


Tm4c gpiolock r.png

GPIO Commit (GPIOCR) Register

The value of the GPIOCR register determines which bits of the GPIOAFSEL, GPIOPUR, GPIOPDR, and GPIODEN registers are committed when a write to these registers is performed. If a bit in the GPIOCR register is cleared, the data being written to the corresponding bit in the GPIOAFSEL, GPIOPUR, GPIOPDR, or GPIODEN registers cannot be committed and retains its previous value. If a bit in the GPIOCR register is set, the data being written to the corresponding bit of the GPIOAFSEL, GPIOPUR, GPIOPDR, or GPIODEN registers is committed to the register and reflects the new value. The contents of the GPIOCR register can only be modified if the status in the GPIOLOCK register is unlocked. Writes to the GPIOCR register are ignored if the status in the GPIOLOCK register is locked.

Tm4c gpiocr r.png

Source Code

  1. /* Read a switch and write it to the LED */
  2. /* This program reads SW2 of Tiva LaunchPad and write the inverse of the value to the red LED.
  3.    SW2 is low when pressed. LED is on when high. */
  4. /* SW2 is connected to PORTF0, which is an NMI pin. */
  5. /* In order to use this pin for any function other than NMI, the pin needs be unlocked first. */
  6.  
  7. #include <stdint.h>
  8. #include "inc/tm4c123gh6pm.h"
  9.  
  10. int main(void)
  11. {
  12.     unsigned int value;
  13.  
  14.     SYSCTL_RCGC2_R |= 0x00000020;       /* enable clock to GPIOF */
  15.  
  16.     GPIO_PORTF_LOCK_R = 0x4C4F434B;     /* unlock commit register */
  17.     GPIO_PORTF_CR_R = 0x01;             /* make PORTF0 configurable */
  18.     GPIO_PORTF_DIR_R = 0x02;            /* set PORTF1 pin as output (LED) pin */
  19.                                         /* and PORTF0 as input, SW2 is on  PORTF0 */
  20.     GPIO_PORTF_DEN_R = 0x03;            /* set PORTF pins 1-0 as digital pins */
  21.     GPIO_PORTF_PUR_R = 0x01;            /* enable pull up for pin 0 */
  22.  
  23.     while(1) {
  24.         value = GPIO_PORTF_DATA_R;      /* read data from PORTF */
  25.         value = ~value;                 /* switch is low active; LED is high active */
  26.         value = value << 1;             /* shift it left to display on red LED */
  27.         GPIO_PORTF_DATA_R = value;      /* put it on red LED */
  28.     }
  29. }