How to use bit operations
-
wrote on 29 Jul 2019, 10:10 last edited by K-Str
Hello to all;
I have gotten verry good tips in this forum.
I would like t write a C application which reads bitwise data from an input device.
This data should be compared and manipulated like SHIFT, AND, OR operations;
Here a simple example:- BYTE svdata, testdata;
BIT inddata;
Do;
read (bit);
SHIFT LEFT svdata <-- 1;
ADD indata to svdata;
COMPARE testdata with svdata;
EndDo:
But now I have a basic question:
- how can I make bit operation ?
- Where can I find examples for shift an AND operation?
- Is thera a good tutorial with examles available?
- BYTE svdata, testdata;
-
Hello to all;
I have gotten verry good tips in this forum.
I would like t write a C application which reads bitwise data from an input device.
This data should be compared and manipulated like SHIFT, AND, OR operations;
Here a simple example:- BYTE svdata, testdata;
BIT inddata;
Do;
read (bit);
SHIFT LEFT svdata <-- 1;
ADD indata to svdata;
COMPARE testdata with svdata;
EndDo:
But now I have a basic question:
- how can I make bit operation ?
- Where can I find examples for shift an AND operation?
- Is thera a good tutorial with examles available?
@k-str said in How to use bit operations:
how can I make bit operation ?
Like defined in C/C++.
| for OR, & for AND, ~ to invert bits and ^ for XOR. << shift left, >> shift right.Best source for such a question is Google or book. See for example https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
Actually you can't read bitwise, the smallest unit is a byte.
- BYTE svdata, testdata;
-
wrote on 5 Aug 2019, 13:26 last edited by K-Str 8 May 2019, 13:37
Hi,
thanks for your answers,
But it doesn't solve my problem:
I get every 200μs data from a device. It seems this are single bit in an integer value .
To analyze the incomming data I would like to collect 200 bits and store it in one value of 200 bit length.
Is there any way to do this like :value bit x(NbrOfBits=200) int input; input = ReadFromDevice(); ShiftLleft value; value = value + input; **OR** value = value XOR input;
Is there a simply solution?
Kurt -
Hi,
thanks for your answers,
But it doesn't solve my problem:
I get every 200μs data from a device. It seems this are single bit in an integer value .
To analyze the incomming data I would like to collect 200 bits and store it in one value of 200 bit length.
Is there any way to do this like :value bit x(NbrOfBits=200) int input; input = ReadFromDevice(); ShiftLleft value; value = value + input; **OR** value = value XOR input;
Is there a simply solution?
Kurt@k-str What are you going to do with all these bits when you got all of them?
What kind of data do they represent?
https://en.cppreference.com/w/cpp/language/bit_field comes to mind. -
wrote on 6 Aug 2019, 14:45 last edited by
@jsulm I want to read data comming bitwise from a weather station.
This have to be analysed by searching the begin of the data block maybe its 00000001.
Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable. -
@jsulm I want to read data comming bitwise from a weather station.
This have to be analysed by searching the begin of the data block maybe its 00000001.
Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable.Lifetime Qt Championwrote on 6 Aug 2019, 16:49 last edited by aha_1980 8 Jun 2019, 16:50@k-str said in How to use bit operations:
@jsulm I want to read data comming bitwise from a weather station.
Ok, may you be a bit more specific? How is that data coming from the weather station? Serial port? Bluetooth? Network? Do they provide an API to read out the data?
This have to be analysed by searching the begin of the data block maybe its 00000001.
And as you can see, these are eight bit which form a byte. There is no modern computer that operates on smaller units, and no transport medium that allows you to transport single bits (well at least I've never seen one, I'm glad to hear opposite examples. And please: bit-banging on a MCU does not count).
In short: wherever you get the data from, it will be an array of bytes. Maybe call it bitstream, but's an array of bytes. Dot.
Then I can find the other data like temperature or wind speed. But first I need all of the bits stored in a variable.
Is there a documentation for that data? Or do you reverse-engineer the protocol?
It is easier for us to help you if you provide what you know for sure, not what you assume.
Regards
-
wrote on 7 Aug 2019, 13:30 last edited by K-Str 8 Jul 2019, 13:40
@aha_1980 ,
thanks for your answer!
Tthe weather station sends data via 868 Mhz radio signal.
Here the detailed configuration:- Weather station : WH1080
- Computer : Raspberry zero
- Radtio receiver : RX868SH-DV
Here some information of the interface :
The incomming data are :
version 1:The transmitter sends 11 bytes of data as follows. Some of these differ from any of the documentation I’ve found on Fine Offset weather stations, so I’ve described them here (letters to the nibbles for the description below): –
Byte 0 1 2 3 4 5 6 7 8 9 Nibble ab cd ef gh ij kl mn op qr st Example a1 82 0e 5d 02 04 00 4e 06 86 I’ve interpreted the nibbles as follows: – abc: device identifier def: temperature gh: humidity ij: average wind speed low byte kl: gust wind speed low byte m: unknown n: rainfall counter high nibble op: rainfall counter q: battery-low indicator r: wind direction st: checksum
version 2:
00000001 T1T2T3T41 A1A2A3V1 T11T12T13T141 T21T22T23T241 T31T32T33T341 F11F12F13F141 F21F22F23F241 W11W12W13W141 W21W22W23W241 W31W32W33W341 C11C12C13C141 C21C22C23C241 C31C32C33C341 B1B2B3B41 Q1Q2Q3Q41 S1S2S3S41 Präambel ___7___ 1_R_0_V ____0.1°___ ____1°_____ ____10°____ ____1%_____ ____10%____ _0.1 km/h__ ___1 km/h__ __10 km/h__ ___R_LSN___ ___R_MID___ ___R_MSN___ __???___ _Check_ _Summe_ R: acrual rain (1 = Regen) V: sign of Temperatur (1 = negativ) W1..W3: 3 * 4Bit Wind speed (BCD) C1..C3: 12 Bit rain counter T1..T3: 3 * 4Bit Temperatur (BCD) F1..F2: 2 * 4Bit humidity (BCD)
I think the first version is the better one,
I will check for the pattern 0xa18 which is the device identifier.
If it doesn't work I try version 2.
Checking for unsigned char = 1.Do you think this is ok?
If you need further information please ask. -
@aha_1980 ,
thanks for your answer!
Tthe weather station sends data via 868 Mhz radio signal.
Here the detailed configuration:- Weather station : WH1080
- Computer : Raspberry zero
- Radtio receiver : RX868SH-DV
Here some information of the interface :
The incomming data are :
version 1:The transmitter sends 11 bytes of data as follows. Some of these differ from any of the documentation I’ve found on Fine Offset weather stations, so I’ve described them here (letters to the nibbles for the description below): –
Byte 0 1 2 3 4 5 6 7 8 9 Nibble ab cd ef gh ij kl mn op qr st Example a1 82 0e 5d 02 04 00 4e 06 86 I’ve interpreted the nibbles as follows: – abc: device identifier def: temperature gh: humidity ij: average wind speed low byte kl: gust wind speed low byte m: unknown n: rainfall counter high nibble op: rainfall counter q: battery-low indicator r: wind direction st: checksum
version 2:
00000001 T1T2T3T41 A1A2A3V1 T11T12T13T141 T21T22T23T241 T31T32T33T341 F11F12F13F141 F21F22F23F241 W11W12W13W141 W21W22W23W241 W31W32W33W341 C11C12C13C141 C21C22C23C241 C31C32C33C341 B1B2B3B41 Q1Q2Q3Q41 S1S2S3S41 Präambel ___7___ 1_R_0_V ____0.1°___ ____1°_____ ____10°____ ____1%_____ ____10%____ _0.1 km/h__ ___1 km/h__ __10 km/h__ ___R_LSN___ ___R_MID___ ___R_MSN___ __???___ _Check_ _Summe_ R: acrual rain (1 = Regen) V: sign of Temperatur (1 = negativ) W1..W3: 3 * 4Bit Wind speed (BCD) C1..C3: 12 Bit rain counter T1..T3: 3 * 4Bit Temperatur (BCD) F1..F2: 2 * 4Bit humidity (BCD)
I think the first version is the better one,
I will check for the pattern 0xa18 which is the device identifier.
If it doesn't work I try version 2.
Checking for unsigned char = 1.Do you think this is ok?
If you need further information please ask.Lifetime Qt Championwrote on 7 Aug 2019, 14:03 last edited by aha_1980 8 Jul 2019, 14:08Hi @k-str,
now it get's interesting - low level programming :)
Computer : Raspberry zero
Radtio receiver : RX868SH-DVStill unclear for me how your receive the data in the PI. Is the receiver connected to and toggles a GPIO pin? How do you sample the data?
The transmitter sends 11 bytes of data as follows.
That's good. So you will need 11 bytes to store the complete data. Plus a bit of handling to detect the start of the frame etc.
Then the fun begins:
#include <array> #include <iostream> using namespace std; int main() { std::array<uint8_t, 11> data; // add sample data data[0] = 0x12; data[1] = 0x30; data[2] = 0x26; // calculate vendor and temperature as example const uint16_t vendor = (data[0] << 4) | (data[1] >> 4); const uint16_t temperature = ((data[1] & 0x0F) << 8) | data[2]; cout << hex << "vendor: " << vendor << endl; cout << dec << "temperature: " << temperature << endl; return 0; } // Output: vendor: 123 temperature: 38
The other data can be used in similar ways. If both nibbles are in the same byte, you can just use it. If they are distributed over several bytes, you need to extract the nibbles and shift. BCD just means, that each nibble encodes a decimal digit of 0..9.
I hope that helps you.
Regards
[Edit: fixed typo in temperature shift]
-
@aha_1980
Thanks for your answer:
yes it is connected to GPIO,
The DATA is connectet to pin 13 (GPIO02) as input
The EN = RX Enable is connected to pin 15 (GPIO03) as output set to on
Kurt@k-str said in How to use bit operations:
The DATA is connectet to pin 13 (GPIO02) as input
And where do you get clock from? Or how do you know when a bit starts and ends?
-
wrote on 8 Aug 2019, 09:18 last edited by K-Str 8 Aug 2019, 09:33
@aha_1980
There is a description only in german. But I try to explain :
When you set the EN to ON then the input on the DATA pin rises when a new bit is received.
the sequence is as follows :- Set EN to ON
- wait until the DATA goes ON
- read from DATA
- set DATA to OFF
- repeat from 2.
Kurt
-
@aha_1980
There is a description only in german. But I try to explain :
When you set the EN to ON then the input on the DATA pin rises when a new bit is received.
the sequence is as follows :- Set EN to ON
- wait until the DATA goes ON
- read from DATA
- set DATA to OFF
- repeat from 2.
Kurt
-
@k-str said in How to use bit operations:
The DATA is connectet to pin 13 (GPIO02) as input
And where do you get clock from? Or how do you know when a bit starts and ends?
wrote on 8 Aug 2019, 19:06 last edited by@aha_1980 said in How to use bit operations:
Or how do you know when a bit starts and ends?
Priceless :)
-
@aha_1980 said in How to use bit operations:
Or how do you know when a bit starts and ends?
Priceless :)
@jonb Usually a bit starts when 3 or 5 V are on the bin right 😉
-
wrote on 10 Aug 2019, 09:38 last edited by K-Str 8 Oct 2019, 12:32
@aha_1980 :
yes here some descriptions in german:
Descripton in german
Its a programm for reading RX868SH.DV and WH1080 weather station as well a description ( see link Datenprotokolle on the end)
and here the link to the R868SH-DV module
@JonB @J-Hilk :
I will check it!
Kurt -
@aha_1980 :
yes here some descriptions in german:
Descripton in german
Its a programm for reading RX868SH.DV and WH1080 weather station as well a description ( see link Datenprotokolle on the end)
and here the link to the R868SH-DV module
@JonB @J-Hilk :
I will check it!
KurtOk, thanks.
As I expected, this is really implemented by polling a GPIO pin. And it leads to a CPU load of 14% on Raspberry Pi when using 200µs polling interval :-O
Regards
-
wrote on 11 Aug 2019, 17:02 last edited by K-Str 8 Nov 2019, 17:09
@aha_1980
Thank you for this tip.
This means I have to implement a timer with a interval of 200µs which kicks off reading of one new bit from the RX868SH-DV.
OK I will try it. And then I give you an answer.
But now I got a new problem using timers in QT-Creator. I make a new topic for this.
my new topic for two timer
1/19