root / Version 0.9 / RS232_MUX.X / hardware_timer.h @ master
Historique | Voir | Annoter | Télécharger (2,955 ko)
1 | 10dcb6e9 | Enzo Niro | /* Microchip Technology Inc. and its subsidiaries. You may use this software
|
---|---|---|---|
2 | * and any derivatives exclusively with Microchip products.
|
||
3 | *
|
||
4 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
|
||
5 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
|
||
6 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
|
||
7 | * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
|
||
8 | * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
|
||
9 | *
|
||
10 | * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
|
||
11 | * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
|
||
12 | * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
|
||
13 | * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
|
||
14 | * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
|
||
15 | * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
|
||
16 | * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
|
||
17 | *
|
||
18 | * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
|
||
19 | * TERMS.
|
||
20 | */
|
||
21 | |||
22 | /*
|
||
23 | * File: hardware_timer.h
|
||
24 | * Author: Enzo Niro
|
||
25 | * Comments: Library made for RS232 MUX board
|
||
26 | * Revision history: 1.0
|
||
27 | */
|
||
28 | |||
29 | // This is a guard condition so that contents of this file are not included
|
||
30 | // more than once.
|
||
31 | #ifndef RS232_HARDWARETIMER_H
|
||
32 | #define RS232_HARDWARETIMER_H
|
||
33 | |||
34 | #include <xc.h> // include processor files - each processor file is guarded. |
||
35 | |||
36 | |||
37 | #define CLK_DIV1 0x00 |
||
38 | #define CLK_DIV2 0x01 |
||
39 | #define CLK_DIV4 0x02 |
||
40 | #define CLK_DIV8 0x03 |
||
41 | #define CLK_DIV16 0x04 |
||
42 | #define CLK_DIV64 0x05 |
||
43 | #define CLK_DIV256 0x06 |
||
44 | #define CLK_DIV1024 0x07 |
||
45 | |||
46 | #define SEL_CLK CLK_DIV1
|
||
47 | |||
48 | #ifdef __cplusplus
|
||
49 | extern "C" { |
||
50 | #endif /* __cplusplus */ |
||
51 | |||
52 | |||
53 | uint32_t _timerA0Cnt = 0;
|
||
54 | |||
55 | //Timer interrupt init
|
||
56 | void timerInterruptInit(uint16_t maxCounts)
|
||
57 | { |
||
58 | //Init Timer interrupt type A
|
||
59 | //To avoid fast interruption that slow main program, divide clock by 2 !
|
||
60 | //We use here timer interrupt 0 in normal mode
|
||
61 | |||
62 | //At first, define the timer period
|
||
63 | TCA0.SINGLE.PERH = maxCounts >> 8;
|
||
64 | TCA0.SINGLE.PERL = maxCounts & 0xFF;
|
||
65 | TCA0.SINGLE.CTRLA = (SEL_CLK << 1) | 1; //Enable and CLK_DIV2 |
||
66 | TCA0.SINGLE.CTRLB = 0x00; //Set Normal operation for timer, no autolock, no waveform output |
||
67 | TCA0.SINGLE.EVCTRL = 0x00; //No specific action and counter event input |
||
68 | TCA0.SINGLE.INTCTRL = 0x01; //Set interrupt on timer overflow |
||
69 | } |
||
70 | |||
71 | //Get number of subcounts made
|
||
72 | uint32_t getTimerCounts(void)
|
||
73 | { |
||
74 | return _timerA0Cnt;
|
||
75 | } |
||
76 | |||
77 | //Timer interrupt callback
|
||
78 | ISR(TCA0_OVF_vect) |
||
79 | { |
||
80 | _timerA0Cnt++; |
||
81 | TCA0.SINGLE.INTFLAGS = 1; //Must clear the flag by writing 1 on this bit (datasheet p.232) |
||
82 | } |
||
83 | |||
84 | |||
85 | #ifdef __cplusplus
|
||
86 | } |
||
87 | #endif /* __cplusplus */ |
||
88 | |||
89 | #endif /* XC_HEADER_TEMPLATE_H */ |