Skip to content

Instantly share code, notes, and snippets.

@bga
Last active April 16, 2021 11:11
Show Gist options
  • Save bga/3fcd985940d41a2171c92ee446e3c3cd to your computer and use it in GitHub Desktop.
Save bga/3fcd985940d41a2171c92ee446e3c3cd to your computer and use it in GitHub Desktop.
ntc thermistor fixed point int no float calculation 16 bit avr stm8 pic16 pure c embedded mcu
/*
Copyright 2021 Bga <bga.email@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//# log2fix16 from https://github.com/dmoulding/log2fix
/*
The MIT License (MIT)
Copyright (c) 2015 Dan Moulding
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BGA__STR(x) BGA__STR_IMPL(x)
#define BGA__STR_IMPL(x) #x
#define BGA__CONCAT(a, b) BGA__CONCAT_IMPL(a, b)
#define BGA__CONCAT_IMPL(a, b) a ## b
int16_t /* (max - precision).precision */ log2fix16(uint16_t x, size_t precision) {
// This implementation is based on Clay. S. Turner's fast binary logarithm
// algorithm[1].
uint16_t b = ((uint16_t)1) << (precision - 1);
int16_t y = 0;
// static_assert(0 < precision && precision < sizeof(uint16_t) * 8 - 1);
if(x == 0) {
return -1; // represents negative infinity
}
while(x < ((uint16_t)1) << precision) {
x <<= 1;
y -= ((uint16_t)1) << precision;
}
while((((uint16_t)2) << precision) <= x ) {
x >>= 1;
y += ((uint16_t)1) << precision;
}
uint32_t z = x;
for(size_t i = 0; i < precision; i++) {
z = z * z >> precision;
if(z >= (((uint32_t)2) << precision)) {
z >>= 1;
y += b;
}
b >>= 1;
}
return y;
}
typedef int_fast16_t FI10_6;
typedef uint16_t Um9_25;
FI10_6 Ntc_convert(
uint16_t r_ohm,
uint16_t m_rIce_ohm, //# measure ntc resistance in ice
Um9_25 m_oneDivB //# power coeff
#if 0
/* maxima code */
/* r in ice */
t0: 0;
r0: 35000;
/* r of your body */
t1: 36.6;
r1: 6090;
T0: 273.15;
temp(t, r, a, b) := 1 / (t + T0) - (a + b * log(r) / log(2));
data: solve([temp(t0, r0, a, b), temp(t1, r1, a, b)], [a, b]), bfloat;
/* result m_oneDivB */
data[1][2] * 2 ** 25;
#endif // 0
) {
//# accepted range - 3.25
uint_fast32_t ratio = ((((uint_fast32_t)r_ohm) << 16) /* 16.16 */ / m_rIce_ohm /* 16.0 */) /* 16.16 */;
int_fast16_t logR = log2fix16(((uint16_t)ratio >> 4 /* 18.14 */) /* 2.14 */, 16 - 4) /* 4.12 */;
int_fast16_t oneDivAbsTemp = ((int16_t)((((uint_fast32_t)m_oneDivB /* -9.25 */) * logR /* 4.12 */) /* -5.37 */ >> 14) /* -7.23 */ + ((int_fast16_t)((int16_t)1.0 / 273.15 * (((uintmax_t)1) << 23))));
return ((int_fast16_t)((int_fast32_t)1UL << (30 - 1)) /* 3.29 */ / oneDivAbsTemp /* -7.23 */) /* 10.6 */ - ((int_fast16_t)((int16_t)273.15 * (((uintmax_t)1) << 6))) /* 10.6 */;
}
int main(int argc, char* argv[]) {
if(argc <= 3) return 0;
#define DEF_AND_TRY_PARSE_UINT(nameArg, argvNoArg) unsigned nameArg; do { \
if(1 != sscanf(argv[(argvNoArg)], "%u", &nameArg)) { \
fprintf(stderr, "Can not parse unsigned int " BGA__STR(nameArg) " at command line pos " BGA__STR((argvNoArg) - 1) "\n" ); \
return (argvNoArg); \
} \
} while(0)
DEF_AND_TRY_PARSE_UINT(r0, 1);
DEF_AND_TRY_PARSE_UINT(oneDivB, 2);
DEF_AND_TRY_PARSE_UINT(r, 3);
#undef DEF_AND_TRY_PARSE_UINT
int_fast16_t ret = Ntc_convert(r, r0, ((Um9_25)oneDivB));
printf("fixed_int_value FI10_6(%d)\n", ((int)ret));
printf("float_value %lf\n", ((double)ret) / (1 << 6));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment