Dependencies: TextLCD mbed PID
main.cpp@0:1f66eaf1013d, 2018-09-19 (annotated)
- Committer:
- nicovv44
- Date:
- Wed Sep 19 17:00:19 2018 +0000
- Revision:
- 0:1f66eaf1013d
- Child:
- 1:f31a46c62d10
start
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nicovv44 | 0:1f66eaf1013d | 1 | // Hello World! for the TextLCD |
nicovv44 | 0:1f66eaf1013d | 2 | |
nicovv44 | 0:1f66eaf1013d | 3 | #include "mbed.h" |
nicovv44 | 0:1f66eaf1013d | 4 | #include "TextLCD.h" |
nicovv44 | 0:1f66eaf1013d | 5 | |
nicovv44 | 0:1f66eaf1013d | 6 | TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7 |
nicovv44 | 0:1f66eaf1013d | 7 | Serial pc(USBTX, USBRX); // tx, rx |
nicovv44 | 0:1f66eaf1013d | 8 | PwmOut pwm1(D9); |
nicovv44 | 0:1f66eaf1013d | 9 | |
nicovv44 | 0:1f66eaf1013d | 10 | AnalogIn analog_pin1(A0); |
nicovv44 | 0:1f66eaf1013d | 11 | |
nicovv44 | 0:1f66eaf1013d | 12 | const float sqrt2 = 1.414213562; |
nicovv44 | 0:1f66eaf1013d | 13 | |
nicovv44 | 0:1f66eaf1013d | 14 | Timeout timeout; |
nicovv44 | 0:1f66eaf1013d | 15 | volatile bool looping = false; |
nicovv44 | 0:1f66eaf1013d | 16 | |
nicovv44 | 0:1f66eaf1013d | 17 | |
nicovv44 | 0:1f66eaf1013d | 18 | |
nicovv44 | 0:1f66eaf1013d | 19 | |
nicovv44 | 0:1f66eaf1013d | 20 | |
nicovv44 | 0:1f66eaf1013d | 21 | |
nicovv44 | 0:1f66eaf1013d | 22 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 23 | // ########## PROTOTYPES ######################## |
nicovv44 | 0:1f66eaf1013d | 24 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 25 | void stopLooping(void); |
nicovv44 | 0:1f66eaf1013d | 26 | float getVolageRMS(AnalogIn ana_pin); |
nicovv44 | 0:1f66eaf1013d | 27 | float getVolageReadedMax(AnalogIn ana_pin); |
nicovv44 | 0:1f66eaf1013d | 28 | float getFrequency(AnalogIn ana_pin); |
nicovv44 | 0:1f66eaf1013d | 29 | |
nicovv44 | 0:1f66eaf1013d | 30 | |
nicovv44 | 0:1f66eaf1013d | 31 | |
nicovv44 | 0:1f66eaf1013d | 32 | |
nicovv44 | 0:1f66eaf1013d | 33 | |
nicovv44 | 0:1f66eaf1013d | 34 | |
nicovv44 | 0:1f66eaf1013d | 35 | |
nicovv44 | 0:1f66eaf1013d | 36 | |
nicovv44 | 0:1f66eaf1013d | 37 | |
nicovv44 | 0:1f66eaf1013d | 38 | |
nicovv44 | 0:1f66eaf1013d | 39 | |
nicovv44 | 0:1f66eaf1013d | 40 | |
nicovv44 | 0:1f66eaf1013d | 41 | |
nicovv44 | 0:1f66eaf1013d | 42 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 43 | // ########## MAIN ############################## |
nicovv44 | 0:1f66eaf1013d | 44 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 45 | int main() { |
nicovv44 | 0:1f66eaf1013d | 46 | float Vm; |
nicovv44 | 0:1f66eaf1013d | 47 | float freq1; |
nicovv44 | 0:1f66eaf1013d | 48 | |
nicovv44 | 0:1f66eaf1013d | 49 | /*// specify period first |
nicovv44 | 0:1f66eaf1013d | 50 | led.period(4.0f); // 4 second period |
nicovv44 | 0:1f66eaf1013d | 51 | led.write(0.50f); // 50% duty cycle, relative to period |
nicovv44 | 0:1f66eaf1013d | 52 | //led = 0.5f; // shorthand for led.write() |
nicovv44 | 0:1f66eaf1013d | 53 | //led.pulsewidth(2); // alternative to led.write, set duty cycle time in seconds |
nicovv44 | 0:1f66eaf1013d | 54 | while(1);*/ |
nicovv44 | 0:1f66eaf1013d | 55 | |
nicovv44 | 0:1f66eaf1013d | 56 | |
nicovv44 | 0:1f66eaf1013d | 57 | Vm = getVolageRMS(analog_pin1); |
nicovv44 | 0:1f66eaf1013d | 58 | lcd.locate(0,0); |
nicovv44 | 0:1f66eaf1013d | 59 | lcd.printf("Vm=%f", Vm); |
nicovv44 | 0:1f66eaf1013d | 60 | freq1 = getFrequency(analog_pin1); |
nicovv44 | 0:1f66eaf1013d | 61 | lcd.locate(0,1); |
nicovv44 | 0:1f66eaf1013d | 62 | lcd.printf("freq1=%f", freq1); |
nicovv44 | 0:1f66eaf1013d | 63 | } |
nicovv44 | 0:1f66eaf1013d | 64 | |
nicovv44 | 0:1f66eaf1013d | 65 | |
nicovv44 | 0:1f66eaf1013d | 66 | |
nicovv44 | 0:1f66eaf1013d | 67 | |
nicovv44 | 0:1f66eaf1013d | 68 | |
nicovv44 | 0:1f66eaf1013d | 69 | |
nicovv44 | 0:1f66eaf1013d | 70 | |
nicovv44 | 0:1f66eaf1013d | 71 | // ############################################## |
nicovv44 | 0:1f66eaf1013d | 72 | // ########## FUNCTIONS ######################### |
nicovv44 | 0:1f66eaf1013d | 73 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 74 | // ISR to stop loping |
nicovv44 | 0:1f66eaf1013d | 75 | void stopLooping(void) { |
nicovv44 | 0:1f66eaf1013d | 76 | looping = false;//looping is volatile bool |
nicovv44 | 0:1f66eaf1013d | 77 | } |
nicovv44 | 0:1f66eaf1013d | 78 | |
nicovv44 | 0:1f66eaf1013d | 79 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 80 | float getVolageRMS(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 81 | float v1;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 82 | float v1Max = 0;//max readed voltage |
nicovv44 | 0:1f66eaf1013d | 83 | float VRMS; //RMS voltage |
nicovv44 | 0:1f66eaf1013d | 84 | looping = true; |
nicovv44 | 0:1f66eaf1013d | 85 | timeout.attach(callback(&stopLooping), 0.002);//T=20ms because f=50Hz |
nicovv44 | 0:1f66eaf1013d | 86 | while(looping){ |
nicovv44 | 0:1f66eaf1013d | 87 | v1 = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 88 | if(v1 > v1Max){ |
nicovv44 | 0:1f66eaf1013d | 89 | v1Max = v1; |
nicovv44 | 0:1f66eaf1013d | 90 | } |
nicovv44 | 0:1f66eaf1013d | 91 | } |
nicovv44 | 0:1f66eaf1013d | 92 | VRMS = (v1Max+0.685)*20.8/sqrt2; |
nicovv44 | 0:1f66eaf1013d | 93 | return VRMS; |
nicovv44 | 0:1f66eaf1013d | 94 | } |
nicovv44 | 0:1f66eaf1013d | 95 | |
nicovv44 | 0:1f66eaf1013d | 96 | |
nicovv44 | 0:1f66eaf1013d | 97 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 98 | float getVolageReadedMax(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 99 | float v1;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 100 | float v1Max = 0;//max readed voltage |
nicovv44 | 0:1f66eaf1013d | 101 | looping = true; |
nicovv44 | 0:1f66eaf1013d | 102 | timeout.attach(callback(&stopLooping), 0.002);//T=20ms because f=50Hz |
nicovv44 | 0:1f66eaf1013d | 103 | while(looping){ |
nicovv44 | 0:1f66eaf1013d | 104 | v1 = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 105 | if(v1 > v1Max){ |
nicovv44 | 0:1f66eaf1013d | 106 | v1Max = v1; |
nicovv44 | 0:1f66eaf1013d | 107 | } |
nicovv44 | 0:1f66eaf1013d | 108 | } |
nicovv44 | 0:1f66eaf1013d | 109 | return v1Max; |
nicovv44 | 0:1f66eaf1013d | 110 | } |
nicovv44 | 0:1f66eaf1013d | 111 | |
nicovv44 | 0:1f66eaf1013d | 112 | // ############################################################################# |
nicovv44 | 0:1f66eaf1013d | 113 | // getFrequency ####### |
nicovv44 | 0:1f66eaf1013d | 114 | float getFrequency(AnalogIn ana_pin){ |
nicovv44 | 0:1f66eaf1013d | 115 | float freq; //frequency |
nicovv44 | 0:1f66eaf1013d | 116 | float maxReadedVoltage;//maximum voltage readed by the ADC |
nicovv44 | 0:1f66eaf1013d | 117 | //float vOld;//old readed voltage |
nicovv44 | 0:1f66eaf1013d | 118 | //float vNew;//new readed voltage |
nicovv44 | 0:1f66eaf1013d | 119 | float readedVoltage;//readed voltage |
nicovv44 | 0:1f66eaf1013d | 120 | int nbrRisingEdge=0;// number of rising edge detected |
nicovv44 | 0:1f66eaf1013d | 121 | float T;//Periode |
nicovv44 | 0:1f66eaf1013d | 122 | Timer timer; |
nicovv44 | 0:1f66eaf1013d | 123 | maxReadedVoltage = getVolageReadedMax(ana_pin); |
nicovv44 | 0:1f66eaf1013d | 124 | /*vOld = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 125 | vNew = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 126 | while(nbrRisingEdge<2){ |
nicovv44 | 0:1f66eaf1013d | 127 | if(vOld<(maxReadedVoltage/2) && (maxReadedVoltage/2)<vNew){//rising detected |
nicovv44 | 0:1f66eaf1013d | 128 | if(nbrRisingEdge==0) |
nicovv44 | 0:1f66eaf1013d | 129 | timer.start(); |
nicovv44 | 0:1f66eaf1013d | 130 | if(nbrRisingEdge==1) |
nicovv44 | 0:1f66eaf1013d | 131 | timer.stop(); |
nicovv44 | 0:1f66eaf1013d | 132 | nbrRisingEdge++; |
nicovv44 | 0:1f66eaf1013d | 133 | } |
nicovv44 | 0:1f66eaf1013d | 134 | vOld = vNew; |
nicovv44 | 0:1f66eaf1013d | 135 | vNew = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 136 | }*/ |
nicovv44 | 0:1f66eaf1013d | 137 | bool aboveLine = true; |
nicovv44 | 0:1f66eaf1013d | 138 | while(nbrRisingEdge<2){ |
nicovv44 | 0:1f66eaf1013d | 139 | readedVoltage = ana_pin.read()*3.3; |
nicovv44 | 0:1f66eaf1013d | 140 | if(readedVoltage<(maxReadedVoltage/2)){//rising edge detection ready |
nicovv44 | 0:1f66eaf1013d | 141 | aboveLine = false; |
nicovv44 | 0:1f66eaf1013d | 142 | } |
nicovv44 | 0:1f66eaf1013d | 143 | if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected |
nicovv44 | 0:1f66eaf1013d | 144 | aboveLine = true; |
nicovv44 | 0:1f66eaf1013d | 145 | if(nbrRisingEdge==0) |
nicovv44 | 0:1f66eaf1013d | 146 | timer.start(); |
nicovv44 | 0:1f66eaf1013d | 147 | if(nbrRisingEdge==1) |
nicovv44 | 0:1f66eaf1013d | 148 | timer.stop(); |
nicovv44 | 0:1f66eaf1013d | 149 | nbrRisingEdge++; |
nicovv44 | 0:1f66eaf1013d | 150 | } |
nicovv44 | 0:1f66eaf1013d | 151 | } |
nicovv44 | 0:1f66eaf1013d | 152 | T = timer.read(); |
nicovv44 | 0:1f66eaf1013d | 153 | freq = 1/T; |
nicovv44 | 0:1f66eaf1013d | 154 | return freq; |
nicovv44 | 0:1f66eaf1013d | 155 | } |
nicovv44 | 0:1f66eaf1013d | 156 | |
nicovv44 | 0:1f66eaf1013d | 157 | |
nicovv44 | 0:1f66eaf1013d | 158 | |
nicovv44 | 0:1f66eaf1013d | 159 | |
nicovv44 | 0:1f66eaf1013d | 160 | |
nicovv44 | 0:1f66eaf1013d | 161 | |
nicovv44 | 0:1f66eaf1013d | 162 | |
nicovv44 | 0:1f66eaf1013d | 163 | |
nicovv44 | 0:1f66eaf1013d | 164 | |
nicovv44 | 0:1f66eaf1013d | 165 | |
nicovv44 | 0:1f66eaf1013d | 166 | |
nicovv44 | 0:1f66eaf1013d | 167 |