• AVR Freaks

Hot!*HELP* 12F683 timer not working as expected

Page: 123 > Showing page 1 of 3
Author
Nemo
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2013/03/17 22:09:57
  • Location: 0
  • Status: offline
2018/12/06 02:19:01 (permalink)
0

*HELP* 12F683 timer not working as expected

Hello everyone,
I have been struggling with the timer of 12F683 running at 8Mhz internal oscillator.

Expected output from my calculation
fosc/4   =   8000KHz/4  =   2000KHz          -----    Instruction cycle rate
t=1/2000KHz  =   0.0005msec  =  0.5µsec       -----    Time taken for each increment of Timer0
 
So accorging to my code I should get a 0.5µsec output, but on hooking up a logic analyser I get 0.1532ms output.
Please help me out in finding what wrong am I doing.
 
 
THE CODE
#include<htc.h>
#include<pic12f683.h>

#define _XTAL_FREQ 8000000
__CONFIG(FOSC_INTOSCIO &WDTE_OFF&PWRTE_OFF&MCLRE_OFF&CPD_OFF&BOREN_OFF&CP_OFF);

void main(){
TRISIO0=1; //PIN7
TRISIO1=0; //PIN6
TRISIO2=0; //PIN5
TRISIO3=0; //PIN4
TRISIO4=0; //PIN3
TRISIO5=1; //PIN2

GP2=0; // Setting GP2 OUTPUT to 0 initially

ANSEL=0x00; // Analog Selection OFF
ADCON0 = 0x00; // Analog to Digital converter OFF
CMCON0 = 0x07;
VRCON=0x00; // Set Voltage Reference OFF

OPTION_REGbits.PSA=1; // Set Prescaler OFF
OPTION_REGbits.PS0=0;
OPTION_REGbits.PS1=0;
OPTION_REGbits.PS2=0;
OPTION_REGbits.T0CS=0;
OPTION_REGbits.T0SE=0;
OPTION_REGbits.nGPPU=1;
OPTION_REGbits.INTEDG=1;

OSCCONbits.IRCF0=1; /****************************************/
OSCCONbits.IRCF1=1; /*** Internal Oscillator set to 8 MHz ***/
OSCCONbits.IRCF2=1; /****************************************/

    while(1){

          if(T0IF){
              GP2=!GP2; // Toggles the LED state
              T0IF=0;
                  } // T0IF flag END


 } // END of While
} // END of MAIN

post edited by nemo4all - 2018/12/06 02:23:01

Attached Image(s)

#1

