C 使led闪烁n次

C 使led闪烁n次,c,arm,microcontroller,C,Arm,Microcontroller,我使用的是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

我使用的是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
    }

}
#包括
#包括
#包括“inc/hw_memmap.h”
#包括“driverlib/debug.h”
#包括“driverlib/gpio.h”
#包括“driverlib/sysctl.h”
#包括
uint32_t i,j//int 1
内部主(空){
SYSCTL\u RCGCGPIO\u R=0X1100;//设置时钟端口n
i=SYSCTL\u RCGCGPIO\u R;//延迟(超过3个周期)
j=0;
GPIO_PORTN_DIR_R=0X03;//启用LED-PN0的GPIO引脚,将方向设置为输出,然后
GPIO\U端口n\u DEN\u R=0X03;//为数字功能启用GPIO引脚
GPIO_PORTJ_AHB_DIR_R=0;
GPIO_PORTJ_AHB_DEN_R=0X03;
GPIO_PORTJ_AHB_PUR_R=0X01;
而(1){
GPIO\u端口n\u数据\u R&=~0X02;//关闭led
while(GPIO\U端口、AHB\U数据和0X01){
GPIO_端口n_数据_R |=0X01;//打开led
sysctlday(2666666);
GPIO\u端口n\u数据\u R&=~0X01;//再次关闭led
sysctlday(2666666);
}
对于(j=0;i而不是

  for (j=0; i<5; i++)
现在可以清楚地看到。写入源代码时,有两个相等的语句序列使LED 0闪烁一次。只要未按下开关0,内部
while
-循环将重复此操作。如果在闪烁时间后按下开关,则循环将离开,而
for
-循环将启动。它使相同的LED闪烁5次。然后LED 1闪烁打开。但只是在很短的时间内,因为外部
while
-循环会重复,从而在开始时关闭LED 1

如果开关仍被按下,则内部
while
-循环将被跳过,直接转到
for
-循环。如果开关不再被按下,内部
while
-循环将等待下一次按下

无论如何,我们可以在LED 0处看到我们在代码中的位置。

而不是

  for (j=0; i<5; i++)
现在可以清楚地看到。写入源代码时,有两个相等的语句序列使LED 0闪烁一次。只要未按下开关0,内部
while
-循环将重复此操作。如果在闪烁时间后按下开关,则循环将离开,而
for
-循环将启动。它使相同的LED闪烁5次。然后LED 1闪烁打开。但只是在很短的时间内,因为外部
while
-循环会重复,从而在开始时关闭LED 1

如果开关仍被按下,则内部
while
-循环将被跳过,直接转到
for
-循环。如果开关不再被按下,内部
while
-循环将等待下一次按下


无论如何,我们可以在LED 0上看到我们在代码中的位置。

确实是一个输入错误,但更正后,它有相同的行为。确实是一个输入错误,但更正后,它有相同的行为。
    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
    }
    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
    }