设置PA7 作为 PWM输出
void TIM_IO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
}
设置Timer1 为PWM模式,使能,使能PWM输出
void TIM_PWM_Config(void)
{
/¼ÆËãÔ¤¶¨±íµÄÖµ£¬Ò²¾ÍÊǶàÉÙ¸öʱÖÓ¼ÆÊýΪһ¸öÖÜÆÚ/
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
/¼ÆËãCCR1 Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ50%ʱ/
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
/* TIM1 ʱÖÓʹÄÜ /
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/ Time ¶¨Ê±»ù´¡ÉèÖÃ*/
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* Time ¶¨Ê±ÉèÖÃΪÉÏÉýÑؼÆËãģʽ*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* ƵµÀ1£¬2£¬3£¬4µÄPWM ģʽÉèÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//ʹÄÜƵµÀ1ÅäÖÃ
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* TIM1 ¼ÆËãÆ÷ʹÄÜ*/
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Ö÷Êä³öʹÄÜ */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
设置PA7 作为 PWM输出
void TIM_IO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
}
设置Timer1 为PWM模式,使能,使能PWM输出
void TIM_PWM_Config(void)
{
/¼ÆËãÔ¤¶¨±íµÄÖµ£¬Ò²¾ÍÊǶàÉÙ¸öʱÖÓ¼ÆÊýΪһ¸öÖÜÆÚ/
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
/¼ÆËãCCR1 Ìøתֵ ÔÚÕ¼¿Õ±ÈΪ50%ʱ/
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
/* TIM1 ʱÖÓʹÄÜ /
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/ Time ¶¨Ê±»ù´¡ÉèÖÃ*/
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* Time ¶¨Ê±ÉèÖÃΪÉÏÉýÑؼÆËãģʽ*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* ƵµÀ1£¬2£¬3£¬4µÄPWM ģʽÉèÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//ʹÄÜƵµÀ1ÅäÖÃ
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* TIM1 ¼ÆËãÆ÷ʹÄÜ*/
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Ö÷Êä³öʹÄÜ */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
举报