How to use bit operations



  • 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?

  • Qt Champions 2018

    @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.



  • @K-Str Where are you getting your bits? Are you bit banging a microcontoller?



  • @jsulm
    Thank you very much for your answer.
    Yes it works in this way.
    @fcarney
    Thank you very much for your answer.
    I get the data from a RX868 receiver. It delivers the data bitwise.
    II think it is a bitstream.
    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


  • Qt Champions 2018

    @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.



  • @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.


  • Qt Champions 2018

    @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



  • @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.


  • Qt Champions 2018

    Hi @k-str,

    now it get's interesting - low level programming :)

    Computer : Raspberry zero
    Radtio receiver : RX868SH-DV

    Still 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


  • Qt Champions 2018

    @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?



  • @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 :

    1. Set EN to ON
    2. wait until the DATA goes ON
    3. read from DATA
    4. set DATA to OFF
    5. repeat from 2.

    Kurt


  • Qt Champions 2018

    @k-str

    There is a description only in german

    No problem for me ;)

    Can you give me a link to that?



  • @aha_1980 said in How to use bit operations:

    Or how do you know when a bit starts and ends?

    Priceless :)


  • Moderators

    @jonb Usually a bit starts when 3 or 5 V are on the bin right  😉



  • @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


  • Qt Champions 2018

    @K-Str

    Ok, 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



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.