我想讓PPT每隔10秒翻頁(yè)一次??墒荘PT翻頁(yè)速度很快,我的延時(shí)程序沒用。 下面是代碼: #include #include "CH375INC.H"
unsigned char idata buf[8]; typedef union _REQUEST_PACK{ unsigned char buffer[8]; struct{ unsigned char bmReuestType; //標(biāo)準(zhǔn)請(qǐng)求字 unsigned char bRequest; //請(qǐng)求代碼 unsigned int wValue; //特性選擇高 unsigned int wIndx; //索引 unsigned int wLength; //數(shù)據(jù)長(zhǎng)度 }r; } mREQUEST_PACKET, *mpREQUEST_PACKET;
//設(shè)備描述符 unsigned char code DevDes[]={ 0x12 //描述符大小 , 0x01 //常數(shù)DEVICE , 0x10 //USB規(guī)范版本信息 , 0x01 , 0x00 //類別碼, , 0x00 //子類別碼 , 0x00 //協(xié)議碼 , 0x08 //端點(diǎn)0的最大信息包大小 , 0x3c //廠商ID , 0x41 , 0x03 //產(chǎn)品ID , 0x20 , 0x00 //設(shè)備版本信息 , 0x02 , 0x01 //索引值 , 0x02 , 0x00 , 0x01 //可能配置的數(shù)目 }; //配置描述符 unsigned char code ConDes[]={ //配置描述符 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x04, 0xa0, 0x23,//配置描述符 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x01, 0x01, 0x05,//接口描述符 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, 0x41, 0x00,//HID類描述符 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x18 //端點(diǎn)描述符,只配置了輸入端點(diǎn),輸出端點(diǎn)用0端點(diǎn) }; //配置描述符
/*報(bào)表描述符*/ unsigned char code Hid_des[]={0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02, 0x95, 0x01, 0x75, 0x05, 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 0x00, 0x2a, 0xff, 0x00, 0x81, 0x00, 0xc0 }; /*關(guān)于全局變量的定義*/ unsigned char mVarSetupRequest; // ;USB請(qǐng)求碼 unsigned char mVarSetupLength; // 后續(xù)數(shù)據(jù)長(zhǎng)度 unsigned char code * VarSetupDescr; //描述符偏移地址 unsigned char VarUsbAddress ; sbit CH375_INT_WIRE =P3^2; //P3.2, INT0, 連接CH375的INT#引腳,用于查詢中斷狀態(tài) */ /*sbit CH375ACT = P1^4;*/ /*sbit CH375_LED=P1^5; */ //sbit PAGEUP=P1^2; //sbit PAGEDOWN=P1^1; //sbit key=P1^0; bit CH375FLAGERR; //錯(cuò)誤清0 bit CH375CONFLAG; unsigned char idata UPDATA_FLAG; mREQUEST_PACKET request;
/*硬件定義,根據(jù)硬件修改*/ unsigned char volatile xdata CH375_CMD_PORT _at_ 0xBDF1; /* CH375命令端口的I/O地址 */ unsigned char volatile xdata CH375_DAT_PORT _at_ 0xBCF0; /* CH375數(shù)據(jù)端口的I/O地址 */
/* 延時(shí)2微秒,不精確 */ void Delay1us() { ; }
void Delay2us( ) { unsigned char i; #define DELAY_START_VALUE 1 /* 根據(jù)單片機(jī)的時(shí)鐘選擇初值,20MHz以下為0,30MHz以上為2 */ for ( i=DELAY_START_VALUE; i!=0; i-- ); }
/* 延時(shí)50毫秒,不精確 */ void Delay50ms( ) { unsigned char i, j; for ( i=200; i!=0; i-- ) for ( j=250; j!=0; j-- ); }
void CH375_WR_CMD_PORT( unsigned char cmd ) { /* 向CH375的命令端口寫入命令,周期不小于4uS,如果單片機(jī)較快則延時(shí) */ CH375_CMD_PORT=cmd; Delay2us( ); }
void CH375_WR_DAT_PORT( unsigned char dat ) { /* 向CH375的數(shù)據(jù)端口寫入數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */ CH375_DAT_PORT=dat; Delay1us(); /* 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無需延時(shí) */ }
unsigned char CH375_RD_DAT_PORT() { /* 從CH375的數(shù)據(jù)端口讀出數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */ Delay1us( ); /* 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無需延時(shí) */ return( CH375_DAT_PORT );
} /* CH375初始化子程序 */ void CH375_Init( ) { // unsigned char i; /* 測(cè)試CH375是否正常工作,可選操作,通常不需要 */ //CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 測(cè)試CH375是否正常工作 */ //CH375_WR_DAT_PORT( 0x55 ); /* 寫入測(cè)試數(shù)據(jù) */ //i = ~ 0x55; /* 返回?cái)?shù)據(jù)應(yīng)該是測(cè)試數(shù)據(jù)取反 */ //if ( CH375_RD_DAT_PORT( ) != i ) { /* CH375不正常 */ //CH375_LED=0; //for ( i=80; i!=0; i-- ) {
//CH375_WR_CMD_PORT( CMD_RESET_ALL ); /* 多次重復(fù)發(fā)命令,執(zhí)行硬件復(fù)位 */
//CH375_RD_DAT_PORT( ); //} //CH375_WR_CMD_PORT( 0 ); //Delay50ms( ); /* 延時(shí)50ms */ //} /* 設(shè)置USB工作模式, 必要操作 */ CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( 1 ); /* 設(shè)置為使用內(nèi)置固件的USB設(shè)備方式 */ for ( ;; ) { /* 等待操作成功,通常需要等待10uS-20uS */ if ( CH375_RD_DAT_PORT( )==CMD_RET_SUCCESS ) break; } }
//端點(diǎn)0數(shù)據(jù)上傳 void mCh375Ep0Up() { unsigned char i,len; if(mVarSetupLength) { //長(zhǎng)度不為0傳輸具體長(zhǎng)度的數(shù)據(jù) if(mVarSetupLength<=8) { len=mVarSetupLength; mVarSetupLength=0; } //長(zhǎng)度小于8則長(zhǎng)輸要求的長(zhǎng)度 else { len=8; mVarSetupLength-=8; } //長(zhǎng)度大于8則傳輸8個(gè),切總長(zhǎng)度減8 CH375_WR_CMD_PORT(CMD_WR_USB_DATA3); //發(fā)出寫端點(diǎn)0的命令 CH375_WR_DAT_PORT(len); //寫入長(zhǎng)度 for(i=0;i!=len;i++) CH375_WR_DAT_PORT(request.buffer[i]); //循環(huán)寫入數(shù)據(jù) } else { CH375_WR_CMD_PORT(CMD_WR_USB_DATA3); //發(fā)出寫端點(diǎn)0的命令 CH375_WR_DAT_PORT(0); //上傳0長(zhǎng)度數(shù)據(jù),這是一個(gè)狀態(tài)階段 } }
//復(fù)制描述符以便上傳 void mCh375DesUp() { unsigned char k; for (k=0; k!=8; k++ ) { request.buffer[k]=*VarSetupDescr; //依次復(fù)制8個(gè)描述符, VarSetupDescr++; } }
/*中斷函數(shù),但是本例用的是查詢方式,只處理了輸入端點(diǎn)1和端點(diǎn)0的事件,其余不處理*/ void mCH375Interrupt( ) { unsigned char InterruptStatus; unsigned char length, c1, len; CH375_WR_CMD_PORT(CMD_GET_STATUS); /* 獲取中斷狀態(tài)并取消中斷請(qǐng)求 */ InterruptStatus =CH375_RD_DAT_PORT(); /* 獲取中斷狀態(tài) */ switch(InterruptStatus){ // 分析中斷狀態(tài) case USB_INT_EP1_OUT: //中斷端點(diǎn)下傳成功,未處理 CH375_WR_CMD_PORT(CMD_RD_USB_DATA); //發(fā)出讀數(shù)據(jù)命令 if( length=CH375_RD_DAT_PORT( ) ) { //長(zhǎng)度為0跳出 for(len=0;len!=length;len++)c1=CH375_RD_DAT_PORT(); //取出下傳數(shù)據(jù) } break; case USB_INT_EP0_SETUP: //控制端點(diǎn)建立成功 CH375_WR_CMD_PORT(CMD_RD_USB_DATA); length=CH375_RD_DAT_PORT(); for(len=0;len!=length;len++)request.buffer[len]=CH375_RD_DAT_PORT(); // 取出數(shù)據(jù) if(length==0x08){ mVarSetupLength=request.buffer[6]; //控制傳輸數(shù)據(jù)長(zhǎng)度最大設(shè)置為128 if((c1=request.r.bmReuestType)&0x40){ //廠商請(qǐng)求,未處理 } if((c1=request.r.bmReuestType)&0x20) { //類請(qǐng)求,未處理 if(request.buffer[1]==0x0a) { } //SET_IDLE else if(request.buffer[1]==0x09) { UPDATA_FLAG=1; } } if(!((c1=request.r.bmReuestType)&0x60)) { //標(biāo)準(zhǔn)請(qǐng)求 mVarSetupRequest=request.r.bRequest; //暫存標(biāo)準(zhǔn)請(qǐng)求碼 switch(request.r.bRequest){ // 分析標(biāo)準(zhǔn)請(qǐng)求 case DEF_USB_CLR_FEATURE: //清除特性 if((c1=request.r.bmReuestType&0x1F)==0X02) { //不是端點(diǎn)不支持 switch(request.buffer[4]) { case 0x82: CH375_WR_CMD_PORT(CMD_SET_ENDP7); //清除端點(diǎn)2上傳 CH375_WR_DAT_PORT(0x8E); //發(fā)命令清除端點(diǎn) break; case 0x02: CH375_WR_CMD_PORT(CMD_SET_ENDP6); CH375_WR_DAT_PORT(0x80); //清除端點(diǎn)2下傳 break; case 0x81: CH375_WR_CMD_PORT(CMD_SET_ENDP5); //清除端點(diǎn)1上傳 CH375_WR_DAT_PORT(0x8E); break; case 0x01: CH375_WR_CMD_PORT(CMD_SET_ENDP4); //清除端點(diǎn)1下傳 CH375_WR_DAT_PORT(0x80); break; default: break; } } else { CH375FLAGERR=1; //不支持的清除特性,置錯(cuò)誤標(biāo)志 } break; case