PWM, multitasking, 20M2

JoeFromOzarks

Senior Member
Hello folks!

Please help a good friend and me out here, where we can read the documentation on this "odd" event:


Using a 20M2, connect an LED to b.7 and your frequency counter to c.2.

Download and run this program first, check the PWM frequency with your meter and observe the flash rate of the LED.

Code:
SYMBOL IR_LED = c.2 'pin out2
PWMOUT IR_LED, 25, 52 'start 38 kHz for IR
#NO_DATA
do
toggle b.7
pause 1000
loop
Frequency counter will read about 38KHz, as expected.

-=-=-=

Then download this code and again, observe the PWM frequency and observe the flash rate of the LED.

Code:
Start0:
suspend 1
SYMBOL IR_LED = c.2 'pin out2
PWMOUT IR_LED, 25, 52 'start 38 kHz for IR
#NO_DATA
do
toggle b.7
pause 1000
loop
start1:
do
loop

Frequency counter will read about 153KHz. The LED on b.7 flashes at about the same rate with either program.

What’s up with this? Multitasking causes a 4x increase in PWM? Where can I read more about this because I've made my eyes bleed reading and reading and reading.

Thank you!!


-joe
 

PhilHornby

Senior Member
A hidden Setfreq M16 is in effect, whenever multi-tasking is enabled (as revealed using PeekSFR OSCCON). Some commands (such as Pause), still operate as though the Picaxe is at its default frequency - whereas others commands (like PWMOUT) don't. The baud rate used by Sertxd doesn't change either.

(From my own experiments - I don't think it's documented anywhere)
 

inglewoodpete

Senior Member
Since there is no single task/multiple task command per se, there needs to be an understanding that when additional tasks are started (or stopped), the core operating frequency will change. This characteristic is described in the last sentence of the paragraph that you have highlighted.
 

PhilHornby

Senior Member
That documentation certainly reads that way, but from the (limited) experiments I did, it just seemed fixed at 16MHz (with 2 tasks). At the very least, you might expect it to use 8MHz for 2 tasks, 12MHz for 3 tasks, and 16MHz for 4 ...

I have a Battery Capacity Tester (doesn't everyone :) ), that uses PWM and a feedback loop in Task 0 to disharge a battery, and Task 1 periodically updates an LCD. The PWM constants assume a CPU frequency of 16MHz - the PWM frequency doesn't change as Task 1 Pauses and Resumes (which it would if the CPU frequency was changing).
 

JoeFromOzarks

Senior Member
InglewoodPete, PhilHornby, thank you for your comments. The statement "The change in background frequency may also affect background pwm pulse generation, so it is recommended that single task mode is used for programs containing pwmout commands" says it all. Sure wish I had the list of the commands and their effect on processor speed.

Again, thank you!!

-joe
 

hippy

Technical Support
Staff member
Whether a program is single task or multi-task is determined by whether StartN: labels are included or not.

If single task the program is started with the default 4MHz operating speed which SETFREQ can then change. When multi-tasking the program is started at 16MHz and SETFREQ commands have no effect, in fact the compiler will prohibit them from being used.

While the firmware generally runs commands at 16MHz when multi-tasking, for some I/O commands where the timing is controlled by the firmware, the frequency is temporarily dropped to 4MHz so the firmware for those commands is exactly the same and has the same timing when multi-tasking as when single task. Once those commands complete the frequency is switched back to 16MHz.

The main reason for this is that it's not possible to alter the commands within firmware to have the same timing at 4MHz and 16MHz without affecting their timings at 4MHz, and there's not enough firmware space to duplicate commands for both single task and multi-task use.

The temporary reduction in speed was deemed an appropriate compromise and does not affect most programs but does affect some, in particular where background PWMOUT is being used.

Some commands which are operating speed dependent have been adjusted to work the same at 16MHz as at 4MHz so do not drop the operating speed. For example PAUSE and the servo related commands.

I cannot recall off-hand if there is a list of those commands where frequency is dropped during multi-tasking execution. I will take a look.
 

PhilHornby

Senior Member
I cannot recall off-hand if there is a list of those commands where frequency is dropped during multi-tasking execution. I will take a look.
I can start the list ... hi2cout is definitely one of them!

I have a Battery Capacity Tester (doesn't everyone :) ), that uses PWM and a feedback loop in Task 0 to discharge a battery, and Task 1 periodically updates an LCD. The PWM constants assume a CPU frequency of 16MHz - the PWM frequency doesn't change as Task 1 Pauses and Resumes (which it would if the CPU frequency was changing).
I've now discovered that the PWM frequency does change when the I²C connected LCD is updated. This causes a sudden, massive change in voltage from the output of its connected R-C smoothing circuit which is being fed to a MosFET to control the discharge of the battery.

After five years of looking, I've finally found out why my carefully monitored discharge current, isn't quite stable :rolleyes:

24453

Before you say anything, a somewhat larger value for R or C, is probably in order too, to get rid of that sawtooth.
 
Top