How to measure the Fcy of a PIC with oscilloscope?

Discussion in 'Embedded Systems and Microcontrollers' started by richiechen, Jan 10, 2013.

  1. richiechen

    Thread Starter Member

    Jan 1, 2012
    93
    0
    Hi everyone

    I am using a dsPIC33EP. And I have configured the dsPIC in the 60MIPS mode. However, how can I check to see if the PIC is actually working under 60MIPS?

    I wrote a simple code:
    while(1) { _LATB5=~_LATB5; }

    And on the oscilloscope it shows a square wave of around 1us. However, I dont understand the relation between the sample code and the Fcy.
    Is it possible that two runs of the while loop will take 30 cycles?

    Could anyone help me?

    Thank you very much.

    Best regards
    Richie CHEN
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    That's one of the first things I also do with a new design: verify it is running as fast as I think it's running. I skipped over PIC33's and went from PIC18 to PIC32, but I believe they are similar to what I've done.

    Are you working inside MPLAB? If so, enable any debugger you like (MPLAB sim if you're new to this) and drop a breakpoint at your code. Run the code till it stops, and pick View | Disassembly listing from the menu. Now you see how many basic instructions your code is actually taking.

    Any jump in a PIC (excepting PIC32's) takes 2 instruction cycles, so the minimum number of cycles your code has is 3 per half cycle. I usually do something like this:

    Typically directly setting a port pin will just take C 1 cycle, so I do this (and check the disassembly listing too):

    Code ( (Unknown Language)):
    1. while(1)
    2. {
    3.   _LATB5 = 0;
    4.   _LATB5 = 1;
    5.   _LATB5 = 0;
    6.   _LATB5 = 1;
    7.  
    8. ...
    9.  
    10.   _LATB5 = 0;
    11.   _LATB5 = 1;
    12. }
    It's just test code to be dumped anyway... so give it LOTS of 1's and 0's so they look nice on your O scope.
     
    richiechen likes this.
  3. richiechen

    Thread Starter Member

    Jan 1, 2012
    93
    0
    Thank you Ernie!! It works!
    Does this:
    BSET LATB, #5

    mean 1 instruction cycle?
    If so , it seems the MCU cannot reach the specification declared.

    Cheers
    Richie
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I use an old trick like this;

    while(1)
    {
    LATA.F0 = TMR0.F7;
    }

    This gives zero average error and on most PICs give a frequency of xtal/1024 (assuming TMR0 prescaler is set to 1:1). On a PIC33 you will need to specify the right timer bit and the right output port pin of course.
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's possible to use a pin to output the clock frequency (or at least a known fraction of it). Look up REFOCON in the datasheet.
     
    richiechen likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    Well, I don't know lol. I don't use those devices but if you are inside MPLAB you can call up the disassembly listing form the menu and see just how many instructions each C statement created. Using MPLAB sim you can single step thru these and insure they do indeed execute in 1 cycle.

    Markd77 may have a better idea using REFOCON. I don't have anything to add to his suggestion but it sounds like other people have had your problem and there is an existing solution.
     
Loading...