Level 3
Level 3

# Decode bitfield to turn things on and off?

This is probably pretty basic, but I'm still relativity new to coding, and suggestions or different ways of doing this will be appreciated. Basically what I'm trying to do is read 8 pins of the PSoC that are connected to external on off switches (done). There are also 8 devices connected to 8 output pins on the PSoC, When switch 1 is on a test pattern will run on device 1, when 2 is on a pattern will run on device 2... etc. When multiple switches are on I want the pattern to run on multiple devices synchronously (I understand that they all cannot be turned on and off at the exact same time but can all be turned on within a few hundred milliseconds or so and off in the same). I pretty much have the code written now so that the pins are all read and put into a bitfield. I want a function to be able to read the bitfield and understand which switches (flags) are on and run the pattern on those specifed devices. I thought using a bitfield might be an easy way to accomplish this but now I'm stumped. If there is a simpler way besides using bitfields i would love to hear it. I thought about using switch statements but then I would need to write 255 switch statements based on all the combinations that the switches could be in, and that seems a little insane. There must be an easy way to do this since it seems like such a basic thing to do. I have attached my project

1 Solution
Level 3
Level 3

# Re: Decode bitfield to turn things on and off?

ended up just passing the bitfield to a control reg

4 Replies
Level 10
Level 10

# Re: Decode bitfield to turn things on and off?

Well, there are many different ways to accomplish this task, from pure software to pure hardware.

A hardware solution would connect the input pin to the output pin with an inverter (NOT gate) when appropriate.

A software solution starts with a bunch of #defines for the bit position of the valves as

#define Valve1 0x01     // you might use 0b00000001

#define Valve2 0x02     // 0b00000010

#define Valve3 0x04     // 0b00000100

up to

#define Valve8 0x80    // 0b10000000

Now with a couple of statements like

Valve3_Pin_Write(InputSwitches & Valve3)    /// Turn Valve 3 on or off

you can handle all your valves. This has to be repeated after each reading of your switches into the variable "InputSwitches" which should be an uint8.

Bob

Level 3
Level 3

# Re: Decode bitfield to turn things on and off?

Bear with me here, I understand using the #define rather than putting it in the enum. As far as using a uint8 "input switches" could i just change and use the "unsigned char myflags" to a "uint8 myflags" that is in my code now and takes the value of the bitfield? Also, why do i need to and "input switches & valve 3", Can I just pass the "input switches" or "myflags" to the valve_pin_write()?

Level 10
Level 10

# Re: Decode bitfield to turn things on and off?

Can I just pass the "input switches" or "myflags" to the valve_pin_write()?

Yes, you may. The restrictions are: all pins must reside on the same port.

When a pcb is made for the project it might be necessary to change signals to different ports to ease the layout. When that happenes a software redesign is mandatory (and as usual: time presses).

Your fantasy is nearly not limited by the PSoC, think of arrays of pointers to functions (which need to be initializes as constant declarations) allowing you to "for" a variable over all your switches and write

ValveArray_Write[ii](SwitchArray_Read[ii]()); // Have a look here

Bob

Level 3
Level 3