18F4550 separate PWM freq.

53 views
Skip to first unread message

Hans van Veldhuizen

unread,
Aug 11, 2025, 6:37:22 AMAug 11
to jallist

Hello everyone,
The 18F4550 has two PWM connections. Is it possible to set the pwm_set_frequency for these outputs differently?
Regards,
Hans

Hans van Veldhuizen

unread,
Aug 11, 2025, 3:20:52 PMAug 11
to jallist
Another thing, I can't get the sample sound to work. Just a strange sound every now and then. This sample works great on other pics. Something special?

Op maandag 11 augustus 2025 om 12:37:22 UTC+2 schreef Hans van Veldhuizen:
18f4550_pwm_sound.jal

Hans van Veldhuizen

unread,
Aug 12, 2025, 3:28:49 AMAug 12
to jallist
have tested on another pcb and another Pic 18F4550, same, no result

Op maandag 11 augustus 2025 om 21:20:52 UTC+2 schreef Hans van Veldhuizen:

Patrick FROUCHT

unread,
Aug 12, 2025, 4:14:14 AMAug 12
to jal...@googlegroups.com
Hello Hans

If you look at §15.4.4  of the DS39632E-page 149 
   SETUP FOR PWM OPERATION
The following steps should be taken when configuring the CCPx module for PWM operation:
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPRxL register and CCPxCON<5:4> bits.
3. Make the CCPx pin an output by clearing the appropriate TRIS bit.
4. Set the TMR2 prescale value, then enable Timer2 by writing to T2CON.
5. Configure the CCPx module for PWM operation
.

You can see that three is only one  PR2 register and 2  sets for the duty cycle  CCPRxL register and CCPxCON<5:4> bits. With x = 1 or 2 .

The 4 outputs drive a motor with the same PWM and duty cycle with dead time and appropriate signal for the drivers.

Patrick

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jallist/f0c0ea1a-c716-4ddf-8044-949a50b4b5adn%40googlegroups.com.
Message has been deleted

Hans van Veldhuizen

unread,
Aug 12, 2025, 7:17:14 AMAug 12
to jallist
Hi Patric,
Because the program (part of which is attached) didn't work, I tried the sample, but as I mentioned, it doesn't work either.
I found what you mentioned, but I don't know how to do it.
Regards,
Hans

Op dinsdag 12 augustus 2025 om 10:14:14 UTC+2 schreef Patrick FROUCHT:
18F4550_BLD.jal

Hans van Veldhuizen

unread,
Aug 12, 2025, 11:06:58 AMAug 12
to jallist
Have tested the pcb in the Bert's book environment and his sound example works perfect. 

Op dinsdag 12 augustus 2025 om 13:17:14 UTC+2 schreef Hans van Veldhuizen:

Rob CJ

unread,
Aug 12, 2025, 2:03:28 PMAug 12
to jallist
Hi Hans,

In your program you did not enable the pwm.

You have to add after include pwm_hardware:
-) pwm1_on()
-) pwm2_on()

And maybe also add:
-) pwm_max_resolution(1)


Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Hans van Veldhuizen <hanz...@zeelandnet.nl>
Verzonden: dinsdag 12 augustus 2025 17:06
Aan: jallist <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Re: 18F4550 separate PWM freq.
 

Hans van Veldhuizen

unread,
Aug 12, 2025, 2:40:44 PMAug 12
to jallist
Hi Rob,
If something doesn't work, I always look up the samples again. Why doesn't the sample work?
Regards,
Hans

Op dinsdag 12 augustus 2025 om 20:03:28 UTC+2 schreef Rob CJ:

Hans van Veldhuizen

unread,
Aug 12, 2025, 3:38:45 PMAug 12
to jallist
Hi Rob,
The program is part of the whole, and the original had pwm1_on and PWM2_on, but not the max resolution.

-- Configure PWM
pin_ccp1_direction = output
pin_CCP2_RC1_direction = output
include pwm_hardware
pwm1_on() -- This was in my original.
pwm2_on() -- This was in my original.
pwm_max_resolution(1) -- Now that I've added this, pwm2 works, but pwm1 doesn't. Pwm1 is also used in the sample. Is the output definition correct?

Op dinsdag 12 augustus 2025 om 20:40:44 UTC+2 schreef Hans van Veldhuizen:

Hans van Veldhuizen

unread,
Aug 13, 2025, 2:11:43 AMAug 13
to jallist
I really don't understand it anymore. Attached is the latest version. See the comments in the forever loop:

forever loop
SOUND_L(100,2) -- NOT WORKING
delay_1S(1)
SOUND_C(100,4) -- JUST pwm1 WORKS, not pmw2
delay_1S(1)
SOUND_R(100,6) -- works correctly
delay_1S(3)
end loop

Op dinsdag 12 augustus 2025 om 21:38:45 UTC+2 schreef Hans van Veldhuizen:
18F4550_BLD.jal

Hans van Veldhuizen

