Easy Embedded Programming: How to Program Counter and PWM

Published By

The second video introduces designing with a basic counter. Learn how to integrate a PWM, and how to configure the design.

In the previous video, in the main loop, a delay was used between each function to drive the LED pin. This is generally poor style as it consumes scared resources for nothing. A better way to turn on and off LEDs is to use PWM (Pulse Width Modulation), timers and counters.

First, let's design a basic counter. Opening the datasheet of the basic counter, the basic counter is programmable from 2 to 32 bits counter. The output of the counter is bus. It’s possible to select which bit of the bus drives the LED.

A simple 2 bit counter is designed in our project. In other words, the 4 values are 00, 01, 10, 11 and then it rolls over to 00. When the push button is connected to the input clock and the LED pin is wired to bit 0 of the counter, each time the button is pressed, the LED toggles. If we want the user to push two times the button in order to toggle the LED, he just needs to connect to line 1 of the bus. In that case, there is no code to write. Hence, just compile and flash the microcontroller to have the LED toggling.

As previously, instead of a switch button, we would like to have the LED flashing at 1 Hz. For that, instead of a switch button, a clock component is connected to the clock input of the basic counter. The default clock is 48Mhz that can be divided by a max of 65536. So, it’s possible to configure a 1-khz clock with a 10-bit counter and the red LED connected to line 9. As 210 = 1,024, the LED will flash at around one hertz. Again, no C code to write, just need to compile and program the microcontroller.

A more complex and versatile component is the TCPWM that stands for Timer Counter PWM component. Let's configure it as a PWM. To make maths simple, the clock is configured at 32kHz, the period of the PWM at 32,000 counts and the compare of the PWM at 16,000. The line output of the TCPWM component is connected to the red LED. The LED is OFF when the counter value is between 0 and compare value and the LED is ON when the counter is between Compare and Period values.

This time, one line of C code is necessary. The TCPWM component is named PWM, and after building the project, APIs are generated. All the information about APIs is in the datasheet. In order to have the PWM running, the function PWM_Start() must be called. Compiling this program will make the LED flash at 1 Hz. To change the frequency or the ratio, the period and compare register values just need to be changed.


Related videos

Latest Videos

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here