This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
OutputCompare.c
165 lines (150 loc) · 4.73 KB
/
OutputCompare.c
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
/*
* File: OutputCompare.c
*
* Created on February 9, 2010, 10:53 AM
*/
#include "OutputCompare.h"
#include "InputCapture.h"
char initializedOC;
/*
*
*/
void init_oc1(void)
{
// Initialize Output Compare Module
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC1RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc2(void)
{
// Initialize Output Compare Module
OC2CONbits.OCM = 0b000; // Disable Output Compare Module
OC2R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC2RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC2CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC2CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc3(void)
{
// Initialize Output Compare Module
OC3CONbits.OCM = 0b000; // Disable Output Compare Module
OC3R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC3RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC3CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC3CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc4(void)
{
// Initialize Output Compare Module
OC4CONbits.OCM = 0b000; // Disable Output Compare Module
OC4R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC4RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC4CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC4CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc5(void)
{
//Initialize Output Compare Module
OC5CONbits.OCM = 0b000; // Disable Output Compare Module
OC5R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC5RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC5CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC5CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc6(void)
{
// Initialize Output Compare Module
OC6CONbits.OCM = 0b000; // Disable Output Compare Module
OC6R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC6RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC6CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC6CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc7(void)
{
// Initialize Output Compare Module
OC7CONbits.OCM = 0b000; // Disable Output Compare Module
OC7R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC7RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC7CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC7CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void init_oc8(void)
{
// Initialize Output Compare Module
OC8CONbits.OCM = 0b000; // Disable Output Compare Module
OC8R = MIDDLE_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688
OC8RS = MIDDLE_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688
OC8CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC8CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)
}
void setPeriod(double time)
{
T2CONbits.TCKPS = 0x01; //1:8 scaler
PR2 = (unsigned int)(time * MSEC);
}
void setOCValue(int ocPin, int time)
{
//Sets the PWM for subsequent pulses
if (ocPin == 1)
{
OC1RS = time;
}
else if (ocPin == 2)
{
OC2RS = time;
}
else if (ocPin == 3)
{
OC3RS = time;
}
else if (ocPin == 4)
{
OC4RS = time;
}
else if (ocPin == 5)
{
OC5RS = time;
}
else if (ocPin == 6)
{
OC6RS = time;
}
else if (ocPin == 7)
{
OC7RS = time;
}
else if (ocPin == 8)
{
OC8RS = time;
}
}
void init_oc(char OC)
{
//Initialize each of the 8 OCs
if (OC & 0b1)
init_oc1();
if (OC & 0b10)
init_oc2();
if (OC & 0b100)
init_oc3();
if (OC & 0b1000)
init_oc4();
if (OC & 0b10000)
init_oc5();
if (OC & 0b100000)
init_oc6();
if (OC & 0b1000000)
init_oc7();
if (OC & 0b10000000)
init_oc8();
}
void initOC(char OC){ //int argc, char** argv) {
init_oc(OC);
//Initialize timer2
initializedOC = OC;
//init_t2();
}