PWM ADVICE PLEASE

For general Flowcode discussion that does not belong in the other sections.
Post Reply
siliconchip
Posts: 16
http://meble-kuchenne.info.pl
Joined: Wed Dec 16, 2020 10:38 am
Has thanked: 4 times

PWM ADVICE PLEASE

Post by siliconchip »

Hi all,
ive been trying to get my head round setting up pwm, im using a pic12f1822, ive set my internal oscillator at 10Mhz and my timer is set at 9765 Hz. my question is have i expressed the code correctly for the pwm or do i just need to put a component macro stating frequency as i believe it is then set automatically to 50% duty cycle and if this is the case would i use my method if i wanted a different duty cycle ?, my chosen frequency's are 2000Hz and 1500Hz but when setting duty cycle to 50% its not 128 as id expect but the values shown ?? thanks in advance
Bob
Attachments
buzzer test.fcfx
(11.52 KiB) Downloaded 8 times

medelec35
Valued Contributor
Posts: 2215
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 681 times
Been thanked: 755 times

Re: PWM ADVICE PLEASE

Post by medelec35 »

Hi Bob The first thing you do is make sure your hardware is setup to rub at the correct clock speed.
Always flash a LED for 1 sec or use a logic analyser or scope
Within projet options you have set the clock at a frequency which does not exist - 10MHz.
The would cause issues with your PWM.
The only internal speeds available are:
Frequencies derived from osccon values.png
Frequencies derived from osccon values.png (99.72 KiB) Viewed 120 times
if using PLL, don't forget to enable it within configurator settings, or set SPELLEN to 1

What I do with 12F1840 is set clock speed to 32 MHz and use a C code block at the start with

Code: Select all

OSCCON = 0xF0;
Martin

siliconchip
Posts: 16
Joined: Wed Dec 16, 2020 10:38 am
Has thanked: 4 times

Re: PWM ADVICE PLEASE

Post by siliconchip »

Hi martin
Thanks for the reply i set the chip at 10Mhz from the config page so im guessing it doesnt slways mean a chip can run at that frequency ?. As for my code itself have i wrote it out correctly other than the duty cycle for said reason
Cheers
Bob

medelec35
Valued Contributor
Posts: 2215
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 681 times
Been thanked: 755 times

Re: PWM ADVICE PLEASE

Post by medelec35 »

siliconchip wrote:
Sat Jan 03, 2026 5:31 pm
i set the chip at 10Mhz from the config page so im guessing it doesnt slways mean a chip can run at that frequency ?.
That is correct.
You have two options.
1. use the internal osccon component.
On the components ribbon use the magnifying glass and search for intosc
Or use the values within the image I posted as they are the only internal oscillator values that can be use.
Its the OSCCON = 0xF0; that sets the hardware speed.
It's the configuration setting that sets the delays e.g. for UART, PWM, delays etc.
So it's vital that they both match!

You set OSCCON to 0x58 = 0b01011000.
Ignoring bit 7 as that is SPLLEN, leaves you with 1011 as 4 bits are used to set internal osc frequency.#
If you look at the image within my first post 1011 = 1MHz, yet you have set clock within configuration settings to 10MHz.
That is a mismatch by a factor of 10, which is why PWM is not working as expected.
After you make adjustments and match clock speed to internal osc speed, the PWM can then be investigated if still an issue.
Martin

chipfryer27
Valued Contributor
Posts: 1800
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 403 times
Been thanked: 619 times

Re: PWM ADVICE PLEASE

Post by chipfryer27 »

Hi

Following up on Martin's posts, you need to set your oscillator as per his posts.

In project options > Configuration, you can set Osc to internal / external etc, so choose as Martins illustrates above and set for internal 32MHz (faster is uaually better<s>). Don't forget the C-code block at the very beginninig of your chart.

The Clock Speed option needs to match actual clock speed. This setting doesn't set clock speeds, it only tells FC what speed you are runing at. Very important to get correct.

A one second flash test is always a good idea to check Osc settings and examples are available in the forum. A quick search will throw up many examples.

