关于STM32输出精确PWM脉冲数的方法
时间:10-02
整理:3721RD
点击:
精确控制脉冲个数;
1. 用DMA来控制timer
2. 用另一个timer在内部用slave模式计数;
用到了Timer Master Slave中的Gate模式
比如TIM1输出PWM, 频率为F
可以用TIM2通过Gate来控制TIM1的输出
将TIM2预分频设为1/(F*2),则TIM2的Period 就是 脉冲个数*2 - 1
/* 1 2 3 4 5 6 7 8 9
__ __ __ __ __ __ __ __ __
| | | | | | | | | | | | | | | | | |
TIM1: ___| |__| |__| |__| |__| |__| |__| |__| |__| |_
->| Period1|<-
1) 2) 3) 4) 5) 6) 7) 8) 9) 10)11)12)13)14)15)16)17)
__________________________________________________
| |
TIM2: ___| |_
->| |<--- Pres2 = Period1/2
|<------------ Period2 = N*2-1 = 17 ------------>|
*/
软件:
IAR 4.42限制版
ST库 2.01
硬件:
万利199开发板 STM3210B-LK1
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
#define PWM_Period 120
int main(void)
{
u16 waveNumber = 10;
/* System Clocks Configuration */
RCC_Configuration();
/* Enable related peripheral clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
/* Config IO for related timers */
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Timer1 Channel 2, PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer3 Channel 4, PB1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Setup Timer3 channel 4, Timer3 is master timer
This timer is used to control the waveform count of timer1 */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = PWM_Period/2 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = waveNumber*2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = waveNumber*2 - 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
}
/* Setup timer1 channel 2, Timer1 is slave timer
This timer is used to output waveforms */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = PWM_Period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_Period/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
timer1 is work under gate control mode, and contrOLED by timer3
timer3's channel 4 is used as the control signal
*/
/* Enable timer's master/slave work mode */
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* timer3's channel 4 is used as the control signal */
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );
/* Check the master/slave is valid or not */
compile_assert((u16)GetInternalTrigger(TIM1,TIM3) != (u16)-1);
/* Config timer1's external clock */
TIM_ITRxExternalClockConfig(TIM1, GetInternalTrigger(TIM1,TIM3));
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);
/* Enable the slave tiemr*/
TIM_Cmd(TIM1,ENABLE);
//SetupAlltimers();
while(1){
/* Check whether the previous action is done or not */
if(!(TIM3->CR1 & 1)){
TIM1->CNT = 0; /* It would be very perfect if gate mode can
reset the slave timer automatically */
TIM3->ARR = waveNumber*2; /* Reload wave number*/
TIM3->CCR4 = waveNumber*2 - 1;
TIM3->CR1|=1; /* Re-enable the timer */
/* update waveform number */
waveNumber++;
if(waveNumber == 13){
waveNumber = 10;
}
}
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCMd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref ); 比较成功后,产生一次TRGO脉冲(即时是高电平),那就是说在正常情况下TRGO 都是高电平,只有在强制产生脉冲的时候,才有一瞬间的低电平,只有这个时候从TIM3才会停止PWM输出,又因为 TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated); 从TIME1的TRGI模式是 TIM_SlaveMode_Gated(当触发信号TRGI为高电平计数器时钟时能),所以即时主TIM1做一个周期后停止工作,TRGO能然是高电平,TIME3继续PWM输出!
这个我的理解,能否找出个解决的办法,可以使主TIM1停止后TIM3也立即停止,使用中断除外!
不会啊
TIM3作为主时钟,配置为OnePulse模式,OC4为PWM模式1
TRGO设置为OC4的Ref信号,OC4Ref在CNT<CCR4时输出高电平(TIM1工作),这之后输出低电平(TIM1停止)
由于TIM3是OnePulse,CNT=ARR之后TIM3就停了,不会再输出了,会保持低电平状态
不可能,我今天用示波器跟踪了,TIM1在TIM3停止工作后,还是输出PWM脉冲,后来我用了TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Enable); 来触发,当主TIM3计数器使能,产生TRGO信号触发从TIM3开始工作,当TIM3计数完毕后,TIM3失效,相应的TRGO应该也是低电压,才是得从TIME1停止工作
1. 用DMA来控制timer
2. 用另一个timer在内部用slave模式计数;
用到了Timer Master Slave中的Gate模式
比如TIM1输出PWM, 频率为F
可以用TIM2通过Gate来控制TIM1的输出
将TIM2预分频设为1/(F*2),则TIM2的Period 就是 脉冲个数*2 - 1
/* 1 2 3 4 5 6 7 8 9
__ __ __ __ __ __ __ __ __
| | | | | | | | | | | | | | | | | |
TIM1: ___| |__| |__| |__| |__| |__| |__| |__| |__| |_
->| Period1|<-
1) 2) 3) 4) 5) 6) 7) 8) 9) 10)11)12)13)14)15)16)17)
__________________________________________________
| |
TIM2: ___| |_
->| |<--- Pres2 = Period1/2
|<------------ Period2 = N*2-1 = 17 ------------>|
*/
软件:
IAR 4.42限制版
ST库 2.01
硬件:
万利199开发板 STM3210B-LK1
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
#define PWM_Period 120
int main(void)
{
u16 waveNumber = 10;
/* System Clocks Configuration */
RCC_Configuration();
/* Enable related peripheral clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
/* Config IO for related timers */
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Timer1 Channel 2, PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer3 Channel 4, PB1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Setup Timer3 channel 4, Timer3 is master timer
This timer is used to control the waveform count of timer1 */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = PWM_Period/2 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = waveNumber*2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = waveNumber*2 - 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
}
/* Setup timer1 channel 2, Timer1 is slave timer
This timer is used to output waveforms */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = PWM_Period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_Period/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
timer1 is work under gate control mode, and contrOLED by timer3
timer3's channel 4 is used as the control signal
*/
/* Enable timer's master/slave work mode */
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* timer3's channel 4 is used as the control signal */
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref );
/* Check the master/slave is valid or not */
compile_assert((u16)GetInternalTrigger(TIM1,TIM3) != (u16)-1);
/* Config timer1's external clock */
TIM_ITRxExternalClockConfig(TIM1, GetInternalTrigger(TIM1,TIM3));
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);
/* Enable the slave tiemr*/
TIM_Cmd(TIM1,ENABLE);
//SetupAlltimers();
while(1){
/* Check whether the previous action is done or not */
if(!(TIM3->CR1 & 1)){
TIM1->CNT = 0; /* It would be very perfect if gate mode can
reset the slave timer automatically */
TIM3->ARR = waveNumber*2; /* Reload wave number*/
TIM3->CCR4 = waveNumber*2 - 1;
TIM3->CR1|=1; /* Re-enable the timer */
/* update waveform number */
waveNumber++;
if(waveNumber == 13){
waveNumber = 10;
}
}
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCMd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC4Ref ); 比较成功后,产生一次TRGO脉冲(即时是高电平),那就是说在正常情况下TRGO 都是高电平,只有在强制产生脉冲的时候,才有一瞬间的低电平,只有这个时候从TIM3才会停止PWM输出,又因为 TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated); 从TIME1的TRGI模式是 TIM_SlaveMode_Gated(当触发信号TRGI为高电平计数器时钟时能),所以即时主TIM1做一个周期后停止工作,TRGO能然是高电平,TIME3继续PWM输出!
这个我的理解,能否找出个解决的办法,可以使主TIM1停止后TIM3也立即停止,使用中断除外!
不会啊
TIM3作为主时钟,配置为OnePulse模式,OC4为PWM模式1
TRGO设置为OC4的Ref信号,OC4Ref在CNT<CCR4时输出高电平(TIM1工作),这之后输出低电平(TIM1停止)
由于TIM3是OnePulse,CNT=ARR之后TIM3就停了,不会再输出了,会保持低电平状态
不可能,我今天用示波器跟踪了,TIM1在TIM3停止工作后,还是输出PWM脉冲,后来我用了TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Enable); 来触发,当主TIM3计数器使能,产生TRGO信号触发从TIM3开始工作,当TIM3计数完毕后,TIM3失效,相应的TRGO应该也是低电压,才是得从TIME1停止工作