External Links

Creative Science Centre

 

Timers

The MX1 family or the one we are using has 5 timers. Timer 1 we will leave for now. Timers 2,3,4 and 5 are four 16 bit timers but 2/3 and 4/5 can be combined to make two 32 bit timers.

The operation is quite straightforward in that there is a TMR register that is constantly incrementing when it reached the value set in the PR register it resets to 0 and sets a flag. It is up to the user to clear the flag for next time. The clock feeding the timer is the peripheral clock that on this system is running at 40MHz (0.025us)

http://byvac.com/mBlib/flb/Tutorial/PIC32MX1_Family/15_Timers/Project_1.bas

// Timer projects
//
#option echo off
#option only on   // only load functions that don't already exists

// include all of the registers although only some are needed
#include "http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_timer.bas"
// interrupts hold the flag addresses
#include "http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_interrupt.bas"

constant TIMERON 1 << 15 // on bit  

// *****************************************************************************
// clocked with PCLK that is running at 40MHz, prescale 256 = 156.25kHz (6.4uS)
// setting PR2 1/6.4uS = 156250 should see the counter reset every second so 
// using 1562500 is 10 seconds.
// *****************************************************************************
function t23_set()
	@TMR2=0  // clear counter
	// timer_on | prescale_256 | 32_bit_on | Internal_clock
	@T2CON = 0x8078
	@PR2=1562500
endf 


// just to look at timer values
function t23_see()
	while comkey?(2) = 0
		print @TMR2,"\n"
		wait(500)	
	wend
endf

Load this project and type:

t23_set
t23_see

This will set timer combination 2/3 to reset every 10 seconds and you can more or less see this happening as the numbers go past. When the timer gets near 1562500 it will reset back to 0. The timer also sets the T3IF flag of register IFS0, you can see this by typing:

print hex$(@IFS0)

and you will see that it is 0×4000 there could also be other bits that are set, T3IF is bit 14 of this register. To verify this check with the PIC32 MX1/2 family data sheet. Once set it stays set until cleared by you the user. Although the flag is part of the interrupt system it is set regardless of whether the interrupt is enabled or not. To illustrate this point load the next code:

http://byvac.com/mBlib/flb/Tutorial/PIC32MX1_Family/15_Timers/Project_1a.bas

// Timer projects
//
#option echo off
#option only on   // only load functions that don't already exists

// include all of the registers although only some are needed
#include "http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_timer.bas"
// interrupts hold the flag addresses
#include "http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_interrupt.bas"

constant TIMERON 1 << 15 // on bit
constant T3IF 1 << 14  

// *****************************************************************************
// clocked with PCLK that is running at 40MHz, prescale 256 = 156.25kHz (6.4uS)
// setting PR2 1/6.4uS = 156250 should see the counter reset every second so 
// using 1562500 is 10 seconds.
// *****************************************************************************
function t23_set()
	@TMR2=0  // clear counter
	// timer_on | prescale_256 | 32_bit_on | Internal_clock
	@T2CON = 0x8078
	@PR2=1562500
endf 


// just to look at timer values
function t23_test()
	t23_set()
	while comkey?(2) = 0
		if (peek(IFS0) & T3IF) = T3IF
			print "\n Flag triggered"
			@IFS0CLR = T3IF // must clear flag
		endif
	wend
endf

This is an example of using the flag to test if the timer has been triggered (trigger when TMRn=PRn), load and type

t23_test

You should see a message every 10 seconds this is because the loop is looking for this flag to be set, when it is it prints out the message and clears the flag.

Recap

All of the timers work in a similar way. As always it is a matter of setting registers to get the timer to do what you want. The timer can also output to an external pin if required and also be driven by an external clock so a pulse counter can be implemented.