Setfreq

thunderace7

New Member
Hi. Can someone help me with understanding the setfreq command, please?
If I explain how I think it works can you either confirm it or correct me?
As I understand it, a picaxe can use the internal oscillator or an external one.
The internal oscillator is enabled by using setfreq mx, where x represents the speed in MHz. (Or kx for lower, KHz, speeds). If a number is chosen that is not supported by the chip in use PE6 will flag an error. If no setfreq command is used the chip defaults to 4MHz (Or 8MHz for x2 chips).
External oscillator is enabled by using setfreq emx in a similar manner to above. As PE6 has no knowledge of the resonator that is fitted it cannot make any checks other than whether the chosen value is possible with the picaxe in use (I imagine that something like setfreq em24 would be flagged as an error). What happens if I use setfreq em64 but have a 4MHz resonator fitted? Does it give an error message? Does it work out what resonator is fitted and correct the setting? Does it drop back to the default frequency? Or something else?
Is it possible to tell what frequency a picaxe is running at?
Thanks.
 

inglewoodpete

Senior Member
No error messages: if you use SetFreq EM64 but have a 4MHz resonator fitted. The chip just does what its hardware inputs, firmware and code tell it to do. In the case of EM64 and a 4MHz resonator, the chip would run at 16MHz because the SetFreq EMxx says "clock off the externally connected resonator/crystal".

The result would be a problem that would need debugging and resolving by changing code or hardware. Actually, this is no different to set a baud rate to, say, T9600 and then connect the microcontroller to an external device that is set for N4800. You have to know what you're doing.

The simplest way to check what speed the PICAXE is running at is, after your SetFreq command, to temporarily add some code like:
Rich (BB code):
Do
   Wait 1
   Toggle B.0
Loop
Obviously, add an LED with a limiting resistor to your selected pin.
 

thunderace7

New Member
Thank you.
So, for an external clock, the picaxe takes no notice of the figure, just the 'em' part of the command? The figure is just an aide memoir for the user?
 

inglewoodpete

Senior Member
Thank you.
So, for an external clock, the picaxe takes no notice of the figure, just the 'em' part of the command? The figure is just an aide memoir for the user?
It does take some notice of the SetFreq command when it comes to some commands, like Servo, IRIn, KBIn, OWIn, OWOut and possibly others.

You have to keep your wits about you when dealing with any functions that combine clock speed and timing functions. This means that if there is any uncertainty, you should confirm the functionality on the real hardware.
 
Top