16F PIC TMR1 Example

   

Written With Proton

 The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt enable bit, TMR1IE (PIE1<0>).  

Timer1 can operate in one of two modes:

  • As a Timer
  • As a Counter
The operating mode is determined by the clock select bit, TMR1CS (T1CON<1>). In Timer mode, Timer1 increments every instruction cycle. In Counter mode, it increments on every rising edge of the external clock input.

    The above paragraphs are exact copies to that in the 16F87XA datasheet. It sums it up fairly well. So basically TMR1 has a 2 bit pre-scaler, and a 16bit register that can be configured to increment on an external clock, or internal FOSC (OSC/4).

    There is a little more to setup than TMR0, but TMR1 is still a great tool for many applications, here's an example of how to set up your program for use with TMR1 (The TMR1 Calculator was used to configure the settings in this program);

Device = 16F877
Xtal = 4
   		   
Dim uS as Word
Dim mS as Word
Dim S as Word	

Symbol GIE = INTCON.7				' Global Interrupt Enable Bit

Symbol TMR1_Val = 64536		  		' Set the initial value of TMR1
Symbol TMR1_mS = 1			  	' Time period of TMR1					
Symbol Timer1 = TMR1L.WORD			' A special way of addressing both TMR1L and TMR1H with one register
Symbol TMR1_Enable = PIE1.0			' TMR1 interrupt enable
Symbol TMR1_Overflow = PIR1.0			' TMR1 overflow flag
Symbol TMR1_On = T1CON.0			' Enables TMR1 to start incrementing
			
ON_INTERRUPT Int_Sub

Goto Initialization			
			
Int_Sub:
	
	GIE = 0			
			
	If TMR1_Overflow = 1 And TMR1_Enable = 1 Then
	   TMR1_Enable = 0
	   Timer1 = Timer1 + TMR1_Val
	   TMR1_Enable = 1
	   TMR1_Overflow = 0
	   mS = mS + TMR1_mS
	   If mS >= 1000 Then
	   	  mS = mS - 1000
		  S = S + 1
	   EndIf
	EndIf	
	
	GIE = 1
	
	Context Restore
	
Initialization:	
	
	TMR1_Enable = 0
		
	INTCON.6 = 1   	  	' Peripheral Interrupts

	T1CON.1 = 0		' 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
			   	' 0 = Internal clock (FOSC/4)
	'TRISC.0 = 1	  	' If External clock, then set clock as an input
	'HPWM 1,128,32000   	' Set TMR1's External Source	
	T1CON.2 = 1		' 1 = Do not synchronize external clock input
			   	' 0 = Synchronize external clock input
				' When T1CON.1 = 0;
				'   this bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
					  	
	T1CON.4 = 0		' 11 = 1:8 prescale value
	T1CON.5 = 0		' 10 = 1:4 prescale value
				' 01 = 1:2 prescale value
				' 00 = 1:1 prescale value
	Timer1 = TMR1_Val
								  
	TMR1_Enable = 1						  
	TMR1_On = 1		
	
	GIE = 1	
	
	
Main:

	Repeat 				' Create a loop
	
	Until S = 60		  	' That waits for 60 seconds
	
	' 60 seconds has occurred
	
	S = 0                           ' Clear the seconds register
	
	Goto Main			' Loop for ever	

 Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Collapse 16F PIC Examples16F PIC Examples
LED's
Switches
Keypads
LCD's
7 Seg Displays
ADC
DAC
Expand MotorsMotors
Expand ThermometersThermometers
DS1307 RTC Chip
DS275
EEPROM's
RF Modules
RGB LED's
Code Snippets
Collapse Code SnippetsCode Snippets
PIC's EEPROM
Collapse Internal TimersInternal Timers
TMR0
TMR1
TMR2
Expand 18F PIC Examples18F PIC Examples
Expand Handy TipsHandy Tips