+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Full Member level 5
    Points: 1,316, Level: 8

    Join Date
    Mar 2017
    Posts
    253
    Helped
    5 / 5
    Points
    1,316
    Level
    8

    16F628A - internal clock precision? Or maybe the scope...

    Hey.
    I have setup my 16F628A with INTOSC and CLKOUT function:
    Code:
    //  Precision internal 4 MHz oscillator factory calibrated to ±1%
    void main() {
    
      TRISA = 0;           // set direction to be output
      TRISB = 0;           // set direction to be output
                PCON.OSCF = 1;   // Set internal oscillator frequency to 4 MHz
               PCON = 0x08;
      do {
      PORTA = 0;
      PORTB = 0;
        Delay_ms(1000);    // 1 second delay
                PORTA = 0xff;
                PORTB = 0xff;
        Delay_ms(1000);    // 1 second delay
      } while(1);          // Endless loop
    }
    I have setup the circuit on the breadboard and added 100nF capacitor between VCC and VDD.
    I have also setup "intosc + CLKOUT" config bit in mikroC PRO for PIC.

    Now, I have measured the output waveform with the old analogue scope, and it shows a correct shape but period is 4.7 divs at 0.1uS time base, which means 0.47uS period, which is around 2.12MHz!
    Even rounding to 5 divs - 0.5uS period gives 2MHz.

    So now, my question is, am I mistaken with PIC configuration and it's infact generating the 2MHz square wave instead of 4MHz, or is there something wrong with my measurement?

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 1
    Points: 1,014, Level: 7
    Achievements:
    7 years registered
    hexreader's Avatar
    Join Date
    Apr 2011
    Location
    England
    Posts
    490
    Helped
    193 / 193
    Points
    1,014
    Level
    7

    Re: 16F628A - internal clock precision? Or maybe the scope...

    CLKOUT option provides Fosc/4, so for internal 4MHz expect 1MHz on CLKOUT pin. (14.2.7 of the datasheet)

    This equates to 1uS period, or 0.5uS low and 0.5uS high.



    •   AltAdvertisement

        
       

  3. #3
    Super Moderator
    Points: 81,387, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,343
    Helped
    4455 / 4455
    Points
    81,387
    Level
    69

    Re: 16F628A - internal clock precision? Or maybe the scope...

    Also note that the program is high level source code, the actual code being executed will add extra delays which may be hard to predict. It would therefore not be reliable to use the port bits toggling as a method of measurement.

    Hexreader correctly quotes 0.5uS low and 0.5uS high on the CLKOUT pin which would correspond with 4MHz system clock. It would be difficult to take an exact reading from an analog scope so I suspect the inaccuracy is in the scope rather than the PIC. I have a project using a 16F628A on my work bench at the moment so I'll temporarily configure it to produce CLKOUT and let you know exactly what it produces. I have accurate measurement equipment here! It's late at night here so I'll reply tomorrow.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  4. #4
    Full Member level 5
    Points: 1,316, Level: 8

    Join Date
    Mar 2017
    Posts
    253
    Helped
    5 / 5
    Points
    1,316
    Level
    8

    Re: 16F628A - internal clock precision? Or maybe the scope...

    Quote Originally Posted by hexreader View Post
    CLKOUT option provides Fosc/4, so for internal 4MHz expect 1MHz on CLKOUT pin. (14.2.7 of the datasheet)

    This equates to 1uS period, or 0.5uS low and 0.5uS high.
    Good point, but it still does not explain the 100% measurement error. (now 2MHz instead of 1MHz).. I will try again tomorrow, and recheck everything.


    Quote Originally Posted by betwixt View Post
    Also note that the program is high level source code, the actual code being executed will add extra delays which may be hard to predict. It would therefore not be reliable to use the port bits toggling as a method of measurement.
    Are you sure that CLKOUT output has anything to do with the code executed in the main loop?

    Quote Originally Posted by betwixt View Post
    Hexreader correctly quotes 0.5uS low and 0.5uS high on the CLKOUT pin which would correspond with 4MHz system clock. It would be difficult to take an exact reading from an analog scope so I suspect the inaccuracy is in the scope rather than the PIC. I have a project using a 16F628A on my work bench at the moment so I'll temporarily configure it to produce CLKOUT and let you know exactly what it produces. I have accurate measurement equipment here! It's late at night here so I'll reply tomorrow.
    I've never had so much big measurement error with that scope, I am using 10x probe



    EDIT: Okay, now it seems that 1MHz output is the correct one. So hexreader answer explained everything. I was relying too much on the intuition that "CLKOUT" means "clock frequency out". Thanks!
    Last edited by Garyl; 5th December 2017 at 03:02.



  5. #5
    Super Moderator
    Points: 81,387, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,343
    Helped
    4455 / 4455
    Points
    81,387
    Level
    69

    Re: 16F628A - internal clock precision? Or maybe the scope...

    This is what my 628A is showing on CLKOUT. It is a well used PIC date coded 2009 running from 5.05V VDD at 19C.

    Click image for larger version. 

