Home > Software design >  Determining 32-bit Mask in C
Determining 32-bit Mask in C

Time:05-11

I'm not a C programmer, but found myself in a situation where I need to do some coding. I am using an ADC to determine voltage levels running through a device, and the ADC has 3 channels (0, 1, 2). I need to be able to determine the voltages on each channel separately. I found a function that allows me to set a channel for scanning, but it requires a mask. I have no idea how this works. Below is the documentation:

// Sets the channel mask void ADC_SetChanMask(uint32 mask) {...}

Parameter mask sets the channel that will be scanned. Setting bits for channels that do not exist will have no effect. For example, if only 6 channels were enabled, setting a mask of 0x0103 would only enable the last two channels (0 and 1).

Example: If the component is setup to sequence through 8 channels, a mask of 0x000F would enable channels 0, 1, 2, and 3.

In my code, I need to enable channels 0, 1, and 2 separately, but never combined. If I could get some help understanding how this works, that would be amazing!

CodePudding user response:

channel 0, 1, 2, 3 ... correspond to the masks 1 << channel which are 1, 2, 4, 8 ... You can make a mask of multiple channels by combining bits with the | (bitwise OR) operator.

The example mask 0x0103 corresponds to channels 0, 1 and 8. Since only 6 channels are enabled the high order bit is ignored.

  • Related