banner



What Register Sets The Length Of The Period? (Assume Clock Frequency And Pre-scale Are Already

In this tutorial we are going to discuss the Timer module of PIC18F4520.
Commencement nosotros will see what are timers, their working and later we will configure the PIC18F4520 timers to generate delay of 100ms and 500ms respectively. At the end we will see how to use the ExploreEmdedded Timer library.

Contents

  • ane Timer Nuts
  • two PIC Timer Module
  • iii Timer Adding
  • four Timer 0
    • 4.1 Timer0 Registers
    • 4.2 Code
  • 5 Timer i
    • 5.ane Timer1 Registers
    • 5.2 Lawmaking
  • 6 Timer 2
    • six.ane Timer2 Registers
    • six.2 Code
  • 7 Downloads

Timer Basics

As the name suggests these are used to measure the fourth dimension or generate the accurate time filibuster. The microcontroller can also generate/measure the required time delays by running loops, but the timer/counter relieves the CPU from that redundant and repetitive task, allowing information technology to classify maximum processing fourth dimension for other tasks.

Timer is nil but a simple binary counter that can exist configured to count clock pulses(Internal/External). One time it reaches the Max value, it will scroll dorsum to zero setting upwardly an OverFlow flag and generates the interrupt if enabled.

Timer.gif

PIC Timer Module

PIC18F4520 has iii indepenndent timer which can be used as timer,Counters or for PWM generation.
Below table provides the details of the three Timers.

Timer Size Control Annals Count Register Min Filibuster Max Delay
TIMER0 8-bit OPTION_REG TMR0 0.2usec thirteen.107ms
TIMER1 sixteen-bit T1CON TMR1H,TMR1L 0.2usec 104.857ms
TIMER2 viii-bit T2CON TMR2 0.2usec 819usec

Timer Calculation

PIC Oscillator frequency is divided by 4 and so fed to the controller, Now this this freq can exist farther divided by presacalar to generate the range of delays.
Fourth dimension to increment the Timer count by 1(timer tick) tin be determined as beneath.
tick = (Prescalar/(Fosc/4)
tick = (Prescalar/(20Mhz/4))
$$tick = (Prescalar * four)/Fosc$$

At present the Timer value for the required delay can be calculated equally below.
Delay = TimerCount * tick
Count = (Filibuster/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Filibuster/tick) = TimerMax - (Delay/((Prescalar *four)/Fosc))
$$RegValue = TimerMax-((Delay * Fosc)/(Prescalar*4))$$

Below table provides the formula for all the iii Timers.

Timer Size Formula for delay calculation
TIMER0 8-bit $$RegValue = 256-((Delay * Fosc)/(Prescalar*4))$$
TIMER1 16-scrap $$RegValue = 65536-((Delay * Fosc)/(Prescalar*4))$$
TIMER2 8-fleck $$RegValue = 256-((Delay * Fosc)/(Prescalar*four))$$

Timer 0

The TMR0 module is an 8-bit timer/counter with the following features:

  • 8-bit timer/counter
  • Readable and writable
  • 8-scrap software programmable prescaler
  • Internal or external clock select
  • Interrupt on overflow from FFh to 00h
  • Edge select for external clock

Timer0 Registers

The beneath table shows the registers associated with PIC18F4520 Timer0 module.

Register Description
OPTION_REG This registers is used to configure the TIMER0 Prescalar, Clock Source etc
TMR0 This register holds the timer count value which volition be incremented depending on prescalar configuration
INTCON This annals contains the Timer0 overflow flag(TMR0IF) and corresponding Inetrrupt Enable flag(TMR0IE).

OPTION_REG
7 6 5 4 3 2 1 0
RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0

RBPU: NA for Timers

INTEDG: NA for Timers

T0CS: TMR0 Clock Source Select chip
1-Transition on T0CKI pin
0-Internal instruction cycle clock (CLKO)

T0SE: TMR0 Source Edge Select bit
1-Increment on loftier-to-low transition on T0CKI pivot
0-Increment on depression-to-loftier transition on T0CKI pivot

PSA: Prescaler Assignment scrap
1-Prescaler is assigned to the WDT
0-Prescaler is assigned to the Timer0

PS2:PS0: Prescaler Charge per unit Select bits Timer0Prescalar.png

  • Note:There is simply one prescaler available which is mutually exclusively shared between the Timer0 module and the Watchdog Timer. A prescaler consignment for the Timer0 module means that there is no prescaler for the Watchdog Timer and vice versa. This prescaler is non accessible but tin be configured using PS2:PS0 bits of OPTION_REG.
INTCON
seven 6 5 4 three ii one 0
GIE PIE TMR0IE INTE RBIE TMR0IF INTF RBIF

GIE: Global Interrupt Enable bit
one-Enables all unmasked interrupts
0-Disables all interrupts

PIE: Peripheral Interrupt Enable bit
1-Enables all unmasked peripheral interrupts
0-Disables all peripheral interrupts

TMR0IE: TMR0 Overflow Interrupt Enable bit
ane-Enables the TMR0 interrupt
0-Disables the TMR0 interrupt

INTE: NA for Timers

RBIE: NA for Timers

TMR0IF: TMR0 Overflow Interrupt Flag bit
1-TMR0 register has overflowed (must be cleared in software)
0-TMR0 register did not overflow

INTF: NA for Timers

RBIF: NA for Timers
Timer0 Block Diagram.png

Filibuster Calculations for 1ms @20Mhz with Prescalar every bit 32: $$RegValue = 256-(Delay * Fosc)/(Prescalar*4)) = 256-((1ms * 20Mhz)/(32*iv)) = 256-156=100$$

Code

Below are the steps for configuring and using the Timer0 for delay generation:

  1. Summate the Timer Count for the required delay.
  2. Fix the Presaclar bits in OPTION_REG every bit per the delay calculations.
  3. Clear the PSAchip for using the prescalar.
  4. Select the Clock Source Internal/External using TOCS scrap.
  5. Load the timer value into TMRO register.
  6. Enable the Timer0 Interrupt by setting TMR0IE flake
  7. Enable the Global and Peripheral interrupts past setting GIE and PIE bits

Below is the sample code to blink the LEDs with 1ms delay. Delay measured using CRO: Timer0 Cro.png


Timer 1

The timer TMR1 module is an xvi-bit timer/counter with the following features:

  • 16-scrap timer/counter with two 8-Chip register TMR1H/TMR1L
  • Readable and writable
  • software programmable prescaler upto 1:8
  • Internal or external clock select
  • Interrupt on overflow from FFFFh to 00h
  • Edge select for external clock

Timer1 Registers

The below tabular array shows the registers associated with PIC18F4520 Timer1 module.

Register Clarification
T1CON: This registers is used to configure the TIMER1 Prescalar, Clock Source etc
TMRIH This register holds the higher 8-bits of timer value. TMR1H and TMR1L are used in pair to increment from 0000 - FFFFh
TMRIL This register holds the lower 8-$.25 of timer value. TMR1H and TMR1L are used in pair to increase from 0000 - FFFFh
PIR1 This annals contains the Timer1 overflow flag(TMR1IF).
PIE1 This annals contains the Timer1 Interrupt Enable flag(TMR1IE).

T1CON
7 6 5 4 3 2 ane 0
T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON

T1CKPS1:T1CKPS0:Timer1 Input Clock Prescale Select bits
11 = 1:8 prescale value
10 = 1:iv prescale value
01 = 1:2 prescale value
00 = 1:1 prescale value

T1OSCEN: Timer1 Oscillator Enable Control bit
1-Oscillator is enabled
0-Oscillator is shut-off

T1SYNC: Timer1 External Clock Input Synchronization Control bit
1-Do non synchronize external clock input
0-Synchronize external clock input

TMR1CS: Timer1 Clock Source Select bit
1-External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
0-Internal clock (FOSC/4)

TMR1ON: Timer1 On bit
one-Enables Timer1
0-Stops Timer1

Timer1 Block Diagram.png

Delay Calculations for 100ms @20Mhz with Prescalar equally eight: $$RegValue = 65536-(Filibuster * Fosc)/(Prescalar*4)) = 65536-((100ms * 20Mhz)/(viii*four)) = 3036 = 0xBDC$$

