bitWrite function code for use with PIC microcontroller

Asked

Viewed 375 times

1

I am therefore an Arduino code for the PIC pro compiler Mikroc. I arrived at a part that contains the following:

byte data[3];

// pulse the clock pin 24 times to read the data
for (byte j = 3; j--;) {
    for (char i = 8; i--;) {
        digitalWrite(PD_SCK, HIGH);
        bitWrite(data[j], i, digitalRead(DOUT));
        digitalWrite(PD_SCK, LOW);
    }
}

I managed to arrange it for:

byte data[3];
int j;
int i;

// pulse the clock pin 24 times to read the data
for (j = 3; j--;) {
    for (i = 8; i--;) {
        RD1_bit = 1;
        bitWrite(data[j], i, RD0_bit);
        RD1_bit = 0;
    }
}

Thus, only the function is missing bitWrite(data[j], i, RD0_bit); work, but I did not reach conclusive results.

I currently have the following code, which is not running:

void bitWrite(uint8_t &x, unsigned int n, int b1) {
    if (n <= 7 && n >= 0) {
        if (b1) {
            x |= (1u << n);
        } else {
            x &= ~(1u << n);
        }
    }
}

Note: Unique include that I’m using is #include <stdint.h>

  • 1

    I don’t know this compiler and I’ve never used Adian. From what I’ve seen byte is not a standard type or compiler http://www.mikroe.com/download/documents/compilers/mikroc/pro/pic/help/arithmetic_types.htm or do stdint.h. Now the most important in the bitWrite it seems that these to pass x as reference, but this does not exist in C and what variable is the 1u?

  • 1

    @Giovani, does the code show an error? This 1u seems to me a constant of some library of the Mikroc, therefore x& = ~(1u << n) is a bit mask and 1u need to have some value for it to work.

  • @krystalgamer thanks for the help, I managed to resolve.

  • 1

    @Avelino thanks for the help, I managed to resolve.

  • @Giovaniracipaganini I was curious to know what this code does. Generates a square wave on pin 3? (Or pin 4 starts at 0)!?

  • 1

    @Avelino Gero is a 24-bit square wave, with each bit representing a bit, as it is a 24-bit converter. To learn more see the HX711.cpp file available on the internet.

Show 1 more comment

1 answer

0


I managed to make it work with the following code:

void bitWrite(char *x, char n, char value) {
   if (value)
      *x |= (1 << n);
   else
      *x &= ~(1 << n);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.