cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0308 TIM15 PWM

vendor
Associate II
Posted on January 11, 2017 at 22:26

I am really stuck on this portion of code..

Attempting to run to 12v DC motors from STM32F0308  PINS PA2 = Channel 1 PA3 = Channel 2 of TIMER TIM15.

I want to be able to do the following for each output - TURN on or OFF, and change PWM timing (OK if timing is the same for each channel)

PA2 & PA3 are each tied low with 4.7k OHM resistor to ground. 

Below is the code for the PA2.  TIM15 CHANNEL1

I 'think' this has something do do with TIMER15 the provided driver indicates both AF0 and AF3. 

Additionally, the reference manual makes note of special circumstances for TIMER15.. (I can't find any example code for this timer) I know the easier answer is to move to a different timer/ pins.. but that is a separate issue....

void MotorPWM_Init() { // This is for PA2 TIM15 Channel 1

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  TIM_OCInitTypeDef TIM_OCInitStructure;

  uint16_t TimerPeriod = 0;

  uint16_t ChannelPulse = 0;

  GPIO_InitTypeDef GPIO_InitStructure;

  / / GPIOA Clocks Enable

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 

// GPIO Configuration Channel as alternate function

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;// Works if OUT not AF !!

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOA, & GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_3); // Have tried AF0.. AF3

// TIM15 Clock ENABLE

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);

// TIM15 Configuration

// Generate PWM Signals with specified Duty Cycles

// TIM15 input clock is set to APB2 clock

TimerPeriod = (SystemCoreClock/ 17570) - 1;

// Formula for Duty Cycle

// Duty Cycle = (uint16_t) (((uint32_t) [DUTYCYCLE_VALUE (0-to 1000)]

// (TimerPeriod -1))/1000)

// Note Value is OFF time, not ON time... eg, 125 is off 125 of 1000 cycles

// DUTY CYCLE is never utilized in code or init.. ?? 

ChannelPulse = (uint16_t) (((uint32_t) TESTVALUE * (TimerPeriod -1))/1000);

  // NOTE TESTVALUE toggles between values of  0 and 1000. 

// TIM15 clock enable //

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);

// Time Base Configuration

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = TimerPeriod;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);

// Channel 1 Configuration in PWM Mode

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

TIM_OCInitStructure.TIM_Pulse = ChannelPulse;

TIM_OC1Init(TIM15, &TIM_OCInitStructure);

// TIM15 Counter Enable

TIM_Cmd(TIM15, ENABLE);

// TIM Main Output Enable

TIM_CtrlPWMOutputs(TIM15, ENABLE);

}
9 REPLIES 9
Oliver Beirne
Senior
Posted on January 12, 2017 at 06:08

Hello 

Lesueur.Rick

 

I have moved your question to the

https://community.st.com/community/stm32-community/stm32-forum?sr=search&ampsearchId=35518e8a-c586-4da6-b7a2-fb415b84622d&ampsearchIndex=0

where someone should be able to help you.

Thanks

Oli

Posted on January 12, 2017 at 09:22

What exactly is your chip's marking, and what is the value read out from DBGMCU_IDCODE  (at 0x40015800) ?

JW

Posted on January 12, 2017 at 13:51

JW,

Chip marking STM32F030R8T6  GH24X 9U  CHN GH 409

DBGMCU_IDCODE (@ 0X40015800) = 40 64

rick43065

Posted on January 12, 2017 at 13:54

Update, 

DBGMCU_IDCODE (@ 0X40015800) = 40 64 00 20

rick43065

vendor
Associate II
Posted on January 12, 2017 at 14:55

I read the memory address IAR View memory. I am unable to obtain the information as you suggested although using ST Link with IAR..  (It might be because this is a Discovery board?) 

No way to add screen shot.. I attempted to send an email but the address didn't work. 

Reading the memory is as follows:

 40015800  40 64 00 20 00 00 00 00 00 00 00 00 00 00 00 00

rick43065

vendor
Associate II
Posted on January 12, 2017 at 15:23

Also I noticed a typo.. it has been corrected but still not operating..

ORIGINAL  

TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);

Was changed to TIM15 (Problem when using example code)

In reviewing the code, I am not sure where TIM15, Channel 1 is declared? Perhaps using the PWM negates the need to specify a TIMER channel? I PA2 is TIM15, Channel 1 according to the datasheet. 

Additionally to avoid confusion or ambiguity I have put in values, (not variables) for timer parameters:

TIM_TimeBaseStructure.TIM_Period = 0x4000; // 16384 TimerPeriod;

TIM_OCInitStructure.TIM_Pulse        = 0x2000; // ChannelPulse;

This should provide 50% Duty cycle 

rick43065

Posted on January 12, 2017 at 14:30

rick43065 wrote:

DBGMCU_IDCODE (@ 0X40015800) = 40 64 00 20

How did you read it out? It's lower 16 bits are supposed to be 0x440 or 0x442 or 0x444 i.e. bytewise it's 0x4X 0x04

If you use ST-Link (on a Nucleo or a Disco, or standalone), please try to use the ST-Link Utility, after 'connect: it displays the ID.

JW

vendor
Associate II
Posted on January 12, 2017 at 15:43

I figured it out..

The example code (provided with IAR examples) listed both AF0 and AF3 for Timer15  As Alternate function for TIMER15 on PA2. !! 

When I corrected the TIM14 error to TIM15 I then retried AF0, and it worked.

FYI on the datasheet the alternate functions are listed as: USART1TX, USART2TX, and TIM15 Channel 1,  (There are footnotes indicating that USART1TX is only available on the 0x4 and 0x6 devices.

Not sure why this is AF0 (not AF1) as USART2TX should be AF0 on 0x8 part... 

Datasheet revision document and revision Page 26; DocID024849 Rev 1  

rick43065

Posted on January 12, 2017 at 15:44

Is the target chip on the same DISCOVERY board than the debugger, or are you using a DISCOVERY to debug a custom board?

Try to read out the ID using the

http://www.st.com/en/embedded-software/stsw-link004.html

instead of IAR.

Also, read out and post the content of TIM15 registers after the setup.

[EDIT] OK I see you got it working now.

JW