Oscilloscope 20Hz20kHZ Labview kl25z
Dependencies: ComparatorIn mbed
main.cpp@0:b04378e035d8, 2016-12-19 (annotated)
- Committer:
- inuD
- Date:
- Mon Dec 19 18:47:36 2016 +0000
- Revision:
- 0:b04378e035d8
Oscilloscope 20hz-20khz LabView kl25z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inuD | 0:b04378e035d8 | 1 | #include "mbed.h" |
inuD | 0:b04378e035d8 | 2 | #include "ComparatorIn.h" |
inuD | 0:b04378e035d8 | 3 | |
inuD | 0:b04378e035d8 | 4 | #define MAX_SAMPLERATE 50000 |
inuD | 0:b04378e035d8 | 5 | #define Min_FREQ 20 |
inuD | 0:b04378e035d8 | 6 | #define MAX_FREQ 20000 |
inuD | 0:b04378e035d8 | 7 | #define MaxFrdmVAnalogRef 3.3 |
inuD | 0:b04378e035d8 | 8 | #define ThresholdOut 1000 |
inuD | 0:b04378e035d8 | 9 | |
inuD | 0:b04378e035d8 | 10 | Serial pc(USBTX,USBRX); |
inuD | 0:b04378e035d8 | 11 | |
inuD | 0:b04378e035d8 | 12 | double AutoTrig, ValTrig, RiseOrFall, nWave; // 1-0 for autotrig, value of trigger, on rise or fall of WaveFront |
inuD | 0:b04378e035d8 | 13 | double AutoSr, ValSr; // 1-0 for autoSampleRate (max Value), value of SampleRate |
inuD | 0:b04378e035d8 | 14 | double NumberSample, TranslationSample; // number of sample send to pc, first or last sample discard |
inuD | 0:b04378e035d8 | 15 | |
inuD | 0:b04378e035d8 | 16 | double Threshold = 0; |
inuD | 0:b04378e035d8 | 17 | double SampleRate = MAX_SAMPLERATE; |
inuD | 0:b04378e035d8 | 18 | double MaxFrdmVAnalog; |
inuD | 0:b04378e035d8 | 19 | |
inuD | 0:b04378e035d8 | 20 | int dimArr; |
inuD | 0:b04378e035d8 | 21 | int stopArray = -1; |
inuD | 0:b04378e035d8 | 22 | int startArray = -1; |
inuD | 0:b04378e035d8 | 23 | int numbWave = 1; |
inuD | 0:b04378e035d8 | 24 | double range; |
inuD | 0:b04378e035d8 | 25 | double BottomLimit; |
inuD | 0:b04378e035d8 | 26 | double TopLimit; |
inuD | 0:b04378e035d8 | 27 | double adcRead; |
inuD | 0:b04378e035d8 | 28 | |
inuD | 0:b04378e035d8 | 29 | |
inuD | 0:b04378e035d8 | 30 | AnalogIn DAC_In (PTB0); |
inuD | 0:b04378e035d8 | 31 | ComparatorIn COMP_In(PTC8, PTE30); // in+ = PTC8, in- = 12-bit DAC |
inuD | 0:b04378e035d8 | 32 | |
inuD | 0:b04378e035d8 | 33 | |
inuD | 0:b04378e035d8 | 34 | // Comparator callback functions |
inuD | 0:b04378e035d8 | 35 | void cmp_rise_ISR(void){ |
inuD | 0:b04378e035d8 | 36 | stopArray = stopArray + 1; |
inuD | 0:b04378e035d8 | 37 | startArray = 0; |
inuD | 0:b04378e035d8 | 38 | } |
inuD | 0:b04378e035d8 | 39 | |
inuD | 0:b04378e035d8 | 40 | void cmp_fall_ISR(void){ |
inuD | 0:b04378e035d8 | 41 | stopArray = stopArray + 1; |
inuD | 0:b04378e035d8 | 42 | startArray = 0; |
inuD | 0:b04378e035d8 | 43 | } |
inuD | 0:b04378e035d8 | 44 | |
inuD | 0:b04378e035d8 | 45 | void Setting(double autoSr, double valSr, double autoTrig, double valTrig, double riseOrFall){ |
inuD | 0:b04378e035d8 | 46 | if (autoSr == 1){ |
inuD | 0:b04378e035d8 | 47 | SampleRate = MAX_SAMPLERATE; |
inuD | 0:b04378e035d8 | 48 | } |
inuD | 0:b04378e035d8 | 49 | |
inuD | 0:b04378e035d8 | 50 | else{ |
inuD | 0:b04378e035d8 | 51 | SampleRate = valSr; |
inuD | 0:b04378e035d8 | 52 | } |
inuD | 0:b04378e035d8 | 53 | |
inuD | 0:b04378e035d8 | 54 | Threshold = (valTrig * MaxFrdmVAnalog) - BottomLimit; //Value Trigger desired / Max Input |
inuD | 0:b04378e035d8 | 55 | //all /MaxFrdmVAnalogRef |
inuD | 0:b04378e035d8 | 56 | //Threshold = (valTrig / MaxFrdmVAnalog) + BottomLimit; |
inuD | 0:b04378e035d8 | 57 | |
inuD | 0:b04378e035d8 | 58 | if (autoTrig == 1){ |
inuD | 0:b04378e035d8 | 59 | |
inuD | 0:b04378e035d8 | 60 | COMP_In.treshold(Threshold); //Set Threshold |
inuD | 0:b04378e035d8 | 61 | |
inuD | 0:b04378e035d8 | 62 | if (riseOrFall == 0){ |
inuD | 0:b04378e035d8 | 63 | COMP_In.rising(cmp_rise_ISR); // Set pointer to rising interrupt functio |
inuD | 0:b04378e035d8 | 64 | } |
inuD | 0:b04378e035d8 | 65 | else { |
inuD | 0:b04378e035d8 | 66 | COMP_In.falling(cmp_fall_ISR); // Set pointer to falling interrupt function |
inuD | 0:b04378e035d8 | 67 | } |
inuD | 0:b04378e035d8 | 68 | } |
inuD | 0:b04378e035d8 | 69 | else{ |
inuD | 0:b04378e035d8 | 70 | COMP_In.treshold(ThresholdOut); |
inuD | 0:b04378e035d8 | 71 | } |
inuD | 0:b04378e035d8 | 72 | } |
inuD | 0:b04378e035d8 | 73 | |
inuD | 0:b04378e035d8 | 74 | void printWave(int dimarr, int sampleRate, int numberSample, int translationSample){ |
inuD | 0:b04378e035d8 | 75 | |
inuD | 0:b04378e035d8 | 76 | double dataLab[dimarr]; |
inuD | 0:b04378e035d8 | 77 | for (int i = 0; i < dimarr; i++){ // fare if di variabile - su callback function |
inuD | 0:b04378e035d8 | 78 | |
inuD | 0:b04378e035d8 | 79 | adcRead = (DAC_In / MaxFrdmVAnalog) + BottomLimit; //all *MaxFrdmVAnalogRef? |
inuD | 0:b04378e035d8 | 80 | dataLab[i] = adcRead; |
inuD | 0:b04378e035d8 | 81 | |
inuD | 0:b04378e035d8 | 82 | double SR = sampleRate; |
inuD | 0:b04378e035d8 | 83 | wait(1 / SR); |
inuD | 0:b04378e035d8 | 84 | } |
inuD | 0:b04378e035d8 | 85 | |
inuD | 0:b04378e035d8 | 86 | int dimarray = dimarr - numberSample; |
inuD | 0:b04378e035d8 | 87 | int startarray = dimarray*translationSample/10; |
inuD | 0:b04378e035d8 | 88 | |
inuD | 0:b04378e035d8 | 89 | for(int j = startarray; j< dimarr - dimarray; j++){ |
inuD | 0:b04378e035d8 | 90 | printf("%f ", dataLab[j]); |
inuD | 0:b04378e035d8 | 91 | } |
inuD | 0:b04378e035d8 | 92 | |
inuD | 0:b04378e035d8 | 93 | } |
inuD | 0:b04378e035d8 | 94 | |
inuD | 0:b04378e035d8 | 95 | |
inuD | 0:b04378e035d8 | 96 | int main() { |
inuD | 0:b04378e035d8 | 97 | |
inuD | 0:b04378e035d8 | 98 | if (MAX_SAMPLERATE < 2*MAX_FREQ) |
inuD | 0:b04378e035d8 | 99 | { |
inuD | 0:b04378e035d8 | 100 | printf("Error SampleRate"); |
inuD | 0:b04378e035d8 | 101 | } |
inuD | 0:b04378e035d8 | 102 | |
inuD | 0:b04378e035d8 | 103 | while(1) { |
inuD | 0:b04378e035d8 | 104 | // there are some parameters only for Labview |
inuD | 0:b04378e035d8 | 105 | pc.scanf("%f,%f,%f,%f,%f,%f,%f,%f,%f", &AutoTrig, &ValTrig, &nWave , &RiseOrFall, &AutoSr, &ValSr, &NumberSample, &TranslationSample, &TopLimit, &BottomLimit); |
inuD | 0:b04378e035d8 | 106 | double DimensionArray = (SampleRate / Min_FREQ) * 2; //at least 2 waveform of the minimum Frequ |
inuD | 0:b04378e035d8 | 107 | dimArr = int (DimensionArray); |
inuD | 0:b04378e035d8 | 108 | //numbWave = int (nWave); |
inuD | 0:b04378e035d8 | 109 | |
inuD | 0:b04378e035d8 | 110 | range = TopLimit - BottomLimit; |
inuD | 0:b04378e035d8 | 111 | MaxFrdmVAnalog = MaxFrdmVAnalogRef / range; |
inuD | 0:b04378e035d8 | 112 | |
inuD | 0:b04378e035d8 | 113 | Setting(AutoSr, ValSr, AutoTrig, ValTrig, RiseOrFall); //testing - setting every some sec(contator intern) |
inuD | 0:b04378e035d8 | 114 | if (AutoTrig == 1){ |
inuD | 0:b04378e035d8 | 115 | if (startArray == 0 && stopArray == 0){ |
inuD | 0:b04378e035d8 | 116 | printWave(dimArr, SampleRate, NumberSample, TranslationSample); |
inuD | 0:b04378e035d8 | 117 | startArray = -1; |
inuD | 0:b04378e035d8 | 118 | stopArray = -1; |
inuD | 0:b04378e035d8 | 119 | } |
inuD | 0:b04378e035d8 | 120 | } |
inuD | 0:b04378e035d8 | 121 | else{ |
inuD | 0:b04378e035d8 | 122 | printWave(dimArr, SampleRate, NumberSample, TranslationSample); |
inuD | 0:b04378e035d8 | 123 | } |
inuD | 0:b04378e035d8 | 124 | } |
inuD | 0:b04378e035d8 | 125 | } |
inuD | 0:b04378e035d8 | 126 |