首页 > 解决方案 > 使 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
    }

}

标签: carmmicrocontroller

解决方案


代替

  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 |= 0x01SwitchLedOn(0)GPIO_PORTN_DATA_R &= ~0x01替换SwitchLedOff(0)GPIO_PORTN_DATA_R让我们对/0x02和做同样的事情SwitchLedO*(1)。接下来我们可以替换GPIO_PORTJ_AHB_DATA_R & 0X01IsSwitchUp(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 处看到我们在代码中的位置。


推荐阅读