PIC microcontroller clock settings

Publisher:ZenMaster123Latest update time:2018-08-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What clock?

  First, let's talk about what a clock is. The clock is the heart of the microcontroller. Every time it beats, all the circuits of the microcontroller move synchronously. It's like when we do radio gymnastics, the beats on the radio are 1234 2234 3234... Then all of us students follow this rhythm to perform one action after another. The faster the beat, the faster we move. The slower the beat, the slower we move.

Internal clock or external clock?

  There are many models of PIC microcontrollers that can be set to use an external clock (such as an external 4MHZ quartz crystal), or an internal RC clock. In addition, there are many models that can select clocks of multiple frequencies.

 For example, the internal clock of PICLF1823 can reach up to 32MHZ and down to 31kHz. This actually provides a new way to reduce power consumption. In general, microcontrollers often use sleep mode to reduce power consumption, and the CPU does nothing during sleep. If you want to reduce power consumption and keep the CPU working, lowering the clock frequency is undoubtedly a very good choice.

 How to set the clock?

The clock setting is generally related to only two places: one is the configuration word, and the other is the oscillator control register OSCCON. Below we take PICLF1823 as an example to explain.

   The clock-related bits in the PICLF1823 configuration word settings are FOSC<2:0>: oscillator selection bits, and PLLEN: PLL enable bits.

   OSCCON is related to the internal clock frequency selection. If only an external clock is used, it is generally ignored. The following is a detailed introduction to the OSCCON register in the data sheet.

   

Example explanation:

Use the internal clock to set the clock frequency to 8MHZ


Development environment: MPLAB X IDE 

Chip model:PICLF1823

#include
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);
__CONFIG(PLLEN_OFF&LVP_OFF) ;//Set FOSC<2:0>: oscillator selection bits to INTOSC,
void init_fosc(void)
{
 OSCCON = 0x70;//8MHZ IRCF<3:0> clock frequency selection bits set to 8MHZ 
}
int main(int argc, char** argv) {

    init_fosc();
    while(1);
}

What is PLL used for? Its function is to convert the 8MHZ clock frequency into 32MHZ by X4. Remember, only 8MHZ can be used, other frequencies cannot be used.


Use the internal clock to set the clock frequency to 32MHZ


Development environment: MPLAB X IDE 

Chip model:PICLF1823

#include

__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON

                   &CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This should be put on the previous line


__CONFIG(PLLEN_ON&LVP_OFF); //Set FOSC<2:0>: oscillator selection bits to INTOSC, enable PLLEN
void init_fosc(void)
{
 OSCCON = 0xF0; //32MHZ IRCF<3:0> clock frequency selection bits set to 8MHZ, enable 4XPLL
}
int main(int argc, char** argv) {

    init_fosc();
    while(1);
}


Reference address:PIC microcontroller clock settings

Previous article:PIC Microcontroller - Buzzer Usage Example
Next article:PIC microcontroller development environment--MPLAB X IDE

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号