timer.c
Go to the documentation of this file.
1 /*
2  * File: timer.c
3  * Author: Chris Hajduk
4  *
5  * Created on July 19, 2015, 8:07 PM
6  */
7 
8 #include "main.h"
9 #include "timer.h"
10 
11 static unsigned long int time = 0;
12 
16 void initTimer2(void)
17 {
18  T2CONbits.TON = 0; // Disable Timer
19  T2CONbits.TCS = 0; // Select internal instruction cycle clock
20  T2CONbits.TGATE = 0; // Disable Gated Timer mode
21  TMR2 = 0x00; // Clear timer register
22  T2CONbits.TCKPS = 0x02; //1:64 scaler
23  PR2 = T2_PERIOD * T2_TICKS_TO_MSEC; //set the period
24  IPC1bits.T2IP = 0x01; // Set Timer 2 Interrupt Priority Level - Lowest
25  IFS0bits.T2IF = 0; // Clear Timer 2 Interrupt Flag
26  IEC0bits.T2IE = 0; // Disable Timer 2 interrupt
27  T2CONbits.TON = 1; // Start Timer
28 }
29 
33 void initTimer4(){
34  T4CONbits.TON = 0; // Disable Timer
35  T4CONbits.TCS = 0; // Select internal instruction cycle clock
36  T4CONbits.TGATE = 0; // Disable Gated Timer mode
37  T4CONbits.TCKPS = 0b10; // Select 1:64 Prescaler
38  TMR4 = 0x00; // Clear timer register
39  PR4 = 642; // Load the period value
40  IPC6bits.T4IP = 0x01; // Set Timer1 Interrupt Priority Level
41  IFS1bits.T4IF = 0; // Clear Timer1 Interrupt Flag
42  IEC1bits.T4IE = 1; // Enable Timer1 interrupt
43  T4CONbits.TON = 1; // Start Timer
44 }
45 
49 void __attribute__((__interrupt__, no_auto_psv)) _T4Interrupt(void){
50  time += 1;
51  IFS1bits.T4IF = 0;
52 }
53 
54 long unsigned int getTime(){
55  return time;
56 }
void __attribute__((__interrupt__, no_auto_psv))
Timer4 interrupt.
Definition: timer.c:49
long unsigned int getTime()
Get current time in ms.
Definition: timer.c:54
void initTimer4()
Initializes Timer4 as a 1ms, 16-bit timer.
Definition: timer.c:33
#define T2_PERIOD
Timer2 Period in ms.
Definition: timer.h:27
static unsigned long int time
Definition: timer.c:11
void initTimer2(void)
Initializes Timer2.
Definition: timer.c:16
#define T2_TICKS_TO_MSEC
Number of Timer2 ticks in a millisecond.
Definition: timer.h:20