📄 zero_pwr.c
字号:
/*zero_pwr.c*/
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
/* C3* C2 C1* C0* */
#define RESET_VALUE 0X04 /* 0 1 0 0 */
#define START_ADC_CONV 0X0c /* 1 1 0 0 */
#define READ_UPPER_NIBBLE 0X01 /* 0 0 0 1 */
#define READ_LOWER_NIBBLE 0X02 /* 0 0 1 0 */
void main(void)
{
int modem_control_reg, dport_lpt1, cport_lpt1, sport_lpt1;
unsigned char adc_status, adc_val, upper_nib, lower_nib, intr_status;
/* Sign ON */
clrscr();
printf("Zero Power (Well, almost!) ADC for the printer adapter, Version 1.0");
printf("\nD.V.GADRE");
/* Get COM1 register address */
modem_control_reg = peek(0x40,0)+4;
if(modem_control_reg == 4)
{printf("\n\n\nCOM1 not available... aborting\n\n\n");
exit(1);
}
printf("\n\n\nCOM1 address = %X",peek(0x40,0) );
/*Get LPT1 port addresses */
dport_lpt1 = peek(0x40,0x08);
if(dport_lpt1 ==0)
{
printf("\n\n\nLPT1 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 */
/* put power On on COM1 */
outportb(modem_control_reg, 03);
printf("\nPutting Power ON...");
/* check if ADC is connected & working*/
/*start ADC conversion & wait for 1 ms, this puts INTR to logic '0' */
/* reset the control port */
outportb(cport_lpt1, RESET_VALUE);
sleep(1);
/* start conversion */
outportb(cport_lpt1, START_ADC_CONV);
outportb(cport_lpt1, RESET_VALUE );
sleep(1);
/* hopefully the conversion is over, so read the INTR status */
/* if everything is OK, INTR should be '0' */
adc_status = inportb(sport_lpt1) & 0x08;
outportb(cport_lpt1, READ_LOWER_NIBBLE);
outportb(cport_lpt1, RESET_VALUE);
/* read the INTR status again */
/* if everything is OK, INTR should be '1' */
intr_status = inportb(sport_lpt1) & 0x08;
if( !( (adc_status == 0) && (intr_status == 0x08) ) )
{ printf("\n\n\nADC not connected... aborting\n\n\n");
exit(1);
}
/* acquire ADC sample */
while(!kbhit())
{
outportb(cport_lpt1, RESET_VALUE);
start_conv: outportb(cport_lpt1, START_ADC_CONV);
outportb(cport_lpt1, RESET_VALUE);
wait_for_conv: adc_status = inportb(sport_lpt1) & 0x08;
while(adc_status)
{
adc_status = inportb(sport_lpt1) & 0x08;
}
read_upper_nibble: outportb(cport_lpt1, READ_UPPER_NIBBLE);
upper_nib = inportb(sport_lpt1) & 0x0f0;
outportb(cport_lpt1, RESET_VALUE);
read_lower_nibble: outportb(cport_lpt1, READ_LOWER_NIBBLE);
lower_nib = inportb(sport_lpt1) >> 4;
outportb(cport_lpt1, RESET_VALUE);
adc_val = (lower_nib | upper_nib) ^ 0x88;
delay(10);
printf("sample = %X ", adc_val);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -