Easy Embedded Programming: How to Program Counter and PWM

Pubblicato da

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.


Video più recenti

Il filtro selezionato non ha restituito alcun risultato.

Non perderti le ultime novità sull'elettronica

Abbiamo aggiornato la nostra politica sulla privacy. Si prega di prendere un momento per rivedere questi cambiamenti. Cliccando su Accetto, l'utente accetta la Politica sulla privacy e Condizioni di utilizzo di Arrow Electronics.

Il nostro sito web mette i cookies sul vostro dispositivo per migliorare la vostra esperienza e il nostro sito. Leggete altre informazioni sui cookies che usiamo e su come disabilitarli qui. I cookies e le tecnologie di tracking possono essere usati per scopi commerciali.

Con un click su “Accept”, voi consentite l'inserimento dei cookies sul vostro dispositivo e l'uso da parte nostra di tecnologie di tracking. Per avere altre informazioni e istruzioni su come disabilitare i cookies e le tecnologie di tracking, clickate su “Read More” qui sotto. Mentre l'accettazione dei cookies e delle tecnologie di tracking è volontaria, una loro disabilitazione potrebbe determinare un funzionamento non corretto del sito web, ed alcuni messaggi di allarme potrebbero essere per voi meno importanti.

Noi rispettiamo la vostra privacy. Leggete qui la nostra politica relativa alla privacy