lab2 vu meter and 7 segment
Dependencies: mbed
main.cpp@0:8a1a4eabe49e, 2017-10-30 (annotated)
- Committer:
- Supermil
- Date:
- Mon Oct 30 06:54:23 2017 +0000
- Revision:
- 0:8a1a4eabe49e
lab2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Supermil | 0:8a1a4eabe49e | 1 | #include "mbed.h" |
Supermil | 0:8a1a4eabe49e | 2 | |
Supermil | 0:8a1a4eabe49e | 3 | AnalogIn analog_value(A2); |
Supermil | 0:8a1a4eabe49e | 4 | DigitalIn button(USER_BUTTON); |
Supermil | 0:8a1a4eabe49e | 5 | |
Supermil | 0:8a1a4eabe49e | 6 | DigitalOut d1(D11); |
Supermil | 0:8a1a4eabe49e | 7 | DigitalOut c1(D10); |
Supermil | 0:8a1a4eabe49e | 8 | DigitalOut b1(D9); |
Supermil | 0:8a1a4eabe49e | 9 | DigitalOut a1(D8); |
Supermil | 0:8a1a4eabe49e | 10 | DigitalOut d2(D5); |
Supermil | 0:8a1a4eabe49e | 11 | DigitalOut c2(D4); |
Supermil | 0:8a1a4eabe49e | 12 | DigitalOut b2(D3); |
Supermil | 0:8a1a4eabe49e | 13 | DigitalOut a2(D2); |
Supermil | 0:8a1a4eabe49e | 14 | |
Supermil | 0:8a1a4eabe49e | 15 | BusOut myled(PA_11,PB_12,PB_2,PB_1,PB_15,PB_14,PB_13,PC_4); |
Supermil | 0:8a1a4eabe49e | 16 | |
Supermil | 0:8a1a4eabe49e | 17 | float meas_r; |
Supermil | 0:8a1a4eabe49e | 18 | int meas_mv; |
Supermil | 0:8a1a4eabe49e | 19 | int meas_v; |
Supermil | 0:8a1a4eabe49e | 20 | int meas_vf; |
Supermil | 0:8a1a4eabe49e | 21 | int state; |
Supermil | 0:8a1a4eabe49e | 22 | int preState; |
Supermil | 0:8a1a4eabe49e | 23 | int buttonState; |
Supermil | 0:8a1a4eabe49e | 24 | int vuStatus,segmentStatus; |
Supermil | 0:8a1a4eabe49e | 25 | |
Supermil | 0:8a1a4eabe49e | 26 | |
Supermil | 0:8a1a4eabe49e | 27 | void Segment(){ |
Supermil | 0:8a1a4eabe49e | 28 | int i,j; |
Supermil | 0:8a1a4eabe49e | 29 | int bin1[4]= {0}; |
Supermil | 0:8a1a4eabe49e | 30 | int bin2[4]= {0}; |
Supermil | 0:8a1a4eabe49e | 31 | i = 0, j = 0; |
Supermil | 0:8a1a4eabe49e | 32 | do { |
Supermil | 0:8a1a4eabe49e | 33 | bin1[i++] = meas_v % 2; |
Supermil | 0:8a1a4eabe49e | 34 | meas_v = meas_v / 2; |
Supermil | 0:8a1a4eabe49e | 35 | }while(meas_v > 0); |
Supermil | 0:8a1a4eabe49e | 36 | do { |
Supermil | 0:8a1a4eabe49e | 37 | bin2[j++] = meas_vf % 2; |
Supermil | 0:8a1a4eabe49e | 38 | meas_vf = meas_vf / 2; |
Supermil | 0:8a1a4eabe49e | 39 | }while(meas_vf > 0); |
Supermil | 0:8a1a4eabe49e | 40 | // printf("SSSS = %d = %d = %d = %d \n", second[0],second[1],second[2],second[3]); |
Supermil | 0:8a1a4eabe49e | 41 | d1 = bin1[3]; |
Supermil | 0:8a1a4eabe49e | 42 | c1 = bin1[2]; |
Supermil | 0:8a1a4eabe49e | 43 | b1 = bin1[1]; |
Supermil | 0:8a1a4eabe49e | 44 | a1 = bin1[0]; |
Supermil | 0:8a1a4eabe49e | 45 | d2 = bin2[3]; |
Supermil | 0:8a1a4eabe49e | 46 | c2 = bin2[2]; |
Supermil | 0:8a1a4eabe49e | 47 | b2 = bin2[1]; |
Supermil | 0:8a1a4eabe49e | 48 | a2 = bin2[0]; |
Supermil | 0:8a1a4eabe49e | 49 | } |
Supermil | 0:8a1a4eabe49e | 50 | |
Supermil | 0:8a1a4eabe49e | 51 | void checkState(){ |
Supermil | 0:8a1a4eabe49e | 52 | preState = state; |
Supermil | 0:8a1a4eabe49e | 53 | while (1){ |
Supermil | 0:8a1a4eabe49e | 54 | if (meas_mv < 412.5){ |
Supermil | 0:8a1a4eabe49e | 55 | state = 1; |
Supermil | 0:8a1a4eabe49e | 56 | break; |
Supermil | 0:8a1a4eabe49e | 57 | } |
Supermil | 0:8a1a4eabe49e | 58 | if (meas_mv >= 412.5 && meas_mv < 825 ){ |
Supermil | 0:8a1a4eabe49e | 59 | state = 2; |
Supermil | 0:8a1a4eabe49e | 60 | break; |
Supermil | 0:8a1a4eabe49e | 61 | } |
Supermil | 0:8a1a4eabe49e | 62 | if (meas_mv >= 825 && meas_mv < 1237.5 ){ |
Supermil | 0:8a1a4eabe49e | 63 | state = 3; |
Supermil | 0:8a1a4eabe49e | 64 | break; |
Supermil | 0:8a1a4eabe49e | 65 | } |
Supermil | 0:8a1a4eabe49e | 66 | if (meas_mv >= 1237.5 && meas_mv < 1650 ){ |
Supermil | 0:8a1a4eabe49e | 67 | state = 4; |
Supermil | 0:8a1a4eabe49e | 68 | break; |
Supermil | 0:8a1a4eabe49e | 69 | } |
Supermil | 0:8a1a4eabe49e | 70 | if (meas_mv >= 1650 && meas_mv < 2062.5 ){ |
Supermil | 0:8a1a4eabe49e | 71 | state = 5; |
Supermil | 0:8a1a4eabe49e | 72 | break; |
Supermil | 0:8a1a4eabe49e | 73 | } |
Supermil | 0:8a1a4eabe49e | 74 | if (meas_mv >= 2062.5 && meas_mv < 2475 ){ |
Supermil | 0:8a1a4eabe49e | 75 | state = 6; |
Supermil | 0:8a1a4eabe49e | 76 | break; |
Supermil | 0:8a1a4eabe49e | 77 | } |
Supermil | 0:8a1a4eabe49e | 78 | if (meas_mv >= 2475 && meas_mv < 2887.5 ){ |
Supermil | 0:8a1a4eabe49e | 79 | state = 7; |
Supermil | 0:8a1a4eabe49e | 80 | break; |
Supermil | 0:8a1a4eabe49e | 81 | } |
Supermil | 0:8a1a4eabe49e | 82 | if (meas_mv >= 2887.5){ |
Supermil | 0:8a1a4eabe49e | 83 | state = 8; |
Supermil | 0:8a1a4eabe49e | 84 | break; |
Supermil | 0:8a1a4eabe49e | 85 | } |
Supermil | 0:8a1a4eabe49e | 86 | } |
Supermil | 0:8a1a4eabe49e | 87 | } |
Supermil | 0:8a1a4eabe49e | 88 | |
Supermil | 0:8a1a4eabe49e | 89 | void segmentSeto(){ |
Supermil | 0:8a1a4eabe49e | 90 | d1 = 0,c1 = 0,b1 = 0,a1 = 0,d2 = 0,c2 = 0,b2 = 0,a2 = 0; |
Supermil | 0:8a1a4eabe49e | 91 | } |
Supermil | 0:8a1a4eabe49e | 92 | |
Supermil | 0:8a1a4eabe49e | 93 | void vuSet0(){ |
Supermil | 0:8a1a4eabe49e | 94 | int m; |
Supermil | 0:8a1a4eabe49e | 95 | for ( m = 0;m <8 ;m++){ |
Supermil | 0:8a1a4eabe49e | 96 | myled[m] = 0; |
Supermil | 0:8a1a4eabe49e | 97 | } |
Supermil | 0:8a1a4eabe49e | 98 | } |
Supermil | 0:8a1a4eabe49e | 99 | |
Supermil | 0:8a1a4eabe49e | 100 | void vuMeter(){ |
Supermil | 0:8a1a4eabe49e | 101 | int k; |
Supermil | 0:8a1a4eabe49e | 102 | k = 0; |
Supermil | 0:8a1a4eabe49e | 103 | if(preState > state){ |
Supermil | 0:8a1a4eabe49e | 104 | vuSet0(); |
Supermil | 0:8a1a4eabe49e | 105 | } |
Supermil | 0:8a1a4eabe49e | 106 | while(vuStatus){ |
Supermil | 0:8a1a4eabe49e | 107 | checkState(); |
Supermil | 0:8a1a4eabe49e | 108 | do{ |
Supermil | 0:8a1a4eabe49e | 109 | myled[k++] = 1; |
Supermil | 0:8a1a4eabe49e | 110 | }while(k < state); |
Supermil | 0:8a1a4eabe49e | 111 | break; |
Supermil | 0:8a1a4eabe49e | 112 | } |
Supermil | 0:8a1a4eabe49e | 113 | } |
Supermil | 0:8a1a4eabe49e | 114 | |
Supermil | 0:8a1a4eabe49e | 115 | void checkButton(){ |
Supermil | 0:8a1a4eabe49e | 116 | int preB; |
Supermil | 0:8a1a4eabe49e | 117 | //buttonState = 0; |
Supermil | 0:8a1a4eabe49e | 118 | preB = buttonState; |
Supermil | 0:8a1a4eabe49e | 119 | if(button == 0){ |
Supermil | 0:8a1a4eabe49e | 120 | if (preB == 0){ |
Supermil | 0:8a1a4eabe49e | 121 | buttonState = 1; |
Supermil | 0:8a1a4eabe49e | 122 | } |
Supermil | 0:8a1a4eabe49e | 123 | if (preB == 1){ |
Supermil | 0:8a1a4eabe49e | 124 | buttonState = 0; |
Supermil | 0:8a1a4eabe49e | 125 | } |
Supermil | 0:8a1a4eabe49e | 126 | wait(0.5); |
Supermil | 0:8a1a4eabe49e | 127 | |
Supermil | 0:8a1a4eabe49e | 128 | } |
Supermil | 0:8a1a4eabe49e | 129 | printf("preButton = %d",preB); |
Supermil | 0:8a1a4eabe49e | 130 | printf("buttonState = %d",buttonState); |
Supermil | 0:8a1a4eabe49e | 131 | |
Supermil | 0:8a1a4eabe49e | 132 | |
Supermil | 0:8a1a4eabe49e | 133 | } |
Supermil | 0:8a1a4eabe49e | 134 | |
Supermil | 0:8a1a4eabe49e | 135 | int main() |
Supermil | 0:8a1a4eabe49e | 136 | { state = 0; |
Supermil | 0:8a1a4eabe49e | 137 | |
Supermil | 0:8a1a4eabe49e | 138 | while(1) { |
Supermil | 0:8a1a4eabe49e | 139 | |
Supermil | 0:8a1a4eabe49e | 140 | meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
Supermil | 0:8a1a4eabe49e | 141 | meas_mv = meas_r * 3300; // Converts value in the 0V-3.3V range |
Supermil | 0:8a1a4eabe49e | 142 | meas_v = meas_mv / 1000; |
Supermil | 0:8a1a4eabe49e | 143 | meas_vf = (meas_mv % 1000)/100; |
Supermil | 0:8a1a4eabe49e | 144 | |
Supermil | 0:8a1a4eabe49e | 145 | // Display values |
Supermil | 0:8a1a4eabe49e | 146 | // printf("measure = %f = %d mV = %d V = %d Vf\n", meas_r, meas_mv, meas_v, meas_vf); |
Supermil | 0:8a1a4eabe49e | 147 | // Segment(); |
Supermil | 0:8a1a4eabe49e | 148 | // vuMeter(); |
Supermil | 0:8a1a4eabe49e | 149 | // printf("State = %d \n",state); |
Supermil | 0:8a1a4eabe49e | 150 | // printf("Prestate = %d \n",preState); |
Supermil | 0:8a1a4eabe49e | 151 | checkButton(); |
Supermil | 0:8a1a4eabe49e | 152 | if (buttonState == 0){ |
Supermil | 0:8a1a4eabe49e | 153 | segmentSeto(); |
Supermil | 0:8a1a4eabe49e | 154 | vuMeter(); |
Supermil | 0:8a1a4eabe49e | 155 | vuStatus = 1; |
Supermil | 0:8a1a4eabe49e | 156 | segmentStatus = 0; |
Supermil | 0:8a1a4eabe49e | 157 | } |
Supermil | 0:8a1a4eabe49e | 158 | else if (buttonState == 1){ |
Supermil | 0:8a1a4eabe49e | 159 | vuSet0(); |
Supermil | 0:8a1a4eabe49e | 160 | Segment(); |
Supermil | 0:8a1a4eabe49e | 161 | vuStatus = 0; |
Supermil | 0:8a1a4eabe49e | 162 | segmentStatus = 1; |
Supermil | 0:8a1a4eabe49e | 163 | |
Supermil | 0:8a1a4eabe49e | 164 | } |
Supermil | 0:8a1a4eabe49e | 165 | printf("vu = %d = seg = %d \n",vuStatus,segmentStatus); |
Supermil | 0:8a1a4eabe49e | 166 | |
Supermil | 0:8a1a4eabe49e | 167 | |
Supermil | 0:8a1a4eabe49e | 168 | } |
Supermil | 0:8a1a4eabe49e | 169 | } |