[求助]關(guān)于CH372模擬鍵盤的問(wèn)題

#include #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; code unsigned char HIDKEY[] = //鍵盤編碼 { 0x00,0x00,0x27,//0 0x00,0x00,0x1e,//1 0x00,0x00,0x1f,//2 0x00,0x00,0x20,//3 0x00,0x00,0x21,//4 0x00,0x00,0x22,//5 0x00,0x00,0x23,//6 0x00,0x00,0x24,//7 0x00,0x00,0x25,//8 0x00,0x00,0x26,//9 } ;

//設(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 ; bit CH375FLAGERR; //錯(cuò)誤清0 bit CH375CONFLAG; bit CH372_UP_DATA0; bit CH372_UP_DATA1;

sbit key =P3^3; sbit led =P3^6; sbit CS = P3^0; sbit WR_DAT = P1^7; sbit RD_DAT = P1^6; sbit A0 = P1^5; sbit CH375_INT_WIRE = P3^2; //P3.2, INT0, 連接CH375的INT#引腳,用于查詢中斷狀態(tài) */ sbit CH375ACT = P1^0; //輸出配置信號(hào)

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í)1ms*/ void Delay1ms(unsigned int count) { unsigned int loop0,loop1; for(loop0 = 0; loop0 < count; loop0++ ) { for(loop1 = 0 ;loop1 < 65; loop1++){ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } }

/* 延時(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í) //delay2us(); CH375_CMD_PORT=cmd; P0 = cmd; A0 = 1; // P0_DIR = output; 對(duì)于標(biāo)準(zhǔn)雙向I/O,請(qǐng)?jiān)诖嗽O(shè)置為輸出方向 RD_DAT = 1; // CH375_CS = 0; WR_DAT = 0; // CH375_CS = 0; 對(duì)于高速單片機(jī),該指令用于延時(shí),以便向CH375_WR產(chǎn)生寬度至少為80nS的低電平脈沖 RD_DAT = 1; WR_DAT = 1; // CH375_CS = 1; A0 = 0; // P0_DIR = input; 對(duì)于標(biāo)準(zhǔn)雙向I/O,請(qǐng)?jiān)诖嗽O(shè)置為輸入方向 P0 = 0xFF;

Delay2us( ); }

void CH375_WR_DAT_PORT( unsigned char dat ) { // 向CH375的數(shù)據(jù)端口寫入數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) CH375_DAT_PORT=dat; /* ******************** 注釋中是用普通I/O引腳模擬8位并口的時(shí)序*/ P0 = dat; // P0_DIR = output; 對(duì)于標(biāo)準(zhǔn)雙向I/O,請(qǐng)?jiān)诖嗽O(shè)置為輸出方向 // CH375_CS = 0; WR_DAT = 0; // CH375_CS = 0; RD_DAT = 1; WR_DAT = 1; // CH375_CS = 1; // P0_DIR = input; 對(duì)于標(biāo)準(zhǔn)雙向I/O,請(qǐng)?jiān)诖嗽O(shè)置為輸入方向 P0 = 0xFF; /******************** */ Delay1us(); // 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無(wú)需延時(shí) }

unsigned char CH375_RD_DAT_PORT( void ) { /* 從CH375的數(shù)據(jù)端口讀出數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */ unsigned char dat ; //delay1us(); /* 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無(wú)需延時(shí) */ /* ******************** 注釋中是用普通I/O引腳模擬8位并口的時(shí)序 */ // P0_DIR = input; 對(duì)于標(biāo)準(zhǔn)雙向I/O,請(qǐng)?jiān)诖嗽O(shè)置為輸入方向 P0 = 0xFF; // CH375_CS = 0; RD_DAT = 0; // CH375_CS = 0; 對(duì)于高速單片機(jī),該指令用于延時(shí),以便向CH375_RD產(chǎn)生寬度至少為80nS的低電平脈沖 dat = P0; RD_DAT = 1; // CH375_CS = 1; P0 = 0xFF; return( dat ); /******************** */ return( CH375_DAT_PORT ); } /* CH375初始化子程序 */ void CH375_Init( ) { /* 設(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_EP1_IN: //中斷端點(diǎn)上傳成功,未處理 CH375_WR_CMD_PORT (CMD_UNLOCK_USB); //釋放緩沖區(qū) if(CH372_UP_DATA0==1) { unsigned char i=0; CH375_WR_CMD_PORT(CMD_WR_USB_DATA5); //發(fā)出寫上傳端點(diǎn)命令 CH375_WR_DAT_PORT(8); for(i=0;i!=8;i++) CH375_WR_DAT_PORT(0x00); CH372_UP_DATA1=1; CH372_UP_DATA0=0; } 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){

請(qǐng)把你的問(wèn)題說(shuō)詳細(xì)一些


主函數(shù)這樣寫能正確發(fā)送數(shù)字2; void main( ) {

Delay50ms( ); /* 延時(shí)等待CH375初始化完成,如果單片機(jī)由CH375提供復(fù)位信號(hào)則不必延時(shí) */ CH375_Init( ); /* 初始化CH375 */ while(1) { /* 主程序 */ if(CH375_INT_WIRE == 0) mCH375Interrupt( );

if(UPDATA_FLAG==0x01) { while(CH372_UP_DATA1==1); CH372_UP_DATA1=0; //sendover(); PutKeyNum(2); while(CH372_UP_DATA1==1); CH372_UP_DATA1=0; sendover(); }要是像我上面帖子那樣寫 就發(fā)不出數(shù)字了? 怎么發(fā)送0123456789回車呀?搞很久了沒搞明白!??!幫幫我...555555555555


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频