Section 13. Timer0 HIGHLIGHTS. Timer0. This section of the manual contains the following major topics:

Similar documents
Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

Section 14. Timer1 HIGHLIGHTS. Timer1. This section of the manual contains the following major topics:

Section 16. Basic Sychronous Serial Port (BSSP)

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

PIC16C84. 8-bit CMOS EEPROM Microcontroller PIC16C84. Pin Diagram. High Performance RISC CPU Features: CMOS Technology: Peripheral Features:

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

Section 21. Addressable USART

PIC16C7X 11.0 SYNCHRONOUS SERIAL PORT (SSP) MODULE SSP Module Overview. Applicable Devices

CENG-336 Introduction to Embedded Systems Development. Timers

Section 24. Comparator

Section 28. WDT and SLEEP Mode

M PIC16F84A. 18-pinEnhanced FLASH/EEPROM 8-Bit Microcontroller. High Performance RISC CPU Features: Pin Diagrams. Peripheral Features:

These 3 registers contain enable, priority,

PIC16C432 OTP 8-Bit CMOS MCU with LIN bus Transceiver

PIC16F8X. 8-Bit CMOS Flash/EEPROM Microcontrollers PIC16F8X PIC16CR8X. Pin Diagram. Devices Included in this Data Sheet:

PIC16F8X 18-pin Flash/EEPROM 8-Bit Microcontrollers

PIC10F200/202/204/206 Data Sheet

PIC16C52. EPROM-Based 8-Bit CMOS Microcontroller PIC16C52. Pin Diagrams. Feature Highlights. High-Performance RISC CPU. Peripheral Features

EEE111A/B Microprocessors

PIC16C Pin, 8-Bit CMOS Microcontroller. Device included in this Data Sheet: Special Microcontroller Features: High-Performance RISC CPU:

PIC16C Pin, 8-Bit CMOS Microcontroller. Device included in this Data Sheet: Special Microcontroller Features: High-Performance RISC CPU:

PIC10F200/202/204/206

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

PIC16F8X PIC16F8X. 18-pin Flash/EEPROM 8-Bit Microcontrollers

PIC12C5XX. 8-Pin, 8-Bit CMOS Microcontroller. CMOS Technology: Devices included in this Data Sheet: High-Performance RISC CPU: Pin Diagram

PIC16F84A. 18-pin Enhanced Flash/EEPROM 8-Bit Microcontroller. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

Using Timers of Microchip PIC18F Microcontrollers

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

Embedded System Design

ME 6405 Introduction to Mechatronics

Section 4. Architecture

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

The University of Texas at Arlington Lecture 21_Review

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

PIC16F630/676 Data Sheet

Mod-5: PIC 18 Introduction 1. Module 5

PIC16F /40-Pin 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram PDIP. Microcontroller Core Features:

PIC16C62X Data Sheet. EPROM-Based 8-Bit CMOS Microcontrollers Microchip Technology Inc. DS30235J

To Our Valued Customers

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

Learning Objectives:

PIC12F629/675 Data Sheet

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Section 30. In-Circuit Serial Programming (ICSP )

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

PIC16F870/ /40-Pin, 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram. Microcontroller Core Features:

PIC12CE5XX. 8-Pin, 8-Bit CMOS Microcontroller with EEPROM Data Memory. Devices Included in this Data Sheet: Pin Diagram: High-Performance RISC CPU:

Performance & Applications

PIC10F200/202/204/206 Data Sheet

PIC16F630/676 Data Sheet

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

Chapter 11: Interrupt On Change

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

Chapter 10 Sections 1,2,9,10 Dr. Iyad Jafar

Input/Output Ports and Interfacing

Section 9. Watchdog Timer (WDT)

EE 361L Digital Systems and Computer Design Laboratory

PIC16F818/819 Data Sheet

PIC Discussion By Eng. Tamar Jomaa

PIC16F87XA Data Sheet

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

PIC Microcontroller Embedded Systems Hadassah College Spring 2012 PIC Microcontroller Dr. Martin Land

PIC16C745/ Bit CMOS Microcontrollers with USB. Pin Diagrams. Devices included in this data sheet: Microcontroller Core Features:

Week1. EEE305 Microcontroller Key Points

