我需要使用
STM32f030 的
tiM1 的CH2和CH3通道输出互补PWM信号,实际测试发现PB14(TIM1_CH2N)PB15(TIM1_CH3N)无法输出,而TIM1_CH2和TIM1_CH3能正常输出。一直找不到原因。TIM1初始化程序如下:
void Time1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//TIM_PeriodÉèÖÃÁËÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ¡£ËüµÄÈ¡Öµ±ØÐëÔÚ0x0000ºÍ0xFFFFÖ®¼ä
TIM_TimeBaseStructure.TIM_Period = TimerPeriod; //ÉèÖö¨Ê±Æ÷Ô¤·ÖƵֵΪ0
//TIM_PrescalerÉèÖÃÁËÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ¡£ËüµÄÈ¡Öµ±ØÐëÔÚ0x0000ºÍ0xFFFFÖ®¼ä
TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖö¨Ê±Æ÷ʱÖÓ·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ʱÖÓ·Ö¸î
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÉèÖö¨Ê±Æ÷ÏòÉϼÆÊý
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //ÉèÖö¨Ê±Æ÷ÖØ×°ÔØÖµ
TIM_TimeBaseInit(TIM1, & TIM_TimeBaseStructure); //ÅäÖö¨Ê±Æ÷1
/* ƵµÀ1£¬2£¬3£¬4µÄPWM ģʽÉèÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1ģʽ
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Êä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //»¥²¹Êä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔΪL
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //»¥²¹Êä³ö¼«ÐÔΪH
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //¿ÕÏÐ״̬ÏÂÊä³ö¼«ÐÔReset
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //¿ÕÏÐ״̬»¥²¹ÏÂÊä³ö¼«ÐÔ
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //ʹÄÜƵµÀ2ÅäÖÃÖÜÆÚ
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; //ʹÄÜƵµÀ3ÅäÖÃÖÜÆÚ
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
//TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜͨµÀ2ԤװÔØ
//TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜͨµÀ3ԤװÔØ
//TIM_ARRPreloadConfig(TIM1, ENABLE);
/* PWM ËÀÇøʱ¼äÉèÖà */
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //Enable Running State
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //Enable Idle State
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //Set the lock level
TIM_BDTRInitStructure.TIM_DeadTime = 20; //ËÀÇøʱ¼ä:0x00-0xff
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //Enable the Break function
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //ʹÄÜ×Ô¶¯Êä³ö.
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update); //Çå³ýÖжϱêÖ¾·ÀÖ¹ÅäÖÃÍê³É´ò¿ªÖжÏʱÁ¢¼´²úÉúÖжÏ
//TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE ); //ʹÄܶ¨Ê±Æ÷1¸üÐÂÖжÏ
TIM_Cmd(TIM1, ENABLE); //ʹÄܶ¨Ê±Æ÷1¹¦ÄÜ
TIM_CtrlPWMOutputs(TIM1, ENABLE); //TIM1 Ö÷Êä³öʹÄÜ
}
IO初始化
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = TxD2 | RxD2 | PWM1L | PWM2L; //¶Ë¿ÚTxD2 -> PA2 / RxD2 -> PA3 / PWM1L -> PA9 / PWM2L -> PA10
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; //¸´ÓÃ
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //×î´óÊä³öËÙ¶È50MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //¸´Óù¦ÄÜÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ; //ÉÏÀ
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); //USART2_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); //USART2_RX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); //TIM1_CH2
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2); //TIM1_CH3
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = TxD1 | RxD1 | PWM1H | PWM2H; //¶Ë¿ÚTxD1 -> PB6 / RxD1 -> PB7 / PWM1H -> PB14 / PWM2H -> PB15
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; //¸´ÓÃ
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //×î´óÊä³öËÙ¶È50MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //¸´Óù¦ÄÜÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ; //
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0); //USART1_TX -> PB6
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0); //USART1_RX -> PB7
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_2); //TIM1_CH2N
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_2); //TIM1_CH3N
找了很久找不到问题点。
我改用TIM15了,谢谢了
一周热门 更多>