Question

I'm trying to send a converted temperature reading from my DS1820 to my PC using a PIC16F877 uart. I am using MPLABX and the XC8 compiler, which has a build in usart.h, though it's only useful for the PIC18 series, so I'm using usart_pic16.h which has been written to work with the PIC16 series by a third party.

I am successfully collecting the temperature in its hex form from the DS1820 and have converted it to a human readable float, but I can't find a way to forward the float value to the PC via the UART.

The usart_pic16.h library allows for direct sending of chars, strings, lines and ints over the usart using the following methods:-

  • void USARTWriteChar(char ch);
  • void USARTWriteString(const char *str);
  • void USARTWriteLine(const char *str);
  • void USARTWriteInt(int16_t val, int8_t field_length);

I'm stuck at finding a way to send the float value across the uart using this library, which includes extraction and sending a decimal point.

I did try sending a string like this:-

  • USARTWriteString( "TempC= %7.3f degrees C \r\n", temp_c );

Where temp_c is the float value of the temp, but it errorred with "too many function arguments" while compiling. Probably obvious to those c gurus out there, which I'm unfortunately not :(

Maybe one way would be to extract each value from the float and sent it as an int, with the exception of the decimal point which could probably be found with an 'if' check of each value, then when the decimal point is found just send it as a char e.g. USARTWriteChar('.');, which does work. Unfortunately I don't know how to extract individual float values or if it's the best way to do it.

I wasn't sure if my code was required to solve this so thought I'd avoid spamming it unless someone asks.

Any help would be great.

Thanks.

Was it helpful?

Solution

The general equivalent would be to include <stdio.h> and do something like the following:

char s[64];
sprintf(s, "TempC= %7.3f degrees C \r\n", temp_c);
USARTWriteString(s);

Although for an embedded platform you may be best to avoid the printf style functions that can use a fair bit of code space on a small microcontroller. Also in the above example it would make sense to break just the floating point conversion into a seperate sprintf and output the rest of the string seperately so the buffer s doesn't have to be so large.

That should get you running for the moment but in the longer term you might want to look at converting the temperature to integer say by multiplying it by 1000 and then decoding the value on the PC, that's assuming eventually you intend to write your own application to communicate with the microcontroller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top