⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad_test.c

📁 a/d驱动实验与d/a驱动实验 基于清华大学的TEB-44B0实验平台
💻 C
字号:
#include <stdio.h>#include <errno.h>#include <sys/select.h>#include <sys/time.h>#define DEVICE_FILE_NAME "/dev/ad0809"#define BUF_N	100struct timeval my_timeout;int main(int argc, char ** argv){ 	int channel;	int fd;	int isTSopen = 0; 	fd_set ReadFd; 	unsigned char value[BUF_N];        int result, i;		if(argc <=1)	{	 printf("Useage: ad_test <channel>\n");	 exit(0);	}		channel = atoi(argv[1]);	if((channel < 0) || (channel > 15))	{	 printf("channel set error! Set channel 0-7.\n");	 exit(1);	}		fd = open(DEVICE_FILE_NAME, 0);	if(fd < 0)	{	 printf("Can't open device file: %s!\n", DEVICE_FILE_NAME);	 exit(2);	}	else {	 printf("Device %s open -> It works!\n", DEVICE_FILE_NAME);	 isTSopen = 1;	}		//result = ioctl(DEVICE_FILE_NAME, fd, 0, channel);	result = ioctl(fd, 0, channel);	if(result < 0)	{	 printf("channel set error!\n");	 //return errno;	}		while(isTSopen)	{ 	 FD_ZERO(&ReadFd);	 FD_SET(fd, &ReadFd);	 		 my_timeout.tv_sec = 0;	 my_timeout.tv_usec = 200000;   //200ms	 	 result = select(fd + 1, &ReadFd, NULL, NULL, &my_timeout);//	 result = select(fd, &ReadFd, NULL, NULL, &my_timeout);	 if(result > 0)	 {	  if(FD_ISSET(fd, &ReadFd)) 	  {	  	   result = read(fd, value, sizeof(value));	   if(result < 0)	     continue;	   for(i = 0; i < result; i++)	   {	    printf("A/D convert value[%d] = 0x%02x\n", i, value[i]);	   }	   break;	   	  }	    	 }	 	}	return 0;	     }	 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -