okhxyyo 发表于 2019-7-10 04:51 具体问题具体分析,你可以把你遇到的具体问题情况发帖说明一下,坛子里的热心网友会帮忙解答的定时器1的配置 unsigned short int CCR1_Val = 0; unsigned short int CCR2_Val = 0; unsigned short int CCR3_Val = 0; volatile unsigned char HALL_Value = 0; //unsigned char HALL_flag =1; /******************************************************************************* * ????: Timer1_Init(); * ????: ???1??? * ????: void * ????: ? ********************************************************************************/ void PWM_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); // ??GPIOA/GPIOB?? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; //PWM???PA8,9,10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); //???GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //PWM???PA7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //???GPIO } /******************************************************************************* * ????: HALL_GPIO_Init(); * ????: ??????? * ????: void * ????: ? ********************************************************************************/ void HALL_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); // ??GPIOC?? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //PWM???PA7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //?????? GPIO_Init(GPIOB, &GPIO_InitStructure); } /******************************************************************************* * ????: Timer1_Init(); * ????: ???1??? * ????: void * ????: ? ********************************************************************************/ void Timer1_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; PWM_GPIO_Init(); //????? HALL_GPIO_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); // ?????1??? // ??20KHZ //??3?PWM??? CCR1_Val = 2799; CCR2_Val = 2799; CCR3_Val = 2799; //????????? TIM_TimeBaseInitStructure.TIM_Prescaler = 0; // 72MHZ TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // ?????? TIM_TimeBaseInitStructure.TIM_Period = 7199; // ???????????? TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; // ??????--??? TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure); //PWM????-CH1 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM?? TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //???? TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//??????? TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //????? TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //?????? TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //????????? TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //??????? TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; //?????????? TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = CCR2_Val ; TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = CCR3_Val ; TIM_OC3Init(TIM1, &TIM_OCInitStructure); //?????????,???????? TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;//????????? TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;//????????? TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //???? TIM_BDTRInitStructure.TIM_DeadTime = 0xAC; //????--2us TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //?????? TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//?????? TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;//?????? TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); TIM_CCPreloadControl(TIM1, ENABLE); //????? // TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE); //?????? TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_1,TIM_CCxN_Disable); TIM_CCxCmd(TIM1,TIM_Channel_2,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_2,TIM_CCxN_Disable); TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_3,TIM_CCxN_Disable); } /******************************************************************************* * ????: BLDC_Hall_Convet(); * ????: ??????????--T1 T3 T5/T4 T6 T2?? * ????: ? * ????: ? ********************************************************************************/ void BLDC_Hall_Convet(void) { // char PB0_Value,PB1_Value,PA1_Value; // PB0_Value = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0); // PB1_Value = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)*2; // PA1_Value = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)*4; // HALL_Value = PB0_Value+PB1_Value+PA1_Value; //HALL_Value =(unsigned char)((GPIO_ReadInputData(GPIOA) & 0x0007)); //????? HALL_Value=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)+(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)<<1)+(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)<<2);//???????MOS???,????? hallValueTemp=GPIO_ReadInputData(GPIOA)&0X7;???HA?HB?HC??????PA0?PA1?PA2? // if(HALL_flag ==1) //?? // { switch(HALL_Value) //??????,??CCER??????????? { case 0x05: //?? T3/T4 /* Channel2 configuration */ TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable); /* Channel1 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active); /* Channel3 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_3,TIM_CCxN_Enable); TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive); break; case 0x04: //T4 T5 /* Channel3 configuration */ TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable); /* Channel1 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active); /* Channel2 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_InActive); break; case 0x06: //T5 T6 /* Channel3 configuration */ TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable); /* Channel2 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_Active); /* Channel1 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_InActive); break; case 0x02: //T6 T1 /* Channel1 configuration */ TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable); /* Channel2 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_Active); /* Channel3 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive); break; case 0x03: // T1 T2 TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable); /* Channel3 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_Active); /* Channel2 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_2,TIM_CCxN_Enable); TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_InActive); break; case 0x01: //T2 T3 /* Channel2 configuration */ TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 ); TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable); TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable); /* Channel3 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_Active); /* Channel1 configuration */ TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable); TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_InActive); break; default: TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_1,TIM_CCxN_Disable); TIM_CCxCmd(TIM1,TIM_Channel_2,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_2,TIM_CCxN_Disable); TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Disable); TIM_CCxNCmd(TIM1,TIM_Channel_3,TIM_CCxN_Disable); break; } } 外部中断配置 void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ʹÄÜPORTBʱÖÓ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //ÉèÖø¡¿ÕÊäÈëģʽ GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOB0,1,2 } void EXTIX_Init(void) { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); HALL_GPIO_Init(); // °´¼ü¶Ë¿Ú³õʼ»¯ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //ʹÄܸ´Óù¦ÄÜʱÖÓ //GPIOA.5 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑØ´¥·¢PA5 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5); EXTI_InitStructure.EXTI_Line=EXTI_Line5; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ //GPIOA.6 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑØ´¥·¢PA6 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6); EXTI_InitStructure.EXTI_Line=EXTI_Line6; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ //GPIOA.7 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑØ´¥·¢ PA7 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7); EXTI_InitStructure.EXTI_Line=EXTI_Line7; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //ʹÄÜ»ô¶ûÐźÅËùÔÚµÄÍⲿÖжÏͨµÀ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2£¬ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //×ÓÓÅÏȼ¶0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ NVIC_Init(&NVIC_InitStructure); } //ÍⲿÖжϷþÎñ³ÌÐò void EXTI9_5_IRQHandler(void) { if((EXTI_GetITStatus(EXTI_Line5) != RESET)|(EXTI_GetITStatus(EXTI_Line6) != RESET)|(EXTI_GetITStatus(EXTI_Line7) != RESET)) { BLDC_Hall_Convet(); } EXTI_ClearITPendingBit(EXTI_Line5); //Çå³ýLINE5ÉϵÄÖжϱê־λ EXTI_ClearITPendingBit(EXTI_Line6); //Çå³ýLINE6ÉϵÄÖжϱê־λ EXTI_ClearITPendingBit(EXTI_Line7); //Çå³ýLINE7ÉϵÄÖжϱê־λ }
已有3人来访过
现在还没有留言