我需要使用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 找了很久找不到问题点。 |
【STM32F0开发日志/评测/笔记】+互补PWM波的产生
【STM32F030探索套件】序列之五 外部中断
【STM32F0开发日志---二】+ucosii.2.92移植在STM32F030
上传个STM32F0+5110+内部温度传感器的菜鸟实例
【STM32F030探索套件使用问题】STM32F030 SPI方式驱动ST7565LCD失败
求一份STM32F051 I2C驱动LCD 12864的例程
STM32F0 M0 向结构体赋值进入HardFault异常
STM32F0 ADC-DMA方式采集2路数据时出现问题
STM32F030C8T6,TIM16定时慢很多问题?
STM32F042无法调试
然后示波器观察波形
最好能对着寄存器看看,有没有使能PWM输出,直接用库有时候会很坑人的。
昨晚熬夜搞定了,确实是被库坑害了。对ST的库真是爱恨交加,害人的货。
你好,请问是什么原因?最后怎么解决的呢?我现在也是遇到这个问题怎么搞都没有输出。。。。。。
好久了,我也不记得改哪里了,要仔细核对一下寄存器。
我改用TIM15了,谢谢了
/**
******************************************************************************
* @file stm32f0xx_tim.c
* @author MCD Application Team
* @version V1.5.0
* @date 05-December-2014
.......*/
void GPIO_Configuration(void)
{//PB15
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_2);
}
void TIM1_Config(void)
{//TIM1_CH3N
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 4000;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 144;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;//关
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
注意CH1,CH1N;这样的区别,配置也会有所不同