1 |
/** |
---|
2 |
TMR1 Generated Driver File |
---|
3 |
|
---|
4 |
@Company |
---|
5 |
Microchip Technology Inc. |
---|
6 |
|
---|
7 |
@File Name |
---|
8 |
tmr1.c |
---|
9 |
|
---|
10 |
@Summary |
---|
11 |
This is the generated driver implementation file for the TMR1 driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs |
---|
12 |
|
---|
13 |
@Description |
---|
14 |
This source file provides APIs for TMR1. |
---|
15 |
Generation Information : |
---|
16 |
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.65.2 |
---|
17 |
Device : PIC16F1579 |
---|
18 |
Driver Version : 2.11 |
---|
19 |
The generated drivers are tested against the following: |
---|
20 |
Compiler : XC8 1.45 |
---|
21 |
MPLAB : MPLAB X 4.15 |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
/* |
---|
25 |
(c) 2018 Microchip Technology Inc. and its subsidiaries. |
---|
26 |
|
---|
27 |
Subject to your compliance with these terms, you may use Microchip software and any |
---|
28 |
derivatives exclusively with Microchip products. It is your responsibility to comply with third party |
---|
29 |
license terms applicable to your use of third party software (including open source software) that |
---|
30 |
may accompany Microchip software. |
---|
31 |
|
---|
32 |
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER |
---|
33 |
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY |
---|
34 |
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS |
---|
35 |
FOR A PARTICULAR PURPOSE. |
---|
36 |
|
---|
37 |
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, |
---|
38 |
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND |
---|
39 |
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP |
---|
40 |
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO |
---|
41 |
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL |
---|
42 |
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT |
---|
43 |
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS |
---|
44 |
SOFTWARE. |
---|
45 |
*/ |
---|
46 |
|
---|
47 |
/** |
---|
48 |
Section: Included Files |
---|
49 |
*/ |
---|
50 |
|
---|
51 |
#include <xc.h> |
---|
52 |
#include "tmr1.h" |
---|
53 |
|
---|
54 |
/** |
---|
55 |
Section: Global Variables Definitions |
---|
56 |
*/ |
---|
57 |
volatile uint16_t timer1ReloadVal; |
---|
58 |
void (*TMR1_InterruptHandler)(void); |
---|
59 |
void (*TMR1G_InterruptHandler)(void); |
---|
60 |
|
---|
61 |
/** |
---|
62 |
Section: TMR1 APIs |
---|
63 |
*/ |
---|
64 |
|
---|
65 |
void TMR1_Initialize(void) |
---|
66 |
{ |
---|
67 |
//Set the Timer to the options selected in the GUI |
---|
68 |
|
---|
69 |
//T1GSS T1G_pin; TMR1GE enabled; T1GTM disabled; T1GPOL high; T1GGO done; T1GSPM enabled; |
---|
70 |
T1GCON = 0xD0; |
---|
71 |
|
---|
72 |
//TMR1H 0; |
---|
73 |
TMR1H = 0x00; |
---|
74 |
|
---|
75 |
//TMR1L 0; |
---|
76 |
TMR1L = 0x00; |
---|
77 |
|
---|
78 |
// Load the TMR value to reload variable |
---|
79 |
timer1ReloadVal=(uint16_t)((TMR1H << 8) | TMR1L); |
---|
80 |
|
---|
81 |
// Clearing IF flag before enabling the interrupt. |
---|
82 |
PIR1bits.TMR1IF = 0; |
---|
83 |
|
---|
84 |
// Enabling TMR1 interrupt. |
---|
85 |
PIE1bits.TMR1IE = 1; |
---|
86 |
|
---|
87 |
// Set Default Interrupt Handlers |
---|
88 |
TMR1_SetInterruptHandler(TMR1_DefaultInterruptHandler); |
---|
89 |
TMR1G_SetInterruptHandler(TMR1G_DefaultInterruptHandler); |
---|
90 |
// Clearing IF flag before enabling the interrupt. |
---|
91 |
PIR1bits.TMR1GIF = 0; |
---|
92 |
|
---|
93 |
// Enabling TMR1 interrupt. |
---|
94 |
PIE1bits.TMR1GIE = 1; |
---|
95 |
|
---|
96 |
// T1CKPS 1:1; nT1SYNC synchronize; TMR1CS FOSC/4; TMR1ON enabled; |
---|
97 |
T1CON = 0x01; |
---|
98 |
} |
---|
99 |
|
---|
100 |
void TMR1_StartTimer(void) |
---|
101 |
{ |
---|
102 |
// Start the Timer by writing to TMRxON bit |
---|
103 |
T1CONbits.TMR1ON = 1; |
---|
104 |
} |
---|
105 |
|
---|
106 |
void TMR1_StopTimer(void) |
---|
107 |
{ |
---|
108 |
// Stop the Timer by writing to TMRxON bit |
---|
109 |
T1CONbits.TMR1ON = 0; |
---|
110 |
} |
---|
111 |
|
---|
112 |
uint16_t TMR1_ReadTimer(void) |
---|
113 |
{ |
---|
114 |
uint16_t readVal; |
---|
115 |
uint8_t readValHigh; |
---|
116 |
uint8_t readValLow; |
---|
117 |
|
---|
118 |
|
---|
119 |
readValLow = TMR1L; |
---|
120 |
readValHigh = TMR1H; |
---|
121 |
|
---|
122 |
readVal = ((uint16_t)readValHigh << 8) | readValLow; |
---|
123 |
|
---|
124 |
return readVal; |
---|
125 |
} |
---|
126 |
|
---|
127 |
void TMR1_WriteTimer(uint16_t timerVal) |
---|
128 |
{ |
---|
129 |
if (T1CONbits.nT1SYNC == 1) |
---|
130 |
{ |
---|
131 |
// Stop the Timer by writing to TMRxON bit |
---|
132 |
T1CONbits.TMR1ON = 0; |
---|
133 |
|
---|
134 |
// Write to the Timer1 register |
---|
135 |
TMR1H = (timerVal >> 8); |
---|
136 |
TMR1L = timerVal; |
---|
137 |
|
---|
138 |
// Start the Timer after writing to the register |
---|
139 |
T1CONbits.TMR1ON =1; |
---|
140 |
} |
---|
141 |
else |
---|
142 |
{ |
---|
143 |
// Write to the Timer1 register |
---|
144 |
TMR1H = (timerVal >> 8); |
---|
145 |
TMR1L = timerVal; |
---|
146 |
} |
---|
147 |
} |
---|
148 |
|
---|
149 |
void TMR1_Reload(void) |
---|
150 |
{ |
---|
151 |
TMR1_WriteTimer(timer1ReloadVal); |
---|
152 |
} |
---|
153 |
|
---|
154 |
void TMR1_StartSinglePulseAcquisition(void) |
---|
155 |
{ |
---|
156 |
T1GCONbits.T1GGO = 1; |
---|
157 |
} |
---|
158 |
|
---|
159 |
uint8_t TMR1_CheckGateValueStatus(void) |
---|
160 |
{ |
---|
161 |
return (T1GCONbits.T1GVAL); |
---|
162 |
} |
---|
163 |
|
---|
164 |
void TMR1_ISR(void) |
---|
165 |
{ |
---|
166 |
|
---|
167 |
// Clear the TMR1 interrupt flag |
---|
168 |
PIR1bits.TMR1IF = 0; |
---|
169 |
TMR1_WriteTimer(timer1ReloadVal); |
---|
170 |
|
---|
171 |
if(TMR1_InterruptHandler) |
---|
172 |
{ |
---|
173 |
TMR1_InterruptHandler(); |
---|
174 |
} |
---|
175 |
} |
---|
176 |
|
---|
177 |
|
---|
178 |
void TMR1_SetInterruptHandler(void (* InterruptHandler)(void)){ |
---|
179 |
TMR1_InterruptHandler = InterruptHandler; |
---|
180 |
} |
---|
181 |
|
---|
182 |
void TMR1G_SetInterruptHandler(void (* InterruptHandler)(void)){ |
---|
183 |
TMR1G_InterruptHandler = InterruptHandler; |
---|
184 |
} |
---|
185 |
|
---|
186 |
void TMR1_DefaultInterruptHandler(void){ |
---|
187 |
// add your TMR1 interrupt custom code |
---|
188 |
// or set custom function using TMR1_SetInterruptHandler() |
---|
189 |
} |
---|
190 |
|
---|
191 |
void TMR1G_DefaultInterruptHandler(void){ |
---|
192 |
// add your TMR1G interrupt custom code |
---|
193 |
// or set custom function using TMR1G_SetInterruptHandler() |
---|
194 |
} |
---|
195 |
|
---|
196 |
void TMR1_GATE_ISR(void) |
---|
197 |
{ |
---|
198 |
// clear the TMR1 interrupt flag |
---|
199 |
PIR1bits.TMR1GIF = 0; |
---|
200 |
if(TMR1G_InterruptHandler) |
---|
201 |
{ |
---|
202 |
TMR1G_InterruptHandler(); |
---|
203 |
} |
---|
204 |
} |
---|
205 |
|
---|
206 |
void TMR1G_Polarity(uint8_t pol) |
---|
207 |
{ |
---|
208 |
if (pol) |
---|
209 |
T1GCONbits.T1GPOL = 1; |
---|
210 |
else |
---|
211 |
T1GCONbits.T1GPOL = 0; |
---|
212 |
} |
---|
213 |
|
---|
214 |
/** |
---|
215 |
End of File |
---|
216 |
*/ |
---|