-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cyclic.cpp
235 lines (181 loc) · 6.39 KB
/
Cyclic.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Tyler Gibney
15 1 9 2 2 3
log data to uSD card (serial port used due to no sd card)
Watch pulse is 9mS
watchdog 2 times per second
Display error condition as flashing blue LED
Show execution time of LCD
*/
#include "mbed.h"
#include "PwmIn.h"
#include "MCP23017.h" // include 16-bit parallel I/O header file
#include "WattBob_TextLCD.h" // include 2*16 character display header file
#include "SDFileSystem.h"
#define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
#define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
MCP23017 *par_port; // pointer to 16-bit parallel I/O object
WattBob_TextLCD *lcd; // pointer to 2*16 chacater LCD object
Serial pc(USBTX, USBRX); //tx, rx
/*-----------------------------------------------------------------------
Pin configuration
-----------------------------------------------------------------------*/
PwmIn FreqRead (p26); //Frequency reading pin
DigitalOut myled(LED1);
DigitalOut check(p8); //used for checking execution times
DigitalOut watchd(p7); //used for checking execution times
DigitalIn Switch_1(p12); //digital input switch
DigitalIn ShutDown(p6); //digital shutdown switch
AnalogIn an1(p15); //analog input 1
AnalogIn an2(p16); //analog input 2
Ticker tick;
//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
/*-----------------------------------------------------------------------
Variables
-----------------------------------------------------------------------*/
float Freq;
float an1Array [4];
float an2Array [4];
float an1Sum = 0;
float an2Sum = 0;
float analog1;
float analog2;
int ti;
int DigIn;
int ErrorC;
int TickC = 0;
/*-----------------------------------------------------------------------
This routine uses the PwmIn library to calculate the frequency of the input
square wave.
-----------------------------------------------------------------------*/
void Frequency() {
Freq = 1/FreqRead.period();
}
/*-----------------------------------------------------------------------
this routine checks the digital switch 1 and the analog values
to see if analog 1 is greater than analog 2. If this is true the error
code is set to 3.
-----------------------------------------------------------------------*/
void DSwitch(){
DigIn = Switch_1;
if (DigIn == 1 && (analog1 > analog2)){
ErrorC = 3;
lcd->locate(1,8);
lcd->printf("E=%d", ErrorC);
}
else {
ErrorC = 0;
}
}
/*-----------------------------------------------------------------------
The two subroutines below take in an analog reading and store it in an array.
The average filtered result of 4 analog readings are calculated and then
stored in the variable either analog1 or analog2
-----------------------------------------------------------------------*/
void Analog1(){
an1Array[0] = an1Array[1];
an1Array[1] = an1Array[2];
an1Array[2] = an1Array[3];
an1Array[3] = an1;
for (int i = 0; i<4; i++){
an1Sum = an1Sum + an1Array[i];
}
analog1 = an1Sum/4;
analog1 = analog1*3.3;
an1Sum = 0;
}
/*-----------------------------------------------------------------------*/
void Analog2(){
an2Array[0] = an2Array[1];
an2Array[1] = an2Array[2];
an2Array[2] = an2Array[3];
an2Array[3] = an2;
for (int i = 0; i<4; i++){
an2Sum = an2Sum + an2Array[i];
}
analog2 = an2Sum/4;
analog2 = analog2*3.3;
an2Sum = 0;
}
/*-----------------------------------------------------------------------
This routine is used for diasplaying information on the LCd display.
It does this by first clearing the display, going to the correct point
and then priting thwe string and variable value.
-----------------------------------------------------------------------*/
void LCD(){
//lcd->cls(); // clear display
lcd->locate(0,0); // set cursor to location (0,0) - top left corner
lcd->printf("A1=%.1f", analog1 ); // print string
lcd->locate(0,8);
lcd->printf("A2=%.1f", analog2 );
lcd->locate(1,0);
lcd->printf("F=%.1f", Freq);
lcd->locate(1,8);
lcd->printf("D=%d", DigIn);
}
/*-----------------------------------------------------------------------
This routine checks if error code 3 is true. If it is it sets the LED to
turn on and off on each cycle creating a blinking effect.
-----------------------------------------------------------------------*/
void ErrInd(){
if(ErrorC == 3){
myled = !myled;
}
else if (ErrorC == 0){
myled = 0;
}
}
/*-----------------------------------------------------------------------
This routine is used as a wachdog pulse which sets a digital output high for
9 milliseconds then turns it low.
-----------------------------------------------------------------------*/
void watch(){
watchd = 1;
wait_ms(9);
watchd = 0;
}
/*-----------------------------------------------------------------------
This routine is the cyclic scheduler which used the tick count to carry
out tasks based on a predetermined schedule.
-----------------------------------------------------------------------*/
void Cycle(){
if (TickC % 10 == 1) {
Analog1();
}
else if(TickC % 10 == 2) {
Analog2();
}
else if(TickC % 20 == 3) {
Frequency();
}
else if(TickC % 10 == 4) {
watch();
}
else if((TickC % 20 == 7) || (TickC % 20 == 10) || (TickC % 20 == 17)){
DSwitch();
}
else if(TickC % 10 == 9) {
ErrInd();
}
else if(TickC % 40 == 15) {
check =1;
LCD();
check = 0;
}
else if(TickC % 100 == 5){
pc.printf("%.1f,%.1f,%.1f,%d\n", Freq , analog1, analog2, DigIn);
}
TickC++;
}
/*-----------------------------------------------------------------------
This is the main loop where the program carrys out the code included.
This starts with an initialisation of the lcd dislay.
-----------------------------------------------------------------------*/
int main() {
par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip
lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display
par_port->write_bit(1,BL_BIT); // turn LCD backlight ON
tick.attach(&Cycle, 0.050);
while(1) {
}
}