-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
170 lines (162 loc) · 4.26 KB
/
helper.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef HELPER_H
#define HELPER_H
#include "xc.h"
#include "math.h"
#include "setup.h"
/**
* Creates a timer given the following parameters.
* @param timerNumber Specifies which of 5 timers is to be used.
* @param periodValue Specifies the period for the timer to reset
* @param prescale Defaults to 1. Accepts 1,8,64, or 256 as a prescale.
*/
void createTimer(int timerNumber, int periodValue, int prescale) {
timer_complete = false;
_T1IE = 1;
_T1IF = 0;
int tckpsValue = 0b0;
//Takes the parameter and turns it into the needed TCKPS bits
switch(prescale) {
case 0:
tckpsValue = 0b00;
break;
case 8:
tckpsValue = 0b01;
break;
case 64:
tckpsValue = 0b10;
break;
case 256:
tckpsValue = 0b11;
break;
}
//Sets the appropriate timer and timer settings
switch(timerNumber) {
case 1:
T1CONbits.TON = 1;
T1CONbits.TCS = 0;
T1CONbits.TCKPS = tckpsValue;
PR1 = periodValue;
TMR1 = 0;
break;
case 2:
T2CONbits.TON = 1;
T2CONbits.TCS = 0;
T2CONbits.TCKPS = tckpsValue;
PR2 = periodValue;
TMR2 = 0;
break;
case 3:
T3CONbits.TON = 1;
T3CONbits.TCS = 0;
T3CONbits.TCKPS = tckpsValue;
PR3 = periodValue;
TMR3 = 0;
break;
case 4:
T4CONbits.TON = 1;
T4CONbits.TCS = 0;
T4CONbits.TCKPS = tckpsValue;
PR4 = periodValue;
TMR4 = 0;
break;
case 5:
T5CONbits.TON = 1;
T5CONbits.TCS = 0;
T5CONbits.TCKPS = tckpsValue;
PR5 = periodValue;
TMR5 = 0;
break;
}
}
void stopTimers(void) {
T1CONbits.TON = 0;
_T1IE = 0;
TMR1 = 0;
}
/**
* Configure a certain PWM to start running
* @param ocNum the specific OC to turn on
* @param period the period in Hz
* @param dutyCycle the duty cycle as a fraction of 1
*/
void configurePWM(int pinNum, double period, double dutyCycle) {
int value = round((1/(period/2))*FCY) - 1;
int dutyValue = round(dutyCycle*value);
switch(pinNum) {
case 14:
if (OC1CON1bits.OCM == 0) { /* If PWM is off, turn it on */
OC1CON1bits.OCM = 0b110; /* Trying to turn PWM on while it is on repeatedly at a high frequency make it do weird things */
}
OC1RS = value; // Period
OC1R = dutyValue; // Duty cycle
break;
case 4:
if (OC2CON1bits.OCM == 0) {
OC2CON1bits.OCM = 0b110;
}
OC2RS = value; // Period
OC2R = dutyValue; // Duty cycle
break;
case 5:
if (OC3CON1bits.OCM == 0) {
OC3CON1bits.OCM = 0b110;
}
OC3RS = value; // Period
OC3R = dutyValue; // Duty cycle
break;
}
}
/**
* Sets the _LAT for the pin to high or low
* @param pinNum The number of the pin (1,2,3,4,...etc.)
* @param pinState High or Low
*/
void setOutputPin(int pinNum, int pinState) {
switch(pinNum) {
case 1:
break;
case 2:
_LATA0 = pinState;
break;
case 3:
_LATA1 = pinState;
break;
case 6:
_LATB2 = pinState;
break;
case 7:
_LATA2 = pinState;
break;
case 8:
_LATA3 = pinState;
break;
case 9:
_LATB4 = pinState;
break;
case 10:
_LATA4 = pinState;
break;
case 11:
_LATB7 = pinState;
break;
case 12:
_LATB8 = pinState;
break;
case 13:
_LATB9 = pinState;
break;
case 15:
_LATB12 = pinState;
break;
case 16:
_LATB13 = pinState;
break;
case 17:
_LATB14 = pinState;
break;
case 18:
_LATB15 = pinState;
break;
}
}
#endif /* HELPER_H */