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

📄 irq_usb.c

📁 usb 低层访问程序,用cy7c64113的IC,用c++编写,不错.
💻 C
字号:
#include "D:\hm1801\hm1801soft\hm1801\header\hm1801.h"


void lpt_send(void);								/* local function prototypes				*/
void usb_send(void);
void usb_receive(char,char);

static char status;
static volatile char read;

#pragma vector = INT4_vect
__interrupt   void usb1_irq(void)

{
	if (system.usb_ctrl&BIT6)						/* in streaming data mode 				*/
		usb_receive(1,USB.UDIN);					/* the irq is the data byte				*/
	else {									/* not in streaming mode				*/
		read   = TRUE;		
		status = USB.STAT;						/* read the status register				*/
		if (status&BIT2)						/* usb has recieved data 				*/
			usb_receive(0,0);
		else if (net.out.ptr && (status&BIT1)) 				/* data to send and USB is ready	 		*/
			usb_send();
		else if (lpt.out.ptr && (status&BIT0))				/* data to send and LPT is ready 			*/
			lpt_send();
	}
}

#pragma vector = INT6_vect
__interrupt void usb2_irq(void)

{
/*	usb2_exception();							 this IRQ currently illegal				*/
}


void usb_receive(char read,char data)

{
	UINT ok;

	ok = 10000;

	if (!read) {
		USB.UDIN = 0;							/* write to data register to start the trasfer		*/
		while(!(PORTB&BIT4) && (--ok));					/* wait for data to be returned				*/
		if (ok)
			data = USB.UDIN;					/* read the data byte					*/
	}

	if (ok) {
		if (data & BIT7) {						/* this is a command byte 				*/
			data &= 0x7F;						/* strip off the command bit 				*/
			if (data) {						/* not a terminator command 				*/
				net.cmd2_in  = data;				/* start of a new command sequence 			*/
				net.in.ptr   = net.in_buf;			/* reset the data pointer 				*/
				net.in.bytes = 0;				/* and the byte counter 				*/
			}
			else {							/* a Terminator (CSM-101?) 				*/
				*net.in.ptr  = 0x00;				/* NULL string needed by some commands 			*/
				set_process(4000);				/* process the command 					*/
			}
		}
		else if (net.in.ptr) {						/* just a data byte 					*/
			*net.in.ptr++    = data;				/* put it in the buffer 				*/
			++net.in.bytes;						/* increment counter 					*/
		}
	}
}


void usb_send(void)

{
	if (!net.out.bytes) {							/* no more bytes to send				*/
		net.out.ptr      = NULL;					/* clear the pointer					*/
		system.usb_ctrl &= ~BIT1;					/* turn off USB xmit IRQ				*/
		system.usb_ctrl |=  BIT3;					/* set last byte of message flag			*/
		USB.CTRL         = system.usb_ctrl;				/* write the control word				*/
	}
	else {									/* still send data					*/
		USB.UDOUT = *net.out.ptr++;
		--net.out.bytes;						/* decrement the counter				*/
	}
}


void lpt_send(void)

{
	if (!lpt.out.bytes)	{						/* nothing more to print				*/
		lpt.out.ptr	 = NULL;					/* pointer to NULL to show done				*/
		system.usb_ctrl &= ~BIT0;					/* turn off LPT IRQs					*/
		USB.CTRL         = system.usb_ctrl;
	}
	else {									/* more to send						*/
		if (*lpt.out.ptr == '\n')					/* end of line detected					*/
			++line_count;						/* increment the line count				*/
		USB.PDOUT = *lpt.out.ptr++;					/* write the byte					*/
		--lpt.out.bytes;						/* decrement byte counter				*/
	}
}


char usb_ctrl_wait(void)
{	

	read     = FALSE;
	USB.CTRL = system.usb_ctrl;						/* write the control word				*/
	system.usb_tmr=3;
	while( (!read) && (system.usb_tmr) )					/* wait for a response from USB controller		*/
	{	WDI;
	}					

	return(status);								/* return the status word				*/
}


⌨️ 快捷键说明

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