Next, you have enabled a timer interrupt to call User Macro Timer2. However nothing is in the Macro so nothing will happen.

The interrupt is set for 9.76Khz so you will get an interrupt every 0.1mS (approx), but as nothing is in your macro, nothing will happen. This however isn't an issue as you do not need the interrupt for your PWM.

if you go to PWM properties you will see option to set your target frequency. You have set this but haven't set "Apply to defaults" to yes. Once set to yes FC will make it's best attempt to match your target frequency.

Many examples of using Set Duty Cycle / Set Frequency etc in forum and also in WiKi.

Sorry to be brief, not much time tonight.

Regards

siliconchip
Posts: 16
Joined: Wed Dec 16, 2020 10:38 am
Has thanked: 4 times

Re: PWM ADVICE PLEASE

Post by siliconchip »

hi martin and chip fryer
thanks for the replys, i can confirm that the config allows me to flash a led at 1 second intervals, ive now used this congfig but still not getting any where i was under the impression if i added a component macro and set frequency then the duty cycle would be 50% automatically but its not, i can alter the peiod and prescaler and get the frequency i like but duty cycle is still not right, ive tried with PLL enable but made no difference, ive also tried with the 10 bit duty cycle set at 512 for 50% but again not right,

cheers bob
Attachments
buzzer test.fcfx
(10.44 KiB) Downloaded 3 times

medelec35
Valued Contributor
Posts: 2215
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 681 times
Been thanked: 755 times

Re: PWM ADVICE PLEASE

Post by medelec35 »

If you are correctly getting 1second then internal osc and clock settings match.
Does the simulation match the hardware?
With PWM the period value represents 100%.
For 8bit, 100% PWM Duty = Period Overflow.
For 10bit, 100% PWM Duty = Period Overflow * 4
124 * 4 = 496.
50% = 496 * 50 / 100 = 248
Try a duty of 248 and check both simulation and embedded both show a PWM with a duty cycle of 50%.

Start at the basics, let the PWM component do all the calculations.
Change Appy To Defaults from No to Yes, then have just 1 * SetDutyCycle10bit to 248.

If that does not work I will check with my hardware.
Martin

siliconchip
Posts: 16
Joined: Wed Dec 16, 2020 10:38 am
Has thanked: 4 times

Re: PWM ADVICE PLEASE

Post by siliconchip »

Hi martin
Am i correct using the 10 bit duty cycle as thought using this would equate to 512 for a 50% duty. Just a thought ive got the oscilator set to 8Mhz would this being low affect my duty cycles ?
Cheers bob

medelec35
Valued Contributor
Posts: 2215
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 681 times
Been thanked: 755 times

Re: PWM ADVICE PLEASE

Post by medelec35 »

512 is wrong, which is your main issue.
I'm assuming you just thought because you are using 10bit PWM then 50% of 1023
You only need to pay attention to Period Overflow as explained in my previous post.
If you look at the image below:
10bit PWM.png
10bit PWM.png (125.28 KiB) Viewed 20 times
You can see the Period Overflow is 124.
In the calculations I showed you that for 50% duty you need to use the value of 248:
medelec35 wrote:
Sun Jan 04, 2026 5:58 pm
For 10bit, 100% PWM Duty = Period Overflow * 4
124 * 4 = 496.
50% = 496 * 50 / 100 = 248
Try a duty of 248 and check both simulation and embedded both show a PWM with a duty cycle of 50%
The above is with PWM frequency set to 1000Hz.

A bit more maths.
If you are using 512 then using transposition, actual PWM is 512*100/(124*4) = 103.2%
That is why you get unexpected results.
Martin

siliconchip
Posts: 16
Joined: Wed Dec 16, 2020 10:38 am
Has thanked: 4 times

Re: PWM ADVICE PLEASE

Post by siliconchip »

Hi martin
Thank you i now understand i will experiment tomorrow but its starting to make sense now thanks i will post to let you know how i go
Cheers bob

Post Reply