riccs_0x
riccs_0x

Reputation: 201

make a led blink n times

I'm using the tiva c 1294, the idea is to blink the led a given number of times, I already have done it here , but right now isn't working, all it does is blink one of the users leds (PF4) and when pressing instead of blinking 5 times and then turn off, it turns on the other led (PF0). When you stop pressing the switch 1 it gets back to blink continuously the first 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
    }

}

Upvotes: 0

Views: 1186

Answers (1)

the busybee
the busybee

Reputation: 12600

Instead of

  for (j=0; i<5; i++)

write

  for (j=0; j<5; j++)

Do you see your typo?


EDIT

Let's look at your endless loop. You wrote it like this (I corrected its white spaces a bit):

    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
    }

First we correct the last comment and add LED numbers based on their bit position. Then let's replace GPIO_PORTN_DATA_R |= 0x01 with SwitchLedOn(0) and GPIO_PORTN_DATA_R &= ~0x01 with SwitchLedOff(0). And let's do the same with GPIO_PORTN_DATA_R / 0x02 and SwitchLedO*(1). Next we can replace GPIO_PORTJ_AHB_DATA_R & 0X01 with 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
    }

Now it's clear to see. As the source is written there are two equal statement sequences that blink LED 0 one time. The inner while-loop repeats this as long as switch 0 is not pressed. If the switch is pressed after a blink period the loop is left and the for-loop starts. It lets the same LED blink 5 times. Then LED 1 is switched on. But just for a very short time because the outer while-loop is repeated which switches off LED 1 right at the beginning.

If the switch is still pressed the inner while-loop will be skipped going straight to the for-loop. If it is nor pressed anymore the inner while-loop will wait for the next press.

Anyway, we can not see at LED 0 where we are in the code.

Upvotes: 2

Related Questions