unread,
Aug 13, 2025, 11:50:34 AMAug 13
to jallist
Have tested on a 16F877a, same bad result. 
Have made the principle of the program (without the freq changes) in the old berts book environnement
There is sound left, sound both and sound righ perfect. So i think the problem is in the PWM lib. 
Op woensdag 13 augustus 2025 om 08:11:43 UTC+2 schreef Hans van Veldhuizen:

Rob CJ

unread,
Aug 13, 2025, 12:45:09 PMAug 13
to jallist
Hi Hans,

Please send me the program that you are using. I will see if I can test it this weekend.

Kind regards,

Rob


Verzonden: woensdag 13 augustus 2025 17:50

Hans van Veldhuizen

unread,
Aug 13, 2025, 1:05:24 PMAug 13
to jallist
Hi  Rob,
Program is atached to my  message from 8.11 yesterday. I hope someone can try this program on another Pic with 2 pwm ports.
regards
Hans
Op woensdag 13 augustus 2025 om 18:45:09 UTC+2 schreef Rob CJ:

Rob CJ

unread,
Aug 13, 2025, 1:22:46 PMAug 13
to jallist
Hi Hans,

I do not want to guess what you need. The program you mentioned used only 1 PWM and the other program you had sent had 2 PWM but was missing the pwm_on.

So please send the program that shows what you want to achieve and that apparently does not yet work.

Thanks.

Kind regards,

Rob



Verzonden: woensdag 13 augustus 2025 19:05

Hans van Veldhuizen

unread,
Aug 13, 2025, 3:38:33 PMAug 13
to jallist
Hi Rob, The program use two pwm's. Here it is again

Op woensdag 13 augustus 2025 om 19:22:46 UTC+2 schreef Rob CJ:
18F4550_BLD (1).jal

Rob CJ

unread,
Aug 15, 2025, 3:57:46 AMAug 15
to jallist
Hi Hans,

I tested your program and it works fine. There is a PWM signal on both the PWM1 pin (17) and PWM2 (16).

So maybe you have bad connection or your PIC is broken.

Kind regards,

Rob


Verzonden: woensdag 13 augustus 2025 21:38

Hans van Veldhuizen

unread,
Aug 15, 2025, 9:07:55 AMAug 15
to jallist
Hi Rob,
I also have a signal on both PWMs, but it doesn't work in the forever loop. Try the following:
a. Only turn on number 1  that is PWM 1; then nothing happens.
b. Only turn on number 2; then only the PWM 1 that should actually be activated at (1) will start, not both.
c. Only turn on number 3, then it is correct PWM2 works.
Forever loop
-----------------------------( 1 )--------------------------------------------
SOUND_L(100,2) -- NOT WORKING
Delay_1S(1)
------------------------------------------( 2 )----------------------------

SOUND_C(100,4) -- JUST pwm1 WORKS, not pmw2
Delay_1S(1)
------------------------------------------( 3 )-----------------------------
SOUND_R(100,6) -- Works correctly
Delay_1S(3)
--------------------------------------------------------------------------
End loop

Op vrijdag 15 augustus 2025 om 09:57:46 UTC+2 schreef Rob CJ:

Rob CJ

unread,
Aug 15, 2025, 10:00:27 AMAug 15
to jallist
Hi Hans,

I tried them independently and together and it worked for me.

As a sanity check I will test it again tomorrow.

Kind regards,

Rob


Verzonden: vrijdag 15 augustus 2025 15:07

Hans van Veldhuizen

unread,
Aug 15, 2025, 11:52:17 AMAug 15
to jallist
Hello Rob,
As I mentioned before, I tested the same program on a 16F877a, with the same results. 
When I test the 18F4550_pwm_sound from the sample series, I only hear crackling.
 But the same 12F683_pwm_sound works fine. It only has one PWM.
I realize I'm being difficult.
Regards,
Hans

Op vrijdag 15 augustus 2025 om 16:00:27 UTC+2 schreef Rob CJ:

Rob CJ

unread,
Aug 16, 2025, 4:27:09 AMAug 16
to jallist
Hi Hans,

I tested it again, first with only one call in the main loop for each procedure and then with all procedures in the main loop.

It seems to work fine. See attached screenshots from my oscilloscope.

Kind regards,

Rob


Verzonden: vrijdag 15 augustus 2025 17:52
Sound_All.bmp
Sound_C.bmp
Sound_L.bmp
Sound_R.bmp

Hans van Veldhuizen

unread,
Aug 16, 2025, 5:23:18 AMAug 16
to jallist
Rob,
I use a little earphone. Can you please send me the hex file you used

Op zaterdag 16 augustus 2025 om 10:27:09 UTC+2 schreef Rob CJ:

Hans van Veldhuizen

unread,
Aug 16, 2025, 6:48:42 AMAug 16
to jallist
Rob,
The mystery is becoming clearer. I was using the 2024 package up until now. I've now used the 2.0.0 package and I get the following results:
1500, 1800, and 2000 work fine. But when I use 1000, 1500, or 2000, the pitch is much higher instead of 1000.

So there's a difference between the 2024 and 2025 packages.

And there's something strange with the frequency calculation. Could you, like me, test it again with one of those small phone earbuds?
Hans

