aboutsummaryrefslogtreecommitdiffstats
path: root/fw/tests/system/speed_systest.c
blob: 72fb271a94cea645c85c0facf8d1d6c7d680b70e (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
#include <xc.h>

#include <stdint.h>

#include "system.h"
#include "types.h"

// (pulse/min) = 60 * (Fosc/4) / ((pre)*(post)*(PR2+1)*(period))
// = 60 * (48e6/4) / (64*16*10*(period))
// = 70313 / (period)
#define SPEED_FACTOR 70313ul
#define MIN_SPEED_PULSE_PER_MIN 2u // (pulse/min) >= SPEED_FACTOR / period_max)
#define EDGE_PER_PULSE 2u // rising and falling edge

static volatile U16 tmr2Period = 0u;

void
main(void) {
	sysInit();

	U16 pulsePerMin = 550u;
	tmr2Period = ((U32)SPEED_FACTOR / pulsePerMin) & 0xFFFF;

	T2CON = 0x7F; // postscaler=1:16, enable=1, prescaler=1:64
	PR2 = 10u-1u; // period = PR2+1

	INTCON = 0x00;
	PIE1bits.TMR2IE = 1;
	INTCONbits.PEIE = 1;
	INTCONbits.GIE = 1;

	for (;;) {

	}
}

void __interrupt()
isr(void) {
	static U16 tmr2Ctr = 0u;

	if (TMR2IF) { // speedometer
		if (++tmr2Ctr >= (tmr2Period/EDGE_PER_PULSE)) {
			tmr2Ctr = 0u;
			SPEED_PIN ^= 1; // toggle speedometer output
		}
		TMR2IF = 0;
	}
}