1
\$\begingroup\$

I am trying to use the following code for blinking LED on CC2530, it would be great if someone could tell me what I am doing wrong? I know this is basic stuff but I cannot figure out what I am doing wrong! The LED is mapped to port 0 and 7th pin (06).

I am using IAR to debug the program, when I run it in steps, LED do toggle! but when I run it without stopping, LED just glows, I thought it might be because of delay being miniscule, but no matter what value of delay I use, it just glows without blinking.

#define LED1_MASK  0x40
#define LED1_PIN   P0_6


int main()
{
  P0SEL &= ~(LED1_MASK);
  P0DIR |= (LED1_MASK);
  while(1)
  {
   //clock_delay(10000);  // i tried running empty for loops and NOP for loop too.
      LED1_PIN = 1;
      for(int i = 0; i< 1000; i++)
      {
        for(int j = 0; j< 1000; j++)
        {
          ASM(nop);
        }
      LED1_PIN = 0;
   }

EDIT: This is the actual program I am running, I made a mistake while pasting before (wasn't changing value of LED_PIN in the code). I am sorry for the trouble.

\$\endgroup\$
9
  • \$\begingroup\$ Um, shouldn't LED1_PIN be changing state between 1 and 0 at some point if you want the LED to turn on and off? \$\endgroup\$ Jul 8, 2015 at 17:02
  • 1
    \$\begingroup\$ You also need a second clock_delay() statement after the LED1_PIN = 0; statement. \$\endgroup\$ Jul 8, 2015 at 17:03
  • \$\begingroup\$ Yeah, sorry about that, i did an error while copying the program, though i cannot understand why i need a second clock_delay(), i tried using it but error still persists. \$\endgroup\$
    – user260994
    Jul 8, 2015 at 17:22
  • \$\begingroup\$ in the posted code, the time between setting p0.6 to 0 and setting p0.6 to 1 is trivial. That is why the LED seems to always be ON. Suggest use local variable to keep track of current state of LED, output p0.6 to the opposite state and change the local variable to reflect the (new) current state. Then have all LED updating at top of loop, not top and bottom of loop \$\endgroup\$ Jul 8, 2015 at 17:27
  • 1
    \$\begingroup\$ In your program, you turn the LED on at the top of the loop, wait a bit, then turn it off at the bottom. The program then returns instantly to the top of the loop and turns the LED on again. The LED will only be off for a few microseconds, so you won't notice it blink. You need a second delay after the LED is turned off, and before the end of the loop. \$\endgroup\$ Jul 8, 2015 at 18:34

2 Answers 2

1
\$\begingroup\$

Based on my vast experience, I think you are not blinking the LED. Change second LED1_PIN = 1; toLED1_PIN = 0; and wait for some time.

Edit: The waiting time after the LED has turned off is so less to perceive it as off by the human eye. As @peter pointed, the delay can be made equal and huge between both turn on and turn off code segments. Congrats ! You got your code working.

\$\endgroup\$
2
  • \$\begingroup\$ Yes - the second LED1_PIN = 1 should be LED1_PIN = 0. \$\endgroup\$ Jul 8, 2015 at 17:05
  • \$\begingroup\$ That was mistake while copying, I am sorry. Can you please get a look at the edited question, please. \$\endgroup\$
    – user260994
    Jul 8, 2015 at 17:24
1
\$\begingroup\$

You might want to consider this code snippet too.

while(1){
    LED1_PIN = LED1_PIN^0x40;            // toggle LED, P6
    Delay();
}


void Delay(void){
unsigned long volatile time;
time = 145448;  // 0.1sec Note this will change per the processor
    while(time){
        time--;
    }
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.