Timing issue


Closed Thread
Results 1 to 10 of 10

Thread: Timing issue

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Timing issue

    Hi everyone;

    I'm facing a problem with the code below. IT works everything fine, except the timing. It takes always the double of the time in the "pause" instructuion. For instance, if i put PAUSE 5000, it takes 10s to execute.
    The micro is 12F683 and is powered by a CR2032 battery.

    Can some one help me? Thanks!!!

    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _FCMEN_OFF & _IESO_OFF  
     DEFINE OSC 4
              '76543210
     OSCCON = %01100111  
     
     '===================SRF AND PINOUT CONFIGURATION - 1-In / 0-Out=================
              '76543210
     TRISIO = %00000001
       GPIO = %00000000       
        IOC = %00000001
        WPU = %00000001   
     INTCON = %00001000
     CMCON0 = 7              
      VRCON = 0   
      ANSEL = %00000000
      OPTION_REG.7=0
    '==================================ALIAS========================================
     BOTAO   VAR GPIO.0
     VAZIO1  VAR GPIO.1
     VAZIO2  VAR GPIO.2
     VAZIO3  VAR GPIO.3
     VAZIO4  VAR GPIO.4
     LED     vAR GPIO.5
    '================================MAIN LOOP======================================
    Main:
    @ SLEEP
     INTCON.0 = 0
     High led
     Pause 5000
     low led 
    GOTO Main
    END
    Thanks and Regards;
    Gadelhas

  2. #2
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    HI again;

    One more thing that i notice is that when i power the circuit for the first time, it always turn on the led, however, the first instruction is "sleep", but it is not executed, but only when i power the circuit, after that it works fine, like it should work!
    Thanks and Regards;
    Gadelhas

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    You should confirm that the device is really running at 4Mhz. Perhaps it is running at 2. You often have to set up the OSCCON register. Check the datasheet.
    Also, the interrupt is triggered right away beause you have not cleared the interrupt flag BEFORE enabling the interrupt (usually PIR.x, but again, check your device).
    Charles Linquist

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    Quote Originally Posted by Charles Linquis View Post
    You should confirm that the device is really running at 4Mhz. Perhaps it is running at 2. You often have to set up the OSCCON register. Check the datasheet.
    Also, the interrupt is triggered right away beause you have not cleared the interrupt flag BEFORE enabling the interrupt (usually PIR.x, but again, check your device).
    Hi Charles;

    First of all, thanks for your answer.

    If you look at my code, you can see that the OSCCON is configured correctly. I allready found the time issue. The pic was actually running at 4Mhz, however the main code was executed 2 times. To resolve this problem, i change this code:

    Code:
    Main:
      @ SLEEP
      INTCON.0 = 0
      High led
      Pause 5000
      low led 
      GOTO Main
    With this, just changed one line;

    Code:
    Main:
      @ SLEEP
      High led
      Pause 5000
      low led 
      INTCON.0 = 0
    GOTO Main
    Now the main code is only executed one time and them goes to sleep.

    However, on power up the led still turns on, but it shoul not, because the first instruction is sleep. I follow your advice of clearing the interrupt flag before, however the behavior is the same.
    I'm still try to figure it out. If someone has any idea please let me know.

    Thanks
    Thanks and Regards;
    Gadelhas

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    It should not make a difference, but it might... Bits 0 - 3 in the OSCCON are status bits. Try setting OSCCON = %01100000

    You have the LED pin TRISed correctly in the setup but you did not make it LOW in the setup.
    TRIS sets direction, the state should also be set.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    Quote Originally Posted by mackrackit View Post
    It should not make a difference, but it might... Bits 0 - 3 in the OSCCON are status bits. Try setting OSCCON = 100000

    You have the LED pin TRISed correctly in the setup but you did not make it LOW in the setup.
    TRIS sets direction, the state should also be set.
    Already tried to put the OSCCON like you said. It is the same thing. like you said, they are status bits.

    You can see on my code, after the TRIS instruction, that i put all the GPIO low, with:

    Code:
              '76543210
    TRISIO = %00000001
       GPIO = %00000000
    Thanks and Regards;
    Gadelhas

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    Sorry, I missed the GPIO setting.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    Hi,

    Three things ...

    1) you use the IOC interrupt ... so that rings the " Mismatch condition " bell in my head ... see Sleep datasheet chapter.

    2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way.

    3) when you push a button ... you obviously release it one day ! makes two input changes ...

    Alain
    Last edited by Acetronics2; - 27th November 2011 at 09:57.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Jan 2009
    Posts
    78


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way
    try to add

    @ sleep
    @ nop

    If the GIE
    bit is clear (disabled), the device continues execution at
    the instruction after the
    SLEEP instruction. If the GIE bit
    is set (enabled), the device executes the instruction
    after the
    SLEEP instruction, then branches to the interrupt
    address (0004h). In cases where the execution of
    the instruction following
    SLEEP is not desirable, the
    user should have a
    NOP after the SLEEP
    instruction.

  10. #10
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Timing issue

    Hi All, and thanks for your answers!!!

    Quote Originally Posted by mackrackit View Post
    Sorry, I missed the GPIO setting.
    No problem Dave!!!


    Quote Originally Posted by Acetronics View Post
    Hi,

    Three things ...

    1) you use the IOC interrupt ... so that rings the " Mismatch condition " bell in my head ... see Sleep datasheet chapter.

    2) Microchip recommends to place a NOP instruction just past the Sleep one, when using the feature this way.

    3) when you push a button ... you obviously release it one day ! makes two input changes ...

    Alain
    Alain; I read the datasheet but couldn't get anywhere, believe me, I tried.

    Quote Originally Posted by bogdan View Post
    try to add

    @ sleep
    @ nop
    bogdan, tried also that, but its not the solution for my problem.

    Ok, the only conclusion that i came up, is this:

    If i disable the WPU on the button pin, put a pull-down resistor in this pin and connecting the VCC to the button, i don't get the issue. I only get the issue, if i take of the pull down resistor, enable the WPU and connect the GND to the button.
    So the conclusion that i came up, not knowing it it is the correct one, is that, on power up the GPIO state is LOW. So because i have the WPU enable on this pin, i think a interrupt is triggered, because i have a mistmach condition on this pin. If i disable the WPU, put a pull-down resistor in this pin and connecting the VCC to the button, i don't have the mistmatch condition, so the PIC goes to sleep.

    If this conclusion is not the correct one, i don't know, why the micro is not going to sleep on power up!!!
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts