Maximum frequency output from a PIC


Closed Thread
Results 1 to 40 of 69

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    82


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Despite the loss of few last posts this tread still has enough information to support my next hypothetical question:
    As posted before I’m employing a PIC12F683 to generate a modulated 500KHz carrier.
    The output is 15 mS carrier ON followed by 5 mS carrier OFF and it drives a LC resonant circuit to transfer power to a remote sensor. In my efforts to keep the part count and manual adjustments to a minimum I was wondering if a second PIC12F683 on the receiving side will be able to perform as a missing pulse detector and flip a pin high when the carrier is present and drive it low when the carrier is OFF?
    I am aware that some jitter will be present but for my intended purposes even 100 uS will be fine.
    How will one go about getting something like that to work?
    I’m thinking that if an input is set for IOC and the interrupt is cleared as soon as possible. This indicates that the carrier is ON.
    If no more interrupts that means carrier is OFF.
    I never used interrupts so any input if this is a good approach will be appreciated. Some possible links to something like this will be more than helpful.

    Regards,

    Nick

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,613


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi,
    It can probably be done with a PIC, not sure it's the best way though....

    What does the signal actually look like at the receiving end? I imagine it's not a nice squarewave at logic levels so, depending on how you do this, you probably need to do some signal conditioning before feeding it further. Because of this I'm not sure a PIC or other logic device is the best tool for the job. Can't you just use a lowpass filter with a suitable time constant followed by a simple transistor buffer?

    Or, if the signal IS conditioned to logic levels, perhaps use a retriggerable flip-flop with something like 2.5us time constant. The 500kHz carrier will then keep triggering the flip-flop, as soon as the carrier goes away the flip-flop output will fall withing 2.5us. If the signal isn't conditioned to logic levels perhaps using a 555 timer as the flip-flop could do it but again, it all depends.....

    If you insist on using a PIC for the job I'm sure we can figure something out though.

    /Henrik.

  3. #3
    Join Date
    Oct 2012
    Posts
    82


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    It is funny but I used both your suggestions in my younger days. I also used a frequency to voltage convertor (LM2917 if my memory serves me right). Each has its limitations and advantages.
    Since I started using PICs I pledge to myself that I will replace any of the analog or logical components that can be replaced especially when it comes to timing circuits. In this case it is so easy to change one line in the code to generate a different frequency or duty cycle with much better accuracy than analog circuits. I also pledge to myself to learn as many special functions of the PICs as I possibly can. Like I said before I’m a slow learner but nobody bits me when it comes to forgetting things (LOL).
    I am aware that some signal conditioning will be necessary but that is my last of the worries. I recently discovered those single gate chips in small packages and I’m sure that a couple resistor and capacitors combined with a trigger smith will do fine.
    At least knowing now that it can be done using a regular PIC I will research this avenue and keep you posted.

    Thank you again for the valuable input.

    Regards,

    Nick

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,613


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Ah, ok, it's the I've got a hammer now everything looks like a nail kind of thing ;-)

    Well, as with most things there are more than one way to skin a cat. What I'd do is probably something like this (for 12F683 but not tested):
    Code:
    ' CMCON, ANSEL, TRIS, whatever not shown here!
    
    oldCount VAR BYTE
    LED VAR GPIO.0
    
    oldCount = 0    'Clear count
    LED = 0    ' Turn off LED
    
    ' Set TMR0 up as a counter, signal fed into T0CKI pin
    OPTION_REG.5 = 1
    
    Main:
       If TMR0 <> oldCnt THEN    ' There has been pulses since last time we checked
           LED = 1
       ELSE
           LED = 0
       ENDIF
    
       oldCnt = TMR0      ' Store current count
    
    ' Now wait a bit to allow any new pulses to "tickle" the counter.
    ' Shortest PauseUs time @8Mhz is 12us.
    ' If faster response is needed replace with a copule of @NOP (each taking 500ns @8MHz) or try without any delay at all. 
    
       PauseUs 25  ' Or go do something else usefull instead....
    
    Goto Main

    Anyway, that's one idea....

    /Henrik.

  5. #5
    Join Date
    Oct 2012
    Posts
    82


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    Thank you for the sample code.
    I will do some “hammering” tonight and report the results.

    Regards,

    Nick

  6. #6
    Join Date
    Oct 2012
    Posts
    82


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Results after the “hammering” session today.

    Testing setup as follows:

    One PIC12F683 acting as Tx module. It output bursts of 15 mS 500Khz carrier on CCP1 pin (pin 5) followed by 5mS no carrier. Pin 2 of the PIC is high when carrier ON and low when carrier is OFF.
    Second PIC12F683 acting as Rx is connected to the same 5V supply and has its TOCKI pin (pin 5) connected to CCP1 pin of the Tx PIC. Pin 2 of this PIC drives a LED.

    After adding some configuration lines and fix some oldCount speling I ran few simple tests to make sure the connections and configs are OK I ran the sample code.
    Unfortunately it did not work and the problem was on the Rx side. Monitoring the signals from Tx they were what they should be. Rx will output some very random signals or no signals at all.

    So I started messing with the code and try few alterations of the Rx program. I realized that some different handling of TMR0 and oldCount needs to be implemented. I have to confess that some of the results did not make sense to me at the time but by pure luck (I left a code line behind in the first WHILE / WEND loop got some meaningful results).
    I do not know enough about the insides of the WHILE / WEND loop but, after violating its condition. I also think I broke few other rules of programming and according to the well known rule if something works there must be at least two mistakes that compensate each other.
    The Rx and Tx codes that follow are working and my old analog scope shows perfectly matched signals on the Tx and Rx PICs.
    Tx code:
    Code:
    @ device  pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
    DEFINE OSC 8           ' We're running at 8 Mhz
    
    OSCCON=%01110111        ' 8MHz internal osc
    ANSEL   = 0             ' Digital only for all PortA pins
    TRISIO=%00100000        ' Make PORTA outputs
    out     var         GPIO.5
    CCP1CON = %00001100     ' Normal PWM 
    PR2 = 3             ' 1MHz output @8MHz system clock
    CCPR1L = 2            ' 50% Dutycycle
    CCP1CON.5 = 0
    CCP1CON.4 =0
        T2CON.2 = 1 
    Main:
        high out
        T2CON.2=1         ' Osc ON
        PAUSE 15
        low out
        T2CON.2=0         ' Osc OFF
        PAUSE 5
    Goto Main
    Rx code:
    Code:
    @ device  pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
    DEFINE OSC 8           ' We're running at 8 Mhz
    
    OSCCON=%01110111        ' 8MHz internal osc
    ANSEL   = 0             ' Digital only for all pins
    TRISIO=%00100100        ' Make PORTA outputs
    
    oldCount    VAR     byte
    LED         VAR     GPIO.5
    
        oldCount = 0    'Clear count
        LED = 0    ' Turn off LED
    
    ' Set TMR0 up as a counter, signal fed into T0CKI pin
    
        OPTION_REG.5 = 1
    
    Main:
        While TMR0 <> oldCount  ' There has been pulses since last time we checked
        high led
        oldCount = TMR0      ' Store current count
        Wend
        low led
        oldCount =0 
        TMR0=0     
        While oldCount=TMR0
        low led
        wend
        GOTO main
    Can somebody explain me why this code is working?

    Regards,

    Nick

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,613


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Sorry about the typo, told you I didn't test it though.

    Currently I have no idea why my original code (less the typo) did not work while your new one does. I just tried it here, again with two 12F1840's so that MIGHT have something to do with it but I don't think so. I also figured out that once you've enabled PWM mode on the CCP pin you can't control that output manually. So in order to force the PWM output low during the OFF period of the carrier you have to disable the CCP module and THEN force it low, I've implemented that in the TX-code below:
    Code:
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    ' ***************************************************************
    ' Compiler directives
    ' ***************************************************************
    DEFINE OSC 8               ; We're running at 8Mhz
    
    ' ***************************************************************
    ' Variables and aliases
    ' ***************************************************************
    i VAR BYTE
    LED VAR PortA.4
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    OSCCON   = %01110000        ' 8MHz internal osc
    ANSELA   = 0                ' Digital only for all PortA pins
    TRISA    = %00000000        ' Make PORTA outputs
    
    CCP1CON = %00001100     ' Normal PWM 
    PR2 = 3                 ' 500kHz output @8MHz system clock
    CCPR1L = 2              ' 50% Dutycycle
    CCP1CON.5 = 0
    CCP1CON.4 = 0
    
    ' ***************************************************************
    ' Actual program
    ' ***************************************************************
    
    ' Show that we're up and running.
    For i = 0 to 9
        Toggle LED
        Pause 200
    NEXT
    
    Main:
        CCP1CON = %00001100     ' Normal PWM mode
        T2CON.2 = 1             ' Timebase ON
        PAUSE 15
    
        T2CON.2 = 0             ' Timebase OFF
        CCP1CON = %00000000     ' Disable PWM to regain control of PortA.2
        PortA.2 = 0             ' Force output low to prevent saturation of coil
        PAUSE 5
    
    Goto Main
    For the RX-side I did this, which is pretty much exactly like I wrote it earlier. I found that I could ditch the delay to speed up the response time since the exection time for the loop is longer than the PWM-period. If you change the frequency you may need to add some delay.
    Code:
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    ' ***************************************************************
    ' Compiler directives
    ' ***************************************************************
    DEFINE OSC 8               ; We're running at 8Mhz
    
    ' ***************************************************************
    ' Variables and aliases
    ' ***************************************************************
    oldCnt VAR BYTE
    i VAR BYTE
    LED VAR PortA.4
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    OSCCON   = %01110000        ' 8MHz internal osc
    ANSELA   = 0                ' Digital only for all PortA pins
    TRISA    = %00000100        ' T0CKI is input, rest outputs
    
    ' ***************************************************************
    ' Actual program
    ' ***************************************************************
    
    OPTION_REG.5 = 1            ' TMR0 as counter
    oldCnt = 0
    TMR0 = 0
    
    ' Show the world that we're alive
    For i = 0 to 9
        Toggle LED
        PAUSE 200
    NEXT
    
    Main:
        IF TMR0 <> oldCnt THEN
            LED = 1
        ELSE
            LED = 0
        ENDIF
        
        oldCnt = TMR0
       
    Goto Main
    I'm going to try to add two screen-grabs from the scope. Last time I did the uploader didn't work but hopefully that's fixed by now. Nope the bloody thing still doesn't work, sorry but you'll have to trust me that it does work here.

    /Henrik.

Similar Threads

  1. internal TMR for frequency output
    By Samoele in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th January 2009, 09:38
  2. How to calculate PIC sampling frequency
    By minmin in forum General
    Replies: 1
    Last Post: - 26th September 2006, 18:02
  3. Replies: 2
    Last Post: - 20th January 2006, 21:09
  4. Maximum frequency count on 16F628/4MHz
    By solara in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 23rd May 2005, 10:38
  5. Low frequency output
    By barkerben in forum General
    Replies: 5
    Last Post: - 16th November 2004, 15:25

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