c - 使 LED 闪烁 n 次
问题描述
我正在使用 tiva c 1294,这个想法是让 LED 闪烁给定的次数,我已经在这里完成了,但现在不起作用,它所做的只是让一个用户的 LED 闪烁(PF4)当按下而不是闪烁5次然后关闭时,它会打开另一个LED(PF0)。当您停止按下开关 1 时,它会重新开始连续闪烁第一个 LED。
#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 i,j; //int 1
int main(void) {
SYSCTL_RCGCGPIO_R=0X1100; // set clock portn
i=SYSCTL_RCGCGPIO_R; // delay (more than 3 cycles)
j=0;
GPIO_PORTN_DIR_R=0X03; //enable the GPIO pin for the LED-PN0, set the direction as output, and
GPIO_PORTN_DEN_R=0X03; //enable the GPIO pin for digital function
GPIO_PORTJ_AHB_DIR_R=0;
GPIO_PORTJ_AHB_DEN_R=0X03;
GPIO_PORTJ_AHB_PUR_R=0X01;
while(1){
GPIO_PORTN_DATA_R &=~0X02; //turn led off
while (GPIO_PORTJ_AHB_DATA_R & 0X01){
GPIO_PORTN_DATA_R |=0X01; //turn led on
SysCtlDelay(2666666);
GPIO_PORTN_DATA_R &=~0X01; //turn led off again
SysCtlDelay(2666666);
}
for (j=0; i<5; i++)
{
GPIO_PORTN_DATA_R |=0X01; //turn led on
SysCtlDelay(2666666);
GPIO_PORTN_DATA_R &=~0X01; //turn led off again
SysCtlDelay(2666666);
}
GPIO_PORTN_DATA_R |=0X02; //clear the interrupt flag before return
}
}
解决方案
代替
for (j=0; i<5; i++)
写
for (j=0; j<5; j++)
你看到你的错字了吗?
编辑
让我们看看你的无限循环。你是这样写的(我稍微纠正了它的空格):
while (1) {
GPIO_PORTN_DATA_R &= ~0X02; //turn led off
while (GPIO_PORTJ_AHB_DATA_R & 0X01) {
GPIO_PORTN_DATA_R |= 0X01; //turn led on
SysCtlDelay(2666666);
GPIO_PORTN_DATA_R &= ~0X01; //turn led off again
SysCtlDelay(2666666);
}
for (j = 0; j < 5; j++) {
GPIO_PORTN_DATA_R |= 0X01; //turn led on
SysCtlDelay(2666666);
GPIO_PORTN_DATA_R &= ~0X01; //turn led off again
SysCtlDelay(2666666);
}
GPIO_PORTN_DATA_R |= 0X02; //clear the interrupt flag before return
}
首先,我们更正最后一条注释并根据它们的位位置添加 LED 编号。然后让我们GPIO_PORTN_DATA_R |= 0x01
用SwitchLedOn(0)
和GPIO_PORTN_DATA_R &= ~0x01
替换SwitchLedOff(0)
。GPIO_PORTN_DATA_R
让我们对/0x02
和做同样的事情SwitchLedO*(1)
。接下来我们可以替换GPIO_PORTJ_AHB_DATA_R & 0X01
为IsSwitchUp(0)
.
while (1) {
SwitchLedOff(1); //turn led 1 off
while (IsSwitchUp(0)) {
SwitchLedOn(0); //turn led 0 on
SysCtlDelay(2666666);
SwitchLedOff(0); //turn led 0 off again
SysCtlDelay(2666666);
}
for (j = 0; j < 5; j++) {
SwitchLedOn(0); //turn led 0 on
SysCtlDelay(2666666);
SwitchLedOff(0); //turn led 0 off again
SysCtlDelay(2666666);
}
SwitchLedOn(1); //turn led 1 on
}
现在很清楚了。在写入源代码时,有两个相同的语句序列使 LED 0 闪烁一次。while
只要不按下开关 0 ,内部循环就会重复此操作。如果在闪烁周期后按下开关,则循环离开并for
开始循环。它让同一个 LED 闪烁 5 次。然后 LED 1 亮起。但这只是很短的时间,因为while
重复了外部循环,它在开始时就关闭了 LED 1。
如果仍然按下开关,while
则将跳过内部循环直接进入for
循环。如果它不再被按下,内部while
循环将等待下一次按下。
无论如何,我们无法在 LED 0 处看到我们在代码中的位置。
推荐阅读
- xml - 尝试构建多个变量的字符串时出现不需要的换行符
- java - 箭头的事件队列含义
- android - 如何删除添加到 FragmentManager 的第一个片段并启动新的 BackStack?
- python - 使用 python3、漂亮的汤和硒(也许)对 howlongtobeat.com 进行数据抓取
- python - 在表单中使用 MultipleChoiceField 作为 Integerfield
- xamarin.forms - Xamarin Forms 主题源
- python - 如何使用 tensorflow 编写这个函数?
- rest - 如果在发布子资源时资源不存在,则在 REST API 中使用哪个 HTTP 状态代码?
- php - PHP 会话变量在重定向时不可用
- amazon-s3 - 如何恢复在二进制文件中复制的 S3 的本地备份