hi,
I'm try to build a wav player, wav data(PCM) is 8 bit unsigned 8000 samples per second, I need to set the PWM frequency to 31.25kHz (I known that can be set in PWMConfig) , start PWM driver, start a 8Khz timer (since the wav is 8000 samples rate), change the PWM duty cycle with PCM data in the timer's "interrupt callback".
my questions is:
1. which API can dynamic change the PWM duty cycle? pwmChangePeriod() seem change the period only?
2. any example about timer?
thanks in advance.
how can I dynamic change the PWM duty cycle in a Timer?
Moderators: RoccoMarco, lbednarz, tfAteba
Re: how can I dynamic change the PWM duty cycle in a Timer?
pwmEnableChannel sets the duty cycle of the timer. It has to be called repeatedly
Alternatively if you know the timer ID(TIMX) and the channel id (0,1,2,3), then you can also do TIMX->CCRY = value; to vary the duty cycle.
I have used PWM only without HAL. Here's an example of how to use the timer PWM so to generate a continuous PWM waveform at 31.25kHz.
P.S. Don't forget to change the pin mode of the particular pin to Alternate function, in this case PB8. Also about remaps.
Alternatively if you know the timer ID(TIMX) and the channel id (0,1,2,3), then you can also do TIMX->CCRY = value; to vary the duty cycle.
I have used PWM only without HAL. Here's an example of how to use the timer PWM so to generate a continuous PWM waveform at 31.25kHz.
Code: Select all
// Init the timer PWM without HAL
// 256 Steps @ 31.25 kHz
// Auto Load Configuration
// TIM4 / CH3 (PB8)
rccEnableTIM4(FALSE);
TIM4->ARR = 255;
// We prescale the clock to 8 MHz ( 8 MHz / 256 = 31.25 kHz)
TIM4->PSC = (STM32_TIMCLK1 / 8000000) - 1;
// Now configure OC3 in PWM Mode 1 OC3M[2:0] = 110
// Enable preload
TIM4->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE;
// Enable OC4 output, active high
TIM4->CCER = TIM_CCER_CC3E;
// Finally, enable auto reload and start the timer
TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
// Set initial duty cycle 0...255
TIM4->CCR3=255;P.S. Don't forget to change the pin mode of the particular pin to Alternate function, in this case PB8. Also about remaps.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: how can I dynamic change the PWM duty cycle in a Timer?
The function (it is a macro in reality) pwmEnableChannelI() is the correct choice.
If a channel is already enabled it just changes the duty cycle by writing that register.
Giovanni
If a channel is already enabled it just changes the duty cycle by writing that register.
Giovanni
Re: how can I dynamic change the PWM duty cycle in a Timer?
Hmm there is no state machine plot regarding the pwm channels in the documentation, right? I see nowhere which actually specifies if pwmEnableChannelI can be called on an enabled channel.
Maybe the documentation should be extended in this regard? (Or even add a pwmChangeChannelI or so?)
Maybe the documentation should be extended in this regard? (Or even add a pwmChangeChannelI or so?)
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: how can I dynamic change the PWM duty cycle in a Timer?
It is more matter of documentation, the function is meant to be called repeatedly but the introduction of a specific function is something I would consider, it is easier to understand.
Giovanni
Giovanni
Re: how can I dynamic change the PWM duty cycle in a Timer?
Abhishek wrote:pwmEnableChannel sets the duty cycle of the timer. It has to be called repeatedly
Alternatively if you know the timer ID(TIMX) and the channel id (0,1,2,3), then you can also do TIMX->CCRY = value; to vary the duty cycle.
I have used PWM only without HAL. Here's an example of how to use the timer PWM so to generate a continuous PWM waveform at 31.25kHz.Code: Select all
// Init the timer PWM without HAL
// 256 Steps @ 31.25 kHz
// Auto Load Configuration
// TIM4 / CH3 (PB8)
rccEnableTIM4(FALSE);
TIM4->ARR = 255;
// We prescale the clock to 8 MHz ( 8 MHz / 256 = 31.25 kHz)
TIM4->PSC = (STM32_TIMCLK1 / 8000000) - 1;
// Now configure OC3 in PWM Mode 1 OC3M[2:0] = 110
// Enable preload
TIM4->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE;
// Enable OC4 output, active high
TIM4->CCER = TIM_CCER_CC3E;
// Finally, enable auto reload and start the timer
TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
// Set initial duty cycle 0...255
TIM4->CCR3=255;
P.S. Don't forget to change the pin mode of the particular pin to Alternate function, in this case PB8. Also about remaps.
thank you!
more question:
virtual timer can generate 8KHz interrupt? in my case, I'm not sure the timer precise enough,
since chVTSetI() always need to called in timer handler for loop, i'm not sure chVTSetI() fast enough
Code: Select all
// in order to generate 8KHz interrupt, virtual timer need to set interval to TIMER_INTVAL
// 1 sec / (1 sec / mcu freq + virtual timer interval) = 8KHz samples rate = 125 usec
#define TIMER_INTVAL 125
static uint8_t g_pcm_data[PCM_SIZE];
static uint16_t g_pcm_data_index = 0;
void wav_timer_handler(void *arg)
{
// change PWM duty cycle
pwmEnableChannelI(pwmp,
mychannel,
g_pcm_data[g_pcm_data_index ++]);
chVTSetI(&vt, US2ST(TIMER_INTVAL), wav_timer_handler, NULL);
}
void wav_play() {
// start PWM with 31.25KHz freq, 1s period
pwmStart(...);
// start virtual timer with 125usec interval, for 8KHz interrupt
chVTSetI(&vt, US2ST(TIMER_INTVAL), wav_timer_handler, NULL);
}
Re: how can I dynamic change the PWM duty cycle in a Timer?
Typically your system tick frequency is 1kHz - so virtual timers are not a good idea. Use the hardware timer.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: how can I dynamic change the PWM duty cycle in a Timer?
The GPT driver is what you need if your application requires high frequency interrupts.
Giovanni
Giovanni