📄 max111.c
字号:
/*max111.c*/
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include <conio.h>
#include <process.h>
/* Interface for 14 bit MAX111 */
/*
Printer adapter pin usage
-------------------------
D7 (9) = Din
D0 (2) = SCLK
C1* (14)= CS*
S7* (11)= Dout
S6 (10)= BUSY
*/
#define TRUE 1
#define FALSE 0
/* Global variable has address of DATA port of LPT1 */
unsigned int dport_lpt1; /* data port address*/
int chk_lpt(void); /*Check if LPT1 is present*
void disble_adc(void); /*Pull ADC CS* high to disable ADC*/
void enable_adc(void); /*Pull ADC CS* low to enable ADC*/
void chk_adc_status(void); /*Check ADC for End of Conversion*/
unsigned int read_adc(unsigned int prog_word);
unsigned int read_adc(unsigned int prog_word)
{
unsigned char temp2, temp1, temp3;
unsigned int temp_val, adc_val, out_val;
out_val=prog_word;
chk_adc_status();
enable_adc();
adc_val=0;
for(temp2=0; temp2<16; temp2++)
{
temp1=inportb(dport_lpt1);
temp1=temp1 & 0x7e;
temp_val=out_val << temp2;
temp_val=temp_val >> 8;
temp_val=temp_val & 0x0080;
temp1=temp1 | (unsigned char)temp_val;
outportb(dport_lpt1, temp1); /*send data*/
temp1=temp1 | 0x01;
outportb(dport_lpt1, temp1); /*send Sclk*/
temp3=inportb(dport_lpt1+1) & 0x80; /*read data*/
temp3=temp3 ^ 0x80;
temp_val=(unsigned int)temp3;
temp_val=temp_val<<8;
temp_val=temp_val & 0x8000;
temp_val=temp_val>>temp2;
adc_val=temp_val | adc_val;
temp1=temp1 & 0xfe;
outportb(dport_lpt1, temp1);
}
outportb(dport_lpt1+2, (inportb(dport_lpt1+2) & 0xfd) );
return adc_val;
}
void chk_adc_status(void)
{
unsigned char tempa;
tempa=inportb( dport_lpt1+1);
tempa=tempa & 0x40;
while(!tempa)
{
tempa=inportb(dport_lpt1+1);
tempa=tempa & 0x40;
}
}
void disable_adc(void)
{
unsigned char tempx;
tempx=inportb(dport_lpt1+2);
tempx=tempx & 0xfd;
outportb(dport_lpt1+2, tempx);
}
void enable_adc(void)
{
unsigned char tempy;
tempy=inportb(dport_lpt1+2);
tempy=tempy | 0x2;
outportb(dport_lpt1+2, tempy);
}
int chk_lpt(void)
{
/*Get LPT1 port addresses */
dport_lpt1 = peek(0x40,0x08);
if(dport_lpt1 == 0)
return FALSE;
/* else return TRUE */
return TRUE;
}
main()
{
float final_vol;
unsigned int adc_val;
unsigned char temp1;
clrscr();
/*Check if Printer port is present*/
if( chk_lpt() == FALSE)
{printf("\nPrinter port not available... aborting"); exit(1);}
printf("\nPrinter port is at %X hex", dport_lpt1);
/*Disable the ADC*/
disable_adc();
/*set clock and data low*/
temp1=inportb(dport_lpt1);
temp1=temp1 & 0x7e;
outportb(dport_lpt1, temp1);
printf("\nWaiting for the ADC to be ready...");
chk_adc_status();
printf("\nADC is ready for conversion...");
/*perform offset correction for channel 1 */
adc_val=read_adc(0x8c8c);
/*perform gain claibration*/
adc_val=read_adc(0x8c88);
/*perform offset null*/
adc_val=read_adc(0x8c84);
/*Now convert indefinately on channel 1*/
for(;;)
{
adc_val=read_adc(0x8c80);
adc_val=adc_val & 0x3fff;
final_vol=(float)adc_val;
final_vol=(final_vol/16384)*1.2235; /*Vref=1.2235V*/
printf("\nADC Value=%2.4f Volts", final_vol);
delay(1000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -