用STM32的定时器控制步进电机怎么控制输出的脉冲个数
用STM32的定时器产生的PWM波控制步进电机,怎么控制输出的脉冲个数? PWM不好控制脉冲个数用输出比较模式 你要非要控制PWM的个数,那么就在定时其中开启一个中断,然后在中断里面计数。 中断处理,频率不要过高,中断里面事情尽量少做。 回复【2楼】tiancaigao7 天才杨威利
-----------------------------------------------------------------------
用了中断,但是还是关不了定时器,麻烦您帮我看看!
中断配置:
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
定时器配置:
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999;//自动装载数999 ARR 方波频率1M/(999+1)=1kHz 即1毫秒1个周期
TIM_TimeBaseStructure.TIM_Prescaler =71; //计数器时钟(72M/2)*2/(71+1)=1M
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //50%占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
中断函数:
void TIM3_IRQHandler(void)
{
count++;
}
主函数:
while (count>10)
{
TIM_Cmd(TIM3, DISABLE);
}
用逻辑分析仪看了,还是关不了,貌似count的值没变,好像没有进入中断啊! count有没有定义为全局变量? 回复【5楼】hpyanghua
-----------------------------------------------------------------------
是全局变量。在main.c里(void main(void)函数之外):vu16 count=0;
在...._it.c里:extern vu16 count;变量定义应该没问题吧。 回复【4楼】gylsxcn山清水秀
-----------------------------------------------------------------------
关于关定时器的问题,我前断时间弄过。你把TIMx_CR1寄存器的最后一位CED设置为0就可以禁止定时器工作啦。你去查一下手册。 回复【4楼】gylsxcn山清水秀
-----------------------------------------------------------------------
写错了,最后一位应该是CEN.你试试看行不行. 这样就可以。
void TIM3_IRQHandler(void)
{
count++;
if(count>20000)
{
TIM_Cmd(TIM3, DISABLE);
}
}
前面的变量定义好像也没有问题。
但是count的值为 10000时才输出4个脉冲,20000时输出8个脉冲这中断怎么理解? 我要学习 我马上要用 都是牛人啊 更新中断时计数,但是用时太长。我用以下程序试了下能控制脉冲数,
extern uint32_t tmp;
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
tmp++;
if(tmp>20)
{
tmp=0;
TIM_Cmd(TIM3, DISABLE);
}
}
}
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
uint16_t CCR1_Val = 1250;
uint16_t CCR2_Val = 3750;
uint16_t CCR3_Val = 500;
uint16_t CCR4_Val = 500;
uint16_t PrescalerValue = 0;
uint32_t tmp=0;
uint16_t tmp1;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/**
* @briefMain program
* @paramNone
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* System Clocks Configuration */
RCC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
NVIC_Configuration();
/* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
clock at 24 MHz the Prescaler is computed as following:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
and Connectivity line devices and to 24 MHz for Low-Density Value line and
Medium-Density Value line devices
The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
= 24 MHz / 666 = 36 KHz
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 10000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 5000;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ITConfig(TIM3,TIM_IT_Update ,ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
while (1)
{
tmp1 = TIM3->CNT;
}
}
/**
* @briefConfigures the different system clocks.
* @paramNone
* @retval None
*/
void RCC_Configuration(void)
{
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
}
/**
* @briefConfigures the NVIC.
* @paramNone
* @retval None
*/
void NVIC_Configuration(void)
{
/* TIM3 clock enable */
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @briefConfigure the TIM3 Ouput Channels.
* @paramNone
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#ifdef STM32F10X_CL
/*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
#else
/* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
} 控制输出的脉冲个数,STM32的某些定时器中本来就有“重复计数器”,如果脉冲个数不是过分多的话,可以使用这个寄存器哦,重复计数器中设置的脉冲个数自动减到0时才来中断。 推荐使用中断 回复【6楼】gylsxcn
-----------------------------------------------------------------------
应该将count定义为volatile型变量,即volatile vu16 count=0; extern volatile vu16 count=0;
若不定义为该类型的变量,即使你在中断程序中修改了count的值(尽管count是全局变量),
主函数中对while (count>10) 进行判断时,会取count的缓存值与10进行比较而不是真实值。
而定义为volatile后,程序每次都会去读count所在存储单元,这样就会得到count的实时值。 可以考虑自己写一个函数,函数功能就是在GPIO口上产生一个脉冲,计数,控制开关什么的都好实现。定时器工作在自动重装模式,若需要控制占空比变化就修改定时器初值。也挺好的。 microEC 发表于 2011-10-27 21:32 static/image/common/back.gif
更新中断时计数,但是用时太长。我用以下程序试了下能控制脉冲数,
extern uint32_t tmp;
给力 给力哈 学习学习、、、、、{:lol:} 我也正在研究这个问题,一直没成功啊。惭愧得很 使用另外一个定时器计数PWM波个数,PWM波频率高时推荐使用ETR外部计数模式,频率不高时,使用外部捕获中断就可以 学习了!你最后成功了吗? 有人想用STC控制PWM脉冲个数,建议用计数器数吧 主函数:
while (count>10)
{
TIM_Cmd(TIM3, DISABLE);
}
while 循环错了 也在考虑这个问题,怎么控制输出脉冲个数
页:
[1]