blob: db6550078d3c2e3744a9bd03d3300813705951d5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <xc.h>
#include <stdint.h>
#include "system.h"
#include "types.h"
// (pulse/min) = 60 * (Fosc/4) / ((pre)*(post)*(2^16 - TMR1))
// = 60 * (48e6/4) / (8*6*(2^16 - TMR1))
// = 15000000 / (2^16 - TMR1)
#define TACH_FACTOR 15000000ul
#define TMR1_POST 6u // TMR1 postscaler -- must be multiple of 2
#define EDGE_PER_PULSE 2u // rising and falling edge
static volatile U16 tmr1Start = 0u;
void
main(void) {
sysInit();
U16 pulsePerMin = 2500u;
tmr1Start = (((U32)1ul<<16u) - (U32)TACH_FACTOR / (U32)pulsePerMin) & 0xFFFF;
T1CON = 0x31; // source=Fosc/4, prescaler=1:8, enable=1
INTCON = 0x00;
PIE1bits.TMR1IE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
for (;;) {
}
}
void __interrupt()
isr(void) {
static U8 tmr1Ctr = 0u;
if (TMR1IF) { // tachometer
if (++tmr1Ctr == (TMR1_POST/EDGE_PER_PULSE)) {
tmr1Ctr = 0u;
TACH_PIN ^= 1; // toggle tachometer output
}
TMR1H = (tmr1Start>>8u)&0xFF; // reset timer
TMR1L = (tmr1Start>>0u)&0xFF;
TMR1IF = 0;
}
}
|