EmbDev.net

Forum: ARM programming with GCC/GNU tools need help porting function to C


Author: Mario Keulertz (lastyle)
Posted on:

Rate this post
0 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

Author: Helper (Guest)
Posted on:

Rate this post
0 useful
not useful
#include <avr/io.h>
#include <math.h>

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

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

1) Ergebnis: temp = 61.4

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

3) Projektoptionen in AVR-Studio:
libm.a hinzufügen

Author: Mario Keulertz (lastyle)
Posted on:

Rate this post
0 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

Author: Helper (Guest)
Posted on:

Rate this post
0 useful
not useful
> but i get the wrong data, mhh

Shit happens.

Author: Mario Keulertz (lastyle)
Posted on:

Rate this post
0 useful
not useful
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 ?

Author: Helper (Guest)
Posted on:

Rate this post
0 useful
not useful
Yes.

The dtostrf() function converts the double value passed in val into an 
ASCII representationthat will be stored under s. The caller is 
responsible for providing sufficient storage in s.

http://www.nongnu.org/avr-libc/user-manual/group__...

Author: Mario Keulertz (lastyle)
Posted on:

Rate this post
0 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

Author: Helper (Guest)
Posted on:

Rate this post
0 useful
not useful
'0' = 0 + '0'
'1' = 1 + '0'
go figure

Author: Mario Keulertz (lastyle)
Posted on:

Rate this post
0 useful
not useful
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'

Author: Helper (Guest)
Posted on:

Rate this post
0 useful
not useful
I don't get it.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.