我的單片機(jī)直接與372相接 P2口接數(shù)據(jù)線,P3口中CH372_A0 = P3^7; CH372_WR = P3^4; CH372_RD = P3^5;CS接地。P3.3接372中斷。我出現(xiàn)的問題是,第一次有提示,并看到有文件復(fù)制,說安裝成功,可是一會我查看根本沒找到設(shè)備。以后連有硬件接入的提示都沒有,晶振好像也不正常,這個(gè)問題已出現(xiàn)很久,始終不得其解。下面是我的完整程序,請各位行家指點(diǎn)一下。
#include #include typedef unsigned char UINT8; typedef unsigned short UINT16; typedef unsigned long UINT32; typedef unsigned char * PUINT8; sfr ADC_CONTR =0xC5; sfr ADC_DATA =0xc6; sfr ADC_LOW2 =0xbe; sfr P1M0 =0x91; sfr P1M1 =0x92; sfr AUXR =0x8E; sbit CH372_A0 = P3^7; sbit CH372_WR = P3^4; sbit CH372_RD = P3^5; #define CMD_SET_USB_MODE 0x15 #define MAX_DATA_SIZE 60 typedef struct _USB_DOWN_PACKET { /* 下傳的數(shù)據(jù)包結(jié)構(gòu),用于命令/寫數(shù)據(jù) */ UINT8 mCommand; UINT8 mLength; /* 下面的緩沖區(qū)的長度,讀寫操作的字節(jié)數(shù) */ UINT8 mBuffer[ 5 ]; /* 數(shù)據(jù)緩沖區(qū) */ } USB_DOWN_PKT; typedef struct _USB_UP_PACKET { UINT8 mStatus; UINT8 mBuffer0[ MAX_DATA_SIZE ]; UINT8 mBuffer1[ MAX_DATA_SIZE ]; /* 數(shù)據(jù)緩沖區(qū) */ } USB_UP_PKT; typedef union _USB_DATA_PACKET { USB_DOWN_PKT down; USB_UP_PKT up; }USB_DATA_PKT; USB_DATA_PKT PACK ;
void Delay1us() { int i; for( i=3;i!=0;i--); } void Delay2us() { int i; for( i=6;i!=0;i--); } void CH375_WR_CMD_PORT(UINT8 CMD) { Delay2us(); /* 至少延時(shí)1uS */ P2 = CMD; /* 向CH372的并口輸出數(shù)據(jù) */ CH372_A0 = 1; CH372_WR = 0; /* 輸出有效寫控制信號, 寫CH372芯片的命令端口 */ CH372_WR = 1; /* 輸出無效的控制信號, 完成操作CH372芯片 */ CH372_A0 = 0; P2 = 0xFF; /* 禁止數(shù)據(jù)輸出 */ Delay2us(); /* 至少延時(shí)2uS */ } void CH375_WR_DAT_PORT(UINT8 dat) { P2 = dat; /* 向CH372的并口輸出數(shù)據(jù) */ CH372_A0 = 0; CH372_RD = 1; CH372_WR = 0; /* 輸出有效寫控制信號, 寫CH372芯片的數(shù)據(jù)端口 */ Delay1us(); CH372_WR = 1; /* 輸出無效的控制信號, 完成操作CH372芯片 */ P2 = 0xFF; /* 禁止數(shù)據(jù)輸出 */ Delay1us();/* 至少延時(shí)1.2uS */ } UINT8 CH375_RD_DAT_PORT(void) { UINT8 mData; Delay2us(); /* 至少延時(shí)1.2uS */ P2 = 0xFF; /* 輸入 */ CH372_A0 = 0; CH372_WR = 1; CH372_RD = 0; /* 輸出有效寫控制信號, 讀CH372芯片的數(shù)據(jù)端口 */ mData = P2; /* 從CH372的并口輸入數(shù)據(jù) */ Delay2us(); CH372_RD = 1; /* 輸出無效的控制信號, 完成操作CH372芯片 */ return( mData ); } UINT8 CH372_Init(void) { UINT8 i; UINT8 j; CH375_WR_CMD_PORT(CMD_CHECK_EXIST); CH375_WR_DAT_PORT(0X55); i=~0x55; if(CH375_RD_DAT_PORT()!=i) { for(i=100;i!=0;i--) { CH375_WR_CMD_PORT(CMD_RESET_ALL); CH375_RD_DAT_PORT(); } CH375_WR_CMD_PORT(0); for(i=250;i!=0;i--)for(j=100;j!=0;j--) Delay1us() ; CH375_WR_CMD_PORT(CMD_SET_USB_MODE); CH375_WR_DAT_PORT(2); for(i=100;i!=0;i--)if(CH375_RD_DAT_PORT()==CMD_RET_SUCCESS)break; if(i==0){return 0;}//硬件連接錯(cuò)誤返回錯(cuò)誤// INT1=1; } return 1;
}
void GET_AD_RESULT(void) { UINT8 *p; UINT8 n=0; UINT8 temp; if(F0==0) p=(UINT8 *)&PACK.up.mBuffer0; else p=(UINT8 *)&PACK.up.mBuffer1; while(1) { while(ADC_CONTR&0x10)break; ADC_CONTR|=0X08; if(n { temp= (ADC_DATA>>6)&0x03; //取高2 位 *(p+n)=temp; n++; *(p+n)=ADC_DATA<<2|ADC_LOW2; 低 n++; } else break; } } void set_p1_adc_channel(void) { P1=0xff; ADC_CONTR=ADC_CONTR|0X80; P1M0=0X01; P1M1=0X01; Delay1us(); } void Senddata(void) { UINT8 cnt; UINT8* buf; CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); /* 向USB端點(diǎn)2的發(fā)送緩沖區(qū)寫入數(shù)據(jù)塊 */ cnt=MAX_DATA_SIZE; CH375_WR_DAT_PORT( cnt ); /* 首先寫入后續(xù)數(shù)據(jù)長度 */ if(F0==0) buf=(UINT8*)&PACK.up.mBuffer0; /* 指向狀態(tài)包緩沖區(qū) */ else buf=(UINT8*)&PACK.up.mBuffer1; do { CH375_WR_DAT_PORT( *buf ); /* 寫入數(shù)據(jù)到CH375 */ buf ++; } while ( -- cnt ); } UINT8 CH372_CHECK(void) { UINT8 Status; UINT8 send=0; while(INT1==1); INT1=1; CH375_WR_CMD_PORT(CMD_GET_STATUS); Status=CH375_RD_DAT_PORT(); if (Status == USB_INT_EP2_IN) //批量數(shù)據(jù)發(fā)送成功,狀態(tài)包已發(fā)送 */ { CH375_WR_CMD_PORT( CMD_UNLOCK_USB ); //釋放當(dāng)前USB緩沖區(qū),收到上傳成功中斷后,必須解鎖USB緩沖區(qū),以便繼續(xù)收發(fā) */ send=1; return (send); } return (send);
}
main(void) {
if(!CH372_Init()) return; set_p1_adc_channel(); ADC_CONTR|=0XE0; F0=0; ADC_DATA=0; ADC_LOW2=0; ADC_CONTR|=0X08; while(1) { Delay2us(); GET_AD_RESULT(); ADC_CONTR|=0x0E8; Senddata(); while(!CH372_CHECK())Senddata(); F0=~F0;
}
}