Brief Analysis of PIC Microcontroller Timer

Publisher:知识阁楼Latest update time:2013-05-08 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What is a timer?

As the name implies, a timer is used for timing. In microcontroller applications, it is often used for various timings. For example, to make an LED light turn on once every 1 second. This 1 second is achieved by a timer.

Instruction cycle

The instruction cycle is the time it takes for the microcontroller to execute an instruction. This is also the minimum time unit for the timer. Clock frequency/4=instruction frequency. 1/instruction frequency=instruction cycle.

Assume that the current clock is 4MHZ, the 4MHz clock becomes 1MHz after being divided by 4, and its period is 0.0000001s, which is 1us. This 1us is the instruction cycle, and this 1us is also the minimum unit of timer timing.

Timer and prescaler

Assuming there is no prescaler, the timer is turned on and incremented by one every instruction cycle. Assuming the clock is 4MHz, the timer increments by one every 1us.

If there is a prescaler, assuming the prescaler is set to divide by 2, the timer will increase by 1 every 2 instruction cycles. If the prescaler is set to divide by 4, the timer will increase by 1 every 4 instruction cycles, and so on.

Timer interrupt flag

For example: TMR0 is an 8-bit timer, which is an 8-bit register. The 8-bit register can represent values ​​from 0 to 255. That is to say, the timer can be incremented from 0 to 255. After reaching 255, it will become 0 again if it is incremented by one. At this time, the TMR0 timer interrupt flag (TMR0IF) becomes 1. (If the interrupt is not enabled, the interrupt program will not be executed.)

What is the relationship between the clock frequency and the timer interrupt overflow?

Below I have drawn a flow chart to understand all this in terms of frequency. Assume the clock frequency is 4MHz, the timer prescaler value is 2, and the timer initial value is 0.

1. First, the 4MHz clock is divided by 4 to become a 1MHz instruction frequency;

2. Then the prescaler divides the frequency by 2 to 0.5MHz and supplies it to the timer;

3. After the timer is divided by 256, the frequency becomes about 1952Hz and the frequency overflow interrupt occurs;

 

 

Then we can understand all this in terms of cycles.

1. First, the 0.25us clock cycle is divided by 4 to become a 1us instruction cycle;

2. Then the prescaler divides the frequency by 2 and turns it into a 2us period to supply the timer;

3. The timer increments by one every 2us, and when it reaches 256 times (256X2us=512us), an overflow interrupt occurs;

 

 

Hopefully the above flowchart will help you sort out the concepts.

Example description:

Assuming the clock cycle is 4MHZ, the LED is turned on every 50MS and off every 50MS. How can such a program be implemented?

How to achieve this 50ms?

1. Get the instruction cycle

4MHz/4=1MHz

1/1MHz=0.0000001s=1us

2. Get the pre-scaling

The maximum time of the timer should exceed 50mS, so the prescaler should be 256.

Pre-scaling X256 = maximum timing time. 256X256 = 65536us = 65.536ms greater than 50ms

3. Calculate the initial value of the timer

(Timer maximum value + 1) - (timing time / pre-division) = timer initial value.

255+1=256

50000/256=195.3125

256-195.3125=60.6875 rounded to the nearest integer, the initial value of the timer is 61.

Set the relevant registers. [page]

We generally need to set three places in the OPTION_REG register.

PS<2:0> is used to set the prescaler. The prescaler range is from 2 to 256.

PSA is set to 0 to assign the prescaler to the Timer0 module.

TMR0CS is set to 0 internal instruction cycle clock.

 

 

Example program:

/*Development environment MPLAB X IDE Model PIC16LF1823*/

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

#define LED LATA5 /* You can also use #define LED RA5, but when PIC16LF1823 outputs the level, directly controlling LATA5 is faster, because the data sent to RA5 is eventually sent to LATA5 for execution*/

void init_fosc(void)

{

OSCCON = 0x68; //Clock is set to 4MHz

}

void init_gpio(void)

{

PORT = 0;

YEARS = 0;

ANSELA = 0;

TRISAbits.TRISA5=0; //Set RA5 port to output to control LED

}

void init_timer0(void)

{

OPTION_REG=0x87; //Prescaler is 256

}

int main(int argc, char** argv)

{

init_dark();

init_gpio();

init_timer0();

TMR0IF=0; //Clear TMR0 interrupt flag

TMR0=61; //Set the interrupt initial value to 61

while(1)

{

if(TMR0IF==1)//Is the timing time up?

{

LED = ~LED; //Change the state of LED

TMR0IF=0; //Clear TMR0 interrupt flag

TMR0=61; //Set the interrupt initial value 61 to prepare for the next 50ms timing

}

}

}

Reference address:Brief Analysis of PIC Microcontroller Timer

Previous article:Design of intelligent battery charging manager
Next article:PIC microcontroller I/O control

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号