完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
因为需要控制步进电机的转速,所以要实时改变PMW的频率。我用输出比较的方法输出一路PMW波形。具体程序如下 : tiM4配置: void TIM4_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8| GPIO_Pin_9|GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 29; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 240; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable); TIM_ARRPreloadConfig(TIM4, DISABLE); TIM_Cmd(TIM4, ENABLE); TIM_ITConfig(TIM4,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3| TIM_IT_CC4, ENABLE); } TIM4中断函数: void TIM4_IRQHandler(void) { u16 capture ; if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); capture = TIM_GetCapture1(TIM4); TIM4_Aim_Counter1 = (CmdCode[2] << 8) | CmdCode[3]; TIM4_Aim_Counter2 = (CmdCode[4] << 8) | CmdCode[5]; TIM_SetCompare1(TIM4,capture + TIM4_Aim_Counter1); TIM4_CH4_Counter++; if(TIM4_CH4_Counter >= TIM4_Aim_Counter2 ) { TIM_Cmd(TIM4, DISABLE); TIM4_CH4_Counter = 0; for(i = 0;i < 10;i++) { CmdCode[i] = 0; } } } } 程序如上面所示。我之前做过实验,通过改变TIM4_Aim_Counter1的来改变PMW的输出频率,并且成功的,波形的占空比都是50% 比如TIM4_Aim_Counter1的值为150,输出PMW频率为8KHz,TIM4_Aim_Counter1的值为300,输出PMW频率为4KHz.但是很多人说改变这个TIM4_Aim_Counter1只能改变占空比不能改频率。但是我这个怎么能改呢?如果我想实时的改变PMW的输出频率,改怎么改呢? 如下图所示。
|
|
相关推荐
3个回答
|
|
|
|
|
|
|
|
|
|
通过修改占空比的份数,就能改变频率吧
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1492 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1427 浏览 3 评论
3261 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1560 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1995 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-5 06:05 , Processed in 0.605406 second(s), Total 80, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号