cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030x PWM output onto PB.6 TIM16_CH1N (AF2) ??? how???

walworc
Associate II
Posted on December 27, 2015 at 08:54

Hello,

Unable to enable PWM on PB.6 using TIM16 (TIM16_CH1N) for STM32F0 STM32F030x (File also attached) 1. Table DocID024849 Rev 2 (STM32F030x) shows AF2 column TIM16_CH1N functionality; 2. ''Normal'' methods for enabling PWM on TIM16_CH1N do not seem to work. (I'm sure I have some funky error in the code, please help.) 3. I want to enable a 50/50 PWM; I expect to measure 3.3v/2 on PB6. 4. I've tried a bunch of different scenarios to try to enable the pin. Code is below and attahed. 5. Please advise any suggestions to fix. 6. Question: what is good ST document to refer to to better understand the TIM16_CH1N functionality? 7. Is it correct to ASSUME that the N in TIM16_CH1N is NEGATIVE polarity ??? (If not what does N mean?) Thanks in advance, -Chris =====

/****************************************************/<
br
>/****************************************************/<
br
>void TIM16_RCC_Configuration(void)<
br
>{<
br
> RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);<
br
> RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);<
br
>}<
br
><
br
>/****************************************************/<
br
>/****************************************************/<
br
>void TIM16_GPIO_Configuration(void)<
br
>{<
br
> GPIO_InitTypeDef GPIO_InitStructure;<
br
><
br
> // PB.06 TIM16_CH1N<
br
> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;<
br
> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;<
br
> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;<
br
> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;<
br
> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;<
br
> GPIO_Init(GPIOB, &GPIO_InitStructure);<
br
><
br
> GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2);<
br
><
br
>}<
br
><
br
>/****************************************************/<
br
>/****************************************************/<
br
>void TIM16_Configuration(void)<
br
>{<
br
> TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;<
br
> TIM_OCInitTypeDef TIM_OCInitStructure;<
br
> NVIC_InitTypeDef NVIC_InitStructure;<
br
><
br
> // Set up TIM4_CH4 to be a 1 Hz time base (1PPS)<
br
> TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);<
br
><
br
> TIM_TimeBaseStructure.TIM_Prescaler = 24000 - 1; // 24 MHz / 24000 = 1 KHz<
br
> TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 KHz / 1000 = 1 Hz<
br
> TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;<
br
> TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;<
br
> TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);<
br
><
br
> // Channel 4 configuration = PB.09 TIM4_CH4<
br
> TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;<
br
> //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;<
br
> //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;<
br
> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;<
br
> TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period + 1) / 2; // 50% Duty<
br
> TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;<
br
><
br
> //TIM_OC4Init(TIM16, &TIM_OCInitStructure);<
br
> TIM_OC1Init(TIM16, &TIM_OCInitStructure);<
br
> // turning on TIM4 and PWM outputs<
br
> TIM_Cmd(TIM16, ENABLE);<
br
> TIM_CtrlPWMOutputs(TIM16, ENABLE);<
br
><
br
> //NVIC_InitStructure.NVIC_IRQChannel = TIM16_IRQn;<
br
> //NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;<
br
> //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;<
br
> //NVIC_Init(&NVIC_InitStructure);<
br
><
br
><
br
> //TIM_GenerateEvent(TIM16, TIM_EventSource_Update);<
br
>}<
br
><
br
>/****************************************************/<
br
>/****************************************************/<
br
>void TIM16_InitializePWMChannel(void)<
br
>{<
br
> int i,j;<
br
><
br
> TIM16_RCC_Configuration();<
br
><
br
> TIM16_GPIO_Configuration();<
br
><
br
> TIM16_Configuration(); // 1Hz Output<
br
><
br
>}<
br
>

#tim16-tim16_ch1n-pwm-stm32f030x
5 REPLIES 5
re.wolff9
Senior
Posted on December 27, 2015 at 13:13

The -N versions of the channels are meant to drive the ''other'' side of a push-pull half-H-bridge. You would think that it is simple to be able to enable none, one, the other or both with two hardware bits. But IIRC, the implementation in the hardware is a bit different. The second channel, IIRC cannot be enabled without the first. Either because they decided that this would never be useful (just use the normal output), or because they have a hardware reason for it. 

So I would suggest that you try to first get the normal channel working. That will eliminate the ''might not work without the normal channel'' possibility.  

Posted on December 27, 2015 at 14:19

Complimentary output.

Enable the N side of the channel?

/**

  * @brief  Fills each TIM_OCInitStruct member with its default value.

  * @param  TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure which will

  *         be initialized.

  * @retval None

  */

void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)

{

  /* Set the default configuration */

  TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;

  TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;

  TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;

 

  TIM_OCInitStruct->TIM_Pulse = 0x0000000;

  TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCNPolarity_High;

 

  TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;

 

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
walworc
Associate II
Posted on December 27, 2015 at 18:29

Hi, Roger,

Thanks for your reply.  Regarding the PB.6/TIM16_CH1N; Thanks.  Complimentary.  I understand.  In essensce, I think that you are telling me to 1: Enable the TIM16; 2: perform gpio PB.6 output waveform via software. 

Do I understand you correctly?

Thanks,

-Chris

walworc
Associate II
Posted on December 27, 2015 at 18:32

Thanks, Clive,

I'll take a look at what you state.

-Chris

re.wolff9
Senior
Posted on December 27, 2015 at 22:06

No, I did not intend to say that you should copy the pin state in software. Your code in the opening post is mangled. And I don't have much experience with the HAL you are using. So as a general debuggin hint I was suggesting that even if you can't use the pin in your final application, you can try to get the normal pin working first. Once/if that works, you know the timer is working and only enabling the OCN pin is holding you back. It seems Clive spotted your problem before you got around to trying my suggestion. :)