Strange Things With simple loop

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Strange Things With simple loop

596件の閲覧回数
rafa_freitas
Contributor I

I am having the follow issue.
When a declare a variable and try to use inside a loop, its value is modified and I do not know where or why.


Like the code below:

 

void main (void) { volatile uint32_t i = 0;    /* Dummy idle counter */  volatile uint8_t seg = 0;
// JUST CONFIGURATION BEGIN ----------------------------------------- {
 initModesAndClock();     /* Initialize mode entries and system clock */  initPeriClkGen();       /* Initialize peripheral clock generation for DSPIs */ disableWatchdog();      /* Disable watchdog */          initPads();                /* Initialize pads used in example */   initADC();                 /* Init. ADC for normal conversions but don't start yet*/   initCTU();                 /* Configure desired CTU event(s) */   initEMIOS_0();             /* Initialize eMIOS channels as counter, SAIC, OPWM */   initEMIOS_0ch3();     /* Initialize eMIOS 0 channel 3 as OPWM and channel 2 as SAIC*/       initEMIOS_0ch0();      /* Initialize eMIOS 0 channel 0 as modulus counter*/ initEMIOS_0ch23();      /* Initialize eMIOS 0 channel 23 as modulus counter*/ initEMIOS_0ch4();      /* Initialize eMIOS 0 channel 0 as OPWM, ch 4 as time base */ initEMIOS_0ch6();      /* Initialize eMIOS 0 channel 0 as OPWM, ch 6 as time base */ initEMIOS_0ch7();      /* Initialize eMIOS 0 channel 1 as OPWM, ch 7 as time base */  init_LinFLEX_0_UART();  // Hace la configuracion basica de el switch e o habilita como dispositivo de entrada. // Hace la configuracion basica de el motor. empiezaSwitchEntrada(); empiezaMotor(); empiezaLEDs();
// JUST CONFIGURATION ENDS ------------------------------------------------------------------- }
        seg  = 0; /* Loop forever */ for(;;)  {// *1 - But, If put "seg = 0" here, It works for Led_1.  // If button one is pressed  if(presionouLED(_LED1))  {   if(seg == 0)   {
                                // Turn on LED_1    conectaLed(_LED1);    seg++;   }   // Turn on LED_2.   else if(seg == 1)   {    conectaLed(_LED2);    seg++;   }      } }}

 

 If I run the code in that way, no one Led is turned up.

But I put the attribution in *1, It works for the Led_1.

 

Neither of this functions change the variable;

 

uint8_t presionouLED(uint8_t led);
uint8_t conectaLed(uint8_t led);

I tested the push buttons and the leds are working very well separete.

 

Have anyone any idea of what is happening?

I am using Code Warrio 5.9.0 and the board: TRK-MPC5604B.

ラベル(1)
タグ(2)
0 件の賞賛
返信
1 返信

385件の閲覧回数
puppetmaster
Contributor II

Your conditions...

   if(presionouLED(_LED1))

        if(seg == 0)   {                              

            // Turn on LED_1  

            conectaLed(_LED1);  

            seg++; 

        }   else if(seg == 1)   {    // Turn on LED_2. 

            conectaLed(_LED2);  

            seg++; 

        }    

    }

Change that to something like this

   if(presionouLED(_LED1))

        if(seg == 0)   {                              

            // Turn on LED_1  

            conectaLed(_LED1);  

        } else if(seg == 1) { // Turn on LED_2. 

            conectaLed(_LED2);  

        }

        seg = (uint8_t)((seg + 1) % 2); 

  }    

If you just increment by 1 each time.. .then

seg == 0 -> First change -> seg = 1

seg == 1 -> Second change -> seg = 2

So you'd never match these conditions again once you turned on the LED_2... Try my code; it should give you the basic idea...

0 件の賞賛
返信