Code

Below are the steps for configuring and using the Timer1 for filibuster generation:

  1. Calculate the Timer Count for the required delay.
  2. Ready the Presaclar bits in T1CON as per the delay calculations.
  3. Select the Clock Source Internal/External using TMR1CS fleck.
  4. Load the timer value into TMR1H,TMR1L annals.
  5. Enable the Timer1 Interrupt by setting TMRIE chip
  6. Enable the Global and Peripheral interrupts by setting GIE and PIE bits
  7. Finally first the timer by setting TMR1ON bit

Below is the sample lawmaking to glimmer the LEDs with 1ms delay. Delay measured using CRO: Timer 1 Cro.png


Timer 2

The TImer2 module is an eight-bit timer/counter with the following features:

  • 8-flake timer/counter
  • Readable and writable
  • Software programmable prescaler/PostScaler upto 1:16
  • Interrupt on overflow from FFh to 00h

Timer2 Registers

The below table shows the registers associated with PIC16f877A Timer0 module.

Annals Description
T2CON This registers is used to configure the TIMER2 Prescalar, Clock Source etc
TMR2 This register holds the timer count value which will exist incremented depending on prescalar configuration
PIR1 This register contains the Timer2 overflow flag(TMR2IF).
PIE1 This register contains the Timer2 Interrupt Enable flag(TMR2IE).

T2CON
seven 6 5 four 3 2 ane 0
TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0

TOUTPS3:TOUTPS0: Timer2 Output Postscale Select $.25
0000 = 1:one postscale
0001 = i:2 postscale
0010 = ane:three postscale



1111 = 1:16 postscale

TMR2ON: Timer2 On chip
1-Timer2 is on
0-Timer2 is off

T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits
00 = Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16

Timer2 Block Diagram.png


Generating 1sec delay using Timer2:
As the timer2 is 8-flake and supports i:xvi prescalar, information technology is not possible to directly generate the delay of 1sec. The max filibuster with ane:16 prescaler will exist:
$$Delay = 256 * (Prescaler*4)/Fosc = 256 * 16*4/20Mhz=819us$$

At present 500us can exist generated using timers which will exist used to increase a counter 2000 times to get 1sec delay.
Filibuster Calculations for 500usec @20Mhz with Prescalar every bit 16: $$RegValue = 256-(Filibuster * Fosc)/(Prescalar*4)) = 256-((500us* 20Mhz)/(xvi*iv)) = 256-156=100$$

Code

Below are the steps for configuring and using the Timer2 for delay generation:

  1. Calculate the Timer Count for the required delay.
  2. Fix the Prescaler bits in T2CON as per the delay calculations.
  3. Load the timer value into TMR2 annals.
  4. Enable the Timer2 Interrupt by setting TMR2IE chip
  5. Enable the Global and Peripheral interrupts by setting GIE and PIE $.25
  6. Finally start the Timer2 by setting TMR2ON flake

Below is the sample code to blink the LEDs with 1sec delay. Delay measured using CRO: Timer 2 Cro.png

00 Lpc1768 Timer.jpg

Downloads

Download the complete project folder from the below link:
Hardware design Files and Code Library


Take a opinion, proffer , question or feedback virtually the article allow it out here!

What Register Sets The Length Of The Period? (Assume Clock Frequency And Pre-scale Are Already,

Source: https://exploreembedded.com/wiki/PIC18F4520_Timer.

Posted by: brooksmirsteve.blogspot.com

0 Response to "What Register Sets The Length Of The Period? (Assume Clock Frequency And Pre-scale Are Already"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel