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

📄 rd_dip.c

📁 _计算实用教程Visual C++6.0实用教程
💻 C
字号:
/*rd_dip.c*/
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>

/* This programme allows the computer to read a set of DIP switches
   These switches are connected to corresponding shift registers,
   which convert their parallel data into serial .
   The serial data is read at the printer (parallel) port of the PC
   The MSB of the printer Status port (S7*) is used as the input data bit
									*/

						/* D3  D2 D1  D0  */
#define RESET_VALUE		0XFF		/* 1   1  1   1   */
#define GEN_LOAD		0XFE		/* 1   1  1   0   */
#define GEN_CLOCK		0XFD		/* 1   1  0   1   */

#define DIP_SW 3        /*total number of DIP switches*/
#define BITS_SW 8       /*bits in 1 DIP switch */

void main(void)
{
int dport_lpt1, sport_lpt1, shift_reg[6], temp, in_count, out_count,
columns, rows;

/* Sign ON */
clrscr();
printf("DIP Switch reading program.");
printf("\nReads the Shift register connected to Printer Control & Status Port");
printf("\nLOAD ---> D0.");
printf("\nSHIFT --> D1.");
printf("\nDATA ---> S7.");
printf("\n\n\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("\n\n\nLPT1 address = %X\n\n", dport_lpt1);
sport_lpt1 = dport_lpt1 + 1;	/* status port address */


/* Initialize the Printer DATA Port*/
outportb(dport_lpt1, RESET_VALUE);

/* kbhit to see of keyboard is hit */
/* come out of loop, if so*/
while (!kbhit())
	{
	/* Generate LOAD signal on D0 bit for the shift registers */

	outportb(dport_lpt1, GEN_LOAD);
	outportb(dport_lpt1, RESET_VALUE);

	for(out_count=0; out_count<DIP_SW; out_count++)
        /* number of DIP Switches */
		{
	for(in_count=0; in_count<BITS_SW; in_count++)
        /* number of bits / Switch */
		{

		/* Read the Status Port bit S7*/

		temp = inportb(sport_lpt1) ;
		temp = temp ^ 0x80; 		/*invert bit no. 7*/
		temp = temp >> 7;    /*shift it to the lower most position*/
		temp = temp & 0x01;  /*mask all bits except the last bit*/

		/* Concatenate it in the variable */
	        shift_reg[out_count] = shift_reg[out_count] << 1;
                /* shift the bit one left*/

                shift_reg[out_count] = shift_reg[out_count] & 0xfffe;
                /* make the LSB 0*/

		shift_reg[out_count] = shift_reg[out_count] | temp;
                /* place the new bit in the LSB*/

		shift_reg[out_count]=shift_reg[out_count] & 0xff;
                /* keep only the lowest 8 bits*/

		outportb(dport_lpt1, GEN_CLOCK); /* clock for the next bit*/
		outportb(dport_lpt1, RESET_VALUE);


		}
printf("Switch %d = %X\n", out_count, shift_reg[out_count]);
		}
printf("\n");
delay(500); /*wait for .5 seconds*/
	 }

/* Repeat loop */
}

⌨️ 快捷键说明

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