Eric Glunn
/
4180Project_Analog
Logic code
main.cpp@2:9a88de905577, 2020-04-21 (annotated)
- Committer:
- EricGlunn
- Date:
- Tue Apr 21 03:31:20 2020 +0000
- Revision:
- 2:9a88de905577
- Parent:
- 1:14e49afb1d7c
- Child:
- 3:60f30ca6a65d
Buggy oScope code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricGlunn | 0:f4b1c6a95f25 | 1 | #include "mbed.h" |
EricGlunn | 2:9a88de905577 | 2 | |
EricGlunn | 2:9a88de905577 | 3 | #define reset 0b00000000 //Define function masks |
EricGlunn | 0:f4b1c6a95f25 | 4 | #define digital 0b00100000 |
EricGlunn | 0:f4b1c6a95f25 | 5 | #define analog 0b01000000 |
EricGlunn | 0:f4b1c6a95f25 | 6 | #define I2C 0b01100000 |
EricGlunn | 2:9a88de905577 | 7 | #define oScope 0b10000000 |
EricGlunn | 2:9a88de905577 | 8 | |
EricGlunn | 2:9a88de905577 | 9 | #define stateMask 0b11100000 //general masks |
EricGlunn | 0:f4b1c6a95f25 | 10 | #define pinMask 0b00011111 |
EricGlunn | 2:9a88de905577 | 11 | #define FULL 0b11111111 |
EricGlunn | 2:9a88de905577 | 12 | |
EricGlunn | 2:9a88de905577 | 13 | #define pin1 0b00010000 //digital and analog masks |
EricGlunn | 0:f4b1c6a95f25 | 14 | #define pin2 0b00001000 |
EricGlunn | 0:f4b1c6a95f25 | 15 | #define pin3 0b00000100 |
EricGlunn | 0:f4b1c6a95f25 | 16 | #define pin4 0b00000010 |
EricGlunn | 0:f4b1c6a95f25 | 17 | #define pin5 0b00000001 |
EricGlunn | 0:f4b1c6a95f25 | 18 | |
EricGlunn | 2:9a88de905577 | 19 | #define ms1 0b00000000 //Oscope masks |
EricGlunn | 2:9a88de905577 | 20 | #define ms5 0b00000001 |
EricGlunn | 2:9a88de905577 | 21 | #define ms10 0b00000010 |
EricGlunn | 2:9a88de905577 | 22 | #define ms50 0b00000011 |
EricGlunn | 2:9a88de905577 | 23 | #define ms100 0b00000100 |
EricGlunn | 2:9a88de905577 | 24 | #define ms250 0b00000101 |
EricGlunn | 2:9a88de905577 | 25 | #define ms500 0b00000110 |
EricGlunn | 2:9a88de905577 | 26 | #define ms1000 0b00000111 |
EricGlunn | 2:9a88de905577 | 27 | #define rise 0b00010000 |
EricGlunn | 2:9a88de905577 | 28 | #define fall 0b00000000 |
EricGlunn | 2:9a88de905577 | 29 | #define timeMask 0b00001111 |
EricGlunn | 2:9a88de905577 | 30 | #define trigger 0b00010000 |
EricGlunn | 0:f4b1c6a95f25 | 31 | |
EricGlunn | 0:f4b1c6a95f25 | 32 | DigitalOut myled(LED1); |
EricGlunn | 0:f4b1c6a95f25 | 33 | DigitalOut DigitalStatus(LED2); //Led for when Digital read is running |
EricGlunn | 0:f4b1c6a95f25 | 34 | DigitalOut AnalogStatus(LED3); //LED for when Analog read is running |
EricGlunn | 2:9a88de905577 | 35 | DigitalOut oScopeStatus(LED4); //LED for Oscilloscope |
EricGlunn | 0:f4b1c6a95f25 | 36 | |
EricGlunn | 0:f4b1c6a95f25 | 37 | DigitalIn d1(p21); //Always set these pins for digital in |
EricGlunn | 0:f4b1c6a95f25 | 38 | DigitalIn d2(p22); |
EricGlunn | 0:f4b1c6a95f25 | 39 | DigitalIn d3(p23); |
EricGlunn | 0:f4b1c6a95f25 | 40 | DigitalIn d4(p24); |
EricGlunn | 0:f4b1c6a95f25 | 41 | DigitalIn d5(p25); |
EricGlunn | 0:f4b1c6a95f25 | 42 | |
EricGlunn | 2:9a88de905577 | 43 | |
EricGlunn | 2:9a88de905577 | 44 | AnalogIn a1(p16); //Always set these pins for analog in |
EricGlunn | 0:f4b1c6a95f25 | 45 | AnalogIn a2(p17); |
EricGlunn | 0:f4b1c6a95f25 | 46 | AnalogIn a3(p18); |
EricGlunn | 0:f4b1c6a95f25 | 47 | AnalogIn a4(p19); |
EricGlunn | 0:f4b1c6a95f25 | 48 | AnalogIn a5(p20); |
EricGlunn | 0:f4b1c6a95f25 | 49 | |
EricGlunn | 0:f4b1c6a95f25 | 50 | |
EricGlunn | 2:9a88de905577 | 51 | Serial PC(USBTX,USBRX); |
EricGlunn | 0:f4b1c6a95f25 | 52 | Ticker tickerboi; //Initialize the ticker for Sampling |
EricGlunn | 0:f4b1c6a95f25 | 53 | |
EricGlunn | 2:9a88de905577 | 54 | Ticker oScopeTicker; |
EricGlunn | 2:9a88de905577 | 55 | Ticker oScopeCut; |
EricGlunn | 2:9a88de905577 | 56 | |
EricGlunn | 0:f4b1c6a95f25 | 57 | volatile unsigned char DigitalSample; |
EricGlunn | 0:f4b1c6a95f25 | 58 | volatile unsigned char AnalogSample[5]; |
EricGlunn | 0:f4b1c6a95f25 | 59 | |
EricGlunn | 0:f4b1c6a95f25 | 60 | volatile unsigned char command; |
EricGlunn | 0:f4b1c6a95f25 | 61 | volatile unsigned char state; |
EricGlunn | 0:f4b1c6a95f25 | 62 | |
EricGlunn | 0:f4b1c6a95f25 | 63 | volatile bool newDigital=false; |
EricGlunn | 0:f4b1c6a95f25 | 64 | volatile bool newAnalog=false; |
EricGlunn | 0:f4b1c6a95f25 | 65 | |
EricGlunn | 2:9a88de905577 | 66 | volatile unsigned char oScopeTime; |
EricGlunn | 2:9a88de905577 | 67 | volatile bool oScope_poll; |
EricGlunn | 2:9a88de905577 | 68 | volatile bool newoScope=false; |
EricGlunn | 2:9a88de905577 | 69 | volatile unsigned char oScopeArray[20000]; |
EricGlunn | 2:9a88de905577 | 70 | volatile int oScopeCount=0; |
EricGlunn | 2:9a88de905577 | 71 | |
EricGlunn | 2:9a88de905577 | 72 | |
EricGlunn | 2:9a88de905577 | 73 | void digital_sample_func(void) //sampling function, samples each pin specified and Ors it |
EricGlunn | 2:9a88de905577 | 74 | { |
EricGlunn | 0:f4b1c6a95f25 | 75 | DigitalSample = digital; |
EricGlunn | 2:9a88de905577 | 76 | |
EricGlunn | 2:9a88de905577 | 77 | if( (command & pin1) == pin1) { |
EricGlunn | 0:f4b1c6a95f25 | 78 | DigitalSample=DigitalSample + d1*pin1; |
EricGlunn | 0:f4b1c6a95f25 | 79 | } |
EricGlunn | 2:9a88de905577 | 80 | if( (command & pin2) == pin2) { |
EricGlunn | 0:f4b1c6a95f25 | 81 | DigitalSample=DigitalSample + d2*pin2; |
EricGlunn | 0:f4b1c6a95f25 | 82 | } |
EricGlunn | 2:9a88de905577 | 83 | if( (command & pin3) == pin3) { |
EricGlunn | 0:f4b1c6a95f25 | 84 | DigitalSample=DigitalSample + d3*pin3; |
EricGlunn | 0:f4b1c6a95f25 | 85 | } |
EricGlunn | 2:9a88de905577 | 86 | if( (command & pin4) == pin4) { |
EricGlunn | 0:f4b1c6a95f25 | 87 | DigitalSample=DigitalSample + d4*pin4; |
EricGlunn | 0:f4b1c6a95f25 | 88 | } |
EricGlunn | 2:9a88de905577 | 89 | if( (command & pin5) == pin5) { |
EricGlunn | 0:f4b1c6a95f25 | 90 | DigitalSample=DigitalSample + d5*pin5; |
EricGlunn | 0:f4b1c6a95f25 | 91 | } |
EricGlunn | 0:f4b1c6a95f25 | 92 | newDigital=true; |
EricGlunn | 0:f4b1c6a95f25 | 93 | } |
EricGlunn | 0:f4b1c6a95f25 | 94 | |
EricGlunn | 0:f4b1c6a95f25 | 95 | |
EricGlunn | 2:9a88de905577 | 96 | void analog_sample_func(void) |
EricGlunn | 2:9a88de905577 | 97 | { |
EricGlunn | 2:9a88de905577 | 98 | for(int i=0; i<5; i++) { |
EricGlunn | 2:9a88de905577 | 99 | AnalogSample[i]=0; |
EricGlunn | 0:f4b1c6a95f25 | 100 | } |
EricGlunn | 2:9a88de905577 | 101 | if( (command & pin1) == pin1) { |
EricGlunn | 1:14e49afb1d7c | 102 | AnalogSample[0]=a1*254; |
EricGlunn | 0:f4b1c6a95f25 | 103 | } |
EricGlunn | 2:9a88de905577 | 104 | if( (command & pin2) == pin2) { |
EricGlunn | 1:14e49afb1d7c | 105 | AnalogSample[1]=a2 * 254; |
EricGlunn | 0:f4b1c6a95f25 | 106 | } |
EricGlunn | 2:9a88de905577 | 107 | if( (command & pin3) == pin3) { |
EricGlunn | 1:14e49afb1d7c | 108 | AnalogSample[2]=a3*254; |
EricGlunn | 0:f4b1c6a95f25 | 109 | } |
EricGlunn | 2:9a88de905577 | 110 | if( (command & pin4) == pin4) { |
EricGlunn | 1:14e49afb1d7c | 111 | AnalogSample[3]=a4*254; |
EricGlunn | 0:f4b1c6a95f25 | 112 | } |
EricGlunn | 2:9a88de905577 | 113 | if( (command & pin5) == pin5) { |
EricGlunn | 1:14e49afb1d7c | 114 | AnalogSample[4]=a5*254; |
EricGlunn | 0:f4b1c6a95f25 | 115 | } |
EricGlunn | 2:9a88de905577 | 116 | newAnalog=true; |
EricGlunn | 2:9a88de905577 | 117 | } |
EricGlunn | 2:9a88de905577 | 118 | |
EricGlunn | 2:9a88de905577 | 119 | |
EricGlunn | 2:9a88de905577 | 120 | void oScope_kill_func(void) |
EricGlunn | 2:9a88de905577 | 121 | { |
EricGlunn | 2:9a88de905577 | 122 | oScopeTicker.detach(); |
EricGlunn | 2:9a88de905577 | 123 | oScopeCut.detach(); |
EricGlunn | 2:9a88de905577 | 124 | newoScope=true; |
EricGlunn | 2:9a88de905577 | 125 | oScopeStatus=0; |
EricGlunn | 2:9a88de905577 | 126 | } |
EricGlunn | 2:9a88de905577 | 127 | |
EricGlunn | 2:9a88de905577 | 128 | void oScope_sample_func(void){ |
EricGlunn | 2:9a88de905577 | 129 | oScopeArray[oScopeCount]=a1*254; |
EricGlunn | 2:9a88de905577 | 130 | oScopeCount++; |
EricGlunn | 2:9a88de905577 | 131 | } |
EricGlunn | 2:9a88de905577 | 132 | |
EricGlunn | 2:9a88de905577 | 133 | void oScope_poll_trig(){ |
EricGlunn | 2:9a88de905577 | 134 | float first, second; |
EricGlunn | 2:9a88de905577 | 135 | bool run_poll =true; |
EricGlunn | 2:9a88de905577 | 136 | while(run_poll){ |
EricGlunn | 2:9a88de905577 | 137 | first = a1; |
EricGlunn | 2:9a88de905577 | 138 | wait (.001); |
EricGlunn | 2:9a88de905577 | 139 | second=a1; |
EricGlunn | 2:9a88de905577 | 140 | if( (second - first)>.1){run_poll = false;} |
EricGlunn | 2:9a88de905577 | 141 | } |
EricGlunn | 2:9a88de905577 | 142 | |
EricGlunn | 2:9a88de905577 | 143 | switch(oScopeTime) { |
EricGlunn | 2:9a88de905577 | 144 | |
EricGlunn | 2:9a88de905577 | 145 | case ms1: |
EricGlunn | 2:9a88de905577 | 146 | oScopeCut.attach(oScope_kill_func, .001); |
EricGlunn | 2:9a88de905577 | 147 | break; |
EricGlunn | 2:9a88de905577 | 148 | |
EricGlunn | 2:9a88de905577 | 149 | case ms5: |
EricGlunn | 2:9a88de905577 | 150 | oScopeCut.attach(oScope_kill_func, .005); |
EricGlunn | 2:9a88de905577 | 151 | break; |
EricGlunn | 2:9a88de905577 | 152 | |
EricGlunn | 2:9a88de905577 | 153 | case ms10: |
EricGlunn | 2:9a88de905577 | 154 | oScopeCut.attach(oScope_kill_func, .01); |
EricGlunn | 2:9a88de905577 | 155 | break; |
EricGlunn | 2:9a88de905577 | 156 | |
EricGlunn | 2:9a88de905577 | 157 | case ms50: |
EricGlunn | 2:9a88de905577 | 158 | oScopeCut.attach(oScope_kill_func, .05); |
EricGlunn | 2:9a88de905577 | 159 | break; |
EricGlunn | 2:9a88de905577 | 160 | |
EricGlunn | 2:9a88de905577 | 161 | case ms100: |
EricGlunn | 2:9a88de905577 | 162 | oScopeCut.attach(oScope_kill_func, .1); |
EricGlunn | 2:9a88de905577 | 163 | break; |
EricGlunn | 2:9a88de905577 | 164 | |
EricGlunn | 2:9a88de905577 | 165 | case ms250: |
EricGlunn | 2:9a88de905577 | 166 | oScopeCut.attach(oScope_kill_func, .25); |
EricGlunn | 2:9a88de905577 | 167 | break; |
EricGlunn | 2:9a88de905577 | 168 | |
EricGlunn | 2:9a88de905577 | 169 | case ms500: |
EricGlunn | 2:9a88de905577 | 170 | oScopeCut.attach(oScope_kill_func, .5); |
EricGlunn | 2:9a88de905577 | 171 | break; |
EricGlunn | 2:9a88de905577 | 172 | |
EricGlunn | 2:9a88de905577 | 173 | case ms1000: |
EricGlunn | 2:9a88de905577 | 174 | oScopeCut.attach(oScope_kill_func, 1); |
EricGlunn | 2:9a88de905577 | 175 | break; |
EricGlunn | 2:9a88de905577 | 176 | } |
EricGlunn | 2:9a88de905577 | 177 | |
EricGlunn | 2:9a88de905577 | 178 | oScopeTicker.attach_us(oScope_sample_func, 50); |
EricGlunn | 0:f4b1c6a95f25 | 179 | |
EricGlunn | 0:f4b1c6a95f25 | 180 | } |
EricGlunn | 0:f4b1c6a95f25 | 181 | |
EricGlunn | 0:f4b1c6a95f25 | 182 | |
EricGlunn | 2:9a88de905577 | 183 | void SerialInterrupt(void) |
EricGlunn | 2:9a88de905577 | 184 | { |
EricGlunn | 2:9a88de905577 | 185 | command=PC.getc(); |
EricGlunn | 2:9a88de905577 | 186 | state= command & stateMask; |
EricGlunn | 2:9a88de905577 | 187 | |
EricGlunn | 2:9a88de905577 | 188 | switch(state) { |
EricGlunn | 2:9a88de905577 | 189 | case reset: |
EricGlunn | 2:9a88de905577 | 190 | tickerboi.detach(); //detach Ticker |
EricGlunn | 2:9a88de905577 | 191 | DigitalStatus=0; //turn off status LED |
EricGlunn | 2:9a88de905577 | 192 | AnalogStatus=0; |
EricGlunn | 2:9a88de905577 | 193 | if(oScopeStatus){ |
EricGlunn | 2:9a88de905577 | 194 | oScope_kill_func(); |
EricGlunn | 2:9a88de905577 | 195 | } |
EricGlunn | 2:9a88de905577 | 196 | |
EricGlunn | 0:f4b1c6a95f25 | 197 | break; |
EricGlunn | 2:9a88de905577 | 198 | |
EricGlunn | 2:9a88de905577 | 199 | case digital: |
EricGlunn | 2:9a88de905577 | 200 | tickerboi.attach(&digital_sample_func, .5); |
EricGlunn | 2:9a88de905577 | 201 | DigitalStatus=1; // turn on status LED |
EricGlunn | 2:9a88de905577 | 202 | break; |
EricGlunn | 2:9a88de905577 | 203 | |
EricGlunn | 2:9a88de905577 | 204 | case analog: |
EricGlunn | 2:9a88de905577 | 205 | tickerboi.attach(&analog_sample_func, .5); |
EricGlunn | 2:9a88de905577 | 206 | AnalogStatus=1; |
EricGlunn | 2:9a88de905577 | 207 | |
EricGlunn | 2:9a88de905577 | 208 | case oScope: |
EricGlunn | 2:9a88de905577 | 209 | oScopeTime=command & timeMask; |
EricGlunn | 2:9a88de905577 | 210 | oScope_poll=true; |
EricGlunn | 2:9a88de905577 | 211 | oScopeStatus=1; |
EricGlunn | 2:9a88de905577 | 212 | break; |
EricGlunn | 2:9a88de905577 | 213 | } |
EricGlunn | 0:f4b1c6a95f25 | 214 | } |
EricGlunn | 0:f4b1c6a95f25 | 215 | |
EricGlunn | 2:9a88de905577 | 216 | int main() |
EricGlunn | 2:9a88de905577 | 217 | { |
EricGlunn | 2:9a88de905577 | 218 | PC.attach(&SerialInterrupt, Serial::RxIrq); |
EricGlunn | 0:f4b1c6a95f25 | 219 | |
EricGlunn | 2:9a88de905577 | 220 | |
EricGlunn | 0:f4b1c6a95f25 | 221 | while(1) { |
EricGlunn | 2:9a88de905577 | 222 | |
EricGlunn | 2:9a88de905577 | 223 | if(newDigital) { //if digital sampling was run |
EricGlunn | 0:f4b1c6a95f25 | 224 | PC.putc(DigitalSample); |
EricGlunn | 0:f4b1c6a95f25 | 225 | newDigital=false; |
EricGlunn | 0:f4b1c6a95f25 | 226 | } |
EricGlunn | 2:9a88de905577 | 227 | |
EricGlunn | 2:9a88de905577 | 228 | if(newAnalog) { //if analog sampling was run |
EricGlunn | 2:9a88de905577 | 229 | PC.putc(FULL); //First bit is 0b01000000 |
EricGlunn | 2:9a88de905577 | 230 | for(int i=0; i<5; i++) { //Send all of the analog bits |
EricGlunn | 2:9a88de905577 | 231 | PC.putc(AnalogSample[i]); |
EricGlunn | 0:f4b1c6a95f25 | 232 | } |
EricGlunn | 0:f4b1c6a95f25 | 233 | PC.putc(command); //Last bit is command from PC |
EricGlunn | 0:f4b1c6a95f25 | 234 | newAnalog=false; |
EricGlunn | 0:f4b1c6a95f25 | 235 | } |
EricGlunn | 0:f4b1c6a95f25 | 236 | |
EricGlunn | 2:9a88de905577 | 237 | if(oScope_poll){ oScope_poll_trig(); } |
EricGlunn | 2:9a88de905577 | 238 | |
EricGlunn | 2:9a88de905577 | 239 | if(newoScope){ |
EricGlunn | 2:9a88de905577 | 240 | PC.putc(FULL); |
EricGlunn | 2:9a88de905577 | 241 | for(int j=0; j<=oScopeCount; j++){ |
EricGlunn | 2:9a88de905577 | 242 | PC.putc(oScopeArray[oScopeCount]); |
EricGlunn | 2:9a88de905577 | 243 | } |
EricGlunn | 2:9a88de905577 | 244 | oScopeCount=0; |
EricGlunn | 2:9a88de905577 | 245 | newoScope=false; |
EricGlunn | 2:9a88de905577 | 246 | } |
EricGlunn | 2:9a88de905577 | 247 | |
EricGlunn | 2:9a88de905577 | 248 | |
EricGlunn | 0:f4b1c6a95f25 | 249 | myled = 1; //indicate main loop is running |
EricGlunn | 0:f4b1c6a95f25 | 250 | wait(0.2); |
EricGlunn | 0:f4b1c6a95f25 | 251 | myled = 0; |
EricGlunn | 2:9a88de905577 | 252 | wait(0.2); |
EricGlunn | 0:f4b1c6a95f25 | 253 | } |
EricGlunn | 0:f4b1c6a95f25 | 254 | } |
EricGlunn | 0:f4b1c6a95f25 | 255 | |
EricGlunn | 0:f4b1c6a95f25 | 256 | |
EricGlunn | 0:f4b1c6a95f25 | 257 |