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


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1
    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 " !!!
    *****************************************

  2. #2
    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

  3. #3
    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 " !!!
    *****************************************

  4. #4
    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)

  5. #5
    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.

  6. #6
    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
    the number I'm seeing is 4500 (not 2500). Here's what I'm seeing withrt comparator 'counts' when feeding a 1Khz signal in...


    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.

    .
    Ok Hank ...

    4500 looks fine ... and you won't get much better using such discrete interrupt.
    remember ??? :
    let's forget Interrupt duration for the moment
    Aaaarghhhh ... time to care of it ...

    PBP interrupt save and restore all the system vars ... and it takes TIME.
    So, you have to compensate ...

    or use TMR1 in " rolling mode " ...

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

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Ok, I've now stripped both comparator interrupt handlers right back to the minimum - there's something very peculiar going on - which I don't just don't understand.

    First here's a scope trace of what I'm feeding into the comparators...

    green trace = comp1 input 1khz signal (the leading signal)
    yellow trace = comp2 input 1khz signal (the lagging signal)



    ok, when I enable just comp1 interrupts on their own,I get as near as damnit a count of 5,000 between comparator 1 interrupts (ie a perfect counting of clocks @1Khz).....
    Code:
    comp1=5002      comp2Time=0   
    comp1=5000      comp2Time=0    
    comp1=5001      comp2Time=0    
    comp1=5002      comp2Time=0    
    comp1=5002      comp2Time=0    
    comp1=5001      comp2Time=0    
    Here's the associated code snippets ...

    Code:
    @ INT_ENABLE  CMP1_INT     ; enable Comparator 1 interrupts
    '@ INT_ENABLE  CMP2_INT
    
    Comp1_Int:
            Comp1Time.Lowbyte = TMR1L +2   'Store away the timer1 count. 
            Comp1Time.Highbyte = TMR1H     'Store away the timer1 count.
            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_RETURN
    
    
    Comp2_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
            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_RETURN
    *BUT*

    when I then enable comp2 interrupts (by uncommenting the enable comp2 interrupt line above - also commenting out the TMR1H = 0 & TMRLl = 0 in the comp1 interrupt, so that the timer now resets in the comp2 handler), it all goes wonky....

    Code:
    comp1=2959      comp2Time=4999 
    comp1=2956      comp2Time=4999 
    comp1=2956      comp2Time=4993 
    comp1=2959      comp2Time=4999 
    comp1=2954      comp2Time=4994 
    comp1=2955      comp2Time=4995 
    comp1=2958      comp2Time=4994 
    comp1=2956      comp2Time=4996 
    So comp1 count should be stored...the timer contunes on

    I can't really account for what's going on here - there's almost nothing inthose interrupt handlers. equally there's plenty of time between the two signals for both handlers to easily finsih what they need to do before the next comparator interrupt arrives.

    Perplexed
    Last edited by HankMcSpank; - 13th September 2010 at 22:55.

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