09-23-2010 02:55 PM
Hi all
I want to control 24 digital outputs of a certain DAQ card. For controlling , i want to
use 24 LEDs as a control from DO_0 to DO_23. The digital output write function of the
DAQ card outputs that value, which is stored in its BUFFER having data type 'BYTE' .
The maximum value on one location of the BUFFER is 255 , which at a time writes 8 digital outputs.
So it means that my BUFFER will have only 3 locations to write to the 24 digital outputs.
My question is that
How can i store the values to the 3 locations of the BUFFER according to the Status of 24
LEDS from DO_0 to DO_23 ? Such that the corresponding decimal value of " DO_0 to DO_7 " stores on
the " ZERO LOCATION OF BUFFER i-e BUFFER[0] " and the corresponding decimal value of
"DO_8 to DO_15" stores to the " FIRST LOCATION OF BUFFER i-e BUFFER[1] " and similarly
the corresponding decimal value of "DO_16 to DO_23" stores to the " SECOND LOCATION OF
BUFFER i-e BUFFER[2]" ?
Any help will be greatly appreciated
Regards
09-23-2010 04:10 PM
You can easily accomplish this task in a loop using aan array of control IDs and some bit shifting to prepare the BYTE value.
Prepare an int array where to store the IDs of your leds (to be run only once at program start: control IDs do not vary at runtime):
int leds[24];
leds[0] = PANEL_LED0;
leds[1] = PANEL_LED1;
...
leds[23] = PANEL_LED24;
Next each time you want to update the logic read values in a loop and prepare the value for the digital output:
int i, val;
int b0 = 0, b1 = 0, b2 = 0;
for (i = 0; i < 8; i++) {
GetCtrlVal (panelHandle, leds[i], &val);
b0 += (val << i);
GetCtrlVal (panelHandle, leds[i + 8], &val);
b1 += (val << i);
GetCtrlVal (panelHandle, leds[i + 16], &val);
b3 += (val << i);
}
Now you have your values b0, b1 and b2 to write to the daq card.
(Code written by memory: may need some trimming or correction)
09-23-2010 05:41 PM
Roberto was a bit quicker than I was (no big surprise). I took the same basic approach: array of control IDs, reading and bit shifting the LED values to form bytes. I chose to update only the byte that was modified, not knowing your full application.
From a performance standpoint, I'm not sure there's much benefit to only reading the byte that was updated, but that's what I did.
From an application standpoint, I'm not sure you need to read the LEDs after each LED was pressed, but that's what I did. For your final application, you may want to have one button to update the buffer, instead of updating it after every click on an LED.
Take a look at the attached sample program. The user interface has a few bells and whistles you don't need, but it made for a good demonstration.