Stupid newbie question- I have a bunch of 8 bit variables in C. Looking at the ADC spec, I need to send the most significant bit first.
I can think of any number of ways to do this, but none are very simple. Is there a really tight way to do this?
With the 8 bit values I think I'm just going to AND it with one of 8 one-hot constants; 10000000, 01000000, etc... and check to see if it's a nonzero result. Is that quickest? Just for kicks, what would you do if it was a 32 bit number?
Ironically, they do begin as constants in this case and it would be far smaller to reverse the bits there, but it just kills the readability of the code. I used to do crap like that, but no longer. Project is too big for stuff like that to be floating around.
Depending on your processor, you might be able to just perform successive left-shifts. Many of them will rotate through the carry bit, then you can do a branch on whether the carry bit is set.
Do you wan to rotate it?
If just shifting, you could use command like
variable<<=1;
this will shift 1 bit to left
to shift 8 bits
for (i=0;i<8;i++)
variable<<=1;
to check for MSB, define it as signed char and check if it is negative number.
if (variable<0)
// do something
I'm not sure about PIC, but in motorola, there is command to left shift the byte and the bit shifted out will be stored in the Carry bit.
Using assembly, the Carry bit can be checked easily.
Ignoring your question altogether, what EXACTLY are you trying to do?.
You mention an ADC, then mention 'send' - with an ADC you 'read' values, not 'send' them.
But in either case you don't need to reverse anything, just rotate the byte the opposite way. The code sample below receives RS232 serial data, it uses "RRF Rcv_Byte , f" to rotate the carry bit into the received word, simply changing the RRF for an RLF will reverse the direction of the bits in the received word.
Code:
Rcv_RS232 BTFSC SER_PORT, SER_IN ;wait for start bit
GOTO Rcv_RS232
CALL Start_Delay ;do half bit time delay
BTFSC SER_PORT, SER_IN ;check still in start bit
GOTO Rcv_RS232
MOVLW 0x08 ;set up to read 8 bits
MOVWF Bit_Cntr
CLRF Rcv_Byte
Next_RcvBit CALL Bit_Delay
BTFSS SER_PORT, SER_IN
BCF STATUS , C
BTFSC SER_PORT, SER_IN
BSF STATUS , C
RRF Rcv_Byte , f
DECFSZ Bit_Cntr , f ;test if all done
GOTO Next_RcvBit
CALL Bit_Delay
MOVF Rcv_Byte, W
RETURN
It's really essential to have a decent understanding of PIC assembler, even if you write mainly in C, you can't really understand what's happening otherwise.