root / Version 1.5 / RS232_MUX.X / hardware_timer.h @ f0dbe1d9
Historique | Voir | Annoter | Télécharger (3,381 ko)
1 | f0dbe1d9 | 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_DIV2
|
||
47 | |||
48 | #ifdef __cplusplus
|
||
49 | extern "C" { |
||
50 | #endif /* __cplusplus */ |
||
51 | |||
52 | |||
53 | //////////////////////////////////////////////
|
||
54 | //prototypes
|
||
55 | void timerInterruptInit(uint16_t maxCounts);
|
||
56 | bool getTimerFlag(void); |
||
57 | void clearTimerFlag(void); |
||
58 | |||
59 | //////////////////////////////////////////////
|
||
60 | |||
61 | |||
62 | |||
63 | //uint32_t _timerA0Cnt = 0;
|
||
64 | bool _timerFlag = false; |
||
65 | |||
66 | |||
67 | //Hardware timer init
|
||
68 | void timerInit(uint16_t maxCounts)
|
||
69 | { |
||
70 | //Init Timer interrupt type A
|
||
71 | //To avoid fast interruption that slow main program, divide clock by 2 !
|
||
72 | //We use here timer interrupt 0 in normal mode
|
||
73 | |||
74 | //At first, define the timer period
|
||
75 | TCA0.SINGLE.PERH = maxCounts >> 8;
|
||
76 | TCA0.SINGLE.PERL = maxCounts & 0xFF;
|
||
77 | TCA0.SINGLE.CTRLA = (SEL_CLK << 1) | 1; //Enable and CLK_DIV2 |
||
78 | TCA0.SINGLE.CTRLB = 0x00; //Set Normal operation for timer, no autolock, no waveform output |
||
79 | TCA0.SINGLE.EVCTRL = 0x00; //No specific action and counter event input |
||
80 | //TCA0.SINGLE.INTCTRL = 0x01; //Set interrupt on timer overflow
|
||
81 | } |
||
82 | |||
83 | //Get flag status
|
||
84 | bool getTimerFlag(void) |
||
85 | { |
||
86 | return (TCA0.SINGLE.INTFLAGS & 0x01); //get flag |
||
87 | } |
||
88 | |||
89 | //Clear the flag
|
||
90 | void clearTimerFlag(void) |
||
91 | { |
||
92 | TCA0.SINGLE.INTFLAGS = 1; //Clear by writing one on that register |
||
93 | } |
||
94 | |||
95 | //Timer interrupt callback
|
||
96 | /*ISR(TCA0_OVF_vect)
|
||
97 | {
|
||
98 | _timerFlag = true;
|
||
99 | TCA0.SINGLE.INTFLAGS = 1; //Must clear the flag by writing 1 on this bit (datasheet p.232)
|
||
100 | }*/
|
||
101 | |||
102 | |||
103 | #ifdef __cplusplus
|
||
104 | } |
||
105 | #endif /* __cplusplus */ |
||
106 | |||
107 | #endif /* XC_HEADER_TEMPLATE_H */ |