0

I apologize beforehand if this is a beginner's question; I'm new to C programming.

I am working on an embedded design project that requires my ATmega128 reads in a 4-byte data from a sensor. The data contains two 14-bit measurements that I need to process. There is one part I'm not completely sure about because I haven't done a lot of C programming yet. My algorithm is as follows:

1) Receive 4 bytes of data from sensor (MS 2 bits and LS 2 bits are to be tossed) 2) Save each byte into 4 unsigned char variables 3) Mask out MS 2 bits from MS byte, same for LS 2 bits from LS byte 4) MS 2 bytes = first measurement, LS 2 bytes = second measurement

This part I can do so far. What I'm not sure about is how C converts binary to decimal. I need to turn my two 14-bit measurements into unsigned int to make it easier to perform calculations. Is there an instruction that automatically does this? Or will the compiler automatically turn it into an int if I use integer operations on it? Or maybe none of these, and I have to write my own function to convert it?

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
dabr david
  • 67
  • 4
  • 9
  • 1
    "...how C converts binary to decimal" - There is no conversion of binary to decimal. They are simply numerical bases and computers use binary. Mathematical operations work as you would expect (four / two is two regardless of what base you are using to represent them.) What you're actually concerned with is how your conversion to a N-byte signed/unsigned int takes place and how the data is ordered initially. – Ed S. Feb 25 '14 at 20:39
  • 2
    You won't need assembler for this. Post how your 4 bytes are read, and someone can provide the missing logic for masking, shifting, conversion. – 500 - Internal Server Error Feb 25 '14 at 20:41
  • Can you explain where decimal comes into this – David Heffernan Feb 25 '14 at 20:43
  • 1
    You need to get together with the other guy doing this. See http://stackoverflow.com/questions/21931789/convert-binary-twos-complement-data-into-integer-in-objective-c . – Hot Licks Feb 26 '14 at 02:05

1 Answers1

0

All C data is represented as binary, so unsigned char values are really just integers. They are represented as binary, but you can just use them as numbers. So +, -, *, etc. work just as you would expect (just watch out for / because integer division and float division are different).

lthreed
  • 444
  • 4
  • 11
  • Thanks! My next question is if I would need to do a manual conversion since the 14-bit data is split into 2 bytes. – dabr david Feb 25 '14 at 22:53