Maximum frequency output from a PIC


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 69
  1. #1
    Join Date
    Oct 2012
    Posts
    81

    Default Maximum frequency output from a PIC

    Not too long ago there was a tread in this forum that wasdealing with maximum frequency output from a PIC for any given master clock. Unfortunatelymy searches did not return any good information.

    I’m interested to get a small PIC device (10F or 12F type)to act as a high frequency clock generator using its internal clock and I wouldlike to know what to expect.
    I have an application where the space is very tight and Ineed a modulated clock source with decent stability in the order of hundreds ofkHz but ideal will be 1MHz.

    Using PBP I understand that something like:

    main_loop:
    Togglepin_x
    GOTOmain_loop

    should give maximum frequency output. Assuming that theToggle and GOTO commands will take two cycles each that will give me possibleoutput frequency of Main_clock/4/4 which will be 250 kHz for a 4MHz clock.
    My question is if one uses Assembly (which is not my strongestfield) will it get any faster? Will itbe possible to make it run in the background?
    Ideally I need a 1 MHz output that will be ON for 15 mS andOFF for 5 mS.

    Thank you in advance for any input in this matter.


  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Nick, Here ia a program I wrote a few years ago to take a 10F222 and use it as a modulator for some serial data I was pushing to the surface about 200 feet below the water surface. It worked quite well. As you can see it is done in assembly wrapped inside PBP. I hope this will give you a starting point.

    ' SURFCOMM1.BAS
    ' WRITTEN FOR 10F222
    ' PROGRAM TO SEND:
    'STATUS DATA READING,TEMPERATURE REDAING,SERVO POSITION READING and
    'DEPTH SENSOR READING TO SURFACE @ 1 SECOND INTERVALS. TRANSMIT DATA
    '@ 3000 BAUD MODULATED @ 125Khz.
    '
    ' WRITTEN BY DAVID PUROLA 07/13/2006
    '
    ' INCORPORATED INTO 216229 REV.D "NEW TUBSUB" (12/28/2006)
    '
    DEFINE OSC 8
    '
    ' ************************************************** ******************
    ' Define Port Variables
    ' ************************************************** ******************
    DATA_OUT VAR GPIO.0 '0-MODULATED DATA TO HEAD UNIT
    TXENAB VAR GPIO.1 '1-TRANSMIT ENABLE INPUT
    DATA_IN VAR GPIO.2 '1-INPUT DATA FROM SOURCE
    SPARE3 VAR GPIO.3 '1-

    ' ************************************************** ******************
    ' Declare Variables
    ' ************************************************** ******************
    ' DO NOT CHANGE THESE LINES
    ' ************************************************** ******************
    TRISIO = %11111110 'SET PORT DIRECTION REGISTER
    OPTION_REG = %11000000 'DISABLE PULL-UPS,PSA to TMR0,PSA 1:2
    ADCON0 = %00000000 ' LEFT JUSTIFIED,REF = VDD,Channel 0,A/D Off

    '************************************************* ********************
    LOOP: 'MAIN LOOP FOR PROGRAM
    '************************************************* ********************
    ASM
    BCF GPIO,0 ;DISABLE TRANSMITTER OUTPUT INITIALLY
    NXTPLS BTFSS GPIO,1 ;TEST DATA ENABLE INPUT PIN BIT STATE
    GOTO NXTPLS ;DON'T WAIT FOR PERIOD TO EXPIRE BEFORE TESTING ENABLE BIT
    BTFSC GPIO,2 ;TEST DATA INPUT PIN BIT STATE
    GOTO NXTPLS ;DON'T WAIT FOR PERIOD TO EXPIRE BEFORE TESTING BIT
    BSF GPIO,0 ;IF NOT SET THEN MODULATE OUTPUT (ENABLE OUTPUT PIN)
    NOP ;BIT ON TIME
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    BCF GPIO,0 ;(DISABLE OUTPUT PIN)
    NOP
    GOTO NXTPLS
    ENDASM
    GOTO LOOP
    STOP
    Dave Purola,
    N8NTA
    EN82fn

  3. #3
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Dave,

    Thank you for your sample code.
    I do expect some PIC10F222 any day now and I will start testing with.
    Just to make things easier for me understanding your code what is the output of this piece of code?

    Regards,

    Nick

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Dave, that was a cool project. Did you use a 'depth sounder transducer' to modulate ? They are generally used at 52 and 125KHZ. How far in water do you suppose the signal can travle at that freq ?
    don

  5. #5
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    While waiting for my 10F PICs I had a little time today and did some experiments with a PIC12F683.
    Using the internal oscillator set for 4MHz and a close loop with Toggle GPIO.x I was getting in the neighborhood of 70 kHz output and 143 kHz for 8 MHz clock.
    Interestingly enough with simple High / Low GPIO.x loop I got about 200 kHz.

    Then I realized that one of the clock options is to use internal clock with clock/4 on clock out pin. This method got me the 1 MHz output for the 4 MHz internal clock and 2 MHz for the 8 MHz internal clock.

    The oscilloscope shows a pretty steady output and my frequency counter gives about 0.15% error.

    If this will prove to be my path I will use a one gate chip to buffer and modulate the output.

    I still wish I will be able to solve it with only one PIC10F and I will try my best to find a solution.

    Nick

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi,
    The toggle command first makes the pin an output, then it reads the state of the pin, then it inverts the state, then it writes the new state to the pin - takes time.
    High/Low first makes the pin an outout then sets the pins state - takes a little less time.
    You'll get more speed of you write to the pin directly
    Code:
    TRISO.0 = 0  'Make pin an output
    DoIt:
    GPIO.0 = 1
    GPIO.0 = 0
    Goto DoIt
    If you can handle some jitter / discontinuity in the output then something likw this will give you even higher average frequency with the drawback that every 6th pulse will be "missing" due to the GOTO.
    Code:
    DoIt:
    GPIO.0 = 1
    GPIO.0 = 0
    GPIO.0 = 1
    GPIO.0 = 0
    GPIO.0 = 1
    GPIO.0 = 0
    GPIO.0 = 1
    GPIO.0 = 0
    GPIO.0 = 1
    GPIO.0 = 0
    Goto DoIt
    Another aproach would be to use the CCP module, though I'm not sure if the 10F series have that built in.

    Of course if all you need is stable 1MHz (or whatever that can be derived from the oscillator) signal then use clock-out option of the chips oscillator.

    /Henrik.

  7. #7
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Amgen, The project was a cannon ball replacement for COHO fishing. The sub would track the temperature gradients and depth then control the lure depth as it was connected to the rear of the sub. The data link was the stainless tow wire going up to the boat. The remote readout would display the depth, temperature as well as plot the depth during the tow on an VFD dot matrix display.
    Dave Purola,
    N8NTA
    EN82fn

  8. #8
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Thank you Henrik for your suggestions.
    I will try the tonight and report the findings.
    If I must I will stay with 12F series which might give me the corect output.
    The goal is to get 1MHz with output with a 15 mS ON and 5 mS OFF from one single chip.

    Regards,

    Nick

  9. #9
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    One other choice will be to have the PIC12f output (the 1MHz) controlled ON and OFF by one of its input pins kind like Dave did in his application. This will give me more flexibility with the rest of the concept which is not completely defined yet.
    Once I have a clearer picture I will present it to the forum.

    Nick

  10. #10
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    If anyone is interested here are the results of the speed test using the two methods suggested by Henrik:
    • Writing directly to the output pin at 8MHz internal clock I got a little over 500 kHz but the duty cycle is 25% or 75% depending on the order you write to the pin 0 or 1.
    • Using x consecutive writes to the pin (but with a gap at the end of the sequence) increases the burst frequency to about 1 MHz with a duty cycle of 50%. BTW I used 10 cycles.

    So far I learned a lot in the process.

    Henrik, based on your prior experience with CCP module I have few questions that you might be able to answer:
    1. What will be the maximum frequency out that can be achieved from a given master clock?
    2. Can the output be turned ON and OFF at some predetermined intervals (let's say 15 mS ON, 5 mS OFF)?
    3. What other requirements are there for the PIC (other the CCP module) to be able to get that?
    4. Will PIC12F683 do the job?

    I will start searching the forum for this kind of applications and I’m sure I might be able to find something to get me started.

    Thank you to all the members of this forum that are making PIC programming accessible to more and more hobbyists.

    Who said you can’t teach old dogs new tricks?:

    Regards,

    Nick

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Henrik, based on your prior experience with CCP module I have few questions that you might be able to answer:
    1. What will be the maximum frequency out that can be achieved from a given master clock?
    As per the datasheet, PWMPeriod = (PR2+1) * 4 * Tosc so with an 8MHz oscillator and PR2=0 the maximum PWM frequency would be 2MHz (1/(1*4*0.000000125)
    2. Can the output be turned ON and OFF at some predetermined intervals (let's say 15 mS ON, 5 mS OFF)?
    Yes, that's what the PWM module does, however the number of available dutycycle ratios depends on the selected output frequency. At the maximum output frequency you have 2bits of resoultion, ie the dutycycle can be 0%, 33%, 66% or 100%.

    You keep saying that you want 1MHz output frequency with 15ms on-time and 5ms off-time. That's a period of 20ms which equals a frequency of 50Hz - not 1MHz. If you mean 15us on-time and 5us off-time it'll be 50kHz, still not 1MHz.
    3. What other requirements are there for the PIC (other the CCP module) to be able to get that?
    No, not really.
    4. Will PIC12F683 do the job?
    I think it might, I'll have to look into it a bit further though.

    /Henrik.

  12. #12
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Henrick, I think what NickMu means is, you have a carrier frequency of 1 Mhz. being generated by an oscillator and you modulate it with a gated signal of 15 Ms. on and 5Ms. off. What looks like OOK modulation. NickMu, am I right? Is this what you are looking for?
    Dave Purola,
    N8NTA
    EN82fn

  13. #13
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    NickMu, If this is what you are looking for then maybe you should look at using an 12F1840. It has a DATA SIGNAL MODULATOR module that is quite usefull for generating waveform like this.
    Dave Purola,
    N8NTA
    EN82fn

  14. #14
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    Thank you for your help and my apologies for not making myself clear.
    I am in need of a modulated high frequency source. The main frequency (carrier) should be between 250 kHz and 4 MHz pending some results of testing in progress. I will start with 1 MHz and move up or down on carrier frequency.
    I need to be able to turn this carrier ON and OFF in bursts of 15 mS ON and 5 mS OFF.

    My project is still in its infant stages, so right now I’m at the point of poking around to see what can be done with a minimum amount of components.

    The larger project this is part of is a non-contact power and data transfer. This week and next week I will be doing a lot of testing on the power transfer part to find an optimal frequency that will make it easier to do both with as little loss as possible.

    My first power transfer test is encouraging (I can transfer about 1.5 W which is more than I need) with about 66% efficiency. For this I’m using a Mosfet driven by a variable gated oscillator. It serves the purpose for testing but in the final design I would like to have something much simpler especially at the slave unit side where I have a lot of limitations space being one of them.

    My research also shows that for power transfer lower frequencies (100 – 400 kHz) are better. For data transfer I would prefer to use higher frequencies since I already tested a wired 4 MHz power and data transfer. Also I understand that higher carrier frequency will allow higher data transfer rate. I must have 9600 but 19200 will be better.

    This week I will do some research on employing the CCP module to do most of this part.
    I will also study Dave’s example which seems to be doing almost what I need only at lower frequencies.

    Dave, you are right and thanks for the suggestion, I will look into it. I am familiar with PIC12F683 but if I must learn new tricks I will.

    So many things, so little time.

    Thanks again for your help.

    Regards,

    Nick

  15. #15
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    No problem, I didn't have a 12F683 at home but I did have a 12F1840. Now, the 1840 has an ECCP module while the 683 has a standard CCP module so the following code MAY need to be changed slightly (the CCP1CON register) in order to work properly on the 683 (I'm not using the data modulator here). It sets the CCP module up to output a 1MHz, 50% dutycycle PWM on PortA.2. The main program then simply toggles the timebase (TMR2) for the CCP module on and off to modulate the output.
    Code:
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' RA0                       -> Not used
    ' RA1                       -> Not used
    ' RA2                       -> CCP1 output
    ' RA3                       -> Not used
    ' RA4                       -> Not used
    ' RA5                       -> Not used
    
    ' ***************************************************************
    ' 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 16Mhz
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    OSCCON   = %01110000        ' 8MHz internal osc
    ANSELA   = 0                ' Digital only for all PortA pins
    TRISA    = %00000000        ' Make PORTA outputs
    
    CCP1CON = %00001100     ' Normal PWM 
    PR2 = 1                 ' 1MHz output @8MHz system clock
    CCPR1L = 0              ' 50% Dutycycle
    CCP1CON.5 = 1
    CCP1CON.4 = 1
    
    ' ***************************************************************
    ' Actual program
    ' ***************************************************************
    Main:
        T2CON.2 = 1             ' Timebase ON
        PAUSE 15
        T2CON.2 = 0             ' Timebase OFF
        PAUSE 5
    Goto Main
    I have a scope screen grab of the output but for some reason the file upload manager on the forum refuses to work, the upload just hangs...aarghh....

    Now, if you intend to do other things with the PIC as well then this simple aproach obviously won't work. But you could use a timer interrupt in order to turn on and off the carrier at the correct intervals. I haven't personally used the data signal modulator module in the 12F1840 but it sure sounds like the "correct" way forward. The 12F1840 also allows you to run at 32MHz using the internal oscillator and 4x PLL which would allow you to get higher output frequency using the CCP module than on the 12F683 which tops out at 8MHz.

    /Henrik.
    Last edited by HenrikOlsson; - 23rd August 2013 at 18:22.

  16. #16
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    I will try your code tonight. I only have 12F683 PICs and to change to 12F1840 will take some time for me (I’m still using an older version of PBP) which, more than sure, does not support this chip.
    If I can get modulated 1 MHz from one chip for now it will save me a lot of time with the testing and a lot of space on the board.
    I will post my findings as soon as I have results.

    As always thank you for your help.

    Regards,

    Nick

  17. #17
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    I had to make few minor changes to Henrik’s code to adapt it to a 12F683 and make the code compatible with my old PBP 2.47.

    Here is the working code that generates bursts of 15 mS ON and 5 mS OFF with a carrier frequency of 1MHz (Sorry Henrik for mutilating your nicely commentated code but REMing some of the lines makes it easier on my eyes).
    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
    CCP1CON = %00001100     ' Normal PWM 
    PR2 = 1                 ' 1MHz output @8MHz system clock
    CCPR1L = 0              ' 50% Dutycycle
    CCP1CON.5 = 1
    CCP1CON.4 =1
    Main:
        T2CON.2 = 1         ' Timebase ON
        PAUSE 15
        T2CON.2 = 0         ' Timebase OFF
        PAUSE 5
    Goto Main
    This will make my testing a lot easier and fine tune the power transfer part. In the real life I will replace the fixed timing of the T2CON.2 control with some other mechanism (something like Dave did). Right now my first priority is to decide on the final frequency and tune my LC circuits for maximum efficiency on power transfer and maximum reliable speed on data transfer.
    Unfortunately I do not have too much time to experiment with the new 12F1840 which is by far the best way to go. It involves so many more variables for me including complete upgrade of my hardware and software and I’m not ready for it yet.

    One question for Dave:
    You used a data speed of 3000 bps. Did you try to push your system to see what will be the maximum data speed that you could get?
    And two questions for everyone:
    Is there a 10F, 12F or low pin count 16F PIC, supported by my older 2.47 PBP that will have a 16 MHz internal clock?
    What kind of minimal hardware / software upgrade to be able use PIC12F1840?

    Thank you in advance.

  18. #18
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Great, I'm glad you've got it working on the '683!

    You'd need to check microchip.com and use the product selector tool they have to find which PICs have the oscillator options you want and then cross reference that with the supported devices list for the compiler version you have.
    If you find yourself in a position where you need to consider upgrading to PBP3 I wouldn't hesitate a second. The big difference is how the CONFIG bits are handled - and it's a big step forward IMO. Then you have the conditional compilation features and (since you're even on pre 2.50) you'll get support for 32bit variables when using PIC18 series devices and a couple of new commands.

    PBP doesn't really know much about the hardware but obviously your device programmer needs to support the chip you're programming for - you need to check the list for your particular device programmer. I have a PICKit3, it's cheap and it works.

    /Henrik.

  19. #19
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Thanks Henrik for your support and encouraging.
    Switching to the latest software and hardware is one of my next priorities.
    I was going to order a PICKit3 just to get familiar with it until I will cross the bridge. Your sample code will be my first try.

    MeLabs use to have a really helpful PIC selection system. What happened to it? I wasn’t aware that Microchip has the product selection tool. I will use it from now on.

    I’m sure that other members of the forum will be interested in some of the aspects of my project and I pledge to keep everyone posted with my progress. It’s time to order some parts and start testing.

    Regards,

    Nick

  20. #20
    Join Date
    Oct 2012
    Posts
    81


    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

  21. #21
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    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.

  22. #22
    Join Date
    Oct 2012
    Posts
    81


    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

  23. #23
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    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.

  24. #24
    Join Date
    Oct 2012
    Posts
    81


    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

  25. #25
    Join Date
    Oct 2012
    Posts
    81


    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

  26. #26
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    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.

  27. #27
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    Thanks again for your patience with me.
    Your code makes much more sense to me so I will try it again tonight. I can only guess that as long as the execution time of the main loop is longer than the carrier period we should be fine. If that is not true there should be short glitches during the carrier ON period. Increasing the carrier frequency should be fine while going lower might require some delay.
    I can only hope that first time it might have been a stupid mistake on my side.
    Now that the setup is fine-tuned there should be no reason why it should not work.
    I will report the findings as soon as I have results.

    Regards,

    Nick

  28. #28
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Yes, as long as the loop takes longer to exectute than the period time of the PWM signal (2us in this case) it'll work without additional delay in there. When running at 8MHz each ASM instruction takes 500ns so there's really only time 4 instructions in this case. The Goto Main takes 2 and the jump caused by the IF-THEN takes 2 so that's 4, then we have the rest so it's pretty safe to run it without any delay.

    Here are the screenshots I mentioned:
    Name:  DS4_QuickPrint1.jpg
Views: 4269
Size:  93.4 KB

    Name:  DS4_QuickPrint2.jpg
Views: 4208
Size:  96.1 KB

    /Henrik.

  29. #29
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    I just can’t wait to try the code again (that will be in a couple of hours after I get off work).
    The signals look really good. Only about 12 uS delay on the falling edge which is better than I need. Is it the same at the rising edge?

    Thanks,

    Nick

  30. #30
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Yes and no....
    Yes because it's going to be in the same ballpark.
    No, because it's not going to be an absolute number (on the rising OR falling edge) because the carrier signal might "come on" (or off) at any time. If it happens to do that just before we check it the delay will be very short, if it happens to do it just after we just checked the delay will be a little longer.

    I measured a couple of pulses here.
    Delay on rising edge: 10, 7.5, 9.5, 10, 7.5, 6.5 us
    Delay on falling edge: 9.5, 11.5, 12, 8, 8, 11 us

    So I think you can consider 12us to be around the worst case for this particular code. Would you prefer constant delay at the cost of it being longer? (Not promising anything, just asking).

    And, don't forget it's Programmers Day today - the 256th day of the year! :-)

    /Henrik.

  31. #31
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Henrik,

    I only asked to confirm my theory about how it will look at the other edge which is exactly your explanation.
    It does not make any difference to me for this application and as I stated before I could live with it even if it was longer. It is important though to know it so I can compensate for it.
    Happy Programmer’s day!

    Regards,

    Nick

  32. #32
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    As much as I hate to say it I managed to release a portion of the built in smoke from one of my PICs just before the load and test of the new codes.
    I did not have the smoke pump to push some back in nor did I have a replacement PIC handy so it will have to wait until tomorrow.

    Regards,

    Nick

  33. #33
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    After fixing the missing smoke problem and modifying the code for PIC12F683 (see my prior posts for configs) the code works without any glitches. I only added on the Tx side one other pin toggle so it made it easier on my scope to compare the Tx and Rx sync. I could see the small amount of jitter at each edge and by changing the time base of the scope I could read (as predicted) delays from about 6 uS to 12 uS. Sorry but I cannot post screen shots from my old analog scope but they are like Henrik’s samples.

    I cannot explain what happened the first time and why it did not work. It might have been my scope that was confused by the complex signals (at that time I did not have the extra pin toggling) and did not latch properly on the trigger.

    BTW Henrik what are you using as your scope? It looks pretty handy. I’m still using at least 10 years old technology and even though I’m getting close to retirement I’m planning on upgrading all my tools once again because this is going to be my favorite way of spending my free time. Hopefully good wife servicing this weekend will provide the necessary budget (LOL).

    I can’t thank enough to Henrik for his help and patience. I learned at least two new things that I was afraid to play with before: PWM side of the CCP1 module and the using TMR0 as fast counter. I always need to start with a simple working sample code to get me started and this exercise gave me the opportunity to build a good foundation for future projects. I envision at least 25 PICs working 24/7 in my house for the next 5 years driving various gadgets. I know some might say that you can buy one gadget that will do the work of all little once but nothing gives me more satisfaction when I see things that I built long ago still working with no failures. And on top of that “I got the hammer” (LOL again).

    Next step: interrupts.

    Regards,

    Nick

  34. #34
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    No worries, I'm glad I could help and I'm glad you got it working.

    My scope is a RIGOL DS4014, it's a 4-channel 100MHz bandwidth scope.
    I've had it for a bit over a year now and I really do enjoy it. There's been a lot of development on the scope market in the last couple of years and there are manufacturers in the far east now churning out some pretty impressive machines (and some quite horrible ones as usual) at prices well below similar specs units from the name brands.

    /Henrik.

  35. #35
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Thank you Henrik for everything.

    Regards,

    Nick

  36. #36
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Couldn't you use the external clock divided by 4 and no program at all?

    or if you need a perfect square wave, come up with a 2MHz signal and drive a Flip-Flop with it?

  37. #37
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Art,

    Thank you for your input.
    Please do not take my reply the wrong way and try to understand my points that follow.

    The exercise was to keep the design to a minimum component count, explore unknown features of the PIC processors (unknown to me at the time), be able to modulate a high frequency carrier with the same PIC 10F or 12F family and finally have the most flexibility over the output frequency and duty cycle output. You can not bit the accuracy of the internal clock of the newer PICs if you are trying to use any RC timing circuits and employing any kind of external crystal oscillator defeats the purpose of the exercise.

    As Henrik pointed it to me I love having the opportunity to use “the hammer” when it comes to replacing older ways of doing things. I grew up with doing things the hard way and I am convinced now that employing different caliber PICs the job is much easier.
    Just to give you a stupid example: try designing an oscillator with a flexible frequency and duty cycle in a wide range using discrete components and you will find yourself in a deep trouble especially when one of the parameters changes.

    Use the results of this thread exercise and you will find that all it takes to change one parameter is one or two lines of code. You can use the same hardware over and over for one or multiple projects and all it takes is code changes.

    I’m old. My eyes are failing me and even though I can still replace a 100 pin SMD chip with 0.5 mm pitch (that means remove the old chip and install the new one) in less than 3 minutes with 100% success (my personal record about 15 years ago was 87 seconds) I do want to keep using this skill to a minimum.

    So to answer your question:

    The external clock divided by 4 gives only 50 / 50 duty cycle and only one output frequency for any given main clock. Second part of your question goes in the territory that I’m trying to escape from and gives very little flexibility when changing any parameter because it has to be done in the hardware.

    I’m not being lazy I’m just trying to be more efficient when it comes to using my time especially when the clock is ticking for me on the down side.

    Thanks for your suggestions and keep up the good work. I’ve been following your posts in this forum and I commend you for your great contributions.

    Regards,

    Nick

  38. #38
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Nick,
    Great post, and objective I've never tried to get out of using an Xtal before,
    so hadn't previously considered some of the difficulties coming up with your own timing.

    I know what you mean, playing with a phone that almost does everything doesn't motivate
    me to build my own computers,
    I do have to come back very soon for a project... kind of have to because I want the thing I'm making

    If it is just a perfectly linear program that can boss the chip out of running any other program,
    such as for a power supply or something... I think as things warm up you lose timing of the internal clock,
    same as if there was voltage fluctuation.
    but for the exercise... with the new rules as I understand them,
    I would try a program that can interrupt itself with an output pin
    connected to the portB interrupt pin. Then you could introduce a linear delay at the interrupt
    vector, and control the delay before turning the interrupt back on.
    I don't know if the jump to the interrupt vector is essentially a GOTO or not.
    It jumps to vector 4, which would not be as fast as resetting the program counter to zero vector.
    and potentially some inaccuracy caused if there's any variation in the response time to the interrupt.
    Cheers, B.

  39. #39
    Join Date
    Oct 2012
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    Hi Art,

    I agree with you on your points. One must be careful when simplifying things, like we’ve been doing in this thread, depending on the application.
    For my application 10% accuracy on frequency and duty cycle will be fine. The beauty of what we did here is that instead of doing it the old fashion way, trying to match RC values for target frequency and duty cycle, we used the built in oscillator (which in my opinion is more precise and reliable than RC setup even when using 1% components) and use the code to make any changes. In the process the component count, board space and I’m sure cost went down. I didn’t dream when I started this that (excluding PSU section) with only two PICs, a couple of resistors and capacitors and one Mosfet I can wirelessly transfer power with good efficiency and have the grounds ready for data transfer.
    As for your suggestion at the end of your post you used the magic word (again I’m talking for myself only) INTERRUPTS.
    Yes, they are next on my list and I will start researching how to use them. As soon as I have a clearer image of interrupts I will try to see if they will do any better on my Rx module. I have the feeling that they will eliminate some if not all the jitter but not the delay.

    Regards,

    Nick

  40. #40
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Maximum frequency output from a PIC

    If you are still going on it, you probably know not to bother looking at BASIC interrupt in this case.
    Usually there is time taken saving and restoring context, but that could likely be avoided
    if the program is under 2K, and doesn't care where it was up to when the interrupt was called.

    I will be writing for a 16F877 soon, and would see how fast I could just cycle a pin,
    but don't have anything to measure it with.

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 : 1

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