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

📄 sadc_ip.c

📁 这是《并行口编程指南》的附盘的源代码 很有参考价值哦
💻 C
字号:
/*sadc_ip.c*/
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include<time.h>

void main(void)
{
int dport_lpt1, cport_lpt1, sport_lpt1, del;
unsigned char adc_status, adc_val, cport, chan_num;
unsigned char nib_0, nib_1, nib_2, nib_3, temp, nibble[5];

/* Sign ON */
clrscr();
printf("AT89C2051 based serial ADC adapter for the printer port, Version 1.0");
printf("\nD.V.GADRE");

/*Get LPT1 port addresses */
dport_lpt1 = peek(0x40,0x08);
if(dport_lpt1 ==0)
	{
	printf("\n\n\nLPT! not available... aborting\n\n\n");
	exit(1);
	}
printf("\nLPT1 address = %X", dport_lpt1);
cport_lpt1 = dport_lpt1 +2; 	/* control port address */
sport_lpt1 = dport_lpt1 + 1;	/* status port address */

/*initialize the ADC strobe signals*/
cport=04;
outportb(cport_lpt1, cport);

/*clear the status of controller*/
/*this generates a low going pulse on C1* pin of the control port*/
cport=cport | 02;
outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);


/* check if ADC is connected & working*/
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;

if(adc_status == 0) printf("\nADC connected\n");
chan_num=0; /*set channel number*/
for(;;)
{
/*set channel number to the controller*/
outportb(dport_lpt1, chan_num);

/*trigger the controller to read the channel number*/
cport=cport | 01;
outportb(cport_lpt1, cport);

/*wait till it reads the channel number and completes conversion*/
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;
while(adc_status ==0)
{
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;
}
/*remove the trigger pulse. take it igh to remove the trigger*/
cport=cport & 0xfe;
outportb(cport_lpt1, cport);

/*clear the status of controller*/
cport=cport | 02;
outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);

/*now read the converted data*/
for(temp=8; temp<11; temp++)

	{
/*set data port to read nibble 0, 1 and 2 in that order*/
		outportb(dport_lpt1, temp);

/*trigger the converter to perform the read nibble process*/
			cport=cport | 01;
			outportb(cport_lpt1, cport);
/*wait till it completes the task*/
		adc_status=inportb(sport_lpt1);
		adc_status=adc_status & 0x08;
		while(adc_status ==0)
		{
		adc_status=inportb(sport_lpt1);
		adc_status=adc_status & 0x08;
		}

/*remove the trigger pulse*/
			cport=cport & 0xfe;
			outportb(cport_lpt1, cport);

/*clear the status of controller*/
cport=cport | 02;
outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);

/*read the nibble and store it temporarily*/
	adc_val=inportb(sport_lpt1);
	nibble[temp-8]=(adc_val ^ 0x80) >> 4;
	}
/*construct the full 12 bit number from the stored nibbles*/
	printf("\n %d mV", nibble[0] + 16*nibble[1] + 256*nibble[2]);

sleep(1); /*sleep for 1 sec*/
}


}

⌨️ 快捷键说明

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