📄 cntrst.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 : cntrst.c
// Revision: 1.0
// Author : Edwin du
// Date : 03/20/03
//
// Description: Reset counter value. first read back counter states,
// then get user's command of which value to be reset,
// then reset the counters
//-------------------------------------------------------------------------
#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 = 1;
int aref;
int range = 0;
int main(void)
{
lsampl_t data[N_SAMPLE];
int save_errno;
int ret;
// unsigned int CounterMode, filter;
unsigned int UserInput, status;
long CounterReg = 0;
comedi_insn insn;
printf("************** cntrst.c****************\n");
printf("\n Please Set Counter Mode First! \n");
printf("\n Comedi driver test program\n\n");
printf("****************************************\n");
// 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 reset value:
while (1)
{
printf("\nWhich value would be reset:\n");
printf("1. 80000000\n2. 00000000\n");
printf("Please select one:\n");
if ((UserInput = getchar()) != '\n')
while(!getchar()); // read '\n'
if (UserInput > 0x30 && UserInput < 0x33)
{
CounterReg = UserInput - '0';
break;
}else{
printf("\nINVALID SELECTION, PLEASE RESELECT!\n\n");
}
}
device = comedi_open(filename);
if(!device){
printf("E: comedi_open(\"%s\"): %s\n",filename,strerror(errno));
}
memset(&insn,0,sizeof(insn));
// Read Back :
insn.subdev = subdevice;
insn.insn = INSN_READ; // first read back states value
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));
}
status = data[1];
if (!status)
{
printf("Please Set Counter Mode First!!\n");
printf("Using default counter mode:\n");
printf("2 pulse input, Softwarelatch\n");
// return 0;
status = 0x104;
}
switch (CounterReg)
{
case 1:
status &= 0xffef;
break;
case 2:
status |= 0x10;
break;
default:
break;
}
// Write command register:
insn.insn = INSN_WRITE;
insn.chanspec = CR_PACK(channel, range, 0);
data[0] = status; // write command mode
data[1] = 1<<channel; // reset channel
ret = comedi_do_insn(device, &insn);
insn.insn = INSN_READ;
ret = comedi_do_insn(device, &insn);
if(ret<0){
comedi_perror(filename);
exit(0);
}
printf("Counter %d latch value:0x%x\n", channel, data[0]);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -