Analysis of the use of STC single chip microcomputer hardware PWM

Publisher:开元轩Latest update time:2019-01-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


How to achieve variable frequency PWM output for STC microcontroller (with PCA function)? Many friends are still using timers to do variable frequency PWM. Here I will introduce how to use hardware PWM and design PWM under the frequency division base of timer 0. 


Write the picture description here


First, let's look at the CMOD register. Here we mainly focus on CPS0, CPS1, and CPS2. These three bits control the selection of the PCA count pulse source. For the PWM function, it is to select the frequency. First, you can choose 6 fixed frequency divisions. As you can see from the picture, they are 1, 2, 4, 6, 8, and 12 system frequency divisions. We need to make a variable frequency PWM, so we need to select mode 2 and select the overflow of timer 0 as the system frequency division base. 


Then, let's see how to calculate and implement frequency division. Here we need the calculation rules in the picture above. Here I give an example to illustrate, for example, the crystal oscillator of our microcontroller uses 12MHZ, and we need to achieve a frequency of 300HZ. What if we get it? 


First, if we use the fixed frequency division of CMOD, we can find that no matter it is 1, 2, 4, 8, or 12, the frequency divided is not 300HZ. So here we can see the benefit of using timer 0 as the frequency division base. 


Write the picture description here


We continue to calculate, 12MHZ needs to be converted to 300HZ, then according to the above figure, first we need to determine the PCA clock input frequency, according to the formula 300*256=76800HZ, this value is the PCA clock input frequency we need. Now the question is, how do we convert 12MHZ to 76.8KHZ, 12000KHZ/76.8KHZ=156.25, this 156.25 is the frequency division base, and this frequency division base is set by our timer overflow parameter, which means that when our timer counts 156.25 overflows, the frequency division base can be 156.25, so we set the count start value of timer 0 to 65536-156=65380, corresponding to TH0=0XFF, TL0=0X64. 


Well, here is the introduction of how to calculate PWM to achieve any frequency we need under timer 0. If we want to dynamically change the frequency, we can dynamically adjust the counting base of timer 0 through communication (actually adjusting the frequency division base), and the frequency can be changed. In addition, it is explained that the timer 0 here does not need interrupts, and the timer is not used to generate PWM, but as a frequency division base!

Reference address:Analysis of the use of STC single chip microcomputer hardware PWM

Previous article:Use of STM8L15x series DAC
Next article:STC15 MCU 6-channel dedicated PWM

Recommended ReadingLatest update time:2024-11-23 07:48

STC89C52 MCU internal EEPROM driver
The STC89C52 microcontroller has 4K storage space, which is divided into 8 sectors, each sector is 512 bytes. The starting address of the first sector is 0x2000 and the ending address is 21FF. The starting address of the eighth sector is 0x2E00 and the ending address is 2FFF. #include reg52.h #include intrins.h #de
[Microcontroller]
8051 MCU (STC89C52) implements countdown timer in timer interrupt mode
Previously, the polling method was used to achieve accurate timer timing. Here, the countdown timer is implemented using the front-end and back-end system architecture.   The interrupt handler T0_INT() is triggered every 10ms. Depending on the value of tcount, it is decided whether to display the unit digit or the t
[Microcontroller]
8051 MCU (STC89C52) implements countdown timer in timer interrupt mode
The main differences between AT and STC microcontrollers
1. Commonly used 51 microcontroller The 51 microcontroller is a traditional 8-bit microcontroller compatible with the 8051 instruction code. For novices who are new to microcontrollers, they mainly have AT microcontrollers and STC microcontrollers. The main difference between the two microcontrollers is that they are
[Microcontroller]
The main differences between AT and STC microcontrollers
STC15w4k58s4 MCU 4 serial ports simultaneous sending and receiving routine
STC15w4k58s4 chip 4 serial ports simultaneous sending and receiving routine The microcontroller source program is as follows: #include "intrins.H" #include "uart.H" #define uchar unsigned char #define uint unsigned int uchar uart1buf ={0xa5,0x5a,0x4a,0xf8,0xff,0xff}; uchar uart2buf ={0xa5,0x5a,0x4a,0xf8,0xff,0xff
[Microcontroller]
GPS positioning and track recorder made of stc51 single chip microcomputer
GPS positioning and track recorder based on C3-370C! I bought a C3-370C GPS from 51hei and prepared to do GPS track recording, because I always wanted to add coordinate information to travel photos... I started to use M48 as the control MCU, but when I was writing, I found that the Flash space was insufficient. I wa
[Microcontroller]
GPS positioning and track recorder made of stc51 single chip microcomputer
STC12C2052AD single chip AD conversion program design
#include STC12C2052AD.h //Defined system header files and global variables #include intrins.h #define uchar unsigned char #define uint unsigned int #define DogReset()    WDT_CONTR=0x35 // T1 timing 0.1ms. Used as system timing, #define vT01ms 2 #define vT10ms 10 #define vT100ms 10 #define vT01S 100 // 1 s = 10 ms * 1
[Microcontroller]
STC89C52 series MCU internal resources - interrupt system
Interrupts are set up to enable the microcontroller to process random external or internal events in real time. The existence of the interrupt function greatly improves the microcontroller's ability to handle external or internal events. It is also one of the most important functions of the microcontroller, and we mus
[Microcontroller]
STC89C52 series MCU internal resources - interrupt system
STC89C52 single chip microcomputer relay experiment
///////////////////////////////////////////////////// /////////////////////////   Function: Turn on the buzzer on the experiment board for 0.3 seconds, then turn off for 0.3 seconds, and then cycle like this.                     At the same time, the light emitting diode next to the relay on the experiment board will
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号