-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmr_delay.c
35 lines (32 loc) · 1.58 KB
/
tmr_delay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Revision 1.0
* File: tmr_delay.c
* Author: David Hladky
* Library for the initialization and generation of a delay on the TIMER1.
* Created in July 16, 2017, 15:00
*/
#include "tmr_delay.h"
void TMR1DelayUs(uint16_t DelayUs) // This function creates max delay approx. 4095 us.
{
T1CONbits.TON = 0; // Disable the TIMER1.
TMR1 = 0x0000; // Reset the counter register of the TIMER1.
PR1 = 16*DelayUs; // Set the period register. An value in the period register determines the period when an owerflow occurs.
IFS0bits.T1IF = 0; // Clear interrupt flag for the TIMER1.
T1CONbits.TCKPS = 0b00; // Set the prescaler to 1 -> 16 MHz = 62.5 ns
T1CONbits.TON = 1; // Enable the TIMER1.
while(IFS0bits.T1IF == 0); // Wait until the TIMER1 overflow.
IFS0bits.T1IF = 0; // Clear interrupt flag for the TIMER1.
T1CONbits.TON = 0; // Disable the TIMER1.
}
void TMR1DelayMs(uint16_t DelayMs) // This function creates max delay approx. 262 ms.
{
T1CONbits.TON = 0; // Disable the TIMER1.
TMR1 = 0x0000; // Reset the counter register of the TIMER1.
PR1 = 250*DelayMs; // Set the period register. An value in the period register determines the period when an owerflow occurs.
IFS0bits.T1IF = 0; // Clear interrupt flag for the TIMER1.
T1CONbits.TCKPS = 0b10; // Set the prescaler to 64 -> 16 MHz = 4 us
T1CONbits.TON = 1; // Enable the TIMER1.
while(IFS0bits.T1IF == 0); // Wait until the TIMER1 overflow.
IFS0bits.T1IF = 0; // Clear interrup flag for the TIMER1.
T1CONbits.TON = 0; // Disable the TIMER1.
}