libpic170x  0.2
Ease of use library for PIC16(L)1705/1709 chips
timer0.c
1 /*
2  Copyright 2018 Paul Konstantin Gerke
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
17 #include "libpic170x/timer0.h"
18 #include "libpic170x/freq.h"
19 
20 #include <xc.h>
21 
23 
24 void timer0_init(Timer0 *timer0) {
25  if (!timer0) timer0 = &pic170x_timer0;
26 
27  // Enable timer mode
28  TMR0CS = 0;
29 
30  // Enable prescaler 1:256
31  OPTION_REG = (unsigned char) (OPTION_REG & 0b11010000) | TIMER0_PRESCALE_BITS;
32 
33  // Enable interrupts
34  TMR0IE = 1;
35 
36  timer0->ms = 0;
37  timer0->us = 0;
38 }
39 
40 void timer0_ih(Timer0* timer0) {
41  if (TMR0IF) {
42  if (!timer0) timer0 = &pic170x_timer0;
43 
44 #if TIMER0_MS_INC > 0
45  timer0->ms += TIMER0_MS_INC;
46 #endif
47  if ((timer0->us += TIMER0_US_INC) > 1000) {
48  timer0->ms += timer0->us / 1000;
49  timer0->us %= 1000;
50  }
51  TMR0IF = 0;
52  }
53 }
Timer0 pic170x_timer0
Definition: timer0.c:22
void timer0_ih(Timer0 *timer0)
Definition: timer0.c:40
void timer0_init(Timer0 *timer0)
Definition: timer0.c:24
uint16_t us
Definition: timer0.h:56
Definition: timer0.h:49
Coarse timing library for measuring times in the background.
uint32_t ms
Definition: timer0.h:52
Global timing definitions used for timing critical library components.