- From: Chuck Haines <chaines_at_gmail.com>
- Date: Mon, 26 Jul 2004 15:14:55 -0400
For the benefit of all who have asked (since I've gotten a few
emails), here is code that demonstrates the use of the counter's on a
National Instruments DAQCard 6024E. This is only test code, so it's
not efficient, I'm simply showing you how to use it.
<begin code snippet>
#include<stdio.h>
#include<comedilib.h>
#include<unistd.h>
int main ( int argc, char *argv[] )
{
comedi_t *card;
lsampl_t op[2], data = 5, lastData;
comedi_insn insn;
unsigned int counter = 4, pfi = 7, counter_chan = 0, pfi_chan = 8;
int n = 0, i = 0, nError = 0;
/* open the device */
card = comedi_open("/dev/comedi0");
if ( NULL == card )
{
printf ( "Error opening comedi card: %s\n", comedi_strerror (
comedi_errno() ) );
return -1;
}
/* lock the subdevice */
nError = comedi_lock(card, counter);
if( -1 == nError )
{
/* there was an error locking the device, we can continue but
might be unexpected results */
printf("Error locking subdevice: %s\n",
comedi_strerror(comedi_errno()));
}
/* set up PFI8 for input */
op[0] = COMEDI_INPUT;
insn.insn = INSN_CONFIG;
insn.n = 1;
insn.data = op;
insn.subdev = pfi;
insn.chanspec = CR_PACK ( pfi_chan, 0, 0 );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
for ( i = 0; i < 5; i++ )
{
switch ( i )
{
case 0:
/* reset all counter settings */
op[0] = GPCT_RESET;
n = 1;
break;
case 1:
/* set the source of the counts
* if this is counter 0, the external pin is PFI8/GPCT0_SOURCE
* if this is counter 1, the external pin is PFI3/GPCT1_SOURCE
*/
op[0] = GPCT_SET_SOURCE;
op[1] = GPCT_EXT_PIN; /* set to external pin as above */
/* op[1] = GPCT_INT_CLOCK; */ /* set to the internal clock */
n = 2;
break;
case 2:
/* set the gate to latch the input to
* if this is counter 0, the external latch would be
PFI10/GPCT0_GATE
* if this is counter 1, the external latch would be
PFI4/GPCT1_GATE
*/
op[0] = GPCT_SET_GATE;
op[1] = GPCT_NO_GATE; /* no latch on the gate */
/* op[1] = GPCT_EXT_PIN; */ /* latch to the
external pin as above */
n = 2;
break;
case 3:
/* set the direction the counter should count */
op[0] = GPCT_SET_DIRECTION;
op[1] = GPCT_UP; /* increment counter */
/* op[1] = GPCT_DOWN; */ /* decrement the counter */
n = 2;
break;
case 4:
/* set the type of counter */
op[0] = GPCT_SET_OPERATION;
op[1] = GPCT_SIMPLE_EVENT; /* count the edges of
the pulse */
n = 2;
break;
}
insn.insn = INSN_CONFIG;
insn.n = n;
insn.data = op;
insn.subdev = counter;
insn.chanspec = CR_CHAN ( counter_chan );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
}
for ( i = 0; i < 31; i ++ )
{
/* stop the counter */
insn.insn = INSN_CONFIG;
insn.n = 1;
op[0] = GPCT_DISARM;
insn.data = op;
insn.subdev = counter;
insn.chanspec = CR_CHAN ( counter_chan );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
/* reset the counter to zero */
insn.insn = INSN_WRITE;
insn.n = 1;
op[0] = 0;
insn.data = op;
insn.subdev = counter;
insn.chanspec = CR_CHAN ( counter_chan );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
/* start the counter */
insn.insn = INSN_CONFIG;
insn.n = 1;
op[0] = GPCT_ARM;
insn.data = op;
insn.subdev = counter;
insn.chanspec = CR_CHAN ( counter_chan );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
sleep ( 2 );
/* read the count from the counter */
comedi_data_read ( card, counter, counter_chan, 0, 0, &data );
printf ( "Counter value: %d\n", data );
/* do a raw read */
/*insn.insn = INSN_READ;
insn.n = 1;
insn.subdev = counter;
insn.chanspec = CR_CHAN ( counter_chan );
if ( ( comedi_do_insn ( card, &insn ) ) < 0 )
{
printf ( "Error performing insn: %s\n", comedi_strerror (
comedi_errno() ) );
}
printf ( "Raw Counter value: %d\n", insn.data[0] );*/
}
return 0;
}
<end code snippet>
Chuck
--
Chuck Haines
chaines_at_gmail.com
-------------------------------------------
Tau Kappa Epsilon Fraternity
WPI Class of 2005
-------------------------------------------
AIM: CyberGrex
YIM: CyberGrex_27
ICQ: 3707881
-------------------------------------------
Received on 2004-07-26Z18:14:55