need help porting function to C

OP #2100315
Hello Guys,

i need help porting a function to c for my Mega32. Could anybody please 
help me with the following ?I tried several Times but failed all the 
time :-(

pullup =  220                            'not as static value, might 
change
adc = 500                                'will be read from mega later
B = 3963                                 'Bwert NTC
Ts = 333.15                              'Bezugstemperatur in Kelvin 
60°C
rs = 221                                 'Widerstand bei 
Bezugstemperatur TS

temp = ((b * Ts) / ((Ts * (Log(((((0.0047898 * adc) * pullup) / (4.9 - 
(0.0047898 * adc))) / rs)))) + b)) - 273.15



Thanks in Advance
Guest #2100610
1
#include <avr/io.h>
2
#include <math.h>
3

4
// Testdata
5
uint16_t pullup = 220; // not as static value, might change
6
uint16_t adc = 500;    // will be read from mega later
7
int16_t B = 3963;      // Bwert NTC
8
double Ts = 333.15;    // Bezugstemperatur in Kelvin 60°C
9
uint16_t rs = 221;     // Widerstand bei Bezugstemperatur TS
10
double temp = 0;
11

12
int main(void)
13
{ 
14
  temp = ((B * Ts) / ((Ts * (log(((((0.0047898 * adc) * pullup) 
15
         / (4.9 - (0.0047898 * adc))) / rs)))) + B)) - 273.15;
16
  asm volatile ("nop");
17
}

1) Ergebnis: temp = 61.4

2) log = natürlicher Logarithmus
http://www.nongnu.org/avr-libc/user-manual/group__avr__math.html#ga7f7d556ab6b6235777a179647c152126

3) Projektoptionen in AVR-Studio:
libm.a hinzufügen
OP #2100616
I worked on the function a bit but it didnt work yet, what i did


uint32_t temp;

// b = 3963                                 'Bwert vom NTC
// Ts = 333.15                              'Bezugstemperatur in Kelvin 
hier 60°C
// rs = 221                                 'Widerstand bei 
Bezugstemperatur TS
analog = adcwert(i);
temp =((3963 * 333.15) / ((333.15 * (log(((((0.0047898 * analog) * 1242) 
/ (4.9 - (0.0047898 * analog))) / 221)))) + 3963)) - 273.15;
sendUSART(temp);

but i get the wrong data, mhh
OP #2100623
Danke,wunderbar das deckt sich fein mit meinen 61,39

libm.a hab ich drin, aner wenn ich das nun über usart schicken möchte 
meckert der compiler rum

sendUSART(temp);

../measure.c:158: error: incompatible type for argument 1 of 'sendUSART'

muss ich das vorher noch wandeln ?
OP #2100702
Works like a Charm now, just one Problem i still have is that i need the 
resulting value temp converted to single hex values.

Meaning if a Temperature result would be 12.9 i want to have a result of

seg0 = 1
seg1 = 2
seg2 = 9

i tried with

  dtostrf(temp,3,1,ascii);sendUSART(ascii); sendUSART("\r\n");

  if ( temp > 10 )
{
        strncpy(seg0, ascii , 1);
  strncpy(seg1, ascii +1, 1);
  strncpy(seg2, ascii +3, 1);
}

but since this still results the ascii values
(seg0 ="1",seg1 ="2" and seg2 ="9")
, how do i convert them to hex codes ?

Thanks in Advance
OP #2100903
Tried that before already, but i stll need to convert the Value since 
the Compiler gives me

error: lvalue required as increment operand

Since i needed to define my seg0 to seg9 values as char i cant add '0'

Reply

A question of your own? This thread is over 15 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.