Oscillators, counts, clock cycles, config settings ......& women


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Oscillators, counts, clock cycles, config settings ......& women

    Ok, so I put in 'women' to shamfully get a few more 'curiousity' hits - lol.

    Women are quite nice - I enjoy some aspects of them very much.

    Ok, that's that bit covered off. To the oscillator related stuff.

    In my cosy little noobesque world, I've until now being running nice copycat config settings with the internal oscillator @4Mhz. But alas, for my needs there wasn't enough resolution so I pushed th boat out & bought a 20Mhz cermaic resonator for the princely so of 20p (30c).

    I'm using a 16f690 - so connected the resonator to pins 2 & 3.

    Fine so what's the problem? Well, counting clock pulses is the problem....there doesn't seem to be enough of them!

    I'm using a 1Khz signal to 'trigger' a comparator internally withing my PIC, this in turn generates a comparator interrupt, which I've setup to start Timer1 running.

    When the next comparator trigger arrives (ie 1ms later for the 1khz signal I'm feeding into the comparator)...I store the timer1 count.

    With me so far?

    Ok, the count I'm getting is 2,500 (it's rock solid - that said if I change the input 'trigger' frequency the timer1 count moves, so this confirms the chain is all working !)

    But with a 20Mhz clock, I should be getting a whole heap more 'clock counts' between the comparator firing off successive interrupts.

    with a 1khz comparator input - by my calculations that should yield an interrupt every 1ms....so I worked out how many clocks there should be during that time (timer1 count). At 20Mhz there should be 20,000 rx'ed in 1ms.

    But I'm only seeing 2,500?!!!

    Now Jerson helpfully queried whether I had a prescaler set (x8 would tie up with what I'm seeing....I'm expecting the clock count to be 20,000, but I'm seeing 2,500) - I don't believe so.

    So how do I get to the bottom of this discrepancy?

    Is it a Osc config setting? (remember this is the first time I've used an External oSicllator - it's highly conceivable I've perhaps configured the PIC wrong). Here's my associated config (I use MPASM)....

    Code:
    @ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
    
    DEFINE  OSC 20          ' set Oscillator at 20Mhz
    OSCCON.0 = 0
    Is it a problem with my approach?

    Is it a problem with my maths?



    As an aside, is there a prescribed method for confirming the clock is running at the frequency you want it to run at?!!!

    Many thanks in anticipation!
    Last edited by HankMcSpank; - 13th September 2010 at 12:08.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Hi, Hank

    Let 's see ...

    @ 20Mhz clock ( better say : Xtal ) ...

    you have .200µs for a timer 1 without prescaler increment ... ( internal clock is 1/4 the Xtal Freq ... )

    so, 1000/.200 = 5000 ... ( let's forget Interrupt duration for the moment )

    if my math still " not so bad " ...

    now ... just a 1:2 ratio to dig out ...

    So, IF you sure your Xtal is a 20 Mhz one ...

    only your settings ( full listing SVP !!! ) AND your interrupt stubb could enlight this 3rd millenium mystery ...

    Alain
    Last edited by Acetronics2; - 13th September 2010 at 12:27.
    ************************************************** ***********************
    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 " !!!
    *****************************************

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Hi Alain.... thanks for getting involved!

    Quote Originally Posted by Acetronics View Post
    you have .200µs for a timer 1 without prescaler increment ... ( internal clock is 1/4 the Xtal Freq ... )
    Aha [the sound of a small penny dropping on a hard surface]...how naive of me - I thought that for my 20p investment, I'd get a positive whizzy PIC...but now you're breaking the news to me that I'm actually only getting 5Mhz of 'doing stuff' speed?!

    Damnit...I want my 20p back.

    The full code/config will have to wait until tonight (I left it at home!)...so the universe will need to wait with baited breath for 6 hours or so before establishing where the other 2,500 'clock count' total @1khz went (my wife is presently checking behind the sofa)

    To paraphrase Arnie "I'll be back"

    Merci beaucoup

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    but now you're breaking the news to me that I'm actually only getting 5Mhz of 'doing stuff' speed?!

    Damnit...I want my 20p back.

    yes ... you " only " *** laughing *** get 5 million ops a sec ... What a pity ... isn't it ???

    looking a bit further ... 16F devices generally can reach 32 Mhz , 16F193x are 32Mhz Nominal ... MalcC realized officially last week 64 Mhz with a 18F45xx ... ( 40 Mhz nominal ) and the new 18F2xK20 are 64Mhz Nominal ...

    so, there's room to play somewhat ... before using higher range devices

    BUT ... may be you simply could open your datasheed and see what the stuff explained in chapter 6 ( Gated Timer 1 W/ Comp2 ) can do for you ...


    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post

    BUT ... may be you simply could open your datasheed and see what the stuff explained in chapter 6 ( Gated Timer 1 W/ Comp2 ) can do for you ...
    Yeah I saw that feature & thought "cool, it's got a gate that I can use" - but I then read that part of the datasheet four or five times, & had to lay down in a darkened room for 2 hours, with a cold wet towel across my forehead.

    In a few weeks it'll likely be a doddle to see what they're getting at (working on the principle that a few weeks ago, just getting the comparators working &/or DT's interrupts working...seemed like having to read the hubble space telescope service manual reflected in a mirror - whereas today they're a veritable breeze), but for now, I'll curl up in the foetal position & just be content with how far I'm pushing the McSpank envelope at present.


    PS re 5 million instruction per second - lol, talk about value for money ....£1.50! (I could launch a lunar mission with less)

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Ok, so it was like 2.00am this morning when I retired from this conundrum & I guess my mind was fogged this morning when I posted - the number I'm seeing is 4500 (not 2500). Here's what I'm seeing withrt comparator 'counts' when feeding a 1Khz signal in...

    comp1=4500 comp2Time=489
    comp1=4495 comp2Time=489
    comp1=4499 comp2Time=490
    comp1=4501 comp2Time=490
    comp1=4498 comp2Time=486
    comp1=4502 comp2Time=490
    comp1=4497 comp2Time=487
    ...and so on.

    Now in the light of your info Alain, I'm actually only looking for a count of 5000 (vs the 20,000 I'd erroneousy calculated @1khz incoming signal), this sheds new light on the situation.

    I now think my Oscillator is working correctly at 20Mhz! (that's the good news bit)

    But the bad news....my comparator1 count still has a 500 count shortfall.

    I reckon the 500 'count' shortfall is due to the way I'm setting the comparators to count becuase if if you add the two together is as near as damint to 5,000, so there's an issue with the way I'm counting (or in other words my code sucks)

    I want comparator1 to count the time between incoming signal 1khz signal 'cycles' (so, 5000 for 1khz input) & the second comparator to time how many clocks before a second lagging signal arrives....

    Code:
    @ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
    
    DEFINE  OSC 20          ' set Oscillator at 4Mhz.
    DEFINE  NO_CLRWDT 1   ' PBP doesn't clear WDT automatically
    DEFINE HSER_TXSTA 24h ' Enable transmit
    DEFINE HSER_SPBRG 129 ' set USART to 9600 baud  @20Mhz
    
    
    '***********************************************************************************************
    
    '16F690 Pin Connections....
    ' PIN# NAME     USE & CONNECTION
                    '  1   Vdd      +5VDC power supply
    TRISC.4 = 0     '  6   RC4      C2OUT
    TRISC.3 = 1     '  7   RC3      C12IN3- (external comparator1 input)
    TRISC.2 = 1     '  14  RC2      C12IN2- (external comparator2 input)
    TRISC.0 = 1     '  16  RCO      C2IN+ 
    TRISA.2 = 0     '  17  RA2      C1OUT
    TRISA.1 = 1     '  18  RA1      External VREF In.
    TRISA.0 = 1     '  19  RCO      C1IN+
                    '  20  Vss      Ground
    '**********************************************************************************
    OSCCON.0 = %0001000 
    txsta = %10100100   'setup the tx register
    RCSTA.7 = 1         ' Enable RB7 for TX USART
    INTCON.0 = 0        ' clears the RABIF Flag (to 0), COULD be 1 on reset (unique to F690)
    ANSEL      = 0      'disable AtoD.
    ANSELH     = 0      'disable AtoD.
    
    'Turn on & Set up Comparator 1
    CM1CON0.7 = 1       'Comparator 1 on.
    CM1CON0.6 = 0       'C1OUT POLARITY BIT
    CM1CON0.5 = 1       '1 = C1OUT is present on the C1OUT pin (pin 17)
    CM1CON0.4 = 1       'C1OUT logic - 0 is not inverted  (1 for inverted)
    CM1CON0.3 = 0       'unimplemented
    CM1CON0.2 = 1       'Connect internally to the output of C1VREF   (or 0 for the the associated comp in pin)
    CM1CON0.1 = 1       'this bit with bit 1 set the external incoming pin - in this case c12in3- (pin 7)
    CM1CON0.0 = 1       'this bit with bit 1 set the external incoming pin - in this case c12in3- (pin 7)
    
    'Turn on & Set up Comparator 2
    CM2CON0.7 = 1       'Comparator 2 on.
    CM2CON0.6 = 0       'C1OUT POLARITY BIT
    CM2CON0.5 = 1       '1 = C2OUT is present on the C2OUT pin (pin 6)
    CM2CON0.4 = 1       'C1OUT logic is not inverted  (1 for inverted)
    CM2CON0.3 = 0       'unimplemented
    CM2CON0.2 = 1       'Connect internally to the output of C1VREF (or 0 for the the associated comp in pin)
    CM2CON0.1 = 1       'this bit with bit 1 set the external incoming pin - in this case c12in2- (pin 14)
    CM2CON0.0 = 0       'this bit with bit 1 set the external incoming pin - in this case c12in2- (pin 14)
    
    
    VRCON.7 = 1         'Comparator1 CV Ref enable bit
    VRCON.6 = 1         'Comparator2 CV Ref enable bit 
    VRCON.5 = 0         'high or low range  0 = High Range, 1 = low range
    VRCON.4 = 0         '0.6V Reference Enable bit   ....0 is disabled
    VRCON.3 = 0         'these 4 bits set the divider of the VREF ladder 16 values - 7 should yield 1/2 VCC
    VRCON.2 = 1         'these 4 bits set the divider of the VREF ladder 16 values - 7 should yield 1/2 VCC
    VRCON.1 = 1         'these 4 bits set the divider of the VREF ladder 16 values - 7 should yield 1/2 VCC
    VRCON.0 = 1         'these 4 bits set the divider of the VREF ladder 16 values - 7 should yield 1/2 VCC
    
    
    Comp1Time       var  word    ' used to amalgamate TMR1 High & Low Bytes.
    Frequency       var  word    'used to convert the count to frequency.
    Comp2Time       VAR WORD
    PHASE           var   word
    
    
    ' the following is pretty much a straight lift from the compiler manual (DIV32 section)
    a Var Word
    b Var Word
    c Var Word
    dummy Var Word
    b = 10000
    c = 5000
    
              
    INCLUDE "DT_INTS-14.bas"     ' Base Interrupt System  PO90OOO9
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   CMP1_INT,  _Comp1_Int,   PBP,  yes
            INT_Handler   CMP2_INT,  _Comp2_Int,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    TMR1H = 0 'Set the high part of the timer value to 0
    TMR1L = 0 'Set the low part of the timer value to 0 
    T1CON.0= 1  'start timer
    
    @ INT_ENABLE  CMP1_INT     ; enable Comparator 1 interrupts
    
    Comp1Time = 0 'clear down Comp1Time, prior to starting.
    comp2Time = 0  'clear down Comp2Time, prior to starting
    T1CON = %00001101
    
    'Main body of Code*********************************************************************************************
    Main:
    
            HSEROUT ["comp1=",dec Comp1Time,9,"comp2Time=", dec comp2time, 13, 10]
            PAUSE 500
            goto Main
            end  
    'Comparator1 Interrupt Handler+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
    
    Comp1_Int:
    @ INT_DISABLE  CMP1_INT
            Comp1Time.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits 
            Comp1Time.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits
            TMR1H = 0 'Set the high part of the timer value to 0
            TMR1L = 0 'Set the low part of the timer value to 0
    @ INT_ENABLE  CMP2_INT
            'Comp1Time =Comp1Time +4 
            'dummy = b * c       '5,000,000 
            'frequency = div32 Comp1time
            'frequency =frequency/2
            'HSEROUT ["mytime = ", dec mytime,9, dec frequency/10,".",dec frequency//10,"Hz",13, 10]
              
    @ INT_RETURN
    
    Comp2_Int:
    @ INT_DISABLE  CMP2_INT
            Comp2Time.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits 
            Comp2Time.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits
            'b = 50000
            'c = 1000
            'phase_shift = div32 Comp1time
            'dummy = b * c       '5,000,000
            'Comp1Time = Comp1Time
            'phase_shift = Comp2Time *10
            'phase_shift = phase_shift/Comp1Time
            'phase_shift = phase_shift*4
            TMR1H = 0 'Set the high part of the timer value to 0
            TMR1L = 0 'Set the low part of the timer value to 0  
    @ INT_ENABLE  CMP1_INT   
    @ INT_RETURN
    I reckon it's all to do with the first comparator interrupt being started when the program initially runs 'at a time unknown' in respect to the incoming signal. Comp1 interrupts are enabled at the top of the porgram - that could be midway through a cycle, then a leading edge from the incoming signal arrives at comparator 1 & the comparator1 handler self disables any further comparator1 interrupts.

    I need a way of starting comparator1 interrupts synced to the actualy incoming signal.

    hey...ho - one problem solved another one (which I thought I'd nailed) starts!


    Many thanks anyway..... I'll get me coat.

Members who have read this thread : 0

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