How to converter char and string and send it through uart in C

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everybody, I'm new here, I'd like to learn how I can converter one character and send it via uart. The issue is that I have to read a register located in a accelerometer then send it from uC to PC. My communication between uC and accelerometer from my point of view is OK also the communication uC-PC but when I send the register I can't see anything in the screen. For example if I read 0x90 I want to see in putty "No se puede conectar con el dispositivo 0x90".

 

This is one part of my code.

 

That's what I want !!

 

Could somebody help me??

Thx.

This topic has a solution.

knowledge first

Last Edited: Wed. Aug 16, 2017 - 03:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mrluigi77 wrote:
I'm new here

You seem also to be very new to microcontroller programming - yes?

 

If you want the value 0x90 to appear on a text display as "90", then you have to send two characters: a '9' followed by a '0'

 

To do that, you have to separate the byte (8-bit) value 0x90 into its separate Hex digits.

 

Each hex digit represents 4 bits - so you want the top (most significant) 4 bits, and the bottom (least significant) 4 bits.

 

Now get your ASCII Table (aka ASCII chart); eg, http://www.asciitable.com/

 

If you look at the characters '0'-'9' it should be obvious that there is an easy conversion from numeric values 0-9

 

Similarly for characters 'A'-'F' and numeric values 10 (0x0A) - 15 (0x0F) ...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another approach would be to use a lookup table ... 

 

http://www.8052.com/faqs/119923

 

https://en.wikipedia.org/wiki/Lo...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I'd just use sprintf() or, if flash was really tight, itoa() with the radix set to 16. So something like:

uint8_t c = 0x2A;
char buff[10];

sprintf(buff, "%02X", c);
uart_enviar_datos(buff);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx buddy, I love this community, you're very clever. I had already tried and this worked. :)

knowledge first

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As I say, if this was a "small flash" (like < 4K) AVR then printf() may consume too much flash space in which case the alternative would have been:

uint8_t c = 0x2A;
char buff[10];

itoa(c, buff, 16);
uart_enviar_datos(buff);

but you get less control over the format that way. For example in sprintf() if I use %02X then even if the value were just 3 this would appear as 03 so in total "0x03" whereas itoa(...16) would give "0x3" without a leading 0.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It didn't work for other values

knowledge first

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

mrluigi77 wrote:

It didn't work for other values

 

it (what it?)

didn't work (what were you expecting, what did you get?)

other values (what other values, give examples!)

 

To get good answers you have to ask good questions?

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mrluigi77 wrote:
It didn't work for other values

???  Show some code for "non-working" values.  Tell what you expected, and what you got.

 

Are you saying that Cliff carefully picked the one single value for which itoa() works?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry guys, I'm new programming in C, learning English and other things. :(. I got confused with my register address from my accelerometer and its value. Thx a lot.

laugh

knowledge first

Last Edited: Wed. Aug 16, 2017 - 03:41 PM