• AVR Freaks

Hot!8 bit Cycle Time

Author
rogersw8n
Junior Member
  • Total Posts : 92
  • Reward points : 0
  • Joined: 2015/04/27 11:30:22
  • Location: Georgia, USA
  • Status: offline
2020/06/17 11:12:57 (permalink)
0

8 bit Cycle Time

Hi
I am working with a PIC16F1503 in the simulator.  I am learning.  I was attempting to nail down my understanding of cycle times... sort of.  So, I wanted to see the impact on my while loop in main when I set different oscillator frequencies.  I am using the internal oscillator.  I have a very simple code set as you see below.
 
#include <xc.h>

void main(void) {
    
    OSCCON = 0x02;
    unsigned int counter = 100;
    
    while(1){
        
        if(counter=='0'){
            counter=100;
            break;
        }
        else{
            counter--;
        }
        
    }
    
    return;
}

 
The code above has the oscillator set to the low setting of 31 kHz.  I set break points on:
 
unsigned int counter = 100
counter = 100
 
Using the simulator stopwatch the first break point is always hit at 10 uS and the second is hit at 739 uS whether I run at 31 kHz or 16 MHz.  Can someone help me understand why?
 
For reference, when I set the oscillator to 16 MHz I set OSCCON = 0x7A.
 
Thank you!
#1

12 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 19023
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: 8 bit Cycle Time 2020/06/17 11:30:49 (permalink)
    +1 (1)
    That is C Code.  The Compiler will make ASM.
    On a PIC16 each OP-Code usually takes 4 clock cycles to complete.
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 12032
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 11:51:10 (permalink)
    +1 (1)
    You have quotes around your '0', and "counter" should be volatile.
    #3
    rogersw8n
    Junior Member
    • Total Posts : 92
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 11:55:39 (permalink)
    0
    Thank you very much for the reply.  I am still confused though.  If I set the oscillator to 16 MHz 1 instruction (4 clock cycles) should take 4/16,000,000 = 250 nanoSecs.  At 31 kHz it would be 4/31,000 = .13 mSec.  Regardless of C or ASM, I don't understand why that OSC change would not impact my loop speed.
    #4
    upand_at_them
    Super Member
    • Total Posts : 739
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: 8 bit Cycle Time 2020/06/17 11:55:41 (permalink)
    0
    Exactly.  When you write '0' it gets interpreted as the ASCII value for the character '0' (zero), which is 48.  If you want to compare to the actual value of zero get rid of the quotes.
    #5
    upand_at_them
    Super Member
    • Total Posts : 739
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: 8 bit Cycle Time 2020/06/17 11:56:45 (permalink)
    +1 (1)
    You say that your posted code sets the oscillator to 32kHz, but it doesn't.  See page 49 of the datasheet.
     
    post edited by upand_at_them - 2020/06/17 11:59:52
    #6
    rogersw8n
    Junior Member
    • Total Posts : 92
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 12:01:07 (permalink)
    0
    I removed the quotes.  Thanks!  Now the code looks like this.
     
    #include <xc.h>

    void main(void) {
        
        OSCCON = 0x7A;
        unsigned int volatile counter = 100;
        
        while(1){
            
            if(counter== 0){
                counter=100;
                break;
            }
            else{
                counter--;
            }
            
        }
        
        return;
    }

     
    However, the stopwatch output is the same for OSSCON = 0x02 and OSCCON = 0x7A.
     
    0x02
    Target halted. Stopwatch cycle count = 10 (10 µs)
    Target halted. Stopwatch cycle count = 1310 (1.31 ms)
    0x7A
    Target halted. Stopwatch cycle count = 10 (10 µs)
    Target halted. Stopwatch cycle count = 1310 (1.31 ms)
     
     
    #7
    upand_at_them
    Super Member
    • Total Posts : 739
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: 8 bit Cycle Time 2020/06/17 12:26:38 (permalink)
    0
    Oops, my mistake.  I think this is an issue with the simulator.
    #8
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 12032
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 12:32:45 (permalink)
    +1 (1)
    The simulator doesn't support certain things, and the oscillator is commonly one of them.  Check simulator documentation for your PIC.  But your time experimenting will be much better spent on a dev board.
    #9
    1and0
    Access is Denied
    • Total Posts : 11501
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 12:38:48 (permalink)
    +3 (3)
    You have to tell the simulator your instruction cycle speed in the project properties.
    #10
    NorthGuy
    Super Member
    • Total Posts : 6404
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 13:00:29 (permalink)
    +3 (3)
    When you run the same program, the number of cycles will be the same regardless of the clock speed. The cycles get shorter as you speed up the clock, but their count doesn't change.
    #11
    ric
    Super Member
    • Total Posts : 28940
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/17 13:21:15 (permalink)
    +3 (3)
    1and0
    You have to tell the simulator your instruction cycle speed in the project properties.

    +1
    The simulator does NOT track what speed you will get from writes to the oscillator control registers.
    It relies on YOU telling it what the CPU speed will be.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    rogersw8n
    Junior Member
    • Total Posts : 92
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: 8 bit Cycle Time 2020/06/18 03:37:03 (permalink)
    +1 (1)
    When I set the simulator CPU speed, the stopwatch results look more like what I would expect and as many of you are describing.
     
    31 kHz Results
    Target halted. Stopwatch cycle count = 10 (322.580645 µs)
    Target halted. Stopwatch cycle count = 1310 (42.258065 ms)
     
    16 MHz Results
    Target halted. Stopwatch cycle count = 10 (625 ns)
    Target halted. Stopwatch cycle count = 1310 (81.875 µs)
     
    Thanks to everyone for your input and insight!
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5