#include "pbdata.h" void RCC_Configuration(void); void GPIO_Configuration(void); void TIM3_Configuration(void); int main(void) { u8 led_fx=1; u16 led_dt=0; RCC_Configuration(); //系统时钟初始化 GPIO_Configuration();//端口初始化 TIM3_Configuration(); while(1) { if(led_fx==1) led_dt++; else led_det--; if(led_dt==300) led_fx=0; if(led_det==0) led_fx=0; TIM_SetCompare2(TIM3, led_dt); } } void RCC_Configuration(void) { SystemInit (); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB1Periph_AFIO,ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //LED GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOB,&GPIO_InitStructure); } void TIM3_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //TIM3初始化 TIM_TimeBaseStruct.TIM_Period=900;//初值 TIM_TimeBaseStruct.TIM_Prescaler=0;//预分频 TIM_TimeBaseStruct.TIM_ClockDivision=0; TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct); //PWM初始化 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM3,&TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_Cmd(TIM3,ENABLE); } |
更多回帖