I think you guys have it backwards.
You're turning a 0 into 165, he wants 165 to be 0.
And it should be adjustable according to each devices calibration numbers. Constants in the formula can't do that.
Here's a simple Y=mX + b type slope intercept.
It's limited to byte values and Positive slopes, but it should do the job.
Code:
DEFINE HSER_BAUD 9600 'Hser baud rate
DEFINE HSER_CLROERR 1 'Hser clear overflow automatically
DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 24h 'Hser transmit status init
Value VAR BYTE
Result VAR BYTE
TempB VAR BYTE
CAL_Low CON 165 ; These are the measurements from calibration
CAL_High CON 245
Result_Low CON 0 ; These are what the final range should be
Result_High CON 254
FOR Value = CAL_Low to CAL_High
GOSUB Scale
HSEROUT [DEC Value," = ",DEC Result,13,10]
NEXT Value
STOP
Scale:
TempB = (Value MAX CAL_Low) MIN CAL_High ; Limit Value to CAL range
Result = ((TempB-CAL_Low)*(Result_High-Result_Low))/(CAL_High-CAL_Low)
RETURN
Which gives these results
Code:
Value = Result
165 = 0
166 = 3
167 = 6
168 = 9
169 = 12
170 = 15
171 = 19
172 = 22
173 = 25
174 = 28
175 = 31
176 = 34
177 = 38
178 = 41
179 = 44
180 = 47
181 = 50
182 = 53
183 = 57
184 = 60
185 = 63
186 = 66
187 = 69
188 = 73
189 = 76
190 = 79
191 = 82
192 = 85
193 = 88
194 = 92
195 = 95
196 = 98
197 = 101
198 = 104
199 = 107
200 = 111
201 = 114
202 = 117
203 = 120
204 = 123
205 = 127
206 = 130
207 = 133
208 = 136
209 = 139
210 = 142
211 = 146
212 = 149
213 = 152
214 = 155
215 = 158
216 = 161
217 = 165
218 = 168
219 = 171
220 = 174
221 = 177
222 = 180
223 = 184
224 = 187
225 = 190
226 = 193
227 = 196
228 = 200
229 = 203
230 = 206
231 = 209
232 = 212
233 = 215
234 = 219
235 = 222
236 = 225
237 = 228
238 = 231
239 = 234
240 = 238
241 = 241
242 = 244
243 = 247
244 = 250
245 = 254
Bookmarks