cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 Series Hardware Timers

Sid P
Associate II
Posted on July 26, 2017 at 14:19

Hi All,

A brief background:

I'm a

hardware

guy and new to all things ARM and STM32 MCUs. I've now jumped on board because ST has an agreement with Keil for STs Cortex M0/M0+ MCUs. I've programmed before but only 8-bit MCUs in Arduino and the CCS PIC C compiler from Custom Computer Services for the Microchip PIC line of MCUs.

For my very first project, I’ve begun work on phase angle control of the T1235 TRIAC also from ST. The processor I’m using is a Cortex M0 from the STM32F0 series. I’ve used STM32CubeMX (again for the first time ever) to generate the initialization code and have written some code in Keil uVision 5 (using this for the first time too). I’m using an opto-TRIAC in between the MCU and the T1235. I also have a zero-crossing detector (ZCD) that gives a pulse to the MCU every 10ms (50Hz mains).

So far, I’ve been able to trigger the TRIAC into a fully ON state and implement some crude dimming. Crude because the dimming does not work properly as the incandescent lamp flickers annoyingly even though.

The code implemented is something along the lines of this:

http://www.electronicsinfoline.com/pin/38328/2/

The ZCD signal is fed to the external (EXTI) interrupt. Now, to stabilize this and get smooth control with no flicker, I need to use one or perhaps more timers.

So far, I’ve referred to the MCU datasheet, errata sheet for device limitations and the following resources:

Google

UM1785 – HAL user manual

RM0360 – Reference manual

AN4776 – General purpose timer cookbook

PM0215 – Cortex M0 programming manual

Technical ref manual for the ARM-CortexM0

AN4013 - STM32 cross-series timer overview

A whole bunch of YouTube videos

A search for ready-made drivers

Now, I will admit that I’ve not read all the above pdfs in great detail but I simply have not been able to find or understand (whatever I have found) how the timer hardware within my processor functions. Furthermore, STs documentation leaves a lot to be desired.

I was thinking of implementing the following logic which needs a good understanding of how timers function. An interrupt starts the timer that’s being clocked and it gives a small (100usec) pulse to fire the TRIAC at very regular intervals.

My questions are:

Can someone please explain (or point me in the right direction) in detail how the STM32F0 timers work since I apparently can’t seem to follow what’s written in RM0360 and AN4776.

Are there any other resources / videos that will explain how to better use the HAL.

Thanks in advance

9 REPLIES 9
Tilen MAJERLE
ST Employee
Posted on July 26, 2017 at 15:59

Hello

sid

‌,

can you please tell us, what exactly is not clear on how timers work? What functionality on timer you would like to use? PWM, Input capture, output compare, etc.

Best regards,

Tilen

Posted on July 26, 2017 at 18:02

Hello!

Nice work!

The triac gate must be pulsed as soon as posible the Mains passing zero voltage(and power) to switch on. (be aware from capacitive and inductive loads)

The Timer configuration you need is One Pulse Mode triggered by the zero-crossing detection circuit.

and connected to a GPIO pin as PWM  output

0690X00000603vfQAA.jpg0690X00000603yXQAQ.jpg

you must have at least 1 as TIMx ARR (zero value will block the timer)

and 1 (least) at PWM Pulse (1 microsecond) This is the delay from trigger to start the pulse(phase) ~1 micro  (can not be zero.. Blocks the Output)

your ''pulse width''  is TIMx ->ARR - PWM Pulse

The max duty cucle at your pulse is ~10000   <10 millisecond

Depending your configuration you can change the polarity of triger(connected to ZCD)

Just to know  I dont have the hardware you own.

I tried this configuration at 32L152 and 32F4  three boards.

Posted on July 26, 2017 at 18:24

Ha ha that's just it, even i'm not so sure! Apologies for not asking narrower/exact questions!

Thank you for the prompt reply Tilen Majerle!

Since I'm used to the 8-bit PICs, my first thought was something like this:

1) Configure Interrupt

2) Interrupt (from ZCD) starts timer counting. Timer counts (either increments or decrements) from internal clock. It does not count the ZCD pulses.

3) Specifically for my application of firing a TRIAC on 50Hz AC mains, timer minimum value = 500usec / max value = 9500usec

4) Once set time (a value anywhere between the above mentioned limits) lapses, a 100usec or 200usec pulse fires the TRIAC. This is to take place periodically.

While the timers are 'chopping the AC mains' the time period needs to be stable and unchanging to avoid flicker.

Other than my above mentioned 4 step method, I'm open to other approaches btw!

Now, PWM doesn't help, neither does input capture. I'm unsure if output compare would help here? I'm unclear how that works.

As far as '

what exactly is not clear on how timers work

' - In the PIC uCs the documentation is easy to understand. But with ST, the documentation is not only unclear and very generic but the timers and their functionality is just too vast. Furthermore most acronyms ST uses are not very self explanatory (apologies again, don't mean to offend anyone). Even after reading the pages that explain all registers associated with the Timers, this too didn't help me make up my mind as how I'm supposed to configure the MCU to output a super stable one shot at regular intervals after an interrupt occurs on EXTI.

I can't seem to figure out how to appropriately configure the timers, their pre-scalers, the auto reload registers or capture compare registers, especially to achieve what i said in point 4 above. I hope I've been specific enough. Thanks.

Posted on July 26, 2017 at 22:27

What is your MCU exactly? to give you a better proposition!!

Post also other hardware details. . Does the ZCD gives positive pulse when pass from zero?

What polarity must be the pulse have to triger the optocoupled DIAC (consider to use an open colector output from MCU)

Posted on July 27, 2017 at 07:19

MCU is STM32F030F4

ZCD gives a falling edge output

A high pulse drives the opto-TRIAC that in-turn drives the main TRIAC

163948CGIL4
Posted on July 27, 2017 at 12:18

Thank you Vangelis for taking the trouble to help me and illustrate this with print screens. It's much appreciated!

I'll let you know how it turned out.

David.Yves
Associate II
Posted on July 28, 2017 at 03:24

Hi,Here is what you need for this:

  • you need to clock the timer to get a resolution of  1degree using the system clock and prescale register
  • you use the zero crossing to trigger the timer start (this will happened 2 times per cycle)
  • load the compare register with the count required for a phase delay ( 180- phase delay) this is where you want the trigger to happened this value will remain in the register until you change it .  
  • Load the ARR register with the max count value should be less than 180 (half a cycle) . you have to account for the frequency stability  ( Â± 1Hz max= 179, Â±2Hz max = 178 ...) other wise you could get flickering

set the following option in the Timer register (they vary with the Timer and channel used so refer to the reference manual): 

  1. set output active when  COUNTER = COMPARE REGISTER
  2. set the pulse polarity to your requirement
  3. transfer the holding register to the working register at the end of the cycle ( you can write new value anytime )
  4. One pulse mode
  5. start the timer from the trigger input

have fun

Yves

Posted on August 17, 2017 at 08:04

Hello Vangelis,

Sorry for the delay. I got pulled away from this due to unavoidable circumstances.

Anyway I tried out the code you so kindly typed out. It didn't work. Instead something strange happened. The ZCD input went high even though i configured everything exactly as you've mentioned. The TRIAC never fires no matter what the value of

SetPhasePercent(value); is set to

. A print screen of my configuration in STM32CubeMX is attached here in. The code's a copy-paste of yours as above. All other settings remain unchanged.

Thank you for your help.

0690X00000607vrQAA.png0690X00000607lWQAQ.png0690X00000607w6QAA.png0690X00000607wGQAQ.png