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



我试图在按下开关时使 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 时钟。

我不熟悉该器件,只简要阅读了数据手册,但如果输入/输出代码本身正确,则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的相应引脚,您需要清除其他三个。 我会把它留给你 - 它超出了最初的问题。

最新更新