Measuring audio phase shift through a circuit with a PIC


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cncmachineguy View Post
    I like Pauls approach, Seems to be the least amount of time between Comp1 and 2. one thing I would do, is program his while loop in ASM. This is because I have no idea how many instructions it takes using PBP. I think this will mask the issue of how long it takes to actually enter the handler. Which is what I think above folks are reffering to with the PBP include.

    heres the while loop in asm:
    Code:
         BCF PIR2,6       --> clears the comp 2 int flag
    HERE                    --> label to jump to
         BTFSS PIR2,6  --> test if comp 2 int flag is set
         GOTO HERE     --> not set, go back to test
    CONTINUE            --> Comp 2 int occured, do what ever needs to be done
    The above will take exactly 1 instruction to enter the loop, then 3 inscrutions to to run per loop. now for some math:

    you are running a 20mHz clock, so thats 5MIPS. so 1 instruction takes .0000002 sec to execute. At 1400 htz, each cycle takes .0071428 sec. that devided by 360 = .00000198sec per degree. so you should be able to measure to almost .5 degree. assuming comp2 fires between BCF and BTFSS, thats 3 instrution cycles, or .0000006 sec.(I think BTFSS may take 2 cycles when true)

    Someone please check my math, but these numbers show you have far too much overhead somewhere that can only be explained by the above posts.
    I agree with the maths...and I agree with the summary (something somewhere is taking way too long). Now that I have a few people interested & feeding in ideas , I will pull the breadboard out of cold storage tonight & try Paul's code.


    Bert, re this...

    Code:
         BCF PIR2,6       --> clears the comp 2 int flag
    HERE                    --> label to jump to
         BTFSS PIR2,6  --> test if comp 2 int flag is set
         GOTO HERE     --> not set, go back to test
    CONTINUE            --> Comp 2 int occured, do what ever needs to be done
    That looks like what will happen when comp2 interrupts....but within my program, how do I setup the 'trigger' condition ...ie to get the program to go and do that when a comp2 interrupt has arrived?

    I will feedback the results (it might be late - ie 21.30 GMT-ish .....got to do the 'kids off to bed' rigamrole first! Hey there's an idea...rather than read them Noddy goes to the Seaside, I could read them some of the ASM code put forward by Bert!! )
    Last edited by HankMcSpank; - 29th September 2010 at 13:56.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    Hey there's an idea...rather than read them Noddy goes to the Seaside, I could read them some of the ASM code put forward by Bert!! )
    LMAO, I always thought datasheets were better for this.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    .got to do the 'kids off to bed' rigamrole first! Hey there's an idea...rather than read them Noddy goes to the Seaside, I could read them some of the ASM code put forward by Bert!! )
    My wife, back when she was studying to be an EMT, would recite her "protocols" (sort of flow charts for emergency medical actions) to get my daughter to go to sleep.

    Anyway, I've never used the DT interrupts. I've heard great things about them but never had cause to use them. I would do this by setting the registers for a single interrupt source, i.e. comparator 1. And I would use PBP interrupts (because I am a simple man...) at least at the offset. Once I had that working I would consider something more complicated (ASM interrupts) if the speed wasn't fast enough.

    Perhaps in the spirit of keeping it simple, don't use *any* interrupt routine. In the main loop, just keep checking the the state of the Comp1 interrupt flag, something like

    Code:
    PIR2.5 = 0    'Clear the Comp1 interrupt flag
    Main_Loop:
        WHILE PIR2.5 = 0
            GOTO Main_Loop
        WEND
            Comp1Time.Lowbyte = TMR1L  'Store away the timer1 count - this  will be the non lagged signal 'period' count.
            Comp1Time.Highbyte = TMR1H    
            TMR1H = 0                     'Set timer1 value to 0
            TMR1L = 0
            PIR2.6 = 0                     'Clear the Comparator C2 Interrupt Flag bit
    Keep_Waiting:
            WHILE  PIR2.6 = 0         'Check to see if the Comparator 2 interrupt flag has flipped
                goto Keep_Wating
            WEND
            Comp2Time.Lowbyte = TMR1L    'Store away the timer1 count again (ie once the lagged signal leading edge arrives - this is the lag count)
            Comp2Time.Highbyte = TMR1H    
            HSEROUT ["comp1=",dec Comp1Time,9,"comp2Time=", dec comp2time, 13, 10]
            PIR2.5 = 0    'Clear the Comp1 interrupt flag
            goto Main_Loop
    END
    Best Regards,
    Paul

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


    Did you find this post helpful? Yes | No

    Default

    Ok, just to show you what is involved in ASM interrupts, this is an example for your particular case. This is the first time I am trying something like this using DT_ints , so, I may be off the mark. Most times I do it the hard way - pure asm code. Someone please correct me if I am wrong here. You should be able to realize significant improvement in the values you see. Darrell's wrapper code is cool and if you still need to save some more, look at the generated LST file and you should be able to sneak a trick or two from there. The ISR vector is at location 0004 in the PIC. So, you will find a goto INTHAND at that address.

    In your case since you are not doing any math functions or gosubs in the INTs, you could keep the PBP code as-is and do away with the ASM. Can save you some agony


    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.
    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- (comparator1 input)
    TRISC.2 = 1     '  14  RC2      C12IN2- (comparator2 input)
    TRISA.2 = 0     '  17  RA2      C1OUT
                    '  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' ext 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' ext 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)
    
    ' Setup the internal VREF
    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.
    Comp2Time       var  word
    
    ;-- Place a copy of these variables in your Main program -------------------
    ;--   The compiler will tell you which lines to un-comment                --
    ;--   Do Not un-comment these lines                                       --
    ;---------------------------------------------------------------------------
    wsave   VAR BYTE    $20     SYSTEM      ' location for W if in bank0
    ;wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
                                             ' if using $70, comment wsave1-3
    
    ' --- IF any of these three lines cause an error ?? ------------------------
    '       Comment them out to fix the problem ----
    ' -- Which variables are needed, depends on the Chip you are using -- 
    wsave1  VAR BYTE    $A0     SYSTEM      ' location for W if in bank1
    wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
    ;wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3
    ' --------------------------------------------------------------------------
    
    
    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   CMP_INT,  _Comp1_Int,   ASM, yes
            INT_Handler   CMP2_INT,  _Comp2_Int,   ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    T1CON.0= 1  'start timer
    
    Comp1Time = 0 'clear down Comp1Time, prior to starting.
    comp2Time = 0  'clear down Comp2Time, prior to starting
    
    @ INT_ENABLE  CMP1_INT     ; enable Comparator 1 interrupts
    @ INT_ENABLE  CMP2_INT     ; enable Comparator 2 interrupts
    
    'Main body of Code****************************************************************
    Main:
            HSEROUT ["comp1=",dec Comp1Time,9,"comp2Time=", dec comp2time, 13, 10]
            PAUSE 80
            goto Main
            end       
    
    'Comparator1 Interrupt Handler++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    Comp1_Int:
    '        Comp1Time.Lowbyte = TMR1L       'Store timer1 Low into a variable  
    '        Comp1Time.Highbyte = TMR1H      'Store timer1 high into a variable.
    '        TMR1H = 0                                   'Set the high part of the timer value to 0
    '        TMR1L = 0                                    'Set the low part of the timer value to 0
    asm
        movf  TMR1L,w
        movwf _Comp1Time+0    ; the lsb
        movf  TMR1H,w
        movwf _Comp1Time+1    ; the msb
        clrw
        movwf TMR1L
        movwf TMR1H
    endasm
    
    @ INT_RETURN
    
    
    'Comparator2 Interrupt Handler++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    Comp2_Int:
    '        Comp2Time.Lowbyte = TMR1L       'Store timer1 Low into a variable 
    '        Comp2Time.Highbyte = TMR1H      'Store timer1 high into a variable.
    asm
        movf  TMR1L,w
        movwf _Comp2Time+0    ; the lsb
        movf  TMR1H,w
        movwf _Comp2Time+1    ; the msb
    endasm
            
    @ INT_RETURN
    Last edited by Jerson; - 29th September 2010 at 14:38. Reason: :)

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Hi Jerson,

    So am I getting this right - you are using DT's interrupt as the 'trigger' to enter an actual routine that has ASM within?

    Kind of a like hybrid solution?

    Apologies for the naive line of questioning, but what's the pros cons of the (3?) type of interrupts available to user of Picbasic....

    PICbasic Interrupts
    DT's Interrupts,
    ASM Interrupts.

    hi Paul,

    i'm digging that idea about keeping it simple - once we've got to the bottom of the 'overhead', I can think about putting the solution back as an interrupt.

    Great stuff guys - I've got a new spring in my step with all this.
    Last edited by HankMcSpank; - 29th September 2010 at 14:41.

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


    Did you find this post helpful? Yes | No

    Default

    Apologies for the naive line of questioning, but what's the pros cons of the (3?) type of interrupts available to user of Picbasic....

    PICbasic Interrupts
    DT's Interrupts,
    ASM Interrupts.
    No need for apologies. We all walked your way once upon a time.....

    PBP interrupts - the worst kind - you use them when you cannot think of anything else.
    It is as cruel as - execute one PBP statement, look for an interrupt, and repeat
    So, the real time nature of interrupts is lost.

    DT's interrupts - a superb wrapper to ease programmer pain
    Just use it and get your job done without having to bother with registers and internals connected with the particular interrupt you need.

    ASM interrupts - for those who like to drive Ferrari
    You know to drive it, don't look back. It is the fastest one there is!!! But you need to know how to ....

    Your best bet, DT_ints is your friend

    Cheers

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Thanks for the clarification...

    Quote Originally Posted by Jerson View Post
    No need for apologies. We all walked your way once upon a time.....

    Your best bet, DT_ints is your friend
    Except it seem most are now telling me to use ASM

    Does this mean I'm being unfaithful?!!

    Darrel - it's not you, i promise ...it's me.
    Last edited by HankMcSpank; - 29th September 2010 at 15:16.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    These are some really good ideas comming forth. I can try Paul's idea, of one interupt
    and poll the the second in the ISR. I am already polling the CMCON.6 and 7. This should
    cut the processing time by a good bit.

    Something occured to me, since you are using schmitt triggers on the inputs, you also don't need to use the comparator interupts. You might find the CCP modules could do the same thing in a more effective way? Or change on portb. This also allows you to use other pics that don't have the comparators.

    I never put togeather my phase shifter. Need to get back on it.

    Interesting project!

  9. #9
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi Jerson,

    Your right, of course, in everything you said. However there is a place for PBP interrupts. First, for beginners (and even people who, like me, use the absolute minimum of assembly) it is simple, straightforward, and easy.

    Second, that high overhead isn't a big factor if used judiciously. For example, here's the entire code for the main loop of one of my products:
    Code:
    On Interrupt goto IntHandler
    INTCON.1 = 0 	'clear the INT0 flag
    PIR1.5 =  0		'clear the USART interrupt
    enable
    '#############
    ' Start of Main Loop
    '#############
    Start:
    	if USB_ON = 1 then
    	  	bConnected = true   
    	else  'USB_ON = 0
    	  	bConnected = FALSE
    	endif	
    
    	if bConnected = false and bStayInFastMode = false then
    		OSCCON =  %10000000		'32kHz, periperals should run in sleep mode
    	endif
    	@ nop
    	@ sleep                                           'snooze soundly until woken
    goto Start
    '############
    ' End of Main Loop
    '############
    disable
    The main loop checks to see if USB (through an FTDI serial converter) is connected, and if so it stays awake. Otherwise it goes to sleep until the interrupt wakes it (which in this case happens to be either a pulse from an external RTC or data in the serial buffer). The other ~1000+ lines of code are after the "disable" so the PBP interrupt overhead is not in them. I'm sure there are other ways to accomplish the same end, but the above works perfectly. I probably could have moved the ENABLE to just before the "@ nop" and saved another few bytes even.

    Incidentally, @ nop and @ sleep are the full extent of my assembly knowledge...8^)

    With all that said, I really end up not using interrupts very much at all (except to wake a sleeping PIC). What I do is look at the interrupt flags when convenient for the code. This keeps it nice and simple and controlled. Like I said before, I am a simple man.

    Again, I'm not disagreeing or disputing anything you said, just trying to carve out some justification for PBP ints for us simpler folk. It just has to work, it doesn't (always) have to be pretty. Ferraris are nice I suppose, but I hear they're high-maintenance and sort of temperamental. I have a real good time kicking around on my little scooter; it certainly gets me there. If someone told me I had to use ASM ints I would have given up before I started.

    I really need to look into these DT Ints that everybody keeps talking about...

    Best Regards,
    Paul

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by prstein View Post
    Hi Jerson,

    there is a place for PBP interrupts. First, for beginners (and even people who, like me, use the absolute minimum of assembly) it is simple, straightforward, and easy.

    [/CODE]

    The main loop checks to see if USB (through an FTDI serial converter) is connected,

    Incidentally, @ nop and @ sleep are the full extent of my assembly knowledge...8^)

    I really end up not using interrupts very much at all (except to wake a sleeping PIC). What I do is look at the interrupt flags when convenient for the code. This keeps it nice and simple and controlled. Like I said before, I am a simple man.

    I have a real good time kicking around on my little scooter; it certainly gets me there. If someone told me I had to use ASM ints I would have given up before I started.
    Hey Paul

    Sorry if I stepped on some toes here. In fact, I was seriously thinking of editiing my post, but, history has had me earlier too So, this is one boo-boo from me to stay here for posterity to see

    Ok, I understand the use of ON INTERRUPT as you use it. My point was about driving home the real-time performance hit and how high the latencies can be. Waking a PIC from sleep? LoL You must be doing much more than that for sure.

    I do not want my post to sound discouraging. Obviously, that will be the first thing a newbie will try when they want interrupts (because.... it is in the Manual)

    I do not have a ferrari either and hope I could buy something better than my old faithful scooter which I have just put up for sale )


    Just to make it clear, I learn new things out here almost everyday. Some of the folks who call themselves newbies and amateurs really astonish me with their coding style. If they only start giving back the little they know, I'm sure they will learn a lot more.

    But, seriously, if you want good realtime performance out of a PIC, DT_ints is the way to go. One more plug for DT_ints

    Cheers

  11. #11
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi Jerson,

    None of my toes were stepped upon; I appreciate that you presented the the opportunity to discuss my own take on interrupts and regret if I came off as defensive. Mr. McSpank appears to be quite sharp but I worry a little about making the learning curve too steep.

    I truly appreciate reading your posts--please don't hold back because of me!

    Your scooter looks nice but the shipping charges would be prohibitive...and my Kymco would be jealous.

    Best Regards,
    Paul

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