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