Easy Embedded Programming: How to Program Counter and PWM

게시자

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.


최신 동영상

죄송합니다. 선택한 필터와 일치하는 결과가 없습니다.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.