PWM frequency

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

PWM frequency

Postby mikenick42 » Sun Aug 16, 2015 8:57 pm

I have a Mega 2560 clone that seems to work fine with the ChibiOS demo. But when I run the PWM hal test, the light just blinks quickly. It looks like maybe the PWM frequency is set too low. Changing the values in pwmEnableChannel makes it blink faster or slower, but it's not the fade I would expect with PWM.
Is there some difference in the 2560 timers vs the 1280 timers that would account for this behavior?

Thanks,
Mike

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: PWM frequency

Postby utzig » Sun Aug 16, 2015 11:20 pm

Hi Mike,

Changing frequency is not supported in the driver AFAIK. I guess the AVR timers are quite limited in what you can really achieve because you need to configure a prescaler that support only a handfull of options. And I remember checking the width in enableChannel with an oscilloscope and it seems to be correct.

Cheers,
Fabio Utzig

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: PWM frequency

Postby mikenick42 » Mon Aug 17, 2015 3:18 am

Hi Fabio,

I went into pwm_lld.c and changed the frequency scalar to 256 instead of 1024. It seems to be working now.
When I set it to 8 the LED seemed to be at 100% no matter what the duty cycle was set to, thought it's possible I messed up and wasn't actually sending 010 to the register.

Thanks,
Mike

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: PWM frequency

Postby mikenick42 » Wed Aug 19, 2015 3:55 am

Saying PWM worked may have been a bit premature.
After noticing that I couldn't adjust the brightness of the LED.
What I've found is that calling:

Code: Select all

pwmStart(&PWMD1, &pwm1cfg);
seems to cause constant resets.
I pulled the PWM code from testhal into the RT-MEGA demo and replaced pwmTogglePad with a pwmEnableChannel and a simple counter, but it never makes it there.

With pwmStart commented out the test thread works and the LED is at full brightness whether I call pwmEnableChannel or not. If I uncomment pwmStart the LED stays at its initial brightness setting and I see:
*** Kernel: 3.0.0
*** ChibiOS/RT test suite
***
repeated endlessly in the serial window.

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: PWM frequency

Postby utzig » Wed Aug 19, 2015 11:27 am

The freerunning demo is probably using the same timer that the PWM driver (TIM1).

You can either change the PWM timer defines in mcuconf to:

Code: Select all

#define AVR_PWM_USE_TIM1                   FALSE
#define AVR_PWM_USE_TIM2                   FALSE
#define AVR_PWM_USE_TIM3                   TRUE
#define AVR_PWM_USE_TIM4                   FALSE
#define AVR_PWM_USE_TIM5                   FALSE


and use PWMD3, or more easily, enable periodic timer for the system timer (which uses TIM0) changing these three values in chconf.h:

Code: Select all

#define CH_CFG_ST_FREQUENCY                 1000
#define CH_CFG_ST_TIMEDELTA                 0
#define CH_CFG_TIME_QUANTUM                 20


Cheers,
Fabio Utzig

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: PWM frequency

Postby mikenick42 » Sat Aug 22, 2015 5:43 am

Hi Fabio,
I'm still having trouble.
I start with a clean copy of the PWM program from testhal and change:

Code: Select all

AVR_SERIAL_USE_USART0
to TRUE in mcuconf.h

Then I added:

Code: Select all

chnWrite(&SD1, (const uint8_t *) "Hello world\r\n", 13);
just above the while(1){}.

If I comment out pwmStart, I see "Hello world" printed one time as expected, but with pwmStart active, I see "Hello world repeated endlessly.

That is the only change I have made to the program.

Thanks,
Mike

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: PWM frequency

Postby utzig » Sat Aug 22, 2015 2:27 pm

Please upload the project directory somewhere and I'll review it.

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: PWM frequency

Postby mikenick42 » Sat Aug 22, 2015 5:21 pm

Thanks for the help Fabio.
I zipped my project folder and included the slightly modified board.h from os/hal/boards/ARDUINO_MEGA/
I have it set up for tickless mode right now so I can use the led that's on the board rather than dealing with breadboards etc.

Thanks,
Mike
Attachments
Mega.zip
Single thread with PWM
(14.69 KiB) Downloaded 221 times

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: PWM frequency

Postby utzig » Sat Aug 22, 2015 11:35 pm

Hi Mike,

That was really a bug in the PWM driver. What happened was that timer interrupts where enabled even without a callback. I did a simple check but a more thorough review should be done, maybe add checks to all interrupt handlers too. Anyway it should be working if you update to the last revision.

Also some small suggestions, set AVR_PWM_USE_TIM2 to FALSE in mcuconf.h (because you don't need it) and comment out the line:

Code: Select all

include $(CHIBIOS)/test/rt/test.mk


in your Makefile because for that will drop your flash usage quite a lot.

Cheers,
Fabio Utzig

mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: PWM frequency

Postby mikenick42 » Mon Aug 24, 2015 3:11 am

Hi Fabio,
Thanks for the fix and the tips.
I'll test out your changes as soon as they hit github.

Mike


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 15 guests