請(qǐng)大家?guī)臀铱纯催@段程序,主要功能是完成372芯片對(duì)數(shù)據(jù)的接受和發(fā)送,本人是初學(xué)者,畢業(yè)設(shè)計(jì)師這方面的題目,希望大家多多指導(dǎo)。謝謝大家!
#include #include typedef unsigned char *PUINT8; typedef unsigned char UINT8; typedef unsigned char volatile xdata IOPORT; IOPORT CH372_CMD_PORT _at_ 0x9001; /*命令口地址*/ IOPORT CH372_DAT_PORT _at_ 0x9000; /*數(shù)據(jù)口地址*/
void Delay( ) /*延時(shí)程序*/ { UINT8 i; for ( i=3; i>0; i-- );
}
void CH372_WR_CMD_PORT(UINT8 cmd) /*向命令口寫(xiě)入命令(cmd=15h)*/ { Delay(); CH372_CMD_PORT=cmd; Delay(); } void CH372_WR_DAT_PORT(UINT8 dat) /*向數(shù)據(jù)口寫(xiě)入數(shù)據(jù)*/ { CH372_DAT_PORT=dat; Delay(); } UINT8 CH372_RD_DAT_PORT() /*讀取數(shù)據(jù)*/ { Delay(); return(CH372_DAT_PORT); }
void CH372_INIT() /*372初始化*/ { UINT8 i; CH372_WR_CMD_PORT(CMD_SET_USB_MODE); /*設(shè)置USB工作模式*/ CH372_WR_DAT_PORT(2); /*設(shè)置USB為內(nèi)置固件方式*/ for(i=100;i>0;i--) if(CH372_RD_DAT_PORT()==CMD_RET_SUCCESS) /*等待操作成功*/ break; IT1=1; /*外部中斷1低電平有效*/ IE1=0; /*清中斷標(biāo)志*/ EX1=1; /*允許372中斷(372已經(jīng)連接到單片機(jī)的INT1腳)*/ }
UINT8 UsbLength; /*USB緩沖區(qū)長(zhǎng)度*/ UINT8 UsbBuffer[CH372_MAX_DATA_LEN]; /*USB緩沖區(qū)*/
/* CH372中斷服務(wù)程序,假定CH372的INT#引腳連接到單片機(jī)的INT1,使用寄存器組1 */ void CH372Interrupt() interrupt 2 using 1 { PUINT8 buf; UINT8 cnt; UINT8 IntStatus; CH372_WR_CMD_PORT(CMD_GET_STATUS); /*獲取中斷狀態(tài)并取消中斷*/ IntStatus=CH372_RD_DAT_PORT(); /*獲取中斷狀態(tài)*/ IE1=0; switch(IntStatus){ case USB_INT_EP2_OUT: /*接收數(shù)據(jù)成功*/ CH372_WR_CMD_PORT(CMD_RD_USB_DATA); UsbLength=cnt=CH372_RD_DAT_PORT(); /*首先讀出數(shù)據(jù)塊的長(zhǎng)度*/ if(cnt) { buf=UsbBuffer; /*指針指向緩沖區(qū)*/ do { *buf=CH372_RD_DAT_PORT(); /*接收數(shù)據(jù)*/ buf++; }while(--cnt); } else break; case USB_INT_EP2_IN: /*發(fā)送數(shù)據(jù)成功*/ CH372_WR_CMD_PORT(CMD_WR_USB_DATA7); cnt=UsbLength; CH372_WR_DAT_PORT(cnt); /*首先寫(xiě)入數(shù)據(jù)塊長(zhǎng)度*/ if(cnt) { buf=UsbBuffer; /*指針指向緩沖區(qū)*/ do { CH372_WR_DAT_PORT(*buf); /*寫(xiě)入數(shù)據(jù)*/ buf++; }while(--cnt); CH372_WR_CMD_PORT( CMD_UNLOCK_USB ); /*釋放緩沖區(qū)*/ } else break; default: CH372_WR_CMD_PORT( CMD_UNLOCK_USB ); /* 釋放當(dāng)前USB緩沖區(qū) */ break; } }
main() { CH372_INIT(); EA=1; while(1); }