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

📄 dio.c

📁 _计算实用教程Visual C++6.0实用教程
💻 C
字号:
/*dio.c*/
/*8-bit analog and digital I/O program*/
/*uses MAX158 ADC*/
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>

/* used for the CONTROL port with C3* held high*/
/*C2, C1* and C0* are used as the inputs of a 3-8 decoder*/
		    /* C3*  C2   C1*  C0* */
#define LLL 3       /*  0   0    1    1   */
#define LLH 2       /*  0   0    1    0   */
#define LHL 1       /*  0   0    0    1   */
#define LHH 0       /*  0   0    0    0   */
#define HLL 7       /*  0   1    1    1   */
#define HLH 6       /*  0   1    1    0   */
#define HHL 5       /*  0   1    0    1   */
#define HHH 4       /*  0   1    0    0   */


#define WAIT_TIME 100000

unsigned int dport, cport, sport;



int chk_adc(void)
{
unsigned char int_stat;
/* check if ADC is connected & working*/
/*put RD* and CS* to high. INT* should be high*/
outportb(cport, LLH);
outportb(cport, HHH);
delay(10);
int_stat=inportb(sport);
int_stat=int_stat & 0x08;
/*printf("\nStat=%x", int_stat);*/
if(int_stat != 0x08) return 0; /* ADC is not connected*/

outportb(cport, LLL); /*trigger ADC conversion*/
outportb(cport, HHH);
delay(10);
int_stat=inportb(sport);
int_stat=int_stat & 0x08;
/*printf("\nStat=%x", int_stat);*/
if(int_stat != 0) return 0;

outportb(cport, LLH);
/*else just complete the readout and return success*/
outportb(cport, HHH);
return 1;

}

int adc_convert(unsigned char chan_num, unsigned char *value_ptr)
{
unsigned char low_nib, high_nib, stat;
long timeout=0;

timeout=0;
chan_num=chan_num & 0x07;
outportb(dport, chan_num);

outportb(cport, LLL); /*trigger ADC conversion*/

stat= ( inportb(sport) & 0x08); /*wait till conversion over*/


while (stat == 0x08)
{
if(timeout > WAIT_TIME)return 0; /*return for timeout*/
stat= ( inportb(sport) & 0x08);
	  timeout++;
}

low_nib=inportb(sport);
outportb(cport, LLH);
high_nib=inportb(sport);
outportb(cport, HHH);

*value_ptr = ( (low_nib >> 4) & 0x0f) | (high_nib & 0xf0);
*value_ptr= *value_ptr ^ 0x88;
return 1;

}

void read_digital_ip(unsigned char *digital_ip)
{
unsigned char low, high;

outportb(cport, HLL);
low=inportb(sport);
outportb(cport, HLH);
high=inportb(sport);

outportb(cport, HHH);

*digital_ip= ( (low>>4) & 0x0f) | (high & 0xf0);
*digital_ip=*digital_ip ^ 0x88;
return;
}

void dout_port2(unsigned char value)
{
outportb(dport, value);
outportb(cport, LHL);
outportb(cport, HHH);
return;
}

void dac_out(unsigned char value)
{
outportb(dport, value);
outportb(cport, LHH);
outportb(cport, HHH);
return;
}


main()
{
unsigned char adc_status, adc_val, upper_nib, lower_nib, intr_status;
unsigned char result, channel=0;
float voltage;


/* Sign ON */
clrscr();
printf("Fast, 8-bit, 8 channel ADC interface the printer adapter");
printf("\nD.V.GADRE");

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

outportb(cport, 0x04); /*Init the control port to all 1's*/
outportb(cport, 0x02); /*pulse Y1 low of the decoder*/
outportb(cport, 0x04);




if( chk_adc() == 0)
{ printf("\n\n\nADC interface not connected or not powered... aborting\n\n\n");
exit(1);
}



/* acquire ADC sample */
while(!kbhit())
	{
if(adc_convert(channel, &result) == 0)
{printf("\nError in ADC Conversion, channel %d. Aborting..", channel);
exit(1);
}
	printf("\nChannel %X, Voltage = %1.2f Volts", channel, 0.01 * (float)result);
	sleep(1);
	}

/*Now reading data from the 8 bit digital i/p channel*/

while(!kbhit())
	{
read_digital_ip(&result);
printf("\nDigital I/P = %X (hex)", result);
	sleep(1);
	}


/*Now writing data to the 8 bit digital o/p channel 2*/
while(!kbhit())
	{
dout_port2(result++);
	sleep(1);
	}

/*Now writing data to the 8 bit digital o/p connected to a DAC*/
while(!kbhit())
	{
dac_out(result++);
	sleep(1);
	}



return 1;

}

⌨️ 快捷键说明

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