123

Dependencies:   mbed

Fork of LG by igor Apu

Committer:
Diletant
Date:
Sun Jul 31 06:19:02 2016 +0000
Revision:
183:c7a9c309086c
Parent:
182:2bd8ec44998f
Device&... update. More AskGld&Techno functionality.NotFinal!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Diletant 149:abbf7663d27d 1 #include "Device.h"
Diletant 149:abbf7663d27d 2 extern Device device;
Diletant 173:7f938afb0447 3 extern unsigned int SystemCoreClock1;
Diletant 149:abbf7663d27d 4
Diletant 167:bedc0a9d559a 5 void InitDitherDefaultSettings(void){
Diletant 173:7f938afb0447 6 //Init pulse default settings
Diletant 183:c7a9c309086c 7 device.dither.pulse.settings.width = 0x00002000; //0.125
Diletant 173:7f938afb0447 8 device.dither.pulse.settings.min = 0x00000400; //0.031
Diletant 173:7f938afb0447 9 device.dither.pulse.settings.max = 0x00004000; //0.5
Diletant 173:7f938afb0447 10
Diletant 173:7f938afb0447 11 //Init noise default settings
Diletant 173:7f938afb0447 12 device.dither.noise.settings.period = 20;
Diletant 173:7f938afb0447 13 device.dither.noise.settings.range = 20;
Diletant 173:7f938afb0447 14 device.dither.noise.settings.amplitude = 0x00000400; //0.031
Diletant 173:7f938afb0447 15
Diletant 173:7f938afb0447 16 //Init dither cycle default settings
Diletant 161:efd949e8d536 17 device.dither.cycle.settings.enabled = 1;
Diletant 173:7f938afb0447 18
Diletant 173:7f938afb0447 19 //Init phase detector default settings
Diletant 173:7f938afb0447 20 device.dither.detector.settings.filter.factor[0] = -0x00010000;
Diletant 173:7f938afb0447 21 device.dither.detector.settings.filter.factor[1] = -0x00010000;
Diletant 173:7f938afb0447 22 device.dither.detector.settings.filter.factor[2] = -0x00010000;
Diletant 173:7f938afb0447 23 device.dither.detector.settings.filter.factor[3] = -0x00010000;
Diletant 173:7f938afb0447 24 device.dither.detector.settings.filter.factor[4] = -0x00010000;
Diletant 173:7f938afb0447 25 device.dither.detector.settings.filter.factor[5] = -0x00010000;
Diletant 173:7f938afb0447 26 device.dither.detector.settings.filter.factor[6] = -0x00010000;
Diletant 173:7f938afb0447 27 device.dither.detector.settings.filter.factor[7] = -0x00010000;
Diletant 161:efd949e8d536 28
Diletant 173:7f938afb0447 29 device.dither.detector.settings.filter.factor[8] = -0x00010000;
Diletant 173:7f938afb0447 30 device.dither.detector.settings.filter.factor[9] = -0x00010000;
Diletant 173:7f938afb0447 31 device.dither.detector.settings.filter.factor[10] = -0x00010000;
Diletant 173:7f938afb0447 32 device.dither.detector.settings.filter.factor[11] = -0x00010000;
Diletant 173:7f938afb0447 33 device.dither.detector.settings.filter.factor[12] = -0x00010000;
Diletant 173:7f938afb0447 34 device.dither.detector.settings.filter.factor[13] = -0x00010000;
Diletant 173:7f938afb0447 35 device.dither.detector.settings.filter.factor[14] = -0x00010000;
Diletant 173:7f938afb0447 36 device.dither.detector.settings.filter.factor[15] = -0x00010000;
Diletant 173:7f938afb0447 37
Diletant 173:7f938afb0447 38 device.dither.detector.settings.filter.factor[16] = 0x00010000;
Diletant 173:7f938afb0447 39 device.dither.detector.settings.filter.factor[17] = 0x00010000;
Diletant 173:7f938afb0447 40 device.dither.detector.settings.filter.factor[18] = 0x00010000;
Diletant 173:7f938afb0447 41 device.dither.detector.settings.filter.factor[19] = 0x00010000;
Diletant 173:7f938afb0447 42 device.dither.detector.settings.filter.factor[20] = 0x00010000;
Diletant 173:7f938afb0447 43 device.dither.detector.settings.filter.factor[21] = 0x00010000;
Diletant 173:7f938afb0447 44 device.dither.detector.settings.filter.factor[22] = 0x00010000;
Diletant 173:7f938afb0447 45 device.dither.detector.settings.filter.factor[23] = 0x00010000;
Diletant 173:7f938afb0447 46
Diletant 173:7f938afb0447 47 device.dither.detector.settings.filter.factor[24] = 0x00010000;
Diletant 173:7f938afb0447 48 device.dither.detector.settings.filter.factor[25] = 0x00010000;
Diletant 173:7f938afb0447 49 device.dither.detector.settings.filter.factor[26] = 0x00010000;
Diletant 173:7f938afb0447 50 device.dither.detector.settings.filter.factor[27] = 0x00010000;
Diletant 173:7f938afb0447 51 device.dither.detector.settings.filter.factor[28] = 0x00010000;
Diletant 173:7f938afb0447 52 device.dither.detector.settings.filter.factor[29] = 0x00010000;
Diletant 173:7f938afb0447 53 device.dither.detector.settings.filter.factor[30] = 0x00010000;
Diletant 173:7f938afb0447 54 device.dither.detector.settings.filter.factor[31] = 0x00010000;
Diletant 161:efd949e8d536 55
Diletant 173:7f938afb0447 56 device.dither.detector.settings.transfer.points = 16;
Diletant 173:7f938afb0447 57 device.dither.detector.settings.transfer.raw[0] = -0x10000; //-1.0
Diletant 173:7f938afb0447 58 device.dither.detector.settings.transfer.restored[0] = -0x1921f; //-1.5707963267948966
Diletant 173:7f938afb0447 59 device.dither.detector.settings.transfer.raw[1] = -0xfa67; //-0.9781476007338056
Diletant 173:7f938afb0447 60 device.dither.detector.settings.transfer.restored[1] = -0x15c81; //-1.361356816555577
Diletant 173:7f938afb0447 61 device.dither.detector.settings.transfer.raw[2] = -0xe9de; //-0.9135454576426009
Diletant 173:7f938afb0447 62 device.dither.detector.settings.transfer.restored[2] = -0x126e4; //-1.1519173063162575
Diletant 173:7f938afb0447 63 device.dither.detector.settings.transfer.raw[3] = -0xcf1b; //-0.8090169943749475
Diletant 173:7f938afb0447 64 device.dither.detector.settings.transfer.restored[3] = -0xf146; //-0.9424777960769379
Diletant 173:7f938afb0447 65 device.dither.detector.settings.transfer.raw[4] = -0xab4c; //-0.6691306063588582
Diletant 173:7f938afb0447 66 device.dither.detector.settings.transfer.restored[4] = -0xbba8; //-0.7330382858376184
Diletant 173:7f938afb0447 67 device.dither.detector.settings.transfer.raw[5] = -0x8000; //-0.5
Diletant 173:7f938afb0447 68 device.dither.detector.settings.transfer.restored[5] = -0x860a; //-0.5235987755982989
Diletant 173:7f938afb0447 69 device.dither.detector.settings.transfer.raw[6] = -0x4f1b; //-0.3090169943749474
Diletant 173:7f938afb0447 70 device.dither.detector.settings.transfer.restored[6] = -0x506c; //-0.3141592653589793
Diletant 173:7f938afb0447 71 device.dither.detector.settings.transfer.raw[7] = -0x1ac2; //-0.10452846326765361
Diletant 173:7f938afb0447 72 device.dither.detector.settings.transfer.restored[7] = -0x1ace; //-0.10471975511965992
Diletant 173:7f938afb0447 73 device.dither.detector.settings.transfer.raw[8] = 0x1ac2; //0.10452846326765339
Diletant 173:7f938afb0447 74 device.dither.detector.settings.transfer.restored[8] = 0x1ace; //0.1047197551196597
Diletant 173:7f938afb0447 75 device.dither.detector.settings.transfer.raw[9] = 0x4f1b; //0.3090169943749474
Diletant 173:7f938afb0447 76 device.dither.detector.settings.transfer.restored[9] = 0x506c; //0.3141592653589793
Diletant 173:7f938afb0447 77 device.dither.detector.settings.transfer.raw[10] = 0x7fff; //0.49999999999999983
Diletant 173:7f938afb0447 78 device.dither.detector.settings.transfer.restored[10] = 0x860a; //0.5235987755982987
Diletant 173:7f938afb0447 79 device.dither.detector.settings.transfer.raw[11] = 0xab4c; //0.669130606358858
Diletant 173:7f938afb0447 80 device.dither.detector.settings.transfer.restored[11] = 0xbba8; //0.7330382858376181
Diletant 173:7f938afb0447 81 device.dither.detector.settings.transfer.raw[12] = 0xcf1b; //0.8090169943749475
Diletant 173:7f938afb0447 82 device.dither.detector.settings.transfer.restored[12] = 0xf146; //0.9424777960769379
Diletant 173:7f938afb0447 83 device.dither.detector.settings.transfer.raw[13] = 0xe9de; //0.9135454576426009
Diletant 173:7f938afb0447 84 device.dither.detector.settings.transfer.restored[13] = 0x126e4; //1.1519173063162573
Diletant 173:7f938afb0447 85 device.dither.detector.settings.transfer.raw[14] = 0xfa67; //0.9781476007338056
Diletant 173:7f938afb0447 86 device.dither.detector.settings.transfer.restored[14] = 0x15c81; //1.3613568165555767
Diletant 173:7f938afb0447 87 device.dither.detector.settings.transfer.raw[15] = 0x10000; //1.0
Diletant 173:7f938afb0447 88 device.dither.detector.settings.transfer.restored[15] = 0x1921f; //1.5707963267948966
Diletant 161:efd949e8d536 89
Diletant 173:7f938afb0447 90 //Init frequency regulator default settings
Diletant 173:7f938afb0447 91 device.dither.frequency.settings.enabled = 1;
Diletant 183:c7a9c309086c 92 device.dither.frequency.settings.min = 0x01630000;//355Hz 0x01860000; //390 Hz in 16.16 format
Diletant 183:c7a9c309086c 93 device.dither.frequency.settings.max = 0x01680000;//365 0x019A0000; //410 Hz in 16.16 format
Diletant 183:c7a9c309086c 94 device.dither.frequency.settings.scale = 0x00010000; //0.0625Hz
Diletant 173:7f938afb0447 95 device.dither.frequency.settings.transfer.points = 16;
Diletant 173:7f938afb0447 96 device.dither.frequency.settings.transfer.error[0] = -0x140000; //-20.0Hz
Diletant 173:7f938afb0447 97 device.dither.frequency.settings.transfer.correction[0] = 0x10000; //1.0
Diletant 173:7f938afb0447 98 device.dither.frequency.settings.transfer.error[1] = -0x115555; //-17.333333333333332Hz
Diletant 173:7f938afb0447 99 device.dither.frequency.settings.transfer.correction[1] = 0xb473; //0.7048888888888889
Diletant 173:7f938afb0447 100 device.dither.frequency.settings.transfer.error[2] = -0xeaaaa; //-14.666666666666668Hz
Diletant 173:7f938afb0447 101 device.dither.frequency.settings.transfer.correction[2] = 0x7aa7; //0.47911111111111115
Diletant 173:7f938afb0447 102 device.dither.frequency.settings.transfer.error[3] = -0xc0000; //-12.0Hz
Diletant 173:7f938afb0447 103 device.dither.frequency.settings.transfer.correction[3] = 0x4fdf; //0.31199999999999994
Diletant 173:7f938afb0447 104 device.dither.frequency.settings.transfer.error[4] = -0x95555; //-9.333333333333334Hz
Diletant 173:7f938afb0447 105 device.dither.frequency.settings.transfer.correction[4] = 0x3161; //0.1928888888888889
Diletant 173:7f938afb0447 106 device.dither.frequency.settings.transfer.error[5] = -0x6aaaa; //-6.666666666666668Hz
Diletant 173:7f938afb0447 107 device.dither.frequency.settings.transfer.correction[5] = 0x1c71; //0.11111111111111113
Diletant 173:7f938afb0447 108 device.dither.frequency.settings.transfer.error[6] = -0x40000; //-4.0Hz
Diletant 173:7f938afb0447 109 device.dither.frequency.settings.transfer.correction[6] = 0xe56; //0.05599999999999999
Diletant 173:7f938afb0447 110 device.dither.frequency.settings.transfer.error[7] = -0x15555; //-1.3333333333333357Hz
Diletant 173:7f938afb0447 111 device.dither.frequency.settings.transfer.correction[7] = 0x452; //0.016888888888888884
Diletant 173:7f938afb0447 112 device.dither.frequency.settings.transfer.error[8] = 0x15555; //1.3333333333333321Hz
Diletant 173:7f938afb0447 113 device.dither.frequency.settings.transfer.correction[8] = -0x452; //-0.016888888888888884
Diletant 173:7f938afb0447 114 device.dither.frequency.settings.transfer.error[9] = 0x40000; //4.0Hz
Diletant 173:7f938afb0447 115 device.dither.frequency.settings.transfer.correction[9] = -0xe56; //-0.05599999999999999
Diletant 173:7f938afb0447 116 device.dither.frequency.settings.transfer.error[10] = 0x6aaaa; //6.666666666666664Hz
Diletant 173:7f938afb0447 117 device.dither.frequency.settings.transfer.correction[10] = -0x1c71; //-0.11111111111111108
Diletant 173:7f938afb0447 118 device.dither.frequency.settings.transfer.error[11] = 0x95555; //9.333333333333332Hz
Diletant 173:7f938afb0447 119 device.dither.frequency.settings.transfer.correction[11] = -0x3161; //-0.1928888888888888
Diletant 173:7f938afb0447 120 device.dither.frequency.settings.transfer.error[12] = 0xc0000; //12.0kHz
Diletant 173:7f938afb0447 121 device.dither.frequency.settings.transfer.correction[12] = -0x4fdf; //-0.31200000000000006
Diletant 173:7f938afb0447 122 device.dither.frequency.settings.transfer.error[13] = 0xeaaaa; //14.666666666666664Hz
Diletant 173:7f938afb0447 123 device.dither.frequency.settings.transfer.correction[13] = -0x7aa7; //-0.47911111111111115
Diletant 173:7f938afb0447 124 device.dither.frequency.settings.transfer.error[14] = 0x115555; //17.33333333333333Hz
Diletant 173:7f938afb0447 125 device.dither.frequency.settings.transfer.correction[14] = -0xb473; //-0.7048888888888889
Diletant 173:7f938afb0447 126 device.dither.frequency.settings.transfer.error[15] = 0x140000; //20.0Hz
Diletant 173:7f938afb0447 127 device.dither.frequency.settings.transfer.correction[15] = -0x10000; //-1.0
Diletant 161:efd949e8d536 128
Diletant 173:7f938afb0447 129 //Init amplitude regulator default settings
Diletant 173:7f938afb0447 130 device.dither.amplitude.settings.enabled = 1;
Diletant 173:7f938afb0447 131 device.dither.amplitude.settings.scale = 0x00001000; //0.0625 (6.25%) of period
Diletant 173:7f938afb0447 132 device.dither.amplitude.settings.transfer.points = 16;
Diletant 173:7f938afb0447 133 device.dither.amplitude.settings.transfer.error[0] = -0x140000; //-20.0kHz
Diletant 173:7f938afb0447 134 device.dither.amplitude.settings.transfer.correction[0] = 0x10000; //1.0
Diletant 173:7f938afb0447 135 device.dither.amplitude.settings.transfer.error[1] = -0x115555; //-17.333333333333332kHz
Diletant 173:7f938afb0447 136 device.dither.amplitude.settings.transfer.correction[1] = 0xb473; //0.7048888888888889
Diletant 173:7f938afb0447 137 device.dither.amplitude.settings.transfer.error[2] = -0xeaaaa; //-14.666666666666668kHz
Diletant 173:7f938afb0447 138 device.dither.amplitude.settings.transfer.correction[2] = 0x7aa7; //0.47911111111111115
Diletant 173:7f938afb0447 139 device.dither.amplitude.settings.transfer.error[3] = -0xc0000; //-12.0kHz
Diletant 173:7f938afb0447 140 device.dither.amplitude.settings.transfer.correction[3] = 0x4fdf; //0.31199999999999994
Diletant 173:7f938afb0447 141 device.dither.amplitude.settings.transfer.error[4] = -0x95555; //-9.333333333333334kHz
Diletant 173:7f938afb0447 142 device.dither.amplitude.settings.transfer.correction[4] = 0x3161; //0.1928888888888889
Diletant 173:7f938afb0447 143 device.dither.amplitude.settings.transfer.error[5] = -0x6aaaa; //-6.666666666666668kHz
Diletant 173:7f938afb0447 144 device.dither.amplitude.settings.transfer.correction[5] = 0x1c71; //0.11111111111111113
Diletant 173:7f938afb0447 145 device.dither.amplitude.settings.transfer.error[6] = -0x40000; //-4.0kHz
Diletant 173:7f938afb0447 146 device.dither.amplitude.settings.transfer.correction[6] = 0xe56; //0.05599999999999999
Diletant 173:7f938afb0447 147 device.dither.amplitude.settings.transfer.error[7] = -0x15555; //-1.3333333333333357kHz
Diletant 173:7f938afb0447 148 device.dither.amplitude.settings.transfer.correction[7] = 0x452; //0.016888888888888884
Diletant 173:7f938afb0447 149 device.dither.amplitude.settings.transfer.error[8] = 0x15555; //1.3333333333333321kHz
Diletant 173:7f938afb0447 150 device.dither.amplitude.settings.transfer.correction[8] = -0x452; //-0.016888888888888884
Diletant 173:7f938afb0447 151 device.dither.amplitude.settings.transfer.error[9] = 0x40000; //4.0kHz
Diletant 173:7f938afb0447 152 device.dither.amplitude.settings.transfer.correction[9] = -0xe56; //-0.05599999999999999
Diletant 173:7f938afb0447 153 device.dither.amplitude.settings.transfer.error[10] = 0x6aaaa; //6.666666666666664kHz
Diletant 173:7f938afb0447 154 device.dither.amplitude.settings.transfer.correction[10] = -0x1c71; //-0.11111111111111108
Diletant 173:7f938afb0447 155 device.dither.amplitude.settings.transfer.error[11] = 0x95555; //9.333333333333332kHz
Diletant 173:7f938afb0447 156 device.dither.amplitude.settings.transfer.correction[11] = -0x3161; //-0.1928888888888888
Diletant 173:7f938afb0447 157 device.dither.amplitude.settings.transfer.error[12] = 0xc0000; //12.0kHz
Diletant 173:7f938afb0447 158 device.dither.amplitude.settings.transfer.correction[12] = -0x4fdf; //-0.31200000000000006
Diletant 173:7f938afb0447 159 device.dither.amplitude.settings.transfer.error[13] = 0xeaaaa; //14.666666666666664kHz
Diletant 173:7f938afb0447 160 device.dither.amplitude.settings.transfer.correction[13] = -0x7aa7; //-0.47911111111111115
Diletant 173:7f938afb0447 161 device.dither.amplitude.settings.transfer.error[14] = 0x115555; //17.33333333333333kHz
Diletant 173:7f938afb0447 162 device.dither.amplitude.settings.transfer.correction[14] = -0xb473; //-0.7048888888888889
Diletant 173:7f938afb0447 163 device.dither.amplitude.settings.transfer.error[15] = 0x140000; //20.0kHz
Diletant 173:7f938afb0447 164 device.dither.amplitude.settings.transfer.correction[15] = -0x10000; //-1.0
Diletant 149:abbf7663d27d 165 }
Diletant 149:abbf7663d27d 166
Diletant 167:bedc0a9d559a 167 void InitDitherState(void){
Diletant 173:7f938afb0447 168 //Init pulse state
Diletant 173:7f938afb0447 169 device.dither.pulse.state.width = device.dither.pulse.settings.width;
Diletant 173:7f938afb0447 170 device.dither.pulse.state.min = device.dither.pulse.settings.min;
Diletant 173:7f938afb0447 171 device.dither.pulse.state.max = device.dither.pulse.settings.max;
Diletant 173:7f938afb0447 172 device.dither.pulse.state.rise = 0;
Diletant 173:7f938afb0447 173 device.dither.pulse.state.fall = 0;
Diletant 173:7f938afb0447 174 device.dither.pulse.state.counter = 0;
Diletant 173:7f938afb0447 175
Diletant 173:7f938afb0447 176 //Init noise state
Diletant 173:7f938afb0447 177 device.dither.noise.state.enabled = device.dither.noise.settings.enabled;
Diletant 173:7f938afb0447 178 device.dither.noise.state.period = device.dither.noise.settings.period;
Diletant 173:7f938afb0447 179 device.dither.noise.state.range = device.dither.noise.settings.range;
Diletant 173:7f938afb0447 180 device.dither.noise.state.amplitude = device.dither.noise.settings.amplitude;
Diletant 173:7f938afb0447 181 device.dither.noise.state.counter = 0;
Diletant 173:7f938afb0447 182 device.dither.noise.state.trigger = 0;
Diletant 173:7f938afb0447 183 device.dither.noise.state.disturbance = 0;
Diletant 173:7f938afb0447 184
Diletant 173:7f938afb0447 185 //Init dither cycle state
Diletant 161:efd949e8d536 186 device.dither.cycle.state.enabled = device.dither.cycle.settings.enabled;
Diletant 161:efd949e8d536 187 device.dither.cycle.state.pin1 = 0;
Diletant 161:efd949e8d536 188 device.dither.cycle.state.pin2 = 0;
Diletant 173:7f938afb0447 189
Diletant 173:7f938afb0447 190 //Init phase detector state
Diletant 173:7f938afb0447 191 device.dither.detector.state.sum = 0;
Diletant 173:7f938afb0447 192 device.dither.detector.state.raw = 0;
Diletant 173:7f938afb0447 193 device.dither.detector.state.phase = 0;
Diletant 173:7f938afb0447 194
Diletant 173:7f938afb0447 195 //Init frequency regulator state
Diletant 173:7f938afb0447 196 //TODO: min(temperature), max(temperature)
Diletant 173:7f938afb0447 197 device.dither.frequency.state.enabled = device.dither.frequency.settings.enabled;
Diletant 173:7f938afb0447 198 device.dither.frequency.state.min = device.dither.frequency.settings.min;
Diletant 173:7f938afb0447 199 device.dither.frequency.state.max = device.dither.frequency.settings.max;
Diletant 173:7f938afb0447 200 device.dither.frequency.state.scale = device.dither.frequency.settings.scale;
Diletant 173:7f938afb0447 201 device.dither.frequency.state.frequency = (device.dither.frequency.state.max + device.dither.frequency.state.min) / 2;
Diletant 173:7f938afb0447 202 device.dither.frequency.state.error = 0;
Diletant 173:7f938afb0447 203 device.dither.frequency.state.correction = 0;
Diletant 173:7f938afb0447 204 //Set measurement timer
Diletant 173:7f938afb0447 205 timersSetMeasurementPeriod((unsigned int)(SystemCoreClock1/(device.dither.frequency.state.frequency >> 11)));
Diletant 173:7f938afb0447 206
Diletant 173:7f938afb0447 207 //Init amplitude regulator state
Diletant 173:7f938afb0447 208 device.dither.amplitude.state.enabled = device.dither.amplitude.settings.enabled;
Diletant 173:7f938afb0447 209 device.dither.amplitude.state.reference = device.dither.amplitude.settings.reference;
Diletant 173:7f938afb0447 210 device.dither.amplitude.state.scale = device.dither.amplitude.settings.scale;
Diletant 173:7f938afb0447 211 device.dither.amplitude.state.frequency = 0;
Diletant 173:7f938afb0447 212 device.dither.amplitude.state.error = 0;
Diletant 173:7f938afb0447 213 device.dither.amplitude.state.correction = 0;
Diletant 149:abbf7663d27d 214 }
Diletant 149:abbf7663d27d 215
Diletant 156:e68ee0bcdcda 216 void DeviceStartDither(void){
Diletant 183:c7a9c309086c 217 LPC_PINCON->PINSEL3 &= ~(0x00<<18); //e. P1.25 is GPIO pin
Diletant 183:c7a9c309086c 218 LPC_PINCON->PINSEL3 |= (0x00<<18); //e. P1.25 is GPIO pin
Diletant 183:c7a9c309086c 219 LPC_PINCON->PINMODE3 |= (3<<18); //e. P1.25 (включениe подтягивающего резистора")
Diletant 183:c7a9c309086c 220 LPC_GPIO1->FIODIR |= (1<<25); //e. P0.5 is output (запись ( 1 ) в 5 бит FIODIR выбор P0.5 как выход)
Diletant 183:c7a9c309086c 221 LPC_GPIO1->FIOCLR |= (1<<25);
Diletant 183:c7a9c309086c 222
Diletant 183:c7a9c309086c 223 LPC_PINCON->PINSEL3 &= ~(0x00<<24); //e. P1.28 is GPIO pin
Diletant 183:c7a9c309086c 224 LPC_PINCON->PINSEL3 |= (0x00<<24); //e. P1.28 is GPIO pin
Diletant 183:c7a9c309086c 225 LPC_PINCON->PINMODE3 |= (3<<24); //e. P1.28 is GPIO pin (запись ( 11 ) в бит PINMODE0 "для включения подтягивающего резистора")
Diletant 183:c7a9c309086c 226 LPC_GPIO1->FIODIR |= (1<<28); //e. P1.28 is output (запись ( 1 ) в 5 бит FIODIR выбор P0.5 как выход)
Diletant 183:c7a9c309086c 227 LPC_GPIO1->FIOCLR |= (1<<28);
Diletant 183:c7a9c309086c 228
Diletant 173:7f938afb0447 229 DeviceStartMeasurementTimer();
Diletant 149:abbf7663d27d 230 }
Diletant 149:abbf7663d27d 231
Diletant 173:7f938afb0447 232 //Call from regular cycle interrupt (or from main cycle?)
Diletant 173:7f938afb0447 233 void ditherCycle(void){
Diletant 183:c7a9c309086c 234 int8_t pin1, pin2;
Diletant 173:7f938afb0447 235 //Use LPC_TIM1->TC instead of device.dither.pulse.state.counter?
Diletant 173:7f938afb0447 236 // device.measurement.state.counter + (LPC_TIM1->TC << 16)/LPC_TIM1->MR0 - cycle phase 0...32 in 16.16 format
Diletant 173:7f938afb0447 237 // "+": 100kHz timer will be just 10kHz timer if call DeviceDitherDoCycle() in main cycle
Diletant 173:7f938afb0447 238 if ((device.dither.pulse.state.counter > device.dither.pulse.state.rise) && (device.dither.pulse.state.counter < device.dither.pulse.state.fall)){
Diletant 149:abbf7663d27d 239 pin1 = 1; pin2 = 1;
Diletant 149:abbf7663d27d 240 } else {
Diletant 149:abbf7663d27d 241 pin1 = 0; pin2 = 0;
Diletant 149:abbf7663d27d 242 }
Diletant 183:c7a9c309086c 243
Diletant 156:e68ee0bcdcda 244 if (device.measurement.counter < 16){
Diletant 149:abbf7663d27d 245 pin2 = 0;
Diletant 149:abbf7663d27d 246 } else {
Diletant 149:abbf7663d27d 247 pin1 = 0;
Diletant 149:abbf7663d27d 248 }
Diletant 161:efd949e8d536 249 if (!device.dither.cycle.state.enabled) {
Diletant 149:abbf7663d27d 250 pin1 = 0; pin2 = 0;
Diletant 149:abbf7663d27d 251 }
Diletant 161:efd949e8d536 252 if (pin1 > device.dither.cycle.state.pin1) LPC_GPIO1->FIOCLR = 1<<25;
Diletant 161:efd949e8d536 253 if (pin1 < device.dither.cycle.state.pin1) LPC_GPIO1->FIOSET = 1<<25;
Diletant 161:efd949e8d536 254 if (pin2 > device.dither.cycle.state.pin2) LPC_GPIO1->FIOCLR = 1<<28;
Diletant 161:efd949e8d536 255 if (pin2 < device.dither.cycle.state.pin2) LPC_GPIO1->FIOSET = 1<<28;
Diletant 161:efd949e8d536 256 device.dither.cycle.state.pin1 = pin1;
Diletant 161:efd949e8d536 257 device.dither.cycle.state.pin2 = pin2;
Diletant 161:efd949e8d536 258 }
Diletant 161:efd949e8d536 259
Diletant 173:7f938afb0447 260 int32_t ditherInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) {
Diletant 173:7f938afb0447 261 int32_t ma, mb;
Diletant 173:7f938afb0447 262 while (1) {
Diletant 173:7f938afb0447 263 if (a1 == a) return b1;
Diletant 173:7f938afb0447 264 if (a2 == a) return b2;
Diletant 173:7f938afb0447 265
Diletant 173:7f938afb0447 266 ma = (a1 + a2) >> 1;
Diletant 173:7f938afb0447 267 mb = (b1 + b2) >> 1;
Diletant 173:7f938afb0447 268
Diletant 173:7f938afb0447 269 if (a < ma) {
Diletant 173:7f938afb0447 270 if (a2 == ma) return mb;
Diletant 173:7f938afb0447 271 if (b1 == mb) return mb;
Diletant 173:7f938afb0447 272 a2 = ma; b2 = mb;
Diletant 173:7f938afb0447 273 } else if (a > ma) {
Diletant 173:7f938afb0447 274 if (a1 == ma) return mb;
Diletant 173:7f938afb0447 275 if (b2 == mb) return mb;
Diletant 173:7f938afb0447 276 a1 = ma; b1 = mb;
Diletant 173:7f938afb0447 277 } else return mb;
Diletant 161:efd949e8d536 278 }
Diletant 161:efd949e8d536 279 }
Diletant 173:7f938afb0447 280
Diletant 183:c7a9c309086c 281 int32_t ditherRestorePhase(int32_t raw) {
Diletant 173:7f938afb0447 282 int32_t r1, r2;
Diletant 173:7f938afb0447 283 for (uint8_t i = 1; i < device.dither.detector.settings.transfer.points; i++) {
Diletant 173:7f938afb0447 284 r1 = device.dither.detector.settings.transfer.raw[i - 1];
Diletant 173:7f938afb0447 285 if (raw < r1) {
Diletant 173:7f938afb0447 286 return device.dither.detector.settings.transfer.restored[i - 1];
Diletant 173:7f938afb0447 287 }
Diletant 173:7f938afb0447 288
Diletant 173:7f938afb0447 289 r2 = device.dither.detector.settings.transfer.raw[i];
Diletant 173:7f938afb0447 290 if (raw < r2) {
Diletant 173:7f938afb0447 291 int32_t p1 = device.dither.detector.settings.transfer.restored[i - 1];
Diletant 173:7f938afb0447 292 int32_t p2 = device.dither.detector.settings.transfer.restored[i];
Diletant 173:7f938afb0447 293 return ditherInterpolate(raw, r1, r2, p1, p2);
Diletant 173:7f938afb0447 294 }
Diletant 173:7f938afb0447 295 }
Diletant 173:7f938afb0447 296 return device.dither.detector.settings.transfer.restored[device.dither.detector.settings.transfer.points - 1];
Diletant 173:7f938afb0447 297 }
Diletant 173:7f938afb0447 298
Diletant 183:c7a9c309086c 299 int32_t ditherFrequencyCorrection(int32_t error) {
Diletant 173:7f938afb0447 300 int32_t e1, e2;
Diletant 173:7f938afb0447 301 for (uint8_t i = 1; i < device.dither.frequency.settings.transfer.points; i++) {
Diletant 173:7f938afb0447 302 e1 = device.dither.frequency.settings.transfer.error[i - 1];
Diletant 173:7f938afb0447 303 if (error < e1) {
Diletant 173:7f938afb0447 304 return device.dither.frequency.settings.transfer.correction[i - 1];
Diletant 173:7f938afb0447 305 }
Diletant 173:7f938afb0447 306
Diletant 173:7f938afb0447 307 e2 = device.dither.frequency.settings.transfer.error[i];
Diletant 173:7f938afb0447 308 if (error < e2) {
Diletant 173:7f938afb0447 309 int32_t c1 = device.dither.frequency.settings.transfer.correction[i - 1];
Diletant 173:7f938afb0447 310 int32_t c2 = device.dither.frequency.settings.transfer.correction[i];
Diletant 173:7f938afb0447 311 return ditherInterpolate(error, e1, e2, c1, c2);
Diletant 173:7f938afb0447 312 }
Diletant 173:7f938afb0447 313 }
Diletant 173:7f938afb0447 314 return device.dither.frequency.settings.transfer.correction[device.dither.frequency.settings.transfer.points - 1];
Diletant 173:7f938afb0447 315 }
Diletant 173:7f938afb0447 316
Diletant 183:c7a9c309086c 317 int32_t ditherPulseCorrection(int32_t error) {
Diletant 173:7f938afb0447 318 int32_t e1, e2;
Diletant 173:7f938afb0447 319 for (uint8_t i = 1; i < device.dither.amplitude.settings.transfer.points; i++) {
Diletant 173:7f938afb0447 320 e1 = device.dither.amplitude.settings.transfer.error[i - 1];
Diletant 173:7f938afb0447 321 if (error < e1) {
Diletant 173:7f938afb0447 322 return device.dither.amplitude.settings.transfer.correction[i - 1];
Diletant 173:7f938afb0447 323 }
Diletant 173:7f938afb0447 324
Diletant 173:7f938afb0447 325 e2 = device.dither.amplitude.settings.transfer.error[i];
Diletant 173:7f938afb0447 326 if (error < e2) {
Diletant 173:7f938afb0447 327 int32_t c1 = device.dither.amplitude.settings.transfer.correction[i - 1];
Diletant 173:7f938afb0447 328 int32_t c2 = device.dither.amplitude.settings.transfer.correction[i];
Diletant 173:7f938afb0447 329 return ditherInterpolate(error, e1, e2, c1, c2);
Diletant 173:7f938afb0447 330 }
Diletant 173:7f938afb0447 331 }
Diletant 173:7f938afb0447 332 return device.dither.amplitude.settings.transfer.correction[device.dither.amplitude.settings.transfer.points - 1];
Diletant 173:7f938afb0447 333 }
Diletant 173:7f938afb0447 334
Diletant 173:7f938afb0447 335 void ditherProcessDetector(void) {
Diletant 173:7f938afb0447 336 //f(t) = f0 * cos(w * t)
Diletant 173:7f938afb0447 337 //x(t) = x0 * cos(w * t - fi)
Diletant 173:7f938afb0447 338 //fi = - arctan(const / (w0^2 - w^2))
Diletant 174:0f86eedd511c 339 int32_t delta = device.counters.dither.state.delta[device.measurement.counter];
Diletant 183:c7a9c309086c 340 int32_t factor = device.dither.detector.settings.filter.factor[(device.measurement.counter + 14) % 32]; //-1...+1 in 16.16 format
Diletant 173:7f938afb0447 341 device.dither.detector.state.sum += delta * factor; //-500...+500 in 16.16 format
Diletant 173:7f938afb0447 342 }
Diletant 173:7f938afb0447 343
Diletant 173:7f938afb0447 344 void ditherProcessPhase(void) {
Diletant 173:7f938afb0447 345 //Raw phase -1...+1 in 16.16 format
Diletant 177:672ef279c8e0 346 if (device.counters.dither.state.amplitude > 0) {//0...+2500 in 32.0 format (1MHz/400Hz)
Diletant 183:c7a9c309086c 347 device.dither.detector.state.raw = device.dither.detector.state.sum / device.counters.dither.state.amplitude; //-1...+1 in 16.16 format
Diletant 183:c7a9c309086c 348 device.dither.detector.state.sum = 0;
Diletant 173:7f938afb0447 349 } else {
Diletant 183:c7a9c309086c 350 device.dither.detector.state.raw = 0;
Diletant 173:7f938afb0447 351 }
Diletant 173:7f938afb0447 352 //TODO: Transform required to actual phase in range -1...+1 or -Pi/2...+Pi/2 or -90...+90 degrees
Diletant 173:7f938afb0447 353 device.dither.detector.state.phase = ditherRestorePhase(device.dither.detector.state.raw);
Diletant 183:c7a9c309086c 354 //sprintf(device.service.buffer,"- %d\r\n", device.dither.detector.state.phase); WriteConcole(); //Development message
Diletant 173:7f938afb0447 355 }
Diletant 173:7f938afb0447 356
Diletant 173:7f938afb0447 357 void ditherProcessFrequency(void) {
Diletant 173:7f938afb0447 358 device.dither.frequency.state.error = -device.dither.detector.state.phase;
Diletant 173:7f938afb0447 359 device.dither.frequency.state.correction = (device.dither.frequency.state.scale * ditherFrequencyCorrection(device.dither.frequency.state.error)) >> 16;
Diletant 173:7f938afb0447 360
Diletant 173:7f938afb0447 361 if (device.dither.frequency.state.enabled){
Diletant 173:7f938afb0447 362 device.dither.frequency.state.frequency += device.dither.frequency.state.correction;
Diletant 173:7f938afb0447 363
Diletant 173:7f938afb0447 364 if (device.dither.frequency.state.frequency > device.dither.frequency.state.max) {
Diletant 173:7f938afb0447 365 device.dither.frequency.state.frequency = device.dither.frequency.state.max;
Diletant 173:7f938afb0447 366 } else if (device.dither.frequency.state.frequency < device.dither.frequency.state.min) {
Diletant 173:7f938afb0447 367 device.dither.frequency.state.frequency = device.dither.frequency.state.min;
Diletant 173:7f938afb0447 368 }
Diletant 173:7f938afb0447 369
Diletant 173:7f938afb0447 370 timersSetMeasurementPeriod((unsigned int)(SystemCoreClock1/(device.dither.frequency.state.frequency >> 11)));
Diletant 173:7f938afb0447 371 }
Diletant 173:7f938afb0447 372 }
Diletant 173:7f938afb0447 373
Diletant 173:7f938afb0447 374 void ditherProcessAmplitude(void) {
Diletant 173:7f938afb0447 375 //Frequency = device.counters.state.frequency * device.dither.frequency.state.frequency / 2;
Diletant 173:7f938afb0447 376 // 500 * 400Hz (0x01900000) / 2 / 1000 = 100kHz (0x00640000)
Diletant 173:7f938afb0447 377 // 500 * 0x19000000 / 2000 / 16 = 100kHz = 0x00640000
Diletant 177:672ef279c8e0 378 device.dither.amplitude.state.frequency = (device.counters.dither.state.amplitude * ((device.dither.frequency.state.frequency << 4) / 2000)) >> 4;
Diletant 173:7f938afb0447 379 device.dither.amplitude.state.error = device.dither.amplitude.state.frequency - device.dither.amplitude.state.reference; //0...200kHz 16.16 format
Diletant 173:7f938afb0447 380 device.dither.amplitude.state.correction = (device.dither.amplitude.state.scale * ditherPulseCorrection(device.dither.amplitude.state.error)) >> 16;
Diletant 173:7f938afb0447 381
Diletant 173:7f938afb0447 382 if (device.dither.amplitude.state.enabled){
Diletant 183:c7a9c309086c 383 //Debug!!
Diletant 173:7f938afb0447 384 device.dither.pulse.state.width += device.dither.amplitude.state.correction;
Diletant 173:7f938afb0447 385 if (device.dither.pulse.state.width < device.dither.pulse.state.min) device.dither.pulse.state.width = device.dither.pulse.state.min;
Diletant 173:7f938afb0447 386 if (device.dither.pulse.state.width > device.dither.pulse.state.max) device.dither.pulse.state.width = device.dither.pulse.state.max;
Diletant 173:7f938afb0447 387 }
Diletant 173:7f938afb0447 388 }
Diletant 173:7f938afb0447 389
Diletant 173:7f938afb0447 390 void ditherProcessNoise(void) {
Diletant 173:7f938afb0447 391 if (device.dither.noise.state.counter == device.dither.noise.state.trigger) {
Diletant 173:7f938afb0447 392 //Set noise update state parameters
Diletant 173:7f938afb0447 393 device.dither.noise.state.counter = 0;
Diletant 173:7f938afb0447 394 device.dither.noise.state.trigger = device.dither.noise.state.period + rand() % device.dither.noise.state.range;
Diletant 173:7f938afb0447 395
Diletant 173:7f938afb0447 396 //Set pulse width disturbance
Diletant 173:7f938afb0447 397 if (device.dither.noise.state.enabled) {
Diletant 173:7f938afb0447 398 device.dither.noise.state.disturbance = (device.dither.noise.state.amplitude * (-0x00010000 + 2 * rand())) >> 16; //-1...1
Diletant 173:7f938afb0447 399 } else {
Diletant 173:7f938afb0447 400 device.dither.noise.state.disturbance = 0;
Diletant 173:7f938afb0447 401 }
Diletant 173:7f938afb0447 402 }
Diletant 173:7f938afb0447 403 }
Diletant 173:7f938afb0447 404
Diletant 173:7f938afb0447 405 void ditherProcessPulse(void) {
Diletant 183:c7a9c309086c 406 //Dither period: device.controller.timer[0].state.MR0 * 32 / CCLK
Diletant 183:c7a9c309086c 407 //Timer #1 period: device.controller.timer[1].state.MR0 / CCLK
Diletant 183:c7a9c309086c 408 //Timer #1 ticks in dither period: device.controller.timer[0].state.MR0 * 32 / device.controller.timer[1].state.MR0
Diletant 183:c7a9c309086c 409
Diletant 183:c7a9c309086c 410 //int32_t max = device.controller.timer[0].state.MR0 / 2;
Diletant 183:c7a9c309086c 411 int32_t max = device.controller.timer[0].state.MR0 * 16 / device.controller.timer[1].state.MR0;
Diletant 173:7f938afb0447 412 int32_t mid = max / 2;
Diletant 183:c7a9c309086c 413 //int32_t half = (max * (device.dither.pulse.state.width + device.dither.noise.state.disturbance)) >> 17; //pulse half width in 10 mks ticks
Diletant 183:c7a9c309086c 414 //int32_t half = (max * device.dither.pulse.state.width) >> 16;
Diletant 183:c7a9c309086c 415 int32_t half = max/3; //Debug!!!
Diletant 173:7f938afb0447 416
Diletant 173:7f938afb0447 417 int32_t rise = mid - half; if (rise < 0) rise = 0;
Diletant 183:c7a9c309086c 418 int32_t fall = mid + half; if (fall > max) fall = max;
Diletant 173:7f938afb0447 419 device.dither.pulse.state.rise = rise;
Diletant 173:7f938afb0447 420 device.dither.pulse.state.fall = fall;
Diletant 183:c7a9c309086c 421
Diletant 173:7f938afb0447 422 /*
Diletant 173:7f938afb0447 423 int32_t delta = (max * (device.dither.amplitude.state.correction + device.dither.noise.state.disturbance)) >> 17;
Diletant 173:7f938afb0447 424
Diletant 173:7f938afb0447 425 device.dither.pulse.state.rise += delta;
Diletant 173:7f938afb0447 426 if (device.dither.pulse.state.rise < 0) device.dither.pulse.state.rise = 0;
Diletant 173:7f938afb0447 427 if (device.dither.pulse.state.rise > mid) device.dither.pulse.state.rise = mid;
Diletant 173:7f938afb0447 428
Diletant 173:7f938afb0447 429 device.dither.pulse.state.fall -= delta;
Diletant 173:7f938afb0447 430 if (device.dither.pulse.state.fall > max) device.dither.pulse.state.fall = max;
Diletant 173:7f938afb0447 431 if (device.dither.pulse.state.fall < mid) device.dither.pulse.state.fall = mid;
Diletant 173:7f938afb0447 432 */
Diletant 173:7f938afb0447 433 }
Diletant 173:7f938afb0447 434
Diletant 173:7f938afb0447 435 //Call from measurement timer interrupt
Diletant 173:7f938afb0447 436 void ditherProcess(void) {
Diletant 173:7f938afb0447 437 ditherProcessDetector();
Diletant 173:7f938afb0447 438 if (device.measurement.counter == 31) {
Diletant 173:7f938afb0447 439 ditherProcessPhase();
Diletant 173:7f938afb0447 440 ditherProcessFrequency();
Diletant 173:7f938afb0447 441 ditherProcessAmplitude();
Diletant 173:7f938afb0447 442 ditherProcessNoise();
Diletant 173:7f938afb0447 443 ditherProcessPulse();
Diletant 173:7f938afb0447 444 }
Diletant 173:7f938afb0447 445 }
Diletant 182:2bd8ec44998f 446
Diletant 182:2bd8ec44998f 447 //
Diletant 182:2bd8ec44998f 448 // Compatibility functions section
Diletant 182:2bd8ec44998f 449 //
Diletant 182:2bd8ec44998f 450 uint32_t ditherGetRawAmplitudeSetting(void) {
Diletant 182:2bd8ec44998f 451 //Value: dither output frequency F - Hz
Diletant 182:2bd8ec44998f 452 //Internal format: device.dither.amplitude.settings.reference - kHz in 16.16 format
Diletant 183:c7a9c309086c 453 //Output format: 2 * 1000 * F - dimensionless units in 32.0 format
Diletant 183:c7a9c309086c 454
Diletant 183:c7a9c309086c 455 //Frequency: device.dither.amplitude.settings.reference / 65536
Diletant 183:c7a9c309086c 456 //Output: 1000 * device.dither.amplitude.settings.reference / 32768
Diletant 183:c7a9c309086c 457
Diletant 183:c7a9c309086c 458 int32_t r = device.dither.amplitude.state.reference * 1000 / 32768;
Diletant 183:c7a9c309086c 459 if (r < 0) r = 0;
Diletant 183:c7a9c309086c 460 return (uint32_t)r;
Diletant 182:2bd8ec44998f 461 }
Diletant 182:2bd8ec44998f 462
Diletant 182:2bd8ec44998f 463 uint16_t ditherGetRawAmplitudeState(void) {
Diletant 182:2bd8ec44998f 464 //Value: dither output frequency F - Hz
Diletant 182:2bd8ec44998f 465 //Internal format: device.dither.amplitude.settings.frequency - kHz in 16.16 format
Diletant 182:2bd8ec44998f 466 //Output format: F/16 - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 467
Diletant 182:2bd8ec44998f 468 //Result: device.dither.amplitude.settings.frequency / 16 * 1000 / 65536
Diletant 183:c7a9c309086c 469 int32_t r = ((device.dither.amplitude.state.frequency >> 5) * 1000) >> 16;
Diletant 183:c7a9c309086c 470 if (r < 0) r = 0;
Diletant 183:c7a9c309086c 471 if (r > 32767) r = 32767;
Diletant 182:2bd8ec44998f 472 return (uint16_t)r;
Diletant 182:2bd8ec44998f 473 }
Diletant 182:2bd8ec44998f 474
Diletant 182:2bd8ec44998f 475 uint16_t ditherGetRawDividerSetting(void) {
Diletant 182:2bd8ec44998f 476 //Value: dither oscillation frequency F - Hz
Diletant 182:2bd8ec44998f 477 //Internal format: device.dither.frequency.settings.frequency - Hz in 16.16 format
Diletant 182:2bd8ec44998f 478 //Output format: frequency divider 7680000/F - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 479
Diletant 182:2bd8ec44998f 480 //Numerator: 7680000 * 256 = 7680000 << 8 = 0x75300000 < maxint32
Diletant 182:2bd8ec44998f 481 //Denominator: F * 256 = device.dither.frequency.settings.frequency >> 8 = 0x15e00 ... 0x1c200
Diletant 182:2bd8ec44998f 482 //Result: numerator/denominator = (7680000 * 256)/(F * 256) = 0x55b6 ... 0x42aa = 21942 ... 17066 < maxint16
Diletant 183:c7a9c309086c 483 int32_t d = 0x75300000 / ((device.dither.frequency.settings.max + device.dither.frequency.settings.min) >> 9);
Diletant 183:c7a9c309086c 484 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 485 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 486 return (uint16_t)d;
Diletant 182:2bd8ec44998f 487 }
Diletant 182:2bd8ec44998f 488
Diletant 182:2bd8ec44998f 489 uint16_t ditherGetRawDividerMaxSetting(void) {
Diletant 182:2bd8ec44998f 490 //Value: dither oscillation min frequency F - Hz
Diletant 182:2bd8ec44998f 491 //Internal format: device.dither.frequency.settings.min - Hz in 16.16 format
Diletant 182:2bd8ec44998f 492 //Output format: frequency max divider 7680000/F - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 493
Diletant 182:2bd8ec44998f 494 //Numerator: 7680000 * 256 = 7680000 << 8 = 0x75300000 < maxint32
Diletant 182:2bd8ec44998f 495 //Denominator: F * 256 = device.dither.frequency.settings.max >> 8 = 0x15e00 ... 0x1c200
Diletant 182:2bd8ec44998f 496 //Result: numerator/denominator = (7680000 * 256)/(F * 256) = 0x55b6 ... 0x42aa = 21942 ... 17066 < maxint16
Diletant 183:c7a9c309086c 497 int32_t d = 0x75300000 / (device.dither.frequency.settings.min >> 8);
Diletant 183:c7a9c309086c 498 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 499 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 500 return (uint16_t)d;
Diletant 182:2bd8ec44998f 501 }
Diletant 182:2bd8ec44998f 502
Diletant 182:2bd8ec44998f 503 uint16_t ditherGetRawDividerMinSetting(void) {
Diletant 182:2bd8ec44998f 504 //Value: dither oscillation max frequency F - Hz
Diletant 182:2bd8ec44998f 505 //Internal format: device.dither.frequency.settings.max - Hz in 16.16 format
Diletant 182:2bd8ec44998f 506 //Output format: frequency min divider 7680000/F - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 507
Diletant 182:2bd8ec44998f 508 //Numerator: 7680000 * 256 = 7680000 << 8 = 0x75300000 < maxint32
Diletant 182:2bd8ec44998f 509 //Denominator: F * 256 = device.dither.frequency.settings.frequency >> 8 = 0x15e00 ... 0x1c200
Diletant 182:2bd8ec44998f 510 //Result: numerator/denominator = (7680000 * 256)/(F * 256) = 0x55b6 ... 0x42aa = 21942 ... 17066 < maxint16
Diletant 183:c7a9c309086c 511 int32_t d = 0x75300000 / (device.dither.frequency.settings.max >> 8);
Diletant 183:c7a9c309086c 512 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 513 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 514 return (uint16_t)d;
Diletant 182:2bd8ec44998f 515 }
Diletant 182:2bd8ec44998f 516
Diletant 182:2bd8ec44998f 517 uint16_t ditherGetRawDividerState(void) {
Diletant 182:2bd8ec44998f 518 //Value: dither oscillation frequency F - Hz
Diletant 182:2bd8ec44998f 519 //Internal format: device.dither.frequency.state.frequency - Hz in 16.16 format
Diletant 182:2bd8ec44998f 520 //Output format: frequency divider 7680000/F - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 521
Diletant 182:2bd8ec44998f 522 //Numerator: 7680000 * 256 = 7680000 << 8 = 0x75300000 < maxint32
Diletant 182:2bd8ec44998f 523 //Denominator: F * 256 = device.dither.frequency.state.frequency >> 8 = 0x15e00 ... 0x1c200
Diletant 182:2bd8ec44998f 524 //Result: numerator/denominator = (7680000 * 256)/(F * 256) = 0x55b6 ... 0x42aa = 21942 ... 17066 < maxint16
Diletant 183:c7a9c309086c 525 int32_t d = 0x75300000 / (device.dither.frequency.state.frequency >> 8);
Diletant 183:c7a9c309086c 526 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 527 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 528 return (uint16_t)d;
Diletant 182:2bd8ec44998f 529 }
Diletant 182:2bd8ec44998f 530
Diletant 182:2bd8ec44998f 531 int16_t ditherGetRawPhaseState(void) {
Diletant 182:2bd8ec44998f 532 //Value: dither raw phase - Ph - -1,0,+1
Diletant 182:2bd8ec44998f 533 //Internal format: device.dither.detector.state.phase - -1...1 in 16.16 format
Diletant 182:2bd8ec44998f 534 //Output format: sum(Ph, 1 sec) - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 535
Diletant 182:2bd8ec44998f 536 //Now: Multiply 512 instead of sum for 1 sec and right shift 16 to transform in format 16.0 (shift 16 - 9 = 7)
Diletant 182:2bd8ec44998f 537 //Signed right shift implementation-defined!!! Required to replace right shift 7 to division on 128 for implementation independence?
Diletant 182:2bd8ec44998f 538 int32_t phase = device.dither.detector.state.phase / 128;
Diletant 183:c7a9c309086c 539 //TODO: make summation for 1 sec as in DSP-based program and tranform 16.0
Diletant 182:2bd8ec44998f 540
Diletant 182:2bd8ec44998f 541 return (int16_t)phase;
Diletant 182:2bd8ec44998f 542 }
Diletant 182:2bd8ec44998f 543
Diletant 182:2bd8ec44998f 544 uint16_t ditherGetRawPulseSetting(void) {
Diletant 182:2bd8ec44998f 545 //Value: dither pulse width W - s
Diletant 182:2bd8ec44998f 546 //Internal format: device.dither.pulse.settings.width - 0...1 of dither half-period in 16.16 format
Diletant 182:2bd8ec44998f 547 //Output format: 15360000 * W - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 548
Diletant 182:2bd8ec44998f 549 //Frequency: F = device.dither.frequency.settings.frequency / 65536
Diletant 182:2bd8ec44998f 550 //Half-period: 1/(2 * F) = 0.0011 ... 0.0014 s
Diletant 182:2bd8ec44998f 551 //Percentage: P = device.dither.pulse.settings.width / 65536
Diletant 182:2bd8ec44998f 552 //Pulse width: W = P/(2 * F)
Diletant 182:2bd8ec44998f 553 //Output: O = W * 1536000 = 768000 * device.dither.pulse.settings.width / device.dither.frequency.settings.frequency
Diletant 182:2bd8ec44998f 554
Diletant 182:2bd8ec44998f 555 //Numerator: (768000 >> 5) * device.dither.pulse.settings.width = 0 ... 0x5dc00000 < maxint
Diletant 182:2bd8ec44998f 556 //Denominator: device.dither.frequency.settings.frequency >> 5 = 0xaf000 ... 0xe1000
Diletant 182:2bd8ec44998f 557 //Result: numerator/denominator = 0 ... 0x892 < maxint16
Diletant 183:c7a9c309086c 558 int32_t d = 0x5dc0 * device.dither.pulse.settings.width / ((device.dither.frequency.settings.max + device.dither.frequency.settings.min) >> 6);
Diletant 183:c7a9c309086c 559 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 560 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 561 return (uint16_t)d;
Diletant 182:2bd8ec44998f 562 }
Diletant 182:2bd8ec44998f 563
Diletant 182:2bd8ec44998f 564 uint16_t ditherGetRawPulseMaxSetting(void) {
Diletant 182:2bd8ec44998f 565 //Value: dither pulse max width W - s
Diletant 182:2bd8ec44998f 566 //Internal format: device.dither.pulse.settings.max - 0...1 of dither half-period in 16.16 format
Diletant 182:2bd8ec44998f 567 //Output format: 15360000 * W - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 568
Diletant 182:2bd8ec44998f 569 //Frequency: F = device.dither.frequency.settings.min / 65536
Diletant 182:2bd8ec44998f 570 //Half-period: 1/(2 * F) = 0.0011 ... 0.0014 s
Diletant 182:2bd8ec44998f 571 //Percentage: P = device.dither.pulse.settings.max / 65536
Diletant 182:2bd8ec44998f 572 //Pulse width: W = P/(2 * F)
Diletant 182:2bd8ec44998f 573 //Output: O = W * 1536000 = 768000 * device.dither.pulse.settings.max / device.dither.frequency.settings.min
Diletant 182:2bd8ec44998f 574
Diletant 182:2bd8ec44998f 575 //Numerator: (768000 >> 5) * device.dither.pulse.settings.max = 0 ... 0x5dc00000 < maxint
Diletant 182:2bd8ec44998f 576 //Denominator: device.dither.frequency.settings.frequency >> 5 = 0xaf000 ... 0xe1000
Diletant 182:2bd8ec44998f 577 //Result: numerator/denominator = 0 ... 0x892 < maxint16
Diletant 183:c7a9c309086c 578 int32_t d = 0x5dc0 * device.dither.pulse.settings.max / (device.dither.frequency.settings.min >> 5);
Diletant 183:c7a9c309086c 579 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 580 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 581 return (uint16_t)d;
Diletant 182:2bd8ec44998f 582 }
Diletant 182:2bd8ec44998f 583
Diletant 182:2bd8ec44998f 584 uint16_t ditherGetRawPulseMinSetting(void) {
Diletant 182:2bd8ec44998f 585 //Value: dither pulse min width W - s
Diletant 182:2bd8ec44998f 586 //Internal format: device.dither.pulse.settings.min - 0...1 of dither half-period in 16.16 format
Diletant 182:2bd8ec44998f 587 //Output format: 15360000 * W - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 588
Diletant 182:2bd8ec44998f 589 //Frequency: F = device.dither.frequency.settings.max / 65536
Diletant 182:2bd8ec44998f 590 //Half-period: 1/(2 * F) = 0.0011 ... 0.0014 s
Diletant 182:2bd8ec44998f 591 //Percentage: P = device.dither.pulse.settings.min / 65536
Diletant 182:2bd8ec44998f 592 //Pulse width: W = P/(2 * F)
Diletant 182:2bd8ec44998f 593 //Output: O = W * 1536000 = 768000 * device.dither.pulse.settings.min / device.dither.frequency.settings.max
Diletant 182:2bd8ec44998f 594
Diletant 182:2bd8ec44998f 595 //Numerator: (768000 >> 5) * device.dither.pulse.settings.min = 0 ... 0x5dc00000 < maxint
Diletant 182:2bd8ec44998f 596 //Denominator: device.dither.frequency.settings.max >> 5 = 0xaf000 ... 0xe1000
Diletant 182:2bd8ec44998f 597 //Result: numerator/denominator = 0 ... 0x892 < maxint16
Diletant 183:c7a9c309086c 598 int32_t d = 0x5dc0 * device.dither.pulse.settings.min / (device.dither.frequency.settings.max >> 5);
Diletant 183:c7a9c309086c 599 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 600 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 601 return (uint16_t)d;
Diletant 182:2bd8ec44998f 602 }
Diletant 182:2bd8ec44998f 603
Diletant 182:2bd8ec44998f 604 uint16_t ditherGetRawPulseState(void) {
Diletant 182:2bd8ec44998f 605 //Value: dither pulse width W - s
Diletant 182:2bd8ec44998f 606 //Internal format: device.dither.pulse.state.width - 0...1 of dither half-period in 16.16 format
Diletant 182:2bd8ec44998f 607 //Output format: 15360000 * W - dimensionless units in 16.0 format
Diletant 182:2bd8ec44998f 608
Diletant 182:2bd8ec44998f 609 //Frequency: F = device.dither.frequency.state.frequency / 65536
Diletant 182:2bd8ec44998f 610 //Half-period: 1/(2 * F) = 0.0011 ... 0.0014 s
Diletant 182:2bd8ec44998f 611 //Percentage: P = device.dither.pulse.state.width / 65536
Diletant 182:2bd8ec44998f 612 //Pulse width: W = P/(2 * F)
Diletant 182:2bd8ec44998f 613 //Output: O = W * 1536000 = 768000 * device.dither.pulse.state.width / device.dither.frequency.state.frequency
Diletant 182:2bd8ec44998f 614
Diletant 182:2bd8ec44998f 615 //Numerator: (768000 >> 5) * device.dither.pulse.state.width = 0 ... 0x5dc00000 < maxint
Diletant 182:2bd8ec44998f 616 //Denominator: device.dither.frequency.state.frequency >> 5 = 0xaf000 ... 0xe1000
Diletant 182:2bd8ec44998f 617 //Result: numerator/denominator = 0 ... 0x892 < maxint16
Diletant 183:c7a9c309086c 618 int32_t d = 0x5dc0 * device.dither.pulse.state.width / (device.dither.frequency.state.frequency >> 5);
Diletant 183:c7a9c309086c 619 if (d < 0) d = 0;
Diletant 183:c7a9c309086c 620 if (d > 32767) d = 32767;
Diletant 182:2bd8ec44998f 621 return (uint16_t)d;
Diletant 183:c7a9c309086c 622 }
Diletant 183:c7a9c309086c 623
Diletant 183:c7a9c309086c 624 //VBN_Tzd s16 adjusted noise period
Diletant 183:c7a9c309086c 625 //int32_t device.dither.noise.settings.period
Diletant 183:c7a9c309086c 626 uint16_t param_VBN_Tzd(void) {
Diletant 183:c7a9c309086c 627 //Value: dither noise period (fixed part) - T [s]
Diletant 183:c7a9c309086c 628 //Internal format: device.dither.noise.state.period - 0...100 of measurement cycles in 16.16 format
Diletant 183:c7a9c309086c 629 //Output format: T [s] * 10000 (in 10000 Hz periods) - dimensionless units in 16.0 format
Diletant 183:c7a9c309086c 630
Diletant 183:c7a9c309086c 631 //Frequency: F = device.dither.frequency.state.frequency / 65536
Diletant 183:c7a9c309086c 632 //Period: 1/F = 0.0022 ... 0.0028 s
Diletant 183:c7a9c309086c 633 //Output: O = device.dither.noise.state.period * 1/F * 10000 =
Diletant 183:c7a9c309086c 634 // = device.dither.noise.state.period * 10000 * 65536 / device.dither.frequency.state.frequency =
Diletant 183:c7a9c309086c 635 // = 65536 * 20000 / device.dither.frequency.state.frequency * device.dither.noise.state.period / 2
Diletant 183:c7a9c309086c 636
Diletant 183:c7a9c309086c 637 int32_t tzd = 0x4e200000 / device.dither.frequency.state.frequency * device.dither.noise.state.period / 2;
Diletant 183:c7a9c309086c 638 if (tzd < 0) tzd = 0;
Diletant 183:c7a9c309086c 639 if (tzd > 32767) tzd = 32767;
Diletant 183:c7a9c309086c 640 return (uint16_t)tzd;
Diletant 183:c7a9c309086c 641 }
Diletant 183:c7a9c309086c 642
Diletant 183:c7a9c309086c 643 //VBN_Ran s16 range of the random component of noise
Diletant 183:c7a9c309086c 644 //int32_t device.dither.noise.settings.range
Diletant 183:c7a9c309086c 645 uint16_t param_VBN_Ran(void) {
Diletant 183:c7a9c309086c 646 //Value: dither noise period (random part) - T [s]
Diletant 183:c7a9c309086c 647 //Internal format: device.dither.noise.state.range - 0...100 of measurement cycles in 16.16 format
Diletant 183:c7a9c309086c 648 //Output format: T [s] * 10000 (in 10000 Hz periods) - dimensionless units in 16.0 format
Diletant 183:c7a9c309086c 649
Diletant 183:c7a9c309086c 650 //Frequency: F = device.dither.frequency.state.frequency / 65536
Diletant 183:c7a9c309086c 651 //Period: 1/F = 0.0022 ... 0.0028 s
Diletant 183:c7a9c309086c 652 //Output: O = device.dither.noise.state.range * 1/F * 10000 =
Diletant 183:c7a9c309086c 653 // = device.dither.noise.state.range * 10000 * 65536 / device.dither.frequency.state.frequency =
Diletant 183:c7a9c309086c 654 // = 65536 * 20000 / device.dither.frequency.state.frequency * device.dither.noise.state.range / 2
Diletant 183:c7a9c309086c 655
Diletant 183:c7a9c309086c 656 int32_t ran = 0x4e200000 / device.dither.frequency.state.frequency * device.dither.noise.state.range / 2;
Diletant 183:c7a9c309086c 657 if (ran < 0) ran = 0;
Diletant 183:c7a9c309086c 658 if (ran > 32767) ran = 32767;
Diletant 183:c7a9c309086c 659 return (uint16_t)ran;
Diletant 183:c7a9c309086c 660 }
Diletant 183:c7a9c309086c 661
Diletant 183:c7a9c309086c 662 //VBN_k s16 adjusted noise constant: pulse width = VB_tau +/- VBN_k
Diletant 183:c7a9c309086c 663 //int32_t device.dither.noise.settings.amplitude
Diletant 183:c7a9c309086c 664 uint16_t param_VBN_k(void) {
Diletant 183:c7a9c309086c 665 //Value: dither pulse width variation (pulse noise) W - s
Diletant 183:c7a9c309086c 666 //Internal format: device.dither.noise.settings.amplitude - 0...1 of dither half-period in 16.16 format
Diletant 183:c7a9c309086c 667 //Output format: 15360000 * W - dimensionless units in 16.0 format
Diletant 183:c7a9c309086c 668
Diletant 183:c7a9c309086c 669 //Frequency: F = device.dither.frequency.settings.frequency / 65536
Diletant 183:c7a9c309086c 670 //Half-period: 1/(2 * F) = 0.0011 ... 0.0014 s
Diletant 183:c7a9c309086c 671 //Percentage: P = device.dither.noise.settings.amplitude / 65536
Diletant 183:c7a9c309086c 672 //Pulse variation: W = P/(2 * F)
Diletant 183:c7a9c309086c 673 //Output: O = W * 1536000 = 768000 * device.dither.noise.settings.amplitude / device.dither.frequency.settings.frequency
Diletant 183:c7a9c309086c 674
Diletant 183:c7a9c309086c 675 //Numerator: (768000 >> 5) * device.dither.noise.settings.amplitude = 0 ... 0x5dc00000 < maxint
Diletant 183:c7a9c309086c 676 //Denominator: device.dither.frequency.settings.frequency >> 5 = 0xaf000 ... 0xe1000
Diletant 183:c7a9c309086c 677 //Result: numerator/denominator = 0 ... 0x892 < maxint16
Diletant 183:c7a9c309086c 678 int32_t k = 0x5dc0 * device.dither.noise.settings.amplitude / ((device.dither.frequency.settings.max + device.dither.frequency.settings.min) >> 6);
Diletant 183:c7a9c309086c 679 if (k < 0) k = 0;
Diletant 183:c7a9c309086c 680 if (k > 32767) k = 32767;
Diletant 183:c7a9c309086c 681 return (uint16_t)k;
Diletant 182:2bd8ec44998f 682 }