使 LED 闪烁 n 次
make a led blink n times
我正在使用 tiva c 1294,我的想法是让 LED 闪烁给定的次数,我已经做到了 here,但现在不起作用,它所做的只是闪烁其中一个用户 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 编号。然后让我们用 SwitchLedOn(0)
替换 GPIO_PORTN_DATA_R |= 0x01
,用 SwitchLedOff(0)
替换 GPIO_PORTN_DATA_R &= ~0x01
。让我们对 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 闪烁一次。只要未按下开关 0,内部 while
循环就会重复此操作。如果在闪烁周期后按下开关,则循环离开并开始 for
循环。它让同一个 LED 闪烁 5 次。然后 LED 1 打开。但是只持续很短的时间,因为重复了外部 while
循环,它在一开始就关闭了 LED 1。
如果仍然按下开关,将跳过内部 while
-循环,直接进入 for
-循环。如果它不再被按下,内部 while
-loop 将等待下一次按下。
无论如何,我们可以不能在 LED 0 看到我们在代码中的位置。
我正在使用 tiva c 1294,我的想法是让 LED 闪烁给定的次数,我已经做到了 here,但现在不起作用,它所做的只是闪烁其中一个用户 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 编号。然后让我们用 SwitchLedOn(0)
替换 GPIO_PORTN_DATA_R |= 0x01
,用 SwitchLedOff(0)
替换 GPIO_PORTN_DATA_R &= ~0x01
。让我们对 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 闪烁一次。只要未按下开关 0,内部 while
循环就会重复此操作。如果在闪烁周期后按下开关,则循环离开并开始 for
循环。它让同一个 LED 闪烁 5 次。然后 LED 1 打开。但是只持续很短的时间,因为重复了外部 while
循环,它在一开始就关闭了 LED 1。
如果仍然按下开关,将跳过内部 while
-循环,直接进入 for
-循环。如果它不再被按下,内部 while
-loop 将等待下一次按下。
无论如何,我们可以不能在 LED 0 看到我们在代码中的位置。