![stm8 cosmic pwm example stm8 cosmic pwm example](https://www.waveshare.com/img/devkit/EX-STM8-Q48a-105/EX-STM8-Q48a-105-1.jpg)
Note that it is a 16-bit timer and can only count up. Timer 16 offers some basic functionality, so we’ll use that. For example, here is a table from section 3.24 of our STM32L476 datasheet giving us information about the different general purpose timers at our disposal. If you look at the datasheet for your microcontroller, you will often find a section talking about the various timers available. Now, our timer would tick once every 1 microsecond and, assuming a 16-bit timer, be able to time events up to a maximum of about 65.5 milliseconds. For example, a prescaler of 80 would turn an 80 MHz clock into a 1 MHz clock. If we wish to measure longer events, we need to use a prescaler, which is a piece of hardware that divides the clock source. A 16-bit timer can count to 65,535 before rolling over, which means we can measure events no longer than about 819 microseconds! We could have a timer tick at 80 MHz, but that might be too fast for many of our applications. In this demo, we will be using an STM32 Nucleo-L476RG, which has a default main clock (HCLK) of 80 MHz. A timer will tick (increment by one) each time it receives a clock pulse. Most will be connected to the microcontroller’s main CPU clock (others, like real time clocks, have their own clock sources).
![stm8 cosmic pwm example stm8 cosmic pwm example](https://i.ytimg.com/vi/I1qwyMq2xpc/maxresdefault.jpg)
How fast do timers run? Well, that depends on how fast you tell them to run. measure execution time) as well as triggering basic interrupts. For now, I’d like to focus on using a timer to measure time between events (i.e. These hardware-connected timer functions will be a topic for another time. By adjusting the on versus off time (duty cycle), you can effectively control the amount of electrical power going to another device. Pulse width modulation (PWM): toggle a pin when a timer reaches a certain value and on rollover.Input capture (IC): measure the number of counts of a timer between events on a pin.Output compare (OC): toggle a pin when a timer reaches a certain value.Here are some of the common hardware functions you’ll see with timers: Additionally, you can often connect other hardware or peripherals inside the microcontroller to the timer, like toggling a specific pin automatically when the timer rolls over. For example, instead of counting to a maximum of 255, you might tell the timer that you want it to roll over at 100 instead. These settings are usually applied via other special function registers inside the microcontroller. You can apply a variety of settings to most timers to change the way they function. So, our 8-bit timer would start over again from 0 once it reaches 255. Most timers will “roll over” once they reach their max value. For example, an 8-bit timer will count from 0 to 255. Their function is simple: they count (up or down, depending on the configuration-we'll assume up for now). A timer (sometimes referred to as a counter) is a special piece of hardware inside many microcontrollers.