初中生
最后登录1970-1-1
在线时间 小时
注册时间2014-6-5
|
想用PA3输出PWM,用TIM5为时基。但怎么都没有波形出来,求助。
程序如下:
static void Pump_IO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void TIM5_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频:不预分频,即为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
}
void PumpTest(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 125; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM5, &TIM_OCInitStructure); //使能通道4
TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能定时器5 重载寄存器
TIM_ARRPreloadConfig(TIM5, ENABLE); // 使能TIM5重载寄存器ARR
TIM_Cmd(TIM5, ENABLE); //使能定时器5
}
int main(void)
{
...
Pump_IO();
TIM5_Mode_Config();
PumpTest();
...
while(1)
{
}
}
|
|