📄 counter.c
字号:
//*************************************************************************
//
// Copyright (c) 2001 IAG Software Team,
//
// Beijing R&D Center
//
// Advantech Co., Ltd.
//
// User program for Linux comedi device driver
//
// This is An Unpublished Work Containing Confidential And Proprietary
// Information Which Is The Property Of Advantech Automation Corp.
//
// Any Disclosure, Use, Or Reproduction, Without Written Authorization From
// Advantech Automation Corp., Is Strictly Prohibit.
//
//
//*************************************************************************
// Program : counter.c
// Revision: 1.0
// Author : Edwin du
// Date : 03/20/03
//
// Description: Perform counter of comedi driver with insn mode.
// Here the command register value are generated through
// user selections, deciding the counter channel, counter mode
// overflow/underflow lock bit setting, and 7 latch modes.
//
//-------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <comedilib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#define N_SAMPLE 2
char *filename="/dev/comedi0";
int verbose_flag;
comedi_t *device;
int subdevice = 6;
int channel = 0;
int aref;
int range = 0;
int main(void)
{
lsampl_t data[N_SAMPLE];
int save_errno;
int ret, i;
int DigitalFilter = 0;
unsigned int UserInput;
long CounterReg = 0,TimerReg = 0;
comedi_insn insn;
//data[0] = 0x104; // command register
//data[1] = 1000000; // compare register
printf("************** counter.c****************\n");
printf("\n Comedi driver test program\n\n");
printf("****************************************\n");
device = comedi_open(filename);
if(!device){
printf("E: comedi_open(\"%s\"): %s\n",filename,strerror(errno));
}
memset(&insn,0,sizeof(insn));
// Acquire counter channel
while (1)
{
printf("Counter channel: (0--3)\t");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput >= 0x30 && UserInput <=0x33)
{
channel = UserInput - '0';
break;
}else{
printf("\nInvalid Channel, please reinput!\n\n");
}
}
// Acquire counter mode control:
while (1)
{
printf("\nCounter input mode control:\n");
printf("1. Quadrature input x1\n2. Quadrature input x2\n");
printf("3. Quadrature input x4\n4. 2 pulse input\n5. 1 pulse input\n");
printf("Please select one:\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput > 0x30 && UserInput < 0x36)
{
//CounterMode = UserInput - '0';
CounterReg |= UserInput - '0';
break;
}else{
printf("\nINVALID SELECTION, PLEASE RESELECT!\n\n");
}
}
// Confirm digital filter
while (1)
{
printf("\nEnable digital filter?(y/N)\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput == 'Y' || UserInput == 'y')
{
DigitalFilter = 1;
CounterReg |= 0x40;
break;
}else if (UserInput == 'N' || UserInput == 'n')
{
CounterReg &= 0xbf; // ~0x40
break;
}else
break;
}
insn.insn = INSN_WRITE;
insn.n = N_SAMPLE;
insn.data = data;
if (DigitalFilter) // select timer
{
printf("\n\nSampling clock select:\n");
printf("1. 8 MHz\n2. 4 MHz\n");
printf("3. 2 MHz\n4. 1 MHz\n");
if((UserInput = getchar()) != '\n')
{
switch (UserInput)
{
case 1:
TimerReg |= 0x0;
break;
case 2:
TimerReg |= 0x1;
break;
case 3:
TimerReg |= 0x2;
break;
case 4:
TimerReg |= 0x3;
break;
default:
break;
}
}
data[0] = TimerReg;
insn.subdev = 8; // timer subdevice
ret = comedi_do_insn(device, &insn); // set timer
if(ret<0){
comedi_perror(filename);
exit(0);
}
}
getchar();
// Confirm Overflow Lock bit
while (1)
{
printf("\nEnable overflow lock?(y/N)\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput == 'Y' || UserInput == 'y')
{
CounterReg |= 0x10;
break;
}else if (UserInput == 'N' || UserInput == 'n')
{
CounterReg &= 0xef; // ~0x10
break;
}else
break;
}
// Confirm Underflow Lock bit
while (1)
{
printf("\nEnable underflow lock?(y/N)\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput == 'Y' || UserInput == 'y')
{
CounterReg |= 0x20;
break;
}else if (UserInput == 'N' || UserInput == 'n')
{
CounterReg &= 0xdf; // ~0x20
break;
}else
break;
}
// Acquire Latch bits
while (1)
{
printf("\nPlease select Latching modes:(multiple)\n");
printf("1. Software Latch\n2. Index Latch\n3. Timer Latch\n");
printf("4. DI0 Latch\n5. DI1 Latch\n6. DI2 Latch\n7. DI3 Latch\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput > 0x30 && UserInput < 0x38)
{
switch (UserInput)
{
case 0x31:
CounterReg |= 0x100;
break;
case 0x32:
CounterReg |= 0x200;
break;
case 0x33:
CounterReg |= 0x400;
break;
case 0x34:
CounterReg |= 0x1000;
break;
case 0x35:
CounterReg |= 0x2000;
break;
case 0x36:
CounterReg |= 0x4000;
break;
case 0x37:
CounterReg |= 0x8000;
default:
break;
}
break;
}else{
printf("\nINVALID SELECTION, PLEASE RESELECT!\n\n");
}
}
data[0] = CounterReg; // Combined ConterReg is given to data[0]
data[1] = 0;
insn.subdev = subdevice;
insn.insn = INSN_WRITE;
insn.n = N_SAMPLE;
insn.chanspec = CR_PACK(channel, range, 0);
insn.data = data;
ret = comedi_do_insn(device,&insn);
save_errno = errno;
if(ret<0){
printf("W: comedi_do_insn: errno=%d %s\n",save_errno,strerror(save_errno));
}
insn.insn = INSN_READ;
printf("Counter Read Back:\n");
for (i=0; i<100; i++) {
ret = comedi_do_insn(device, &insn);
printf("Latch register=0x%x\n", data[0]);
sleep(1);
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -