EmbDev.net

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


von Mario K. (lastyle)


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

von Helper (Guest)


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

von Mario K. (lastyle)


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

von Helper (Guest)


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

Shit happens.

von Mario K. (lastyle)


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

von Helper (Guest)


Rate this post
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__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42

von Mario K. (lastyle)


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

von Helper (Guest)


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

von Mario K. (lastyle)


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

von Helper (Guest)


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

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.