Op zaterdag 16 augustus 2025 om 11:23:18 UTC+2 schreef Hans van Veldhuizen:

Rob CJ

unread,
Aug 16, 2025, 7:39:13 AMAug 16
to jallist
Hi Hans,

I measured the signals. When you selected 1000 Hz the result is 4000 Hz. When looking in the file pwm_common.jal there is the following table:

-- -----------------------------------------------------------------------------------------
-- ||*Prescale*||  *4MHz*  ||  *8MHz*  || *10MHz*  ||  *20MHz*  ||  *32MHz*  ||  *48MHz*  ||
-- ||     1    || 3.90 kHz || 7.81 kHz || 9.77 kHz || 19.53 kHz || 31.25 kHz || 46.88 kHz ||
-- ||     4    ||  976  Hz || 1.95 kHz || 2.44 kHz ||  4.88 kHz ||  7.81 kHz || 11.72 kHz ||
-- ||    16    ||  244  Hz ||  488  Hz ||  610 Hz  ||  1.22 kHz ||  1.95 kHz ||  2.93 kHz ||
-- ||    64    ||   61  Hz ||  122  Hz ||  152 Hz  ||   305 Hz  ||   488 Hz  ||   732 Hz  ||
-- -----------------------------------------------------------------------------------------

What you see here is that when you use a clock of 20 MHz and the PIC only has 2 prescaler bits (like the PIC4550), it can only prescale by 16 and so the minium clock is 1.22 kHz. If you want 1 kHz tone, you have to lower your clock by using the internal oscillator or use a PIC that does has 3 prescaler bit. The lowest frequency is then 305 Hz with a clock of 20  MHz.

Kind regards,

Rob


Verzonden: zaterdag 16 augustus 2025 12:48

Hans van Veldhuizen

unread,
Aug 16, 2025, 9:27:31 AMAug 16
to jallist
Hi Rob,
Wow !!! Again a lesson!! Thanks for all your patience!!!! 
regards
Hans

Op zaterdag 16 augustus 2025 om 13:39:13 UTC+2 schreef Rob CJ:

Hans van Veldhuizen

unread,
Aug 16, 2025, 2:51:25 PMAug 16
to jallist
Rob,
Last remark : the   the 18F4550_pwm_sound.jal  must be wrong .
regards
Hans

Op zaterdag 16 augustus 2025 om 15:27:31 UTC+2 schreef Hans van Veldhuizen:

Rob CJ

unread,
Aug 17, 2025, 3:59:09 AMAug 17
to jallist
Hi Hans,

I know that many sample files where generated using a script. That's why there are so many sample files (I would be OK if we agreed to reduce the number of variations).  I assume that at least one of them was tested but I do not know if all where tested.

I always create sample files manually and test them before release so that I am sure that both the sample file and the library works correctly.

Kind regards,

Rob


Verzonden: zaterdag 16 augustus 2025 20:51

Hans van Veldhuizen

unread,
Aug 17, 2025, 9:06:58 AMAug 17
to jallist
Hi everyone,
This means that many of the samples in the list have only been ported with a script.
 They will compile fine, but they haven't been tested to see if they actually do what they're supposed to do.
 Could the sample folder then be split into SCRIPT ONLY and PRACTICALLY TESTED versions?
Regards,
Hans

Op zondag 17 augustus 2025 om 09:59:09 UTC+2 schreef Rob CJ:

Rob CJ

unread,
Aug 17, 2025, 10:34:44 AMAug 17
to jallist
Hi Hans,

I agree with you that all samples that are in a Jallib release should have been tested.

I do not know the history of why samples where created with a script and what was tested.

The number of samples is huge and my question is if it would not be 'better'  if we had indeed only tested samples AND have only one sample per PIC type (so e.g. not 5 samples for PIC18Fxx). There if course the exception that PICs in the same range (12F, 16F, 18F) may behave different so you would have more samples in that case for the same range. That sample is needed anyway for testing the library that should support variations in PICs.

For default settings of any PIC for porting an existing sample you can always use the blink routines as input.

Not sure what the others think about this. Share your opinion. To summarize:
  1. All samples must have been tested
  2. You only need at least one sample to show that the library works . Of course you can make mutiple different samples but not copy the sample for other PICs in de same range unless the PIC in that range - or a PIC in another range - behaves different. The later is also needed to show that the library also works for that variation of PICs.

The result would be that you have less - but all tested - samples to maintain and you could see the trees through the forest better.

Kind regards,

Rob


Verzonden: zondag 17 augustus 2025 15:06

Hans van Veldhuizen

unread,
Aug 17, 2025, 11:09:02 AMAug 17
to jallist
Hi Rob,
My idea would be: Create a second TESTED SAMPLES folder and put the samples you've tested there. Ask anyone who has practically tested a sample from the list to let you know. Give them credit by mentioning it in the header and put it in the new folder, and clear out the old one.
Regards,
Hans

Op zondag 17 augustus 2025 om 16:34:44 UTC+2 schreef Rob CJ:
Reply all
Reply to author
Forward
0 new messages