Name:	628ACLKOUT.png 
Views:	8 
Size:	19.3 KB 
ID:	143075

    The frequency implies 3.994MHz internal clock which is an error of only about 0.57%
    There is quite a lot of jitter on the clock, hence the high deviation figure which is quite noticeable if I beat CLKOUT against my local AM radio station on 990KHz but it confirms the central frequency is accurate.

    The program doesn't change CLKOUT but using it as a way of assessing the speed is unreliable of course.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  6. #6
    Advanced Member level 4
    Points: 7,811, Level: 21

    Join Date
    Jan 2015
    Posts
    1,095
    Helped
    346 / 346
    Points
    7,811
    Level
    21

    Re: 16F628A - internal clock precision? Or maybe the scope...

    Check out Table 17-5 in the data sheet for the precision of the internal oscillator and also Figure 17-5 (just below it) for the relationship between the oscillator and CLKOUT.
    Susan


    1 members found this post helpful.

  7. #7
    Full Member level 5
    Points: 1,316, Level: 8

    Join Date
    Mar 2017
    Posts
    253
    Helped
    5 / 5
    Points
    1,316
    Level
    8

    Re: 16F628A - internal clock precision? Or maybe the scope...

    Quote Originally Posted by betwixt View Post
    This is what my 628A is showing on CLKOUT. It is a well used PIC date coded 2009 running from 5.05V VDD at 19C.

    Click image for larger version. 

Name:	628ACLKOUT.png 
Views:	8 
Size:	19.3 KB 
ID:	143075

    The frequency implies 3.994MHz internal clock which is an error of only about 0.57%
    There is quite a lot of jitter on the clock, hence the high deviation figure which is quite noticeable if I beat CLKOUT against my local AM radio station on 990KHz but it confirms the central frequency is accurate.

    The program doesn't change CLKOUT but using it as a way of assessing the speed is unreliable of course.

    Brian.
    Thx, just out of curiousity, can you check how much this frequency can be pulled by adding capacitance to this pin?



    •   AltAdvertisement

        
       

  8. #8
    Super Moderator
    Points: 81,387, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,343
    Helped
    4455 / 4455
    Points
    81,387
    Level
    69

    Re: 16F628A - internal clock precision? Or maybe the scope...

    I tried it and as expected, there was absolutely no change in frequency when a 2.2nF capacitor was connected from CLKOUT to VSS.
    The CLKOUT signal must pass through some digital buffering to allow the pin can be reassigned as I/O so in internal oscillator mode it isn't directly connected to the oscillator circuit.

    The overshoot in the picture is caused by the wiring to an LCD module by the way, it isn't coming out of the PIC. in normal operation, that pin is driving the 'E' pin on the LCD module and I left it connected for the test.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



--[[ ]]--