#include "pwm.h"
void TIM13_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_LED_Struct;
TIM_TimeBaseInitTypeDef TIM13_PWM_InitStruct;
TIM_OCInitTypeDef TIM13_OC1InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM13);
GPIO_LED_Struct.GPIO_Mode = GPIO_Mode_AF;
GPIO_LED_Struct.GPIO_Pin = GPIO_Pin_9;
GPIO_LED_Struct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_LED_Struct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_LED_Struct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOF,&GPIO_LED_Struct);
TIM13_PWM_InitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM13_PWM_InitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM13_PWM_InitStruct.TIM_Period=arr;
TIM13_PWM_InitStruct.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM13,&TIM13_PWM_InitStruct);
TIM13_OC1InitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM13_OC1InitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM13_OC1InitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC1Init(TIM13,&TIM13_OC1InitStruct);
TIM_Cmd(TIM13,ENABLE);
TIM_OC1PreloadConfig(TIM13,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM13,ENABLE);
}
void TIM14_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_LED_Struct;
TIM_TimeBaseInitTypeDef TIM14_PWM_InitStruct;
TIM_OCInitTypeDef TIM14_OC1InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
GPIO_PinAFConfig(GPIOF,GPIO_PinSource10,GPIO_AF_TIM14);
GPIO_LED_Struct.GPIO_Mode = GPIO_Mode_AF;
GPIO_LED_Struct.GPIO_Pin = GPIO_Pin_10;
GPIO_LED_Struct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_LED_Struct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_LED_Struct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOF,&GPIO_LED_Struct);
TIM14_PWM_InitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM14_PWM_InitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM14_PWM_InitStruct.TIM_Period=arr;
TIM14_PWM_InitStruct.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM14,&TIM14_PWM_InitStruct);
TIM14_OC1InitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM14_OC1InitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM14_OC1InitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC1Init(TIM14,&TIM14_OC1InitStruct);
TIM_Cmd(TIM14,ENABLE);
TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM14,ENABLE);
}
#include "led.h"
#include "delay.h"
#include "pwm.h"
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init(168);
LED_Init ();
TIM13_PWM_Init(500-1,84-1);
TIM14_PWM_Init(500-1,84-1);
while(1)
{
delay_ms(10);
if(dir)
led0pwmval++;
else
led0pwmval--;
if(led0pwmval>300)
dir=0;
else if(led0pwmval==0)
dir=1;
TIM_SetCompare1(TIM13,led0pwmval);
TIM_SetCompare1(TIM14,led0pwmval);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>