//直接从.c文件复制黏贴过来的,要用自己再把下面的代码另存份.c的.#defineBYTEunsignedchar//**************************************************//基本定义函数//**************************************************BYTEFIFO_RD_CHECK(BYTEnum){BYTEFIFO_FLAG=0;UCC|=num;//选择Endpoint,0,1,2,3,4,MISC&=0xf8;//低3位置0MISC|=0X00;//TX位置0;Delay_3us();MISC|=0X01;//SetRequestDelay_28us();if(MISC&0X40!=0)FIFO_FLAG=0x0f;//READYif(MISC&0x80!=0)FIF0_FLAG&=0xf0;//Len0DetectedMISC&=0xfe;//clearREQreturnFIFO_FLAG;}BYTEFIFO_WR_CHECK(BYTEnum){BYTEFIFO_FLAG=0;//后4位表示Ready位,前四位表示Len0位的状态是否有被设置UCC|=num;//选择Endpoint,0,1,2,3,4,MISC&=0xf8;//低3位置0MISC|=0X02;//TX位置0;Delay_3us();MISC|=0X01;//SetRequestDelay_28us();if(MISC&0X40!=0)FIFO_FLAG=0x0f;//READYif(MISC&0x80!=0)FIF0_FLAG&=0xf0;//Len0DetectedMISC&=0xfe;//clearREQreturnFIFO_FLAG;}voidRead_FIFO(BYTE*Fifo_Addr,BYTEFifo_Size,BYTE*buffer){inti=0;MISC|=0x01;//SetRequestfor(i=0;iFifo_Size;i++){buffer[i]=Fifo_Addr[0];//从FIFO地址取值Delay_28us();if(MISC&0x40==0)break;//NotReady}MISC^=0X02;//改变TX位状态Delay_3us();MISC&=0XFE;//ClearRequest}voidWrite_FIFO(BYTE*Fifo_Addr,BYTESendLenth,BYTE*buffer){inti=0;MISC|=0X01;//SetRequestfor(i=0;iSendLenth;i++){Fifo_Addr[0]=buffer[i];//将要发送的数据放至相应的Fifo中Delay_28us();if(MISC&0x40==0)break;//NotReady}MISC^=0X02;//改变TX位状态Delay_3us();MISC&=0XFE;//ClearRequest}BOOLCheck_Real_Cmd(){BYTEbFlag_Real_Cmd=FALSE;if(MISC&0X20!=0)bFlag_Real_Cmd=TRUE;if(MISC&0X80!=0)bFlag_Real_Cmd=TRUE;returnbFlag_Real_Cmd;}voidSend_hand(){BOOLIsRealCmd,Status_Flag;while(1){IsRealCmd=Check_Real_Cmd();if(IsRealCmd==TRUE)return;else{Status_Flag=FIFO_WR_CHECK(0);if(Status_Flag&0x0f!=0)//Fifo_Readybreak;elsecontinue;}}MISC|=0x01;//SetRequsetMISC^=0X02;//改变TX位状态Delay_3us();MISC&=0XFE;//ClearRequest}voidcontrol_read(WORD*rom_dataptr,BYTE*buffer)//从中读取数据,{//rom_dataptr是全局的在其他函数中赋值改变BYTEtemp,i=0;if(buffer[7]!=0)//FIFO_WLENGTHHreturn;if(buffer[6]==0)//FIFO_WLENGTHLreturn;if(buffer[6]data_start[0])//请求的数据超出ROM数据return;data_count=buffer[6];if(MISC&0x40!=0)//是控制指令return;if(data_count==0){Send_Hand_Shake();return;}//开始真正的control_readwhile(1){if(bFlag_RD_HTable==0)//ReadLowByte{bFlag_RD_HTable=1;buffer[i]=(BYTE)*rom_dataptr;//取其低位i++;data_count--;//全局变量,要发送的总长度if(data_count=1)break;if(i+1==8)//FIFO_size=8break;}else//ReadHightByte{bFlag_RD_HTable=0;buffer[i]=(BYTE)(*rom_dataptr)8;//取其高位rom_dataptr++;//将ROM指针前移if(buffer[i]==0x3f)continue;else{i++;if(data_count=1)break;if(i+1==8)break;}}}//填充完毕,结束循环,开始向FIFO发送数据while(1){if(Check_Real_Cmd()==TRUE)return;if(FIFO_WR_CHECK(0)&0xff==0)continue;//NotReady}Write_FIFO(Fifo_0_Addr,0x02,buffer);}//**************************************************//SetAddress():重新设定Endpoint地址函数//**************************************************voidSetAddress(BYTE*buffer){BYTEDev_Addr;Dev_Addr=buffer[2]//FIFO_OUT3,FIFO_WVALUELSIES|=0x01;//主机从设备读操作后将更新在AWR中的地址,否则为0则立即更新Dev_Addr&=0XFE;USB_AWR=Dev_Addr;Send_Hand_Shake();}//**************************************************//SetConfiguration():设置配置函数//**************************************************voidSetConfiguration(BYTE*buffer,BYTE*USB_Configuration){USVC|=0x80;PGA_CTRL|=0x80;*USB_Configuration=buffer[2];//FIFO_WVALUELSTALL&=0x00;//设置好以上的寄存器Send_Hand_Shake();}//**************************************************//ClearFeature():清徐Feature函数//**************************************************voidClearFeature(BYTE*buffer){BYTEtemp;temp=buffer[2];//FIFO_wValueLif(temp=0x01){Send_Hand_Shake();}elseSTALL0|=0x01;//SendStall0();}voidClearFeature_Endpoint(BYTE*buffer){BYTEtemp;temp=buffer[4];//FIFO_wIndexLif(bFlag_SetConfiguration_Ready==TRUE);//表明已经配置过了temp=GetPipeBit(temp);//获取对应位temp=~temp;//取反STALL=temp&STALL;//与STALL寄存器相与,将需要的位置0Send_Hand_Shake();}//**************************************************//SetFeature():设置Feature函数//包括Endpoint部分//**************************************************voidSetFeature(BYTE*buffer){BYTEFIFO_wValueL=buffer[2];BYTEFIFO_wValueH=buffer[3];if(FIFO_wValueH==0x00){if(FIFO_wValueL==0x01){Send_Hand_Shake();}elseSTALL0|=0x01;//SendStall0();}elseif(FIFO_wValueH=0X81=0X84){if(FIFO_wValueL==0x00){Send_Hand_Shake();}elseSTALL0|=0x01;//SendStall0();}elseSTALL0|=0x01;//SendStall0();}voidSetFeature_Endpoint(BYTE*buffer){BYTEtemp;temp=buffer[4];//FIFO_wIndexLif(bFlag_SetConfiguration_Ready!=TRUE)return;//有没有被SetConfiguration()设置temp&=0x7f;temp=GetPipeBit(temp);//获取对应位STALL=temp|STALL;//将指定的Enpoint置1Send_Hand_Shake();}//**************************************************//SetInterface():设置Interface函数////**************************************************BOOLSetInterface(BYTE*buffer,BYTE*USB_Interface_Alt,BYTE*USB_Interface){*USB_Interface_Alt=buffer[2];//FIFO_WVALUEL*USB_Interface=buffer[4];//FIFO_WINDEXLPA|=0X01;//SetPA.0Send_Hand_Shake();returnTRUE;}//**************************************************//GetDescriptor():获取Descriptor//描述的函数//**************************************************voidGetDescriptor(BYTE*buffer){BYTEFIFO_WvalueH=buffer[3];BYTEFIFO_wIndexL=buffer[4];switch(FIFO_WvalueH){case01://device,80060001GetDeviceDescriptor();return;case02://configurati