42 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 02:59:48 (permalink)
    +2 (2)
    The interrupt flag is set each time the timer rolls over, which happens every 256 counts,
    so I would expect the period to be 256 * 0.5µsec = 0.128 ms, a little shorter than your measured 0.1532ms
     

    Nearly there...
    #2
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 03:04:48 (permalink)
    +2 (2)
    Timer0 is a counter that counts from 0 up to 255.  When it wraps around from 0 to 255 it generates an interrupt.
     
    Therefore your clock rate is divided by 256.
    256 * 0.5µsec = 0.128ms
     
    Can't say why your measured rate is 0.1532ms.  That's about 20% out, and I'd expect better accuracy from the internal oscillator.
     
    #3
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 03:49:15 (permalink)
    0
    qhb
    The interrupt flag is set each time the timer rolls over, which happens every 256 counts,
    so I would expect the period to be 256 * 0.5µsec = 0.128 ms, a little shorter than your measured 0.1532ms
     



    Ahh... My mistake.. Coding after a long time, just forgot the calculation of TOIF counting. But still why instead of 0.128ms the logic analyser is reading 0.1532ms..?
    #4
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 03:49:15 (permalink)
    0
    pcbbc
    Can't say why your measured rate is 0.1532ms.  That's about 20% out, and I'd expect better accuracy from the internal oscillator.
     



    Exactly.... Thats what I am thinking now..Its my mistake that I forgot about the TOIF's 256 counts but the analyser should atleast read 0.1532ms
    post edited by nemo4all - 2018/12/06 03:53:38
    #5
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:02:43 (permalink)
    0
    Now I have enabled the prescaller  and set it to 1:8.
    So now I should get a precise output of 0.5µsec * 8 * 256 = 1024µsec = 1.02ms

    But again I read 1.208ms on the analyzer, which is again around 18.43% extra..


    post edited by nemo4all - 2018/12/06 04:06:04
    #6
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:10:25 (permalink)
    +1 (1)
    You are 20% out again.  Seems like your OSC is not 8MHz, or your analyser is not accurate.
    Try cross checking measurements with a different instrument.
    Try configuring to output 8MHz OSC directly to pin 3 CLKOUT, and measure that instead.
     
    Datasheet says 1% factory calibrated for precision internal oscillator, so 20% is way off the mark.
     
    Edit: I am sure this probably has nothing to do with it, but 50Hz is also a 20% increase over 60Hz.  Is something somehow calibrated to mains frequency?!
    post edited by pcbbc - 2018/12/06 04:18:10
    #7
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:15:33 (permalink)
    +1 (1)
    Not your problem, but you should delete this line
    #include<pic12f683.h>

    That file has already been included by htc.h, so there's no point doing it again.
     

    Nearly there...
    #8
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:21:37 (permalink)
    0
    qhb
    Not your problem, but you should delete this line
    #include<pic12f683.h>

    That file has already been included by htc.h, so there's no point doing it again.
     


    I have removed
    #include<pic12f683.h>
    but this did not help.
    Same result.
    #9
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:23:21 (permalink)
    +2 (2)
    That's what "not your problem" meant!

    Nearly there...
    #10
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:24:08 (permalink)
    +1 (1)
    If you do not have another instrument to measure against, divide further until you can measure accurately with a stopwatch.
    Your code looks fine, and I can think of no conceivable reason for roughly 18-20% error other than:
    a) Faulty measurement
    b) Faulty part
    #11
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:25:35 (permalink)
    0
    pcbbc
    Try cross checking measurements with a different instrument.
     

     
    Tried another analyzer, but still same result. I have checked both the analyzer with another simulator, and the analyzer seems to give correct results of the simulator.
     
    pcbbc
    Try configuring to output 8MHz OSC directly to pin 3 CLKOUT, and measure that instead.
     


    I tried the same before to set the output on GP4 which is PIN3 of the 12F683, but still the result is same.

    Now just let me try the default 4MHZ oscilator and see the result.


    #12
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:37:21 (permalink)
    0
    Now I have set the default internal oscillator which is 4Mhz and I have set the prescaler to 1:4.
    So according to calculation I should again get output of 1.02 ms but I again read 1.208ms on the analyzer.

    #include<htc.h>

    #define _XTAL_FREQ 4000000
    __CONFIG(FOSC_INTOSCIO &WDTE_OFF&PWRTE_OFF&MCLRE_OFF&CPD_OFF&BOREN_OFF&CP_OFF);//&IESO_OFF&FCMEN_OFF);

    void main(){
    TRISIO0=1; //PIN7
    TRISIO1=0; //PIN6
    TRISIO2=0; //PIN5
    TRISIO3=0; //PIN4
    TRISIO4=0; //PIN3
    TRISIO5=1; //PIN2

    GP4=0; // Setting GP4 OUTPUT to 0 initially

    ANSEL=0x00; // Analog Selection OFF
    ADCON0 = 0x00; // Analog to Digital converter OFF
    CMCON0 = 0x07;
    VRCON=0x00; // Set Voltage Reference OFF

    OPTION_REGbits.PSA=0; // Set Prescaler OFF
    OPTION_REGbits.PS0=1;
    OPTION_REGbits.PS1=0;
    OPTION_REGbits.PS2=0;
    OPTION_REGbits.T0CS=0;
    OPTION_REGbits.T0SE=0;
    OPTION_REGbits.nGPPU=1;
    OPTION_REGbits.INTEDG=1;


    //OSCCONbits.IRCF0=1; /****************************************/
    //OSCCONbits.IRCF1=1; /*** Internal Oscillator set to 8 MHz ***/
    //OSCCONbits.IRCF2=1; /****************************************/

        while(1){

        if(T0IF){
            GP4=!GP4; // Toggles the LED state
            T0IF=0;
                } // T0IF flag END

     } // END of While
    } // END of MAIN

    #13
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:38:33 (permalink)
    0
    Check bit 2 of OSCCON
     

    bit 2 HTS: HFINTOSC Status bit (High Frequency – 8 MHz to 125 kHz)
    1 = HFINTOSC is stable
    0 = HFINTOSC is not stable
     
    Perhaps your clock is not stable?  Noisy power supply?
    #14
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:51:14 (permalink)
    +1 (1)
    nemo4allNow I have set the default internal oscillator which is 4Mhz and I have set the prescaler to 1:4.
    4MHz is just the 8MHz oscillator divided by 2 by a prescaler.
    So if 8MHz is out by 18-20%, 4MHz (and 2MHz, 1MHz, 500kHz, 250kHz and 125kHz which all derive from HFINTOSC) will be also.
    #15
    1and0
    Access is Denied
    • Total Posts : 9285
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:51:56 (permalink)
    +2 (2)
    Try changing the PIC device.
    #16
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 04:56:22 (permalink)
    0
    Was just wondering if my configuration bits playing the bad role..
    FOSC_INTOSCIO

    I set this to use the pin 3 of the PIC. The datasheet says to use INTOSC to get oscillator putput from osc1 and osc2, but the compiler does not have the option of INTOSC...

    In the meantime will try HFINTOSC and a new PIC
    #17
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 05:02:56 (permalink)
    +1 (1)
    Try "FOSC_INTOSCCLK"
     

    Nearly there...
    #18
    Nemo
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2013/03/17 22:09:57
    • Location: 0
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 05:07:58 (permalink)
    0

    1and0
    Try changing the PIC device.



    Just changed the PIC and things worked like a charm..
    Probably I have testing plenty of codes on this same PIC for years and too many WRITEs have made it bad..
    A precise output of 1.025ms..

    Thank you everyone for your help..

    Now my question how can I obtain a precise 1ms timer instead of 1.02ms..?


    post edited by Nemo - 2018/12/06 05:09:41
    #19
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: *HELP* 12F683 timer not working as expected 2018/12/06 05:18:53 (permalink)
    +2 (2)
    Use Timer 2 and the PR2 register.
    Set Prescaler to 1:16 and PR2=124 and it will roll over once per millisecond with an 8MHz clock.
    post edited by qhb - 2018/12/06 05:20:35

    Nearly there...
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5