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


    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.

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

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

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Hi Hank

    The second comp int should not reset the timer to 0. Comp1 int is your reference point and you zero the timer in there. Comp2 int will not come if there is no comp1 signal. This is from your circuit.

    So, if you see my previous postings on the other thread,
    COMP1 int
    read the timer (gets you frequency counts)
    Timer Reset


    COMP2 Int
    read the timer (gets you phase counts)


    These should be the only steps involved. I think you will be successful in this

    Regards

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    BINGO! .....I've got it working at about 1.30am this morning shortly after making that last post

    Jerson - many thanks ....re your post above - you were bang on the money.

    In conclusion to brigng the thread back on topic, for anyone wanting a pointer wrt configuring their PIC to run on an external clock - well, for a 16f690 @20Mhzthese settings work for me....

    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 = %0001000
    (for the above to work, you need to be using MPASM)

    And if you're doing anything with timers/counting clocks, bear in mind what Alain said above ....ie that the number of clocks counted during your own particular timeframe will be the oscillator frequency divided by 4. (which caught me out nicely)


    Many thanks to both Alain & Jerson for taking the time to help me out......you've made a happy man very old (or should that be the other way round?)

    Got to start learning about LUTs & arrays now - there's no end to all this learning!
    Last edited by HankMcSpank; - 14th September 2010 at 10:04.

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Hehehe - I guess you saw the color red in that post

    Congratulations on a job well done.

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Oh dear - I spoke to soon! (5 nights & counting for this seemingly simple task! Eek!)

    by & large the overall process works, however.....

    If I take the phase shift control of my all pass filter circuit down to as near as damnit zero degrees phase shift (ie no lag for the second signal feeding into comparator 2), then there ought to be almost a zero count when the second comparator interrupts, alas, in actual fact there appears to be count of about 325 for comparator2 in this scenario (@1khz). If I take the tst frequency down to 100Hz, I see a comparator2 count of a whopping 3000 (albeit comparator1 reads 50,000 in this scenarion - so it's pro rata)

    this means @1khz, with that error I'm getting a phase shift of about 23 degrees being erroneouly calculated - ie 5000 (1st timer count) divided by 360 (degrees) muyltiplied by 325 (second timer count) - but I know for a fact there's actually no phase shift present at all (I can see this on my scope).

    Now I know I'm relatively new to all this, but I can't help feeling there something going seriously wrong 'behind the scenes' (or a config register setting?).... my reasoning is that there's virtually no code for me to screw up with here!

    here's my interrupt handler...

    Code:
    Comp1_Int:
            Comp1Time.Lowbyte = TMR1L '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      'Store away the timer1 count 
            Comp2Time.Highbyte = TMR1H     'Store away the timer1 count
    @ INT_RETURN
    Anyone got any ideas?!
    Last edited by HankMcSpank; - 15th September 2010 at 00:41.

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