[mw_shl_code=c,true]#include "led.h"
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
}
#include "pwm.h"
#include "led.h"
void pwm_init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
TIM_DeInit(TIM5);
//GPIO_PinRemapConfig(0,ENABLE);
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM5,&TIM_OCInitStrue);
TIM_TimeBaseInitStrue.TIM_Period=arr;
TIM_TimeBaseInitStrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStrue.TIM_ClockDivision=0;
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStrue);
TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_Cmd(TIM5,ENABLE);
}
#include "led.h"
#include "pwm.h"
#include "delay.h"
#include "sys.h"
int main(void)
{
u16 rare=1,make=0;
led_init();
delay_init();
pwm_init(899,0);
while (1)
{
delay_ms(10);
if (make==1) rare++;
else if (make==0) rare--;
if (rare==300) make=0;
if (rare==0) make=1;
TIM_SetCompare1(TIM5,rare);
}
}
[/mw_shl_code]
我就是把例程的代码改成了用TIM5定时器的CH1做PWM,PA0输出,我外接LED灯 没有任何反应
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>