Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Shifting numbers in MMBasic
Author | Message | ||||
DuinoMiteMegaAn Senior Member Joined: 17/11/2011 Location: AustraliaPosts: 231 |
I have 3 bytes that I am re-combining from an I2C read operation from the Bosch BMP085. The below formula is needed to find the raw pressure. Can this be done in MMBasic? Any help is appreciated. ref (y = 0-3) x = (byte1<<16 + byte2<< 8 + byte3) >> (8-y) <-------- Formula |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
x=(byte1*(2^16) + byte2*(2^8) + byte3) \ 2^(8-y) I had a bit of uncertainty about the 8-y. Not sure what you wanted. I assumed right shift by 8-y. |
||||
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164 |
In a word - yes. I don't have the time for a full code example now, but essentially to LeftShift (<<) by 16 or 8 bytes, do this: byte1 = (byte1 * (2 ^ 16)) And &hFFFF byte2 = (byte2 * (2 ^ 8)) And &hFF x = byte1 + byte2 + byte3 Then RightShift(>>) by 8-y bytes is: x = x \ (2 ^ (8 - y)) Cheers, crackerjack |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
and you can toss away a lot of the parentheses as exponentiation has the highest priority in processing. I just checked the manual. So byte1*(2^16) can become byte1*2^16 and it will process correctly. A further thought: there is a maximum number that you can process before you lose accuracy. A quote from page 8 of the reverence manual: The range of integers (whole numbers) that can be manipulated without loss of accuracy is ±16777100
This is just short of 256*2^16 which is 0xFFFF8C. It's an odd figure. Anyway, with your calculation you could easily exceed that figure. |
||||
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164 |
Apologies - another pair of FF's are required on the end of the Left Shift operations in my post. The AND'ing is not strictly necessary, but does force the truncation of any bits that may have been in, for example, byte1 if byte1 was not "byte-sized" to begin with. |
||||
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164 |
Andy, the left shift part of your formula may be handled by the byte2num function. I've never used it, but it just occurred to me that it could do at least part of the job in terms of that formula. Let us know how you go. Cheers.... |
||||
DuinoMiteMegaAn Senior Member Joined: 17/11/2011 Location: AustraliaPosts: 231 |
The problem with shifting bytes to reconstruct a number from I2C buffer reads is that the sign bit is missing? I know for a fact that the BMP085 16 bit constants have some/few "signed" constants. How do I restore the sign? BMP085_AC1 = (BMP085_buff(0)*2^8) + BMP085_buff(1)
BMP085_AC2 = (BMP085_buff(2)*2^8) + BMP085_buff(3) BMP085_AC3 = (BMP085_buff(4)*2^8) + BMP085_buff(5) BMP085_AC4 = (BMP085_buff(6)*2^8) + BMP085_buff(7) BMP085_AC5 = (BMP085_buff(8)*2^8) + BMP085_buff(9) BMP085_AC6 = (BMP085_buff(10)*2^8) + BMP085_buff(11) BMP085_B1 = (BMP085_buff(12)*2^8) + BMP085_buff(13) BMP085_B2 = (BMP085_buff(14)*2^8) + BMP085_buff(15) BMP085_MB = (BMP085_buff(16)*2^8) + BMP085_buff(17) BMP085_MC = (BMP085_buff(18)*2^8) + BMP085_buff(19) BMP085_MD = (BMP085_buff(20)*2^8) + BMP085_buff(21) |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
don't know about this chip / sensor but some (many?) use twos complement numbers. Check out my post here on how to manage negative twos complement numbers or look in wikipedia. You would have to combine your bytes before processing as a twos complement. |
||||
bigmik Guru Joined: 20/06/2011 Location: AustraliaPosts: 2914 |
OMG... My brain is sore after reading these posts... I thought I could program in basic but now I am not so sure... Glad there are some smarties out there for us Dummies. Good work Lads, Mick Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<< |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
I had a look at the datasheet for the BMP085 and while it indicated which constants are signed it did not say the method. I suspect Bosch are relying on people using their code examples to do the job. Andy, you may have bitten off a large piece to chew on. Assuming twos complement: read your pair of bytes, combine them into a 16 bit number as shown above, check if the number is equal or greater than 0x8000 (means it is negative), if not then treat it as positive else invert it (number XOR &HFFFF) and add 1 and then change the sign to negative. |
||||
DuinoMiteMegaAn Senior Member Joined: 17/11/2011 Location: AustraliaPosts: 231 |
Andy, you may have bitten off a large piece to chew on.
@BobD This BMP085 temp/pressure/altitude/weather calculations in FP is probably the hardest algorithms I have encountered to date. The trouble is, I bought this complex sensor and now I have to make it work but being a hacker and I live by my hacker moto "don't give up" Can you spare a logic anaylzer for this $%#$% chip? BTW ... thanks for your 2's complement help |
||||
Print this page |