Question about PIC16F877A PWM calculation

Publisher:平和宁静Latest update time:2019-01-31 Source: eefocusKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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);

}

Keywords:PIC16F877A Reference address:Question about PIC16F877A PWM calculation

Previous article:Use of printf in PIC
Next article:PIC16F877A Timer 1 Experiment

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号