Eric Glunn
/
4180Project_Analog
Logic code
main.cpp@1:14e49afb1d7c, 2020-04-21 (annotated)
- Committer:
- EricGlunn
- Date:
- Tue Apr 21 00:07:44 2020 +0000
- Revision:
- 1:14e49afb1d7c
- Parent:
- 0:f4b1c6a95f25
- Child:
- 2:9a88de905577
Changed analog start bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricGlunn | 0:f4b1c6a95f25 | 1 | #include "mbed.h" |
EricGlunn | 0:f4b1c6a95f25 | 2 | #define reset 0b00000000 //Define masks |
EricGlunn | 0:f4b1c6a95f25 | 3 | #define digital 0b00100000 |
EricGlunn | 0:f4b1c6a95f25 | 4 | #define analog 0b01000000 |
EricGlunn | 0:f4b1c6a95f25 | 5 | #define I2C 0b01100000 |
EricGlunn | 0:f4b1c6a95f25 | 6 | #define stateMask 0b11100000 |
EricGlunn | 0:f4b1c6a95f25 | 7 | #define pinMask 0b00011111 |
EricGlunn | 0:f4b1c6a95f25 | 8 | #define pin1 0b00010000 |
EricGlunn | 0:f4b1c6a95f25 | 9 | #define pin2 0b00001000 |
EricGlunn | 0:f4b1c6a95f25 | 10 | #define pin3 0b00000100 |
EricGlunn | 0:f4b1c6a95f25 | 11 | #define pin4 0b00000010 |
EricGlunn | 0:f4b1c6a95f25 | 12 | #define pin5 0b00000001 |
EricGlunn | 1:14e49afb1d7c | 13 | #define FULL 0b11111111 |
EricGlunn | 0:f4b1c6a95f25 | 14 | |
EricGlunn | 0:f4b1c6a95f25 | 15 | |
EricGlunn | 0:f4b1c6a95f25 | 16 | DigitalOut myled(LED1); |
EricGlunn | 0:f4b1c6a95f25 | 17 | DigitalOut DigitalStatus(LED2); //Led for when Digital read is running |
EricGlunn | 0:f4b1c6a95f25 | 18 | DigitalOut AnalogStatus(LED3); //LED for when Analog read is running |
EricGlunn | 0:f4b1c6a95f25 | 19 | |
EricGlunn | 0:f4b1c6a95f25 | 20 | DigitalIn d1(p21); //Always set these pins for digital in |
EricGlunn | 0:f4b1c6a95f25 | 21 | DigitalIn d2(p22); |
EricGlunn | 0:f4b1c6a95f25 | 22 | DigitalIn d3(p23); |
EricGlunn | 0:f4b1c6a95f25 | 23 | DigitalIn d4(p24); |
EricGlunn | 0:f4b1c6a95f25 | 24 | DigitalIn d5(p25); |
EricGlunn | 0:f4b1c6a95f25 | 25 | |
EricGlunn | 0:f4b1c6a95f25 | 26 | AnalogIn a1(p16); //Always set these pins for analog in |
EricGlunn | 0:f4b1c6a95f25 | 27 | AnalogIn a2(p17); |
EricGlunn | 0:f4b1c6a95f25 | 28 | AnalogIn a3(p18); |
EricGlunn | 0:f4b1c6a95f25 | 29 | AnalogIn a4(p19); |
EricGlunn | 0:f4b1c6a95f25 | 30 | AnalogIn a5(p20); |
EricGlunn | 0:f4b1c6a95f25 | 31 | |
EricGlunn | 0:f4b1c6a95f25 | 32 | |
EricGlunn | 0:f4b1c6a95f25 | 33 | Serial PC(USBTX,USBRX); |
EricGlunn | 0:f4b1c6a95f25 | 34 | Ticker tickerboi; //Initialize the ticker for Sampling |
EricGlunn | 0:f4b1c6a95f25 | 35 | |
EricGlunn | 0:f4b1c6a95f25 | 36 | volatile unsigned char DigitalSample; |
EricGlunn | 0:f4b1c6a95f25 | 37 | volatile unsigned char AnalogSample[5]; |
EricGlunn | 0:f4b1c6a95f25 | 38 | |
EricGlunn | 0:f4b1c6a95f25 | 39 | volatile bool DigitalRunning; |
EricGlunn | 0:f4b1c6a95f25 | 40 | volatile bool AnalogRunning; |
EricGlunn | 0:f4b1c6a95f25 | 41 | |
EricGlunn | 0:f4b1c6a95f25 | 42 | volatile unsigned char command; |
EricGlunn | 0:f4b1c6a95f25 | 43 | volatile unsigned char state; |
EricGlunn | 0:f4b1c6a95f25 | 44 | |
EricGlunn | 0:f4b1c6a95f25 | 45 | volatile bool newDigital=false; |
EricGlunn | 0:f4b1c6a95f25 | 46 | volatile bool newAnalog=false; |
EricGlunn | 0:f4b1c6a95f25 | 47 | |
EricGlunn | 0:f4b1c6a95f25 | 48 | |
EricGlunn | 0:f4b1c6a95f25 | 49 | |
EricGlunn | 0:f4b1c6a95f25 | 50 | void digital_sample_func(void){ //sampling function, samples each pin specified and Ors it |
EricGlunn | 0:f4b1c6a95f25 | 51 | DigitalSample = digital; |
EricGlunn | 0:f4b1c6a95f25 | 52 | |
EricGlunn | 0:f4b1c6a95f25 | 53 | if( (command & pin1) == pin1){ |
EricGlunn | 0:f4b1c6a95f25 | 54 | DigitalSample=DigitalSample + d1*pin1; |
EricGlunn | 0:f4b1c6a95f25 | 55 | } |
EricGlunn | 0:f4b1c6a95f25 | 56 | if( (command & pin2) == pin2){ |
EricGlunn | 0:f4b1c6a95f25 | 57 | DigitalSample=DigitalSample + d2*pin2; |
EricGlunn | 0:f4b1c6a95f25 | 58 | } |
EricGlunn | 0:f4b1c6a95f25 | 59 | if( (command & pin3) == pin3){ |
EricGlunn | 0:f4b1c6a95f25 | 60 | DigitalSample=DigitalSample + d3*pin3; |
EricGlunn | 0:f4b1c6a95f25 | 61 | } |
EricGlunn | 0:f4b1c6a95f25 | 62 | if( (command & pin4) == pin4){ |
EricGlunn | 0:f4b1c6a95f25 | 63 | DigitalSample=DigitalSample + d4*pin4; |
EricGlunn | 0:f4b1c6a95f25 | 64 | } |
EricGlunn | 0:f4b1c6a95f25 | 65 | if( (command & pin5) == pin5){ |
EricGlunn | 0:f4b1c6a95f25 | 66 | DigitalSample=DigitalSample + d5*pin5; |
EricGlunn | 0:f4b1c6a95f25 | 67 | } |
EricGlunn | 0:f4b1c6a95f25 | 68 | newDigital=true; |
EricGlunn | 0:f4b1c6a95f25 | 69 | } |
EricGlunn | 0:f4b1c6a95f25 | 70 | |
EricGlunn | 0:f4b1c6a95f25 | 71 | |
EricGlunn | 0:f4b1c6a95f25 | 72 | void analog_sample_func(void){ |
EricGlunn | 0:f4b1c6a95f25 | 73 | for(int i=0; i<5; i++){ |
EricGlunn | 0:f4b1c6a95f25 | 74 | AnalogSample[i]=0; |
EricGlunn | 0:f4b1c6a95f25 | 75 | } |
EricGlunn | 0:f4b1c6a95f25 | 76 | if( (command & pin1) == pin1){ |
EricGlunn | 1:14e49afb1d7c | 77 | AnalogSample[0]=a1*254; |
EricGlunn | 0:f4b1c6a95f25 | 78 | } |
EricGlunn | 0:f4b1c6a95f25 | 79 | if( (command & pin2) == pin2){ |
EricGlunn | 1:14e49afb1d7c | 80 | AnalogSample[1]=a2 * 254; |
EricGlunn | 0:f4b1c6a95f25 | 81 | } |
EricGlunn | 0:f4b1c6a95f25 | 82 | if( (command & pin3) == pin3){ |
EricGlunn | 1:14e49afb1d7c | 83 | AnalogSample[2]=a3*254; |
EricGlunn | 0:f4b1c6a95f25 | 84 | } |
EricGlunn | 0:f4b1c6a95f25 | 85 | if( (command & pin4) == pin4){ |
EricGlunn | 1:14e49afb1d7c | 86 | AnalogSample[3]=a4*254; |
EricGlunn | 0:f4b1c6a95f25 | 87 | } |
EricGlunn | 0:f4b1c6a95f25 | 88 | if( (command & pin5) == pin5){ |
EricGlunn | 1:14e49afb1d7c | 89 | AnalogSample[4]=a5*254; |
EricGlunn | 0:f4b1c6a95f25 | 90 | } |
EricGlunn | 0:f4b1c6a95f25 | 91 | newAnalog=true; |
EricGlunn | 0:f4b1c6a95f25 | 92 | |
EricGlunn | 0:f4b1c6a95f25 | 93 | } |
EricGlunn | 0:f4b1c6a95f25 | 94 | |
EricGlunn | 0:f4b1c6a95f25 | 95 | |
EricGlunn | 0:f4b1c6a95f25 | 96 | void SerialInterrupt(void){ |
EricGlunn | 0:f4b1c6a95f25 | 97 | command=PC.getc(); |
EricGlunn | 0:f4b1c6a95f25 | 98 | state= command & stateMask; |
EricGlunn | 0:f4b1c6a95f25 | 99 | switch(state){ |
EricGlunn | 0:f4b1c6a95f25 | 100 | case reset: |
EricGlunn | 0:f4b1c6a95f25 | 101 | tickerboi.detach(); //detach Ticker |
EricGlunn | 0:f4b1c6a95f25 | 102 | DigitalRunning=false; //turn off status LED |
EricGlunn | 0:f4b1c6a95f25 | 103 | AnalogRunning=false; |
EricGlunn | 0:f4b1c6a95f25 | 104 | break; |
EricGlunn | 0:f4b1c6a95f25 | 105 | |
EricGlunn | 0:f4b1c6a95f25 | 106 | case digital: |
EricGlunn | 0:f4b1c6a95f25 | 107 | tickerboi.attach(&digital_sample_func, .1); //Ticker will call digital read every 100 ms |
EricGlunn | 0:f4b1c6a95f25 | 108 | DigitalRunning=true; // turn on status LED |
EricGlunn | 0:f4b1c6a95f25 | 109 | break; |
EricGlunn | 0:f4b1c6a95f25 | 110 | |
EricGlunn | 0:f4b1c6a95f25 | 111 | case analog: |
EricGlunn | 1:14e49afb1d7c | 112 | tickerboi.attach(&analog_sample_func, 1); //Ticker will call analog read every 10ms |
EricGlunn | 0:f4b1c6a95f25 | 113 | AnalogRunning=true; |
EricGlunn | 0:f4b1c6a95f25 | 114 | } |
EricGlunn | 0:f4b1c6a95f25 | 115 | } |
EricGlunn | 0:f4b1c6a95f25 | 116 | |
EricGlunn | 0:f4b1c6a95f25 | 117 | int main() { |
EricGlunn | 0:f4b1c6a95f25 | 118 | PC.attach(&SerialInterrupt, Serial::RxIrq); |
EricGlunn | 0:f4b1c6a95f25 | 119 | |
EricGlunn | 0:f4b1c6a95f25 | 120 | |
EricGlunn | 0:f4b1c6a95f25 | 121 | while(1) { |
EricGlunn | 0:f4b1c6a95f25 | 122 | |
EricGlunn | 0:f4b1c6a95f25 | 123 | DigitalStatus=DigitalRunning; //Indicate DigitalRead is running |
EricGlunn | 0:f4b1c6a95f25 | 124 | AnalogStatus=AnalogRunning; //Indicate analogRead is running |
EricGlunn | 0:f4b1c6a95f25 | 125 | |
EricGlunn | 0:f4b1c6a95f25 | 126 | if(newDigital){ //if digital sampling was run |
EricGlunn | 0:f4b1c6a95f25 | 127 | PC.putc(DigitalSample); |
EricGlunn | 0:f4b1c6a95f25 | 128 | newDigital=false; |
EricGlunn | 0:f4b1c6a95f25 | 129 | } |
EricGlunn | 0:f4b1c6a95f25 | 130 | |
EricGlunn | 0:f4b1c6a95f25 | 131 | if(newAnalog){ //if analog sampling was run |
EricGlunn | 1:14e49afb1d7c | 132 | PC.putc(FULL); //First bit is 0b01000000 |
EricGlunn | 0:f4b1c6a95f25 | 133 | for(int i=0; i<5; i++){ //Send all of the analog bits |
EricGlunn | 0:f4b1c6a95f25 | 134 | PC.putc(AnalogSample[i]); |
EricGlunn | 0:f4b1c6a95f25 | 135 | } |
EricGlunn | 0:f4b1c6a95f25 | 136 | PC.putc(command); //Last bit is command from PC |
EricGlunn | 0:f4b1c6a95f25 | 137 | newAnalog=false; |
EricGlunn | 0:f4b1c6a95f25 | 138 | } |
EricGlunn | 0:f4b1c6a95f25 | 139 | |
EricGlunn | 0:f4b1c6a95f25 | 140 | myled = 1; //indicate main loop is running |
EricGlunn | 0:f4b1c6a95f25 | 141 | wait(0.2); |
EricGlunn | 0:f4b1c6a95f25 | 142 | myled = 0; |
EricGlunn | 0:f4b1c6a95f25 | 143 | wait(0.2); |
EricGlunn | 0:f4b1c6a95f25 | 144 | } |
EricGlunn | 0:f4b1c6a95f25 | 145 | } |
EricGlunn | 0:f4b1c6a95f25 | 146 | |
EricGlunn | 0:f4b1c6a95f25 | 147 | |
EricGlunn | 0:f4b1c6a95f25 | 148 |