When I first saw this picture, I felt a little confused ~ Later I understood it a lot better.
For PWM experiments, we usually only care about frequency and duty cycle. Let me explain this picture:
First, assume that the microcontroller is connected to an external 4MHz crystal oscillator;
The period of the PWM wave is determined by the PR2 register, and the duty cycle is composed of CCPR1L+2 bits to form a 10-bit resolution;
But the period is 8-bit resolution, and the numerator is larger than the denominator. Can this PWM still work normally?
I believe that many people may have the same doubts as me at the beginning!
Let's take a look at how the cycle is calculated in the official data manual:
PWM cycle = (PR2+1)*4*Tosc*TMR2 frequency division number
There are two things to note in this formula:
① Tosc is 4MHz, not 1MHz
② Multiply by 4, which means CCPR1L<<2 (shift right 2 bits)
Suppose I want to implement a 10kHz square wave with a duty cycle of 90, how should I fill in the values of the relevant registers?
First calculate the 10kHz period, which is 100us
100=(PR2+1)*4*0.25*1(frequency division number)
Then PR2=99
CCPR1L:X:Y=90*4*0.25*1
You can take X:Y=0:0 (both sides of the equation are shifted right by 2 bits)
Then CCPR1L=90
So far I haven't answered why the numerator is larger than the denominator? In fact, both the numerator and the denominator are 10 bits, but the lowest 2 bits of the denominator are always 0;
The 10 bits of the numerator can be set, which has the advantage that the desired duty cycle can be accurately designed, such as 27%, 61%, 84%...
The experimental procedure is as follows:
#include
__CONFIG(0xFF32);
void init_ccp1(void)
{
TRISC2=0; //RC2/CCP1 is set as output
#if 0
PR2=61; //Period is 1ms, duty cycle is 40%
T2CON=0x06; //TMR2 pre-divided to 1:16, start working
CCPR1L=0x19;
#else
PR2=99; //Period is 100us, duty cycle is 90%
T2CON=0x04; //TMR2 pre-divided to 1:1, start working
CCPR1L=90;
#endif
CCP1CON=0x0c; //PWM mode
}
void main()
{
init_ccp1();
while(1);
}
Previous article:Use of printf in PIC
Next article:PIC16F877A Timer 1 Experiment
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Ranking of installed capacity of smart driving suppliers from January to September 2024: Rise of independent manufacturers and strong growth of LiDAR market
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Advanced Designer v21.5.1.32
- Announcement: Participants in the first round of the lottery will receive a multimeter, soldering station, and development board for 11.11 yuan.
- TI microcontroller I8669/I44445 (MCU) MSP430 combines low power consumption with high performance
- Purgatory Legend-Serial Communication War
- A circuit implementation of a precision full-wave rectifier circuit
- Happy Valentine's Day~ May all lovers in the world be happy and contented.
- Should you enter the IT industry? Four factors will tell you at a glance
- What are the commonly used chips that make up DSP systems?
- Using STM32L452 to drive STTS751 through C-Driver-MEMS
- [RTT & Renesas high-performance CPK-RA6M4] 1. Hardware appreciation and RT-Thread development environment construction