我正在尝试打开我的 tiva 启动板 (tm4c123) 中的 LED。
当前代码:
while (1)
{
switch(GPIO_PORTF_DATA_R & 0x11) // 0x11 = 10001
{
case 0x00: //both switches are pressed
{
GPIO_PORTF_DATA_R = 0x02; // turn on the red led
break;
}
case 0x01: //SW1 is pressed, SW2 is not pressed
{
GPIO_PORTF_DATA_R = 0x04; // turn on the blue led
break;
}
case 0x10: //SW2 is pressed, SW1 is not pressed
{
GPIO_PORTF_DATA_R = 0x08; // turn on the green led
break;
}
default:
GPIO_PORTF_DATA_R = 0;
break;
}
}
如果没有这两行代码,会发生这样的事情:
- 电路板开始时绿色 LED 亮起。
- 当我同时按下或仅按下 SW2 时,没有任何反应。
- 当我按下 SW1 时,红色 LED 亮起。
谁能给我解释一下为什么?
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R = 0x1F;
最佳答案
您可以像这样在您的 tiva 启动板上读取您的开关:
SW1 = GPIO_PORTF_DATA_R&0x10; // read PF4 into SW1
SW2 = GPIO_PORTF_DATA_R&0x01; // read PF0 into SW2
然后您可以检查这些输入的状态并做出决定。
if (!SW1 && !SW2) { // both pressed
GPIO_PORTF_DATA_R = 0x04;
} else if (!SW1) { // SW1 pressed
GPIO_PORTF_DATA_R = 0x02;
} else if (!SW2) { // SW2 pressed
GPIO_PORTF_DATA_R = 0x08;
} else { // neither
GPIO_PORTF_DATA_R = 0x00;
}
如果您查看 TM4C123GH6PM datasheet ,在第 205 页,它说;
Protection is provided for the GPIO pins that can be used as the four JTAG/SWD pins
如果您查看第 1329 页的信号表,您会看到 PF0 是其中之一,其中一个开关连接到该位置。所以你需要解锁它。
在第 684 页上,它说
Writing 0x4C4F.434B to the GPIOLOCK register unlocks the GPIOCR register.
关于c - 按下两个开关时打开 LED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108491/