This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Toggling LED

Hello all

I am quite new to Keil STR9 Series. I was just learning the whole thing by going through and trying some small examples with it.
I want to know how to control just one LED ( say 7.0) to blink at a time.

   while (1)
         {
            for (n = 0x01; n <= 0xFF; n <<= 1)
            {
               GPIO7->DR[0x3FC] = n;
            }
         }

This piece of code turns on the all 8 LEDS at the same time. How can i blink just one LED (7.0) every 1 second?

Thanks

  • 1) Change the state of just one bit of the register.

    2) Insert a 1-second delay.

  • How do u insert 1 second delay??

      wait(1000);
    

    When i give this commad, it shows error
    Error: Undefined symbol wait

  • Wouldn't the manual you used to locate that wait() function tell you that?

  • I looked for it . but couldnt get clear expalanation.
    There is an example code which do waiting

    void wait (void)  {                       /* Wait function                    */
      int d;
    
      d = AD_last;                            /* Read AD_last value               */
      if (d != AD_last)                       /* Make sure that AD interrupt did  */
        d = AD_last;                          /* not interfere with value reading */
    
      d *= 500;
      d += 50000;                             /* Scale analog value for delay     */
                                              /* lower value -> longer delay      */
      while (d--);                            /* Only to delay for LED flashes    */
    }
    

    I tried to modify this, using like this in my case.

    
    void wait (void)
    {                                       /* Wait function                    */
      int d= 1000;
      while (d--);                            /* Only to delay for LED flashes    */
    }
    
    
    while(1) {
               for (n = 0x01; n <= 0x1; n <<= 1)
                {
                   GPIO7->DR[0x3FC] = n;
                   wait();_
               }
    
    

    But nothing happens

  • So your first reference to wait(ms) was just a random chance? Not based on you having found such a function in any runtime library documentation for any runtime library you are using? Is that a good way to write code?

    Your next attempt - did you test to search about delays on this forum? If you did, you would have found quite a number of discussions about the problems with using busy-loops in C without binding the delay time to some real hardware property.

    Another thing - how long do you think your puny 1000-step delay takes? Do you really expect that your eye will be able to see any result of such a short delay?

    Yet another thing - if you want to blink a LED, you need to repeatedly do:
    - Turn on the LED.
    - Wait for x milliseconds.
    - Turn off the LED.
    - Wait for 1000-x milliseconds.

    Based on the documentation for your processor:
    - Do you see code both for turning on and for turning off any LED?
    - Does the processor documentation suggest that a 1000-step for loop would be a suitable delay?

    Note that turn on/turn off can be handled by the same primitive, if you use a primitive that toggles the pin state.

  • Your first post is I think correct (all you need to insert is the wait() in the for loop) and it looks like it was taken from the Blinky example. This app makes the LED blink rate controlled by the ADC. You should try to rotate the potentiometer and see if you can see the LED blink. I think on the extreme fast side, the LED "seems" to turn on ALL LED (the eye is too slow to notice the actual blinking).

    The STR9 chip is unique, i.e. there is a mask register for the port. You can either control the mask (0x3FC is all bits go through) or control the data to toggle the port bit. It is up to you to choose. The blinky example controls the data with the mask = 0x3FC.

    The mask is there so you don't have to do a read-modify-write. It makes the code simpler and faster.

    It's been a while since I worked on the STR9.

  • "The STR9 chip is unique, i.e. there is a mask register for the port."

    That isn't unique. A number of NXP chips has it too, and not unlikely other manufacturers too. You may take a look at the GPIO registers for the NXP LPC23xx chips, but several NXP product lines has mask registers.

  • wow... that really an easy method.

    I did it like this

    while(1) {
    RTC_GetTime(BINARY,&time);
            if (old_time != time.seconds)
                    {
                            old_time = time.seconds;
                            GPIO_Write(GPIO7, old_time &0x1);
                      }
             }
    


    but how is the port bit organised ??
    Is it LED 7.0 = bit 1,LED 7.1 = bit 2,... and so on ?

    when i gave 0x1 , LED 7.0 blinked every 1 second. It was jus a random guess. How does it work with other LEDs? what is the port bit for other LEDs?

    thnx

  • There is a schematic that you can check for connections - you should check it before writing your code. I believe it is in the Keil folder when you installed the s/w. Normally, I would make a summary of connections for easy lookup.

    IC6 74LVC244 P7.7 => LED7 (1 = ON) P7.6 => LED6 (1 = ON) P7.5 => LED5 (1 = ON) P7.4 => LED4 (1 = ON) P7.3 => LED3 (1 = ON) P7.2 => LED2 (1 = ON) P7.1 => LED1 (1 = ON) P7.0 => LED0 (1 = ON)

  •             IC6
             74LVC244
      P7.7      =>     LED7 (1 = ON)
      P7.6      =>     LED6 (1 = ON)
      P7.5      =>     LED5 (1 = ON)
      P7.4      =>     LED4 (1 = ON)
      P7.3      =>     LED3 (1 = ON)
      P7.2      =>     LED2 (1 = ON)
      P7.1      =>     LED1 (1 = ON)
      P7.0      =>     LED0 (1 = ON)
    

  • I was referring to a general concept of uC vs the STR9 - I probably should have worded it carefully. But you are right, the NXP's have also the port mask reg.