Sweep Frequency Oscillator

MartinM57

Moderator
Arbitrary Frequency Generator (was SFO)

Anyone got any real code/want to take the challenge of writing real code for the following pseudo code?

Would be the basis of a nice piece of test equipment...
- autosweeping within limits
- set the frequency via serin
- set the frequency by a pot on an ADC
- etc

Code:
PICAXE xx
SETFREQ xx
FOR x = 1 TO 100,000 STEP 1
    Produce a square wave at x hertz using any technique, including but not limited to:
        - PWM with and without PWMDIVxx
        - changing SETFREQ
        - hardcoded pause loops etc
        ...but not taking "too long" to switch to the new frequency
    Pause <<some duration>> - 1ms to 1 second to 1ms resolution?
NEXT
I've looked at the PWM wizard for the low end frequencies (min seems to be 230Hz with a 4MHz chip) but as you increase the Hz, PWMDIV comes in and out and I can't see (aka don't have the time to derive) the algorithm being used so that you could code it into the loop
 
Last edited:

Dippy

Moderator
Don't forget the range/step/resolution limits of a PIC Martin.

Get a copy of PICMultiCalc and you can look at the nitty gritty.

I can't help in PICAXE BASIC, but to get the ball rolling, here is an example of a function (subroutine) to set the PIC PWM frequency to the nearest. (LImited by the crystal and PIC guts of course).

Fosc = the crystal
pFrequency is your chosen frequency. It will choose the settings closest to required, but never guaranteeed spot-on as that is impossible.

Code:
   PRConst = Fosc / pFrequency / 4
   Prescale = 1
   Result = false
   Repeat
      PR2Value = PRConst / Prescale - 1            // calculate a PR2 value
      If (PR2Value < 256) And (PR2Value > 1) Then  // if it is a valid value, then...
         Result = true                             // function return true (success)
         Select Prescale                           // configure T2CON prescale
            Case 1  : T2CON = %00000000            // prescale 1
	        Case 4  : T2CON = %00000001            // prescale 4
	        Case 16 : T2CON = %00000011            // prescale 16
         End Select
	     PR2 = PR2Value                            // initialise PR2
         FMaxDuty = Fosc / (pFrequency * Prescale) // determine maximum duty...
         SetDutyPercent(pInitialDuty)              // set to initial duty
         Start                                     // start PWM
         Exit                                      // exit the sub
      EndIf   
      Prescale = Prescale * 4
   Until Prescale > 16
Edit:
Oh, sorry, I've just realised this uses 4 byte variables , so maybe it can't be used.
Anyway, I'll leave it there to give you an idea.
Edit2:
Oh, I've also realised you may hace to twiddle the CCP1CON.4/5 and CCPR1L too.
Actually, forget I posted it .... I forgot to read my original code.
I'll go back to sleep now :)
 
Last edited:

MartinM57

Moderator
I guess what I'm really after is a Arbitrary Function Generator but I'll have to sell a lot more more widgets to justify a Tek AFG3000 series...

...there must be a cheapo USB pluggable AFG out there somewhere - like what the Picoscope does compared with a proper standalone scope.

Any ideas?
 

BeanieBots

Moderator
Make sure you know and understand your requirements.
Pico 'scopes are fine for many applications, but they have their limits as would any PICAXE based function generator.
If you NEED the functionality/resolution of an expensive device, you NEED an expensive device.
 

Dippy

Moderator
Perhaps it would be easier to control a FuncGen chip?
That could also allow square, triangle and sine too.

If you moved to dsPIC you could generate everything mathematically.
Obv you would be limited at top end etc.
 

MartinM57

Moderator
True

I think my requirements are more at the "Picoscope" level than the AFG3000-series for example - a nice GUI on my PC that can define square, sine, triangle, PWM from 0.1Hz to 4Mhz at n% accuracy, 0 to some-voltage-adjustable and then a pack-of-cards sized USB connected box with a few output connectors on it.

Willing to compromise on functionality/accuracy vs price. Just can't seem to find anything like it..

Happy to buy rather than make - I don't want to enjoy this journey, just want to be there ;)
 
Last edited:

BeanieBots

Moderator
Can't say that I'm aware of anything specific to that function, but there are several USB 'boxes' available which do general purpose analogue and digital outputs. Maybe there's an oportunity to write something in VB/mathlab to drive one?
 

hippy

Ex-Staff (retired)
I think my requirements are more at the "Picoscope" level than the AFG3000-series for example - a nice GUI on my PC that can define square, sine, triangle, PWM from 0.1Hz to 4Mhz at n% accuracy
Should be easy enough. Create a purely hardware design which can do it then map that into microprocessor hardware and software.

My thought is a lookup table in external ROM/RAM with N-bit output driving a DAC. Change the ROM addresses at the right time and you can generate the stored waveform at any frequency. Multiple wave tables give square, triangle, sine and other waveforms. Then it's a simple question of stepping through the addresses at the right rate.

The system clock could be any value, say 1GHz, and only needs a programmable divide-by-N to give the right clocking rate to the wave table addresses, such a divide-by-N can be set and controlled by even the humblest of PICAXE.

Alternatively use the Direct Digital Synthesis (DDS) approach; increment the ROM addresses by a programmable N on every system clock tick. Again that hardware's easy enough to set and control from a PICAXE.

Now all you need to do is consider how that hardware could be brought within a PICAXE and what limitation does PICAXE operating speed have on the results you desire. If TOGGLE takes 1us you cannot use that to control better than a 1MHz signal, and if pause takes 1uS you cannot use that with TOGGLE to get a frequency between 1MHz and 2MHz. Likewise a PWMOUT with period of 1 giving 4MHz and a period of 2 giving 2MHz you cannot generate any specific frequency between 2MHz and 4MHz. External divide-by-N and DDS techniques overcome those limitations.

I suspect you won't get what you require from a PICAXE and if it requires external hardware to get higher frequency performance you may as well use that to also get lower speed performance, though there may be a cut-off point where it becomes easier to switch clocking from one to another.

The problem you face, whichever micro you chose, is that it needs to run faster than what you intend to generate. I suspect most people would consider building the hardware ( which is mainly purely digital ) in FPGA or CLPD.

Happy to buy rather than make - I don't want to enjoy this journey, just want to be there ;)
If you don't want the fun of hardware design and building, the challenge of creating a software solution, I'd say simply buy something which does the job you require.
 
Top