初级会员

- 积分
- 166
- 金钱
- 166
- 注册时间
- 2016-4-2
- 在线时间
- 42 小时
|
5金钱
各位好,我想用PB5(TIM3 的CH2)输出pwm来驱动舵机。做出来的效果是,舵机在通电的一刻动了一下 之后就没反应了,后来我试着摇动一下杜邦线(vcc接的板上的5v,还有GND),舵机又动了一下,然后就不不动了,下面贴出代码,。望各位朋友指教。
#include "pwm.h"
void PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef PWM_GPIO_structure;
TIM_TimeBaseInitTypeDef PWM_TIME_struture;
TIM_OCInitTypeDef PWM_DATA_structure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
PWM_GPIO_structure.GPIO_Mode=GPIO_Mode_AF_PP;
PWM_GPIO_structure.GPIO_Pin=GPIO_Pin_5;
PWM_GPIO_structure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&PWM_GPIO_structure);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
PWM_TIME_struture.TIM_ClockDivision=TIM_CKD_DIV1;
PWM_TIME_struture.TIM_Period=arr;
PWM_TIME_struture.TIM_Prescaler=psc;
PWM_TIME_struture.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&PWM_TIME_struture);
PWM_DATA_structure.TIM_OCMode=TIM_OCMode_PWM2;
PWM_DATA_structure.TIM_OCPolarity=TIM_OCPolarity_High;
PWM_DATA_structure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC2Init(TIM3,&PWM_DATA_structure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE);
}
int main(void)
{
u16 data;
delay_init();
LED_Init();
Hongwai_Init();
PWM_Init(899,0);
while(1);
|
最佳答案
查看完整内容[请看2#楼]
我的代码
[mw_shl_code=applescript,true]/******************************************************************************
定义PWM输出频率,Period值统一定义成100
******************************************************************************/
#define Camera_PWM_Prescaler (uint16_t)72 //分频prs:72000 000 / 72 = 1MHz
#define Camera_PWM_Period ...
|