PIC16C433 Data Sheet. 8-Bit CMOS Microcontroller with LIN bus Transceiver Microchip Technology Inc. Advance Information DS41139A

Flow Charts and Assembler Programs

EPROM/ROM-Based 8-Bit Microcontroller Series

PIC 16F84A programming (II)

PIC18F452 Timer adjustment

evsjv `k cigvyy kw³ Kwgkb BANGLADESH ATOMIC ENERGY COMMISSION

PIC16C433 Data Sheet. 8-Bit CMOS Microcontroller with LIN Transceiver Microchip Technology Inc. Preliminary DS41139B

Contents. PIC Mini Data Sheets

OTP-Based 8-Bit Microcontroller

OTP-Based 8-Bit Microcontroller

Laboratory 9. Programming a PIC Microcontroller - Part I

Introduction to PIC Programming

CPEG300 Embedded System Design. Lecture 8 Timer

PIC12F529T48A Data Sheet

Section 8. Reset HIGHLIGHTS. Reset. This section of the manual contains the following major topics:

PIC18F452 Timer adjustment

DRPIC166X IP Core. High Performance 8-bit RISC Microcontroller v. 2.17

Section 8. Reset. Reset HIGHLIGHTS. This section of the manual contains the following topics:

PIC16F872 Data Sheet. 28-Pin, 8-Bit CMOS Flash Microcontroller with 10-Bit A/D Microchip Technology Inc. DS30221C

Introduction to PIC Programming

8051 Peripherals. On-Chip Memory Timers Serial Port Interrupts. Computer Engineering Timers

Microprocessors B (17.384) Spring Lecture Outline

ECE 354 Introduction to Lab 2. February 23 rd, 2003

Section 9. Watchdog Timer and Power-up Timer

D:\PICstuff\PartCounter\PartCounter.asm

Using the 8-Bit Parallel Slave Port

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

Embedded Systems Programming and Architectures

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

Lecture (04) PIC16F84A (3)

PIC16F Pin, 8-Bit Flash Microcontroller. Processor Features: Microcontroller Features: CMOS Technology: Peripheral Features:

Transcription:

Section 13. Timer0 HIGHLIGHTS This section of the manual contains the following major topics: 13.1 Introduction... 13-2 13.2 Control Register... 13-3 13.3 Operation... 13-4 13.4 Timer0 Interrupt... 13-5 13.5 Using Timer0 with an External Clock... 13-6 13.6 Timer0 Prescaler... 13-7 13.7 Initialization... 13-9 13. Design Tips... 13-10 13.9 Related Application Notes... 13-11 13.10 Revision History... 13-12 13 Timer0 2000 Microchip Technology Inc. DS39513A-page 13-1

PIC1C Reference Manual 13.1 Introduction Figure 13-1: The Timer0 module has the following features: Software selectable as an -bit or 16-bit timer/counter Readable and writable Dedicated -bit software programmable prescaler Clock source selectable to be external or internal Interrupt on overflow from FFh to 00h (FFFFh to 0000h in 16-bit mode) Edge select for external clock Figure 13-1 shows a simplified block diagram of the Timer0 module in -bit mode and Figure 13-2 shows a simplified block diagram of the Timer0 module in 16-bit mode. Timer0 Block Diagram in -bit Mode T0CKI pin T0SE FOSC/4 0 1 T0CS Programmable Prescaler 3 T0PS2:T0PS0 POUT 0 1 PSA Sync with Internal clocks (2 TCY delay) PSOUT Data Bus TMR0 Set interrupt flag bit T0IF on overflow Note 1: T0CS, T0SE, PSA, T0PS2:T0PS0 (T0CON<5:0>). 2: Upon reset, Timer0 is enabled in -bit mode, with clock input from T0CKI, max. prescale. Figure 13-2: Timer0 Block Diagram in 16-bit Mode T0CKI pin T0SE FOSC/4 0 1 T0CS Programmable Prescaler 3 T0PS2:T0PS0 POUT 0 1 PSA Sync with Internal clocks (2 TCY delay) PSOUT TMR0L TMR0 High Byte TMR0H Set interrupt flag bit T0IF on overflow L Write TMR0L Data Bus<7:0> Note 1: T0CS, T0SE, PSA, T0PS2:T0PS0 (T0CON<5:0>). 2: Upon reset, Timer0 is enabled in -bit mode, with clock input from T0CKI, max. prescale. DS39513A-page 13-2 2000 Microchip Technology Inc.

Section 13. Timer0 13.2 Control Register The T0CON register is a readable and writable register that controls all the aspects of Timer0, including the prescale selection. Register 13-1: T0CON: TImer0 Control Register R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 R/W-1 TMR0ON T0BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0 bit 7 bit 0 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2-0 TMR0ON: Timer0 On/Off Control bit 1 = Enables Timer0 0 =StopsTimer0 T0BIT: Timer0 -bit/16-bit Control bit 1 = Timer0 is configured as an -bit timer/counter 0 = Timer0 is configured as a 16-bit timer/counter T0CS: Timer0 Clock Source Select bit 1 = Transition on T0CKI pin is clock (counter mode) 0 = Internal instruction cycle is clock (timer mode) T0SE: Timer0 Source Edge Select bit 1 = Increment on high-to-low transition on T0CKI pin 0 = Increment on low-to-high transition on T0CKI pin PSA: Timer0 Prescaler Assignment bit 1 = Timer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler. 0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output. T0PS2:T0PS0: Timer0 Prescaler Select bits These bits are ignored if PSA = 1 111 = 1:256 prescale value 110 = 1:12 prescale value 101 = 1:64 prescale value 100 = 1:32 prescale value 011 = 1:16 prescale value 010 = 1: prescale value 001 = 1:4 prescale value 000 = 1:2 prescale value 13 Timer0 Legend R = Readable bit W = Writable bit U = Unimplemented bit, read as 0 - n = Value at POR reset 1 = bit is set 0 = bit is cleared x = bit is unknown 2000 Microchip Technology Inc. DS39513A-page 13-3

PIC1C Reference Manual 13.3 Operation 13.3.1 -Bit/16-Bit Modes 13.3.1.1 16-Bit Mode Timer Reads 13.3.1.2 16-Bit Mode Timer Write 13.3.1.3 16-Bit Read/Modify Write When initializing Timer0, several options need to be specified. This is done by programming the appropriate bits in the T0CON register. Timer0 can be configured as an -bit or a 16-bit counter. The default state for Timer0 is an -bit counter. To configure the timer as a 16-bit counter, the T0BIT bit (T0CON register) must be cleared. If the timer is configured as an -bit timer, the MSB of TMR0 (TMR0H) is held clear and will read 00h. Normally once the mode of the timer is selected, it is not changed. Some applications may require the ability to switch back and forth between -bit and 16-bit modes. The two cases are: 1. Changing from -bit to 16-bit mode 2. Changing from 16-bit to -bit mode The condition when bit 7 of the Timer0 rolls over must be addressed. If Timer0 is configured as an -bit timer and is changed to a 16-bit timer on the same cycle as a rollover occurs, no interrupt is generated. If Timer0 is configured as a 16-bit timer and is changed to an -bit timer on the same cycle as a rollover occurs, the TMR0IF bit will be set. TMR0H is not the high byte of the timer/counter, but actually a buffered version of the high byte of Timer0. The high byte of the Timer0 counter/timer is not directly readable or writable. TMR0H is updated with the contents of the high byte of Timer0 during a read of TMR0L. This provides a user with the ability to read all 16 bits of Timer0 without having to verify that the read of the high and low byte were valid due to a rollover between successive reads of the high and low byte. The user simply reads the low byte of Timer0, followed by a read of TMR0H, which contains the value in the high byte of Timer0 at the time that the low byte was read. A write to the high byte of Timer0 must also take place through the TMR0H buffer register. Timer0 high byte is updated with the contents of TMR0H when a write occurs to TMR0L. This allows a user to update all 16 bits to both the high and low bytes of Timer0 at once (see Figure 13-2). When performing a write of TMR0, the carry is held off during the write of the TMR0L register. Writes to the TMR0H register only modify the holding latch, not the timer (TMR0<15:>). Steps to write to the TMR0: 1. Load the TMR0H register. 2. Write to the TMR0L register. Read-modify-write instructions like BSF or BCF, read the contents of a register, make the appropriate changes, and place the result back into the register. The read cycle of a read-modify-write instruction of TMR0L will not update the contents of the TMR0H buffer. The TMR0H buffer will remain unchanged. When the write cycle (to TMR0L) of the instruction takes place, the contents of TMR0H are placed into the high byte of Timer0. DS39513A-page 13-4 2000 Microchip Technology Inc.

Section 13. Timer0 13.3.2 Timer/Counter Modes 13.4 Timer0 Interrupt Figure 13-3: Timer mode is selected by clearing the T0CS bit (T0CON register). In timer mode, the Timer0 module will increment every instruction cycle (without prescaler). If the TMR0 register is written, the increment is inhibited for the following two instruction cycles. The user can work around this by writing an adjusted value to the TMR0 register. Counter mode is selected by setting the T0CS bit (T0CON register). In counter mode, Timer0 will increment either on every rising or falling edge of the T0CKI pin. The incrementing edge is determined by the Timer0 Source Edge Select bit T0SE (T0CON register). Clearing the T0SE bit selects the rising edge. Restrictions on the external clock input are discussed in detail in Section 13.5.1. The TMR0 interrupt flag bit is set when the TMR0 register overflows. When TMR0 is in -bit mode, this means the overflow from FFh to 00h. When TMR0 is in 16-bit mode, this means the overflow from FFFFh to 0000h. This overflow sets the TMR0IF bit (INTCON register). The interrupt can be disabled by clearing the TMR0IE bit (INTCON register). The TMR0IF bit must be cleared in software by the interrupt service routine. The TMR0 interrupt cannot awaken the processor from SLEEP, since the timer is shut off during SLEEP. See Figure 13-3 for Timer0 interrupt timing. TMR0 Interrupt Timing OSC1 CLKO (3) Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 13 -bit Timer0 TMR0IF bit (INTCON<2>) FEh 1 1 FFh 00h 01h 02h 16-bit Timer0 FFFEh FFFFh 0000h 0001h 0002h 1 Timer0 GIE bit (INTCON<7>) INSTRUCTION FLOW PC PC PC + 2 PC + 4 PC + 4 000h Instruction fetched Inst (PC) Inst (PC+2) Inst (PC+4) Inst (000h) Instruction executed Inst (PC-2) Inst (PC) Inst (PC+2) Dummy cycle Dummy cycle Note 1: Interrupt flag bit TMR0IF is sampled here (every Q1). 2: Interrupt latency = 4TCY where TCY = instruction cycle time. 3: CLKO is available only in RC oscillator mode. 2000 Microchip Technology Inc. DS39513A-page 13-5

PIC1C Reference Manual 13.5 Using Timer0 with an External Clock When an external clock input is used for Timer0, it must meet certain requirements as detailed in 13.5.1 External Clock Synchronization. The requirements ensure the external clock can be synchronized with the internal phase clock (TSCLK). Also, there is a delay in the actual incrementing of Timer0 after synchronization. 13.5.1 External Clock Synchronization 13.5.2 TMR0 Increment Delay When no prescaler is used, the external clock input is used instead of the prescaler output. The synchronization of T0CKI with the internal phase clocks is accomplished by sampling the prescaler output on the Q2 and Q4 cycles of the internal phase clocks (Figure 13-4). Therefore, it is necessary for T0CKI to be high for at least 2TSCLK (and a small RC delay) and low for at least 2TSCLK (and a small RC delay). Refer to parameters 40, 41 and 42 in the electrical specification of the desired device. When a prescaler is used, the external clock input is divided by the prescaler so that the prescaler output is symmetrical. For the external clock to meet the sampling requirement, the ripple-counter must be taken into account. Therefore, it is necessary for T0CKI to have a period of at least 4TSCLK (and a small RC delay) divided by the prescaler value. The only requirement on T0CKI high and low time is that they do not violate the minimum pulse width requirement. Refer to parameters 40, 41 and 42 in the electrical specification of the desired device. Since the prescaler output is synchronized with the internal clocks, there is a small delay from the time the external clock edge occurs to the time the Timer0 module is actually incremented. Figure 13-4 shows the delay from the external clock edge to the timer incrementing. Figure 13-4: Timer0 Timing with External Clock External Clock Input or Prescaler output (2) External Clock/Prescaler Output after sampling Increment Timer0 (Q4) Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Small pulse misses sampling (3) (1) Timer0 T0 T0 + 1 T0 + 2 Note 1: Delay from clock input change to Timer0 increment is 3Tosc to 7Tosc. (Duration of Q = Tosc). Therefore, the error in measuring the interval between two edges on Timer0 input = ±4Tosc max. 2: External clock if no prescaler selected, prescaler output otherwise. 3: The arrows indicate the points in time where sampling occurs. DS39513A-page 13-6 2000 Microchip Technology Inc.

Section 13. Timer0 13.6 Timer0 Prescaler Figure 13-5: An -bit counter is available as a prescaler for the Timer0 module (Figure 13-5). The PSA and T0PS2:T0PS0 bits (T0CON register) are the prescaler enable and prescale select bits. All instructions that write to the Timer0 (TMR0) register (such as: CLRF TMR0; BSF TMR0,x; MOVWF TMR0;...etc.) will clear the prescaler if enabled. The prescaler is not readable or writable. Writes to TMR0H do not clear the Timer0 prescaler in 16-bit mode, because a write to TMR0H only modifies the Timer0 latch and does not change the contents of Timer0. The prescaler is only cleared on writes to TMR0L. Block Diagram of the Timer0 Prescaler CLKO (=FOSC/4) Data Bus T0CKI pin T0SE T0PS2:T0PS0 0 M 0 U X 1 1 T0CS -bit Prescaler -to-1mux M U X PSA Synchronization 2TCY delay T0BIT TMR0L T0BIT TMR0 high reg TMR0H Data Bus Set flag bit TMR0IF on overflow for TMR0L Set flag bit TMR0IF on overflow 13 Note: T0CS,T0SE,PSA,T0PS2:T0PS0arelocatedintheT0CONregister. The prescaler for Timer0 is enabled or disabled in software by the PSA bit (T0CON register). Setting the PSA bit will enable the prescaler. The prescaler can be modified under software control through the T0PS2:T0PS0 bits. This allows the prescaler reload value to be readable and writable. The prescaler count value (the contents of the prescaler) can not be read or written. When the prescaler is enabled, prescale values of 1:2, 1:4,..., 1:256 are selectable. Timer0 2000 Microchip Technology Inc. DS39513A-page 13-7

PIC1C Reference Manual Any write to the TMR0 register will cause a 2 instruction cycle (2TCY) inhibit. That is, after the TMR0 register has been written with the new value, TMR0 will not be incremented until the third instruction cycle later (Figure 13-6). When the prescaler is assigned to the Timer0 module, any write to the TMR0 register will immediately update the TMR0 register and clear the prescaler. The incrementing of Timer0 (TMR0 and Prescaler) will also be inhibited 2 instruction cycles (TCY). So if the prescaler is configured as 2, then after a write to the TMR0 register, TMR0 will not increment for 4 Timer0 clocks (Figure 13-7). After that, TMR0 will increment every prescaler number of clocks later. Figure 13-6: Timer0 Timing: Internal Clock/No Prescale PC (Program Counter) Instruction Fetch Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 PC-2 PC PC+2 PC+4 PC+6 PC+ PC+10 PC+12 MOVWF TMR0 MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W TMR0 T0 T0+1 T0+2 NT0 NT0 NT0 NT0+1 NT0+2 T0 Instruction Executed Write TMR0 executed + 1 + 2 Figure 13-7: Timer0 Timing: Internal Clock/Prescale 1:2 PC (Program Counter) Instruction Fetch Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 PC-2 PC PC+2 PC+4 PC+6 PC+ PC+10 PC+12 MOVWF TMR0 MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W MOVF TMR0,W TMR0 T0 T0+1 NT0 NT0+1 PC+6 Instruction Execute Write TMR0 executed + 1 Table 13-1: Registers Associated with Timer0 Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Value on POR, BOR Value on all other resets TMR0L Timer0 Module s Low Byte Register xxxx xxxx uuuu uuuu TMR0H Timer0 Module s High Byte Register 0000 0000 0000 0000 INTCON GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF 0000 000x 0000 000u T0CON TMR0ON T0BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0 1111 1111 1111 1111 TRISA PORTA Data Direction Register --11 1111 --11 1111 Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. Shaded cells are not used by Timer0. DS39513A-page 13-2000 Microchip Technology Inc.

Section 13. Timer0 13.7 Initialization Since Timer0 has a software programmable clock source, there are two examples to show the initialization of Timer0 with each source. Example 13-1 shows the initialization for the internal clock source (timer mode), while Example 13-2 shows the initialization for the external clock source (counter mode). Example 13-1: Timer0 Initialization (Internal Clock Source) CLRF TMR0 ; Clear Timer0 register CLRF INTCON ; Disable interrupts and clear T0IF BCF INTCON2, RBPU ; MOVLW 0x0 ; PortB pull-ups are disabled, MOVWF T0CON ; Interrupt on rising edge of RB0, ; TMR0 = 16-Bit Time ; Timer0 increment from internal clock ; with a prescaler of 1:2. ;** BSF INTCON, T0IE ; Enable TMR0 interrupt ;** BSF INTCON, GIE ; Enable all interrupts ; ; The TMR0 interrupt is disabled, do polling on the overflow bit ; T0_OVFL_WAIT BTFSS INTCON, T0IF GOTO T0_OVFL_WAIT ; Timer has overflowed Example 13-2: Timer0 Initialization (External Clock Source) 13 CLRF TMR0 ; Clear Timer0 register CLRF INTCON ; Disable interrupts and clear T0IF BCF INTCON2, RBPU ; MOVLW 0xBF ; PortB pull-ups are enabled, MOVWF T0CON ; Interrupt on falling edge of RB0 ; Timer0 increment from external clock ; on the high-to-low transition ; of T0CKI ; with a prescaler of 1:256. ;** BSF INTCON, T0IE ; Enable TMR0 interrupt ;** BSF INTCON, GIE ; Enable all interrupts ; ; The TMR0 interrupt is disabled, do polling on the overflow bit ; T0_OVFL_WAIT BTFSS INTCON, T0IF GOTO T0_OVFL_WAIT ; Timer has overflowed Timer0 2000 Microchip Technology Inc. DS39513A-page 13-9

PIC1C Reference Manual 13. Design Tips Question 1: I am implementing a counter/clock, but the clock loses time or is inaccurate. Answer 1: If you are polling TMR0 to see if it has rolled over to zero, you could do this by executing: wait MOVF TMR0,W ; read the timer into W BTFSS STATUS,Z ; see if it was zero, if so, ; break from loop GOTO wait ; if not zero yet, keep waiting Two possible scenarios to lose clock cycles are: 1. If you are incrementing TMR0 from the internal instruction clock (or an external source that is about as fast), the overflow could occur during the two cycle GOTO, soyoucouldmiss it. In this case, the TMR0 source should be prescaled. 2. When writing to TMR0, two instruction clock cycles are lost. Often you have a specific time period you want to count, say 100 decimal. In that case, you might put 156 into TMR0 (256-100 = 156). However, since two instruction cycles are lost when you write to TMR0 (for internal logic synchronization), you should actually write 15 to the timer. DS39513A-page 13-10 2000 Microchip Technology Inc.

Section 13. Timer0 13.9 Related Application Notes This section lists application notes that are related to this section of the manual. These application notes may not be written specifically for the Enhanced family (that is, they may be written for the Base-Line, the Mid-Range or High-End families), but the concepts are pertinent, and could be used (with modification and possible limitations). The current application notes related to Timer0 are: Title Application Note # Frequency Counter Using PIC16C5X AN592 A Clock Design using the PIC16C54 for LED Display and Switch Inputs AN590 Note: Please visit the Microchip Web site for additional software code examples. These code examples are stand alone examples to assist in the understanding of the PIC1CXXX. The web address for these examples is: http://www.microchip.com/10/faqs/codeex/ 13 Timer0 2000 Microchip Technology Inc. DS39513A-page 13-11

PIC1C Reference Manual 13.10 Revision History Revision A This is the initial released revision of the Enhanced MCU Timer0 Module description. DS39513A-page 13-12 2000 Microchip Technology Inc.