need help porting function to C

OP #2100315
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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 #2100702
Rate this post
• ▲ useful
▼ not useful
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

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

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.