Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

need help with simple blinking led delay problem

I'm targeting a DE0-Nano development board, and I've created a NIOS ii/e embedded processor. I want to blink a LED, using the following C code from 'my first nios ii software tutorial':

# include <stdio.h># include "system.h"# include "altera_avalon_pio_regs.h"

int main()

{

int count = 0;

int delay;

while(1)

{

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, count & 0x01);

delay = 0;

while(delay < 2000000)

{

delay++;

}

count++;

}

return 0;

}

When I debug, I can step through and see the LED light, the value 'count' increment, but there is no 'delay' - it skips the 'while(delay...)' statement.

I do not understand - can somebody help?

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    I do not understand - can somebody help?

    --- Quote End ---

    The compiler is doing you a favor, and eliminating the code, since it appears to do nothing (but waste its time). Try

    volatile int delay;

    Cheers,

    Dave
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Dave,

    Works! thanks- but now I wonder why 'delay' is not declared in the Altera tutorial as volatile? Is there a better way to do this - a timer implemented in the NIOS processor?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    but now I wonder why 'delay' is not declared in the Altera tutorial as volatile?

    --- Quote End ---

    Possibly because the original author was using a different version of gcc, or had optimization turned off ... or left it out as a lesson to the user :)

    --- Quote Start ---

    Is there a better way to do this - a timer implemented in the NIOS processor?

    --- Quote End ---

    That is one way. Another way is to create an Avalon-MM slave hardware component to generate pulses (a pulse width modulator) and use that to control the LED.

    Ultimately it depends what you are trying to learn.

    Finding all the different ways to blink LEDs will keep you busy for hours :)

    Cheers,

    Dave