高中生
最后登录1970-1-1
在线时间 小时
注册时间2015-10-21
|
如上图所示,我把PB0端口复用于TIM3_CH3通道产生一个PWM波形,那么现在我的PB0端口还可以用做普通的IO端口输出高低电平了吗?在野火的MINI开发板上PB0为一个小灯,我通过改变PWM的占空比,想做一个呼吸灯的实验,但是实验的结果却是PB0一直亮,并没有出现呼吸灯的情况,调了很久真是不知道哪里错了,还望高手解答。程序如下int main(void)
{
int dir=1,led0pwmval=0;
//GPIO_INIT_Confg();
PWM_INIT_Confg (led0pwmval);
while(1)
{
Delay_ms(10);
if(dir)
led0pwmval++;
else
led0pwmval--;
if(led0pwmval>999)
dir=0;
if(led0pwmval==0)
dir=1;
TIM_SetCompare3(TIM3,led0pwmval);
}
}
void PWM_INIT_Confg (int CCR_VAL)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=999;
TIM_TimeBaseInitStruct.TIM_Prescaler=0;
//TIM_TimeBaseInitStruct.TIM_RepetitionCounter=
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse=CCR_VAL;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
|
-
|