📄 adtdemo.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/times.h>
#include <signal.h>
#include "adt600.h"
#define ADT_BASE 0x300 /*ADT600-1 base address */
#define ADT_IRQ 7 /*ADT600-1 interrupts number */
#define ADT_VOL_RANGE 10 /* A/D voltage range */
/*
signal handler.
when interrupt coming ,the driver will send a signal SIGIO
we catch it and excute this function
*/
void input_handler(int signo)
{
unsigned char irqstatus=0;
float vol;
unsigned char ch;
int channel, ADValue;
irqstatus=inb(BaseAddress+CLEAR_INT);
if ((signo==SIGIO)&&(test_bit(0,irqstatus))) {
printf("\n*****************************************************\n");
printf("waiting conversion....\n");
for(channel=0; channel<8; channel++)
{
SetChannel(channel);
usleep(100000);
StartConversion();
while (ConversionDone()==0);
ADValue=ReadData();
vol=DigitalToSBS(ADValue);
printf("Channel%d VOL is %6.2f\n", channel, vol);
//outb_p(irqstatus&0xfc,BaseAddress+CLEAR_INT);
}
}
}
/* open devices file and enable interrupt */
int open_irq(void (*func)(int))
{
int result;
int fd;
SetIRQStatus(DISABLED);
fd=open("/dev/ADT600/device0",O_RDWR);
if (fd<0) {
printf("open failed\n");
return fd;
}
/* set current process as the owner of the device /dev/adt600 */
result=fcntl(fd,F_SETOWN,getpid());
if (result==-1) {
printf("register failed\n");
return result;
}
/*set async I/O */
result=fcntl(fd,F_SETFL, FASYNC|fcntl(fd,F_GETFL));
if (result==-1) {
printf("register failed\n");
return result;
}
signal(SIGIO,func);
outb_p(0,BaseAddress+CLEAR_INT);
SetIRQStatus(ENABLED);
return fd;
}
int close_irq(int fd)
{
SetIRQStatus(DISABLED);
if (close(fd)) {
printf("close failed\n");
return -1;
}
return 0;
}
int main(void)
{
int fd;
int i;
unsigned char ch;
clock_t clock,clock1;
struct tms t;
/* get rights to access I/O port */
if (iopl(3)) {
printf("forbid access io ");
exit(-1);
}
InitializeBoardSettings(ADT_BASE,ADT_VOL_RANGE,UNIPOLAR/*BIPOLAR*/);
ResetBoard();
/*select channel 1 as A/D */
//SetChannel(0);
/*set frequence of interrupts */
SetUserClock(1);
fd=open_irq(&input_handler);
if (fd<0) {
printf("ADT600 is running or open device failed \n");
exit(-1);
}
//StartConversion();
/*waitting conversion finished */
printf("******************************************\n");
printf("waiting conversion.... ,please wait \n");
printf("press any key to quit \n");
getchar();
SetIRQStatus(DISABLED);
close_irq(fd);
iopl(0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -