Solar panel
Loading...
Searching...
No Matches
tmr0.c
Go to the documentation of this file.
1
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
51#include <xc.h>
52#include "tmr0.h"
53
59
60volatile uint16_t timer0ReloadVal;
61
68{
69 // Set TMR0 to the options selected in the User Interface
70
71 //Enable 16bit timer mode before assigning value to TMR0H
72 T0CONbits.T08BIT = 0;
73
74 // TMR0H 158;
75 TMR0H = 0x9E;
76
77 // TMR0L 87;
78 TMR0L = 0x57;
79
80
81 // Load TMR0 value to the 16-bit reload variable
82 timer0ReloadVal = (uint16_t)((TMR0H << 8) | TMR0L);
83
84 // Clear Interrupt flag before enabling the interrupt
85 INTCONbits.TMR0IF = 0;
86
87 // Enabling TMR0 interrupt.
88 INTCONbits.TMR0IE = 1;
89
90 // Set Default Interrupt Handler
92
93 // T0PS 1:2; T08BIT 16-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON disabled; PSA not_assigned;
94 T0CON = 0x18;
95}
96
98{
99 // Start the Timer by writing to TMR0ON bit
100 T0CONbits.TMR0ON = 1;
101}
102
104{
105 // Stop the Timer by writing to TMR0ON bit
106 T0CONbits.TMR0ON = 0;
107}
108
109uint16_t TMR0_ReadTimer(void)
110{
111 uint16_t readVal;
112 uint8_t readValLow;
113 uint8_t readValHigh;
114
115 readValLow = TMR0L;
116 readValHigh = TMR0H;
117 readVal = ((uint16_t)readValHigh << 8) + readValLow;
118
119 return readVal;
120}
121
122void TMR0_WriteTimer(uint16_t timerVal)
123{
124 // Write to the Timer0 register
125 TMR0H = timerVal >> 8;
126 TMR0L = (uint8_t) timerVal;
127}
128
129void TMR0_Reload(void)
130{
131 // Write to the Timer0 register
132 TMR0H = timer0ReloadVal >> 8;
133 TMR0L = (uint8_t) timer0ReloadVal;
134}
135
136void TMR0_ISR(void)
137{
138
139 // clear the TMR0 interrupt flag
140 INTCONbits.TMR0IF = 0;
141
142 // reload TMR0
143 // Write to the Timer0 register
144 TMR0H = timer0ReloadVal >> 8;
145 TMR0L = (uint8_t) timer0ReloadVal;
146
148 {
150 }
151
152 // add your TMR0 interrupt custom code
153}
154
155
156void TMR0_SetInterruptHandler(void (* InterruptHandler)(void)){
157 TMR0_InterruptHandler = InterruptHandler;
158}
159
161 // add your TMR0 interrupt custom code
162 // or set custom function using TMR0_SetInterruptHandler()
163}
164
void TMR0_SetInterruptHandler(void(*InterruptHandler)(void))
Definition: tmr0.c:156
void TMR0_StartTimer(void)
Definition: tmr0.c:97
void TMR0_Reload(void)
Definition: tmr0.c:129
void TMR0_Initialize(void)
Definition: tmr0.c:67
void TMR0_WriteTimer(uint16_t timerVal)
Definition: tmr0.c:122
void(* TMR0_InterruptHandler)(void)
Definition: tmr0.c:58
void TMR0_ISR(void)
Definition: tmr0.c:136
void TMR0_DefaultInterruptHandler(void)
Definition: tmr0.c:160
uint16_t TMR0_ReadTimer(void)
Definition: tmr0.c:109
void TMR0_StopTimer(void)
Definition: tmr0.c:103
volatile uint16_t timer0ReloadVal
Definition: tmr0.c:60