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

Failure in using of volatile

Hallo ALL,
I have the following problem ....
I use F005 from cygnal.
I have a timer:
......................................
idata U8 volatile HeartbeatLEDTimer;
idata U8 volatile DACTimer;
...................................
void Timer0InterruptHandler(void) interrupt 1 using 1
{
// Reload the timer registers

TR0 = 0;

TL0 = T0_RELOAD_LOW;
TH0 = T0_RELOAD_HIGH;



// DECREMENT_TIMER(HeartbeatLEDTimer);
DECREMENT_TIMER(DACTimer);

TR0 = 1;

}



and in the main()
/*
if(!HeartbeatLEDTimer) {pin_LED_D1 = ~pin_LED_D1; HeartbeatLEDTimer = HEART_BEAT_LED_TIMER;}
*/
.............................

// INTERRUPTS_OFF;
DACTimer=DAC_TIMER;
// INTERRUPTS_ON;
while(DACTimer != 0){; }//delay



it works without INTERRUPTS_OFF if I don't use HeartbeatLEDTimer, but if I use it, I should disable interrupts only before DACTimer=DAC_TIMER and not before HeartbeatLEDTimer = HEART_BEAT_LED_TIMER???
I'm really confused and I thought the problem is in VOLATILE! But it's not so clear.
Please help me to understand what here is really happened.
With best regards,
Sergey

  • here it is in assembler....

    for DACTimer with interrupts not active(does not work)

    213: // INTERRUPTS_ON;
    C:0x372F 789C MOV R0,#DACTimer(0x9C)
    C:0x3731 76D0 MOV @R0,#PSW(0xD0)
    214: while(DACTimer != 0){;}
    215:
    C:0x3733 789C MOV R0,#DACTimer(0x9C)
    C:0x3735 E6 MOV A,@R0
    C:0x3736 70FB JNZ C:3733



    for DACTimer with interrupts active(it works)

    211: INTERRUPTS_OFF;
    C:0x36F2 C2AF CLR EA(0xA8.7)
    212: DACTimer=DAC_TIMER;
    C:0x36F4 789C MOV R0,#DACTimer(0x9C)
    C:0x36F6 76D0 MOV @R0,#PSW(0xD0)
    213: INTERRUPTS_ON;
    C:0x36F8 D2AF SETB EA(0xA8.7)
    214: while(DACTimer != 0){;}
    215:
    C:0x36FA 789C MOV R0,#DACTimer(0x9C)
    C:0x36FC E6 MOV A,@R0
    C:0x36FD 70FB JNZ C:36FA

    for HeartbeatLEDTimer LED

    369: if(!HeartbeatLEDTimer) {pin_LED_D1 = ~pin_LED_D1; HeartbeatLEDTimer = HEART_BEAT_LED_TIMER;}
    370:
    C:0x2151 789D MOV R0,#HeartbeatLEDTimer(0x9D)
    C:0x2153 E6 MOV A,@R0
    C:0x2154 7004 JNZ C:215A
    C:0x2156 B2B5 CPL pin_LED_D1(0xB0.5)
    C:0x2158 761E MOV @R0,#0x1E

  • and in the main()
    /*
    if(!HeartbeatLEDTimer)
    {
      pin_LED_D1 = ~pin_LED_D1;
      HeartbeatLEDTimer = HEART_BEAT_LED_TIMER;
    }
    */
    // INTERRUPTS_OFF;
    DACTimer=DAC_TIMER;
    // INTERRUPTS_ON;
    while(DACTimer != 0){; }//delay
    
    If you did not try to convolute your code and wrote it in the style above, you would imeediately see what the problem is.

    Erik

  • Hello ALL!
    Now it works!
    The interrurpt should be enabled before while(DACTimer!=0); and I was completely sure that it is , but....
    this "while" is in the big setup function befor main loop and in one of the setup subfunction befor this call the Interrupt was disabled and it was deadly for this "while".
    In any case THANK YOU ALL VERY MUCH for all your help !
    With respect,
    S.