I want to write a simple message code based on 2 bytes to send sensor readings to computer thru serial communication.
For that I have created this simple table
1 byte | 2byte op | sensor | daa | data OO SS | XXDD | DDDD DDDD
So, in the first 2 bits (from left to right) i represent the operation code. In following 2 i say which sensor is that about. Then, there are two bits which are don't cares. The remaining bits are for representing the 1024 possible values generated by the 10 bit ADC.
My problem is in creating the first byte. I have some difficulties in handling bits with bitwise operations.
If i have the opcode and the sensorPin and the sensor reading, how can i put the op code in the first two bits (from left to right), followed by the sensor pin number, followed by two don't cares and finally followed by the 2 msb bits in the reading value?
Thank you very much,