#include "bsp_spwm_output.h" uint16_t CCR1_Val ; uint16_t CCR2_Val = 1500 ; uint16_t CCR3_Val = 900; static void TIM1_Mode_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* GPIOA clocks enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 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_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); /* Connect TIM pins to AF6 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_6); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_6); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Period =2999; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //ÉèÖÃÔ¤·ÖƵ£º²»Ô¤·ÖƵ£¬¼´Îª72MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ(ÕâÀïÓò»µ½) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; //ÏòÉϼÆÊýģʽ TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /*PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; TIM_OC2Init(TIM1, &TIM_OCInitStructure); //ʹÄÜͨµÀ1 TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM1, &TIM_OCInitStructure); //ʹÄÜͨµÀ4 TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_UpdateRequestConfig( TIM1, TIM_UpdateSource_Regular); TIM_ITConfig(TIM1,TIM_IT_Update | TIM_IT_Trigger,ENABLE);/* TIM3 enable counter */ TIM_Cmd(TIM1, ENABLE); //ʹÄܶ¨Ê±Æ÷3 TIM_ClearFlag(TIM1, TIM_FLAG_Update); TIM_CtrlPWMOutputs(TIM1, ENABLE); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1ÖÐ¶Ï NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM1_SPWM_Init(void) { TIM1_Mode_Config(); NVIC_Configuration(); } // /*****/ |
另外 你到底是哪个STM32型号,注意启动文件的一致性;
评分
查看全部评分