Skip to main content
MM..1
Super User
August 14, 2024
Solved

Unknown PWM CH2 error STM32F030C8

  • August 14, 2024
  • 4 replies
  • 1310 views

Hi in custom design arrive problem , that seems as hard to solve. TIM3 CH1 and CH2 config:

 

 TIM_TimeBaseStructInit(&timer);
		timer.TIM_Prescaler = 1-1; //48Mhz/1/2000=24kHz
		timer.TIM_Period = 2000-1;
		timer.TIM_ClockDivision = 0;
		timer.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimeBaseInit(TIM3, &timer);

		TIM_OCStructInit(&timerPWM);
		
		timerPWM.TIM_OCMode = TIM_OCMode_PWM1;
		timerPWM.TIM_OutputState = TIM_OutputState_Enable;
		timerPWM.TIM_Pulse = 1000;
		timerPWM.TIM_OCPolarity = TIM_OCPolarity_High;
		TIM_OC1Init(TIM3, &timerPWM);
		
		TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
		
 TIM_OC2Init(TIM3, &timerPWM);

		TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

 TIM_Cmd(TIM3, ENABLE); /* TIM3 counter enable */
 TIM_CtrlPWMOutputs(TIM3, ENABLE); /* TIM3 Main Output Enable */
 
 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); 

 /* Enable the TIM3 Trigger and commutation interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure); 	 

 

 

and irq for speed up two phase sinewave from low to target speed.
All vars is volatile float except int16 deltaC.

 

 

void TIM3_IRQHandler(){
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); 	
if(angle >= 360.0f) angle-=360.0f;

temp = (powerL1+deltaL1)*deg(angle);	
TIM3->CCR1=floatToPWM(temp);

temp = (powerL1+deltaL1)*deg(angle+90.0f);	
TIM3->CCR2=floatToPWM(temp);
angle+=anglestep+anglespd;
periodcnt++;
//error correction float 
if((periodcnt%64)==0) angle-= 1.112620037940625e-7f;
if(deltaC==1) deltaL1=0.0f;
if(deltaC>0) deltaC--;
}

 

 

other irq on prio 1 handle anglestep speedup values. But issue is with line

 

 

temp = (powerL1+deltaL1)*deg(angle+90.0f);

 

 

on channel 2 sinewave shifted 90 deg. Code works ok, but on CH2 random jumps in signal and signal isnt shifted copy of perfect CH1. But only in speedup time = anglestep is changed repeatly. After speed is ok anglestep leave constant and CH1 CH2 works ok. Too if removed +90 both channels work equal. deg is in real sint func.

Have somebody any idea pls?

This topic has been closed for replies.
Best answer by MM..1

Ofcourse i test subtract problem not solved, and header sint say supported -5400 to +5400 degree...

Mystery solved. My fault, speed up routine shift phase in max area of CH1 amplitude, and this result to invisible change on CH1 , but on CH2 +90 deg result to big jumps. Then no issue here, thanks.

4 replies

TDK
August 14, 2024

Show the floatToPWM function. Does it handle angles over 360 appropriately?

Might as well show the deg function while you're at it.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MM..1
MM..1Author
Super User
August 14, 2024

floatToPWM do convert +- 1.0f to 0-2000 PWM

deg is sint from STM32. Про синус / Хабр (habr.com)

But project is in KEIL , then maybe some float glitch...

TDK
August 14, 2024

Problem is deg/sint doesn't handle angles over 360. Need to check angle + 90 and subtract 360 if necessary before calling deg/sint.

No chance it's a float glitch.

"If you feel a post has answered your question, please click ""Accept as Solution""."