You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TI Tiva Arm开发板EK-TM4C123GXL读取按键控制板载LED程序运行故障调试求助

Debugging Tips for TI EK-TM4C123GXL Switch-to-LED Program (p2_7.c)

Hey there, let's work through why your switch-to-LED program isn't running right on the EK-TM4C123GXL. I've looked over the code and your setup, so here are the most likely issues and fixes:

First, Let's Break Down the Potential Bugs in the Original Code

Your code has a couple of subtle issues that are probably causing the misbehavior:

1. Unmasked Data Register Read

When you read GPIOF->DATA, you're grabbing the entire port's state—including unused pins (PF0, PF1, PF2) which might be floating and introducing random noise. This can make the LED behave unpredictably even when you press SW1.

2. Missing Clock Stabilization Delay

After enabling the GPIO clock with SYSCTL->RCGCGPIO |= 0x20, the TM4C needs a few cycles for the clock to propagate to the GPIO module. Skipping this delay can lead to your configuration registers not being applied correctly.

3. (Less Likely) GPIO Lock Register Edge Case

While PF4 (SW1) doesn't require unlocking unlike PF0 (SW2), it's worth confirming the lock register isn't interfering if you've messed with other pins previously.

Fixed Version of the Code

Here's the adjusted code that addresses these issues:

/* p2_7.c: Read a switch and write it to the LED */
/* Reads SW1 (PF4) on Tiva LaunchPad, inverts state, outputs to green LED (PF3) */
#include "TM4C123GH6PM.h"

int main(void) {
    unsigned int value;
    
    SYSCTL->RCGCGPIO |= 0x20; /* Enable clock to GPIOF */
    while((SYSCTL->PRGPIO & 0x20) == 0); /* Wait for GPIO clock to stabilize */
    
    GPIOF->DIR = 0x08; /* Set PF3 (green LED) as output, PF4 as input */
    GPIOF->DEN = 0x18; /* Enable digital I/O on PF3 and PF4 */
    GPIOF->PUR = 0x10; /* Enable pull-up resistor on PF4 (SW1) */
    
    while(1) {
        value = GPIOF->DATA & 0x10; /* Only read PF4 state (mask out other pins) */
        if(value == 0) { /* SW1 is pressed (low active) */
            GPIOF->DATA |= 0x08; /* Turn green LED on (high active) */
        } else {
            GPIOF->DATA &= ~0x08; /* Turn green LED off */
        }
    }
}

Step-by-Step Debugging Checks

If the fixed code still doesn't work, try these troubleshooting steps:

  • Check Hardware Connections: Ensure your LaunchPad is powered correctly, and SW1 (PF4) isn't damaged. You can use a multimeter to verify PF4 reads ~3.3V when unpressed, and ~0V when pressed.
  • Use a Debugger: Connect your board to a debugger (like Code Composer Studio) and inspect the GPIO registers:
    • Confirm SYSCTL->RCGCGPIO has bit 5 set (0x20)
    • Check GPIOF->DIR is 0x08, GPIOF->DEN is 0x18, GPIOF->PUR is 0x10
    • Watch GPIOF->DATA while pressing SW1 to see if it changes as expected
  • Verify LED Functionality: Test the green LED directly by setting GPIOF->DATA = 0x08 in your code—if it lights up, the LED itself is working.

内容的提问来源于stack exchange,提问作者Rachit Remote

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:47:51