在 Tiva C 中按下开关时闪烁 LED

Blink leds while switches are pressed in Tiva C

我试图让 PF0 和 PF4 LED 在按下开关时闪烁。但它根本不点亮任何 LED。

我被告知我需要使用两个端口,我不明白为什么,因为这可以只用一个端口完成——在本例中是端口 D——但有人建议我使用端口 K 作为嗯(?).
该板是 Tiva C TM4c1294nctpd

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include <inc/tm4c1294ncpdt.h>

uint32_t SW1,SW2; //

int main(void) {

    while(1){
    SYSCTL_RCGCGPIO_R=0X1100; // Enable port D
    GPIO_PORTD_DIR_R=0X03;      //enable the GPIO pin PN0, 
    GPIO_PORTD_DEN_R=0X03;  
    GPIO_PORTK_AHB_DIR_R=0;
    GPIO_PORTK_AHB_DEN_R=0X03;
    GPIO_PORTK_AHB_PUR_R=0X01;

    SW1 = GPIO_PORTD_DATA_R&0x10;     // read PF4 into SW1
    SW2 = GPIO_PORTD_DATA_R&0x01;     // read PF0 into SW2
    if (!SW1 && !SW2) { // both pressed
        GPIO_PORTD_DATA_R = 0x04;
    } else if (!SW1) { // SW1 pressed
        GPIO_PORTD_DATA_R = 0x02;
    } else if (!SW2) { // SW2 pressed
        GPIO_PORTD_DATA_R = 0x08;
    } else { // neither
        GPIO_PORTD_DATA_R = 0x00;
    }
  }

}
  • 您只启用了 D0 和 D1,但似乎正在使用 D0、D1、D2、D3 和 D4。
  • 您已将 D0 和 D1 设置为输出,但似乎正在使用 D1、D2、D3 作为输出。
  • 您已将 D0 设置为输出,但尝试将其作为输入读取。
  • 如果你不使用它,PORTK 的配置是完全无关紧要的。
  • RCGCGPIO 为您根本没有使用的 PORTN 和 PORTJ 启用时钟。

我不熟悉这部分,只是简单地阅读了数据sheet,但是如果input/output代码本身,PORTD时钟、方向和数字使能配置应该如下正确。

SYSCTL_RCGCGPIO_R = 0x0008; // Enable port D clock
GPIO_PORTD_DIR_R = 0x0E;    // D4, D0 input, D1 to D3 output.
GPIO_PORTD_DEN_R = 0x1F;    // Enable D0 to D4  

这些初始化设置只需要一次 - 循环之前,而不是里面它。

int main(void) 
{
    SYSCTL_RCGCGPIO_R = 0x0008; // Enable port D
    GPIO_PORTD_DIR_R = 0x0E;    // D4, D0 input, D1 to D3 output.
    GPIO_PORTD_DEN_R = 0x1F;    // Enable D0 to D4  

    for(;;)
    {
        uint32_t SW1 = GPIO_PORTD_DATA_R & 0x10;  // read PD4 into SW1
        uint32_t SW2 = GPIO_PORTD_DATA_R & 0x01;  // read PD0 into SW2

        if (!SW1 && !SW2)  // both pressed
        {
            GPIO_PORTD_DATA_R = 0x04;
        } 
        else if (!SW1)     // SW1 pressed
        { 
            GPIO_PORTD_DATA_R = 0x02;
        } 
        else if (!SW2)     // SW2 pressed
        {
            GPIO_PORTD_DATA_R = 0x08;
        } 
        else               // neither 
        { 
            GPIO_PORTD_DATA_R = 0x00;
        }
    }
}

经过深思熟虑:

大概复制粘贴的代码中的注释表明该板可能是 EK-TM4C1294XL。在这种情况下,LED 是 调用的 D1、D2、D3、D4(D 代表 二极管 ,而不是 _PORTD),但在 GPIO PN1 上,分别为PN0、PF4和PF0,开关在PJ0和PJ1上。

在那种情况下,也许以下方法会更成功:

int main(void) 
{
    SYSCTL_RCGCGPIO_R |= (1<<5 | 1<<8 | 1<<12); // Enable port F, J and N clocks
    GPIO_PORTN_DIR_R |= 0x03;   // PN1 = LED0, PN0 = LED1 (Outputs)
    GPIO_PORTN_DEN_R |= 0x03;   // Enable PN0 and PN1  
    GPIO_PORTF_DIR_R |= 0x11;   // PF4 = LED3, PF0 = LED4 (Outputs)
    GPIO_PORTF_DEN_R |= 0x11;   // Enable PF0 and PF4  

    GPIO_PORTJ_DIR_R &= ~0x03;   // PJ0 = SW1, PJ1 = SW2 (Inputs)
    GPIO_PORTJ_DEN_R &= ~0x03;   // Enable PJ0 and PJ4  

    for(;;)
    {
        uint32_t SW1 = GPIO_PORTJ_DATA_R & 0x01;  // read PJ0 into SW1
        uint32_t SW2 = GPIO_PORTJ_DATA_R & 0x02;  // read PJ1 into SW2

        if (!SW1 && !SW2)  // both pressed
        {
            GPIO_PORTF_DATA_R = 0x01;  // LED4
        } 
        else if (!SW1)     // SW1 pressed
        { 
            GPIO_PORTF_DATA_R = 0x10;  // LED3
        } 
        else if (!SW2)     // SW2 pressed
        {
            GPIO_PORTN_DATA_R = 0x01;  // LED2
        } 
        else               // neither 
        { 
            GPIO_PORTN_DATA_R = 0x02;  // LED1
        }
    }
}

这仍然是错误的,因为代码只会打开 LED——它不考虑可能连接到端口 F 和 N 上其他引脚的其他硬件;您需要添加代码以读取-修改-写入您设置的到达 LED 的相应引脚,您需要清除其他三个。我会把它留给你 - 它超出了原来的问题。