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

📄 dsp_pc.c

📁 _计算实用教程Visual C++6.0实用教程
💻 C
字号:
/* Program to connect the PC through the printer adapter to the
DSP based CCD controller.*/
/*dsp_pc.c*/


#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<time.h>

/* external variables */
extern unsigned dport, sport, cport;

/* external routines. gets the addresses of the 3 ports from the DOS
data RAM  */
extern void set_lpt_base_address(int);

/* status port */
#define pin_11	0x80
#define pin_10	0x40
#define pin_12	0x20
#define pin_13	0x10
#define pin_32	0x08

/* control port */
#define pin_17	0x08
#define pin_16	0x04
#define pin_14	0x02
#define pin_1	0x01

/* op & ip semaphores */


#define ip_buffer_flag 0x04
#define ip_buffer_Flag 0xfb
/* this flag is on bit 2 (pin 16 ) of the control port
and can set by a logic low on the pin 16*/

#define op_latch_flag 0x08
#define op_latch_Flag 0xf7
/* this flag is set by pulsing a low on pin 17 (bit 3)
of the control port. SET condition of this flag indicates
that the oplatch contains a new byte  */


/* local routines */
unsigned char txstat(void); 
/* check to see if the o/p latch is empty; empty=0 */

unsigned char rxstat(void); 
/* check to see if the i/p buffer has any char; empty=0 */


void tx(unsigned char);	/* transmit the char to the latch */

unsigned char  rx(void);/* receive a char from the buffer */

void enable_nibble(unsigned char); 
/* this function controls which nibble gets connected to 
the status port pins */


/* txstat: This routines checks pin_13 of printer adapter status port
if the PIN is SET, the o/p latch is full & should not be written to
again. When the DSP reads the latch, the PIN is RESET. Now the latch
can be written to again */
/* return value: 1 is latch full
		 0 if latch empty
*/

unsigned char txstat(void)
{
char latch_status;
enable_nibble(1); /* this function connects the sport to nibble 1*/
latch_status=inportb(sport) & pin_13;
return latch_status;
}



/* rxstat: this function checks pin_12 of the status port. If the PIN is
set, the buffer is full & should be read. if RESET, it is empty. */

/* return value:	0 if the buffer is empty
			1 if the buffer is full
*/

unsigned char rxstat(void)
{
char buffer_status;

enable_nibble(1); /* this function connects the sport to nibble 1*/

buffer_status=inportb(sport) & pin_12;
return buffer_status;

}
/* tx: This routine latches a byte into the o/p latch */
/* return value: none */

void tx(unsigned char op_byte)
{
unsigned char temp;

outportb(dport, op_byte);  /* latch the byte*/

/*
 now set up the op_latch_flag to indicate that a new byte is
available
*/
temp=inportb(cport) & (0xff ^ op_latch_flag);

temp=temp ^ op_latch_flag;
outportb(cport, temp);
temp=temp ^ op_latch_flag;
temp=temp | op_latch_flag;
temp=temp ^ op_latch_flag;

outportb(cport, temp);

return;
}

/* rx: This routine reads the i/p 8 bit buffer */
/* return value: the byte read from the buffer */

unsigned char rx(void)
{
unsigned char ip_byte, temp;

enable_nibble(3); /* set the buffer to read the lower nibble */

temp=inportb(sport);

temp=temp >> 4;

enable_nibble(2); /* set up the buffer to read upper nibble */

ip_byte=inportb(sport);

ip_byte = ip_byte & 0xf0; /* reset lower 4 bits */
ip_byte=0x88 ^ (ip_byte | temp); /* concatenate the 2 nibbles & flip
7th & 3rd bit */

/* now reset the flag to indicate that the byte has been read */
temp=inportb(cport) & (0xff ^ ip_buffer_flag);
outportb(cport, temp);
temp = temp | ip_buffer_flag;
outportb(cport, temp);

return ip_byte; /* return the converted byte */
}


void enable_nibble(unsigned char nibble_number)
{
unsigned char cport_status;
cport_status=( inportb(cport) & 0xfc) ;  /* isolate bit 0 & 1*/
nibble_number = nibble_number & 0x03;
nibble_number = 0x03 ^ nibble_number;  /* invert bit 0 & 1 */
cport_status=cport_status | nibble_number;
outportb(cport, cport_status);
return;
}

main()
{
unsigned long count;
unsigned char portval, tempp, tempq;
time_t t1,t2;
FILE *fp1;


int temp=1;

clrscr();

printf("\n\nFinding Printer adapter lpt%d...", temp);

set_lpt_base_address(temp);
if(dport == 0) {printf("\nPrinter adapter lpt%d not installed...", temp); exit(0); }
else
{
printf("found. Base address: %xhex", dport);
portval=inportb(sport);

printf("\n\n                     D7  D6  D5  D4  D3  D2  D1  D0");
printf("\nStatus  port value = %x   %x   %x   %x   %x   X   X   X ", \
(portval & pin_11)>>7, (portval & pin_10)>>6, (portval & pin_12)>>5, \
(portval & pin_13)>>4, (portval & pin_32)>>3 );

portval=inportb(cport);

printf("\nControl port value = X   X   X   X   %X   %X   %X   %X ", \
(portval & pin_17)>>3, (portval & pin_16)>>2, (portval & pin_14)>>1, \
(portval & pin_1) );


portval=inportb(dport);
printf("\nData    port value = %X   %X   %X   %X   %X   %X   %X   %X ", \
(portval & 0x80)>>7, (portval & 0x40)>>6, (portval & 0x20)>>5, \
(portval & 0x10)>>4, (portval & 0x08)>>3, (portval & 0x04)>>2, \
(portval & 0x02)>>1, portval & 0x01  );
printf("\n\n\n");
}

/* set up reset states on the control port, all pins to logic 1 */
outportb(cport,0x04);

fp1=fopen("tx_rx", "w");


t1=time(NULL); /* just to log time */

for (count=0;count<256;count++)
{

while (!txstat()); 
/* wait till the DSP application reads the previous byte*/

tx(tempp); /* transmit a byte*/


while(rxstat()); 
/* wait till a byte is transmitted by the DSP */

tempq=rx(); 
/* byte is available, read it */

fprintf(fp1, "TX=%x, RX=%x\n ", tempp, tempq); /* store it in a file */

tempp=tempp++;

}
fclose(fp1);

t2=time(NULL);

printf("time taken = %ld secs", t2-t1);


return 1;

}

⌨️ 快捷键说明

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