為什么初始化CH376時候有時候能成功,有時候失敗? 成功起碼說明時序和硬件是沒有問題的,但又為什么會失敗的,返回值不是發(fā)送值的取反呢?求解啊
有時候成功有時候失敗這種不穩(wěn)定現(xiàn)象說明時序或者硬件還是有問題的,比如數(shù)據(jù)之間間隔是否足夠?要確保每次測試命令都是通過的。
您能幫我看一下我的代碼時序對不? #include #include #include #include #include #define uchar unsigned char #define uint unsigned int #define ulong unsigned long
#define CH376_DATA_DAT_OUT(d) { P0 = d; } /* 向并口輸出數(shù)據(jù) */ #define CH376_DATA_DAT_IN() ( P0 ) /* 從并口輸入數(shù)據(jù) */ #define CH376_DATA_DIR_OUT() /* 設置并口方向為輸出 */ #define CH376_DATA_DIR_IN() { P0 = 0xFF; } /* 設置并口方向為輸入 */
sbit CH376_A0 = P3^4; //設置CH376引腳 sbit CH376_PCS = P3^5; sbit CH376_WR = P3^6; sbit CH376_RD = P3^7; sbit CSD = P3^3; //SD換卡信號 sbit LED1 = P3^0; sbit LED2 = P3^1; sbit INT = P1^0; //查詢CH367中斷標志
void main() //主程序 {
delay1ms(200); init(); //系統(tǒng)初始化 data_cunchu(); }
void init(void) //系統(tǒng)初始化 { /*CH376初始化*/ uchar res,m,n; m=0x05; n=0x11; CH376_PORT_INIT(); /* 接口硬件初始化 */ xWriteCH376Cmd(0x06); /* 測試單片機與CH376之間的通訊接口 */ xWriteCH376Data(0x55); //發(fā)送數(shù)據(jù)55H,應該返回AAH res=xReadCH376Data(); //讀取返回值 send(res); // delay3us(20);// if (res!=0xAA) /* 通訊接口不正常,可能原因有:接口連接異常,其它設備影響(片選不唯一),串口波特率,一直在復位,晶振不工作 */ { CH376_flag=0; //不成功 send(0x11); } while(m) { delay1ms(200); xWriteCH376Cmd(CMD_SET_USB_MODE); /* 設備SD工作模式 */ xWriteCH376Data(0x03); //選取SD卡 delay3us(7); //延遲一段時間 res = xReadCH376Data(); if(res==CMD_RET_SUCCESS) CH376_flag=1; //成功 else { CH376_flag=0; //不成功 send(0x12); m--; continue; } xWriteCH376Cmd(CMD_DISK_MOUNT); //初始化SD卡 while(n) /* 一直等中斷 */ { SCONV = 1; delay10us(1); if(ADCDATAL<0x99) n=0x00; } res = CH376GetIntStatus(); /* 檢測到中斷 */ send(res); if(res==0x14) { CH376_flag=1; //成功 break; } else { CH376_flag=0; //不成功 send(0x13); m--; continue; } } } void CH376_PORT_INIT(void) /* 由于使用通用I/O模擬并口讀寫時序,所以進行初始化 */ { CH376_DATA_DIR_IN( ); /* 設置并口輸入 */ CH376_PCS = 1; CH376_WR = 1; CH376_RD = 1; CH376_A0 = 0; } void xWriteCH376Cmd(uchar mCmd) /* 向CH376寫命令 */ { CH376_DATA_DAT_OUT(mCmd); /* 向CH376的并口輸出數(shù)據(jù) */ CH376_DATA_DIR_OUT( ); /* 設置并口方向為輸出 */ CH376_WR=1; //jia CH376_RD=1; //jia CH376_A0 = 1; CH376_WR = 0; /* 輸出有效寫控制信號, 寫CH376芯片的命令端口 */ CH376_PCS = 0; CH376_WR = 1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_PCS = 1; CH376_A0 = 0; CH376_DATA_DIR_IN( ); /* 禁止數(shù)據(jù)輸出 */ delay1ms(200); /* 延時3uS確保讀寫周期大于1.5uS */ } void xWriteCH376Data(uchar mData) /* 向CH376寫數(shù)據(jù) */ { CH376_DATA_DAT_OUT(mData); /* 向CH376的并口輸出數(shù)據(jù) */ CH376_DATA_DIR_OUT(); /* 設置并口方向為輸出 */ CH376_WR=1; //jia CH376_RD=1; //jia CH376_A0 = 0; CH376_WR = 0; /* 輸出有效寫控制信號, 寫CH376芯片的數(shù)據(jù)端口 */ CH376_PCS = 0; // CH376_CS = 0; /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS */ CH376_WR = 1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_PCS = 1; CH376_DATA_DIR_IN(); /* 禁止數(shù)據(jù)輸出 */ delay1ms(200); /* 確保讀寫周期大于0.6uS */ } uchar xReadCH376Data(void) /* 從CH376讀數(shù)據(jù) */ { uchar mData; CH376_DATA_DIR_IN(); /* 設置并口方向為輸入 */ CH376_A0 = 0; CH376_PCS = 0; CH376_WR = 1; //jia CH376_RD = 0; /* 輸出有效讀控制信號, 讀CH376芯片的數(shù)據(jù)端口 */ CH376_PCS = 0; /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS,強烈建議此處執(zhí)行一條空指令延時以確保并口有足夠時間輸入數(shù)據(jù) */ mData = CH376_DATA_DAT_IN(); /* 從CH376的并口輸入數(shù)據(jù) */ CH376_RD = 1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_PCS = 1; delay1ms(10); return(mData); } uchar xReadCH376Cmd(void) /* 從CH376讀狀態(tài) */ { uchar mData; CH376_DATA_DIR_IN(); /* 設置并口方向為輸入 */ CH376_A0 = 1; CH376_PCS = 0; CH376_WR = 1; //JIA CH376_RD = 0; /* 輸出有效讀控制信號, 讀CH376芯片的狀態(tài)端口 */ CH376_PCS = 0; /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS,強烈建議此處執(zhí)行一條空指令延時以確保并口有足夠時間輸入數(shù)據(jù) */ mData = CH376_DATA_DAT_IN(); /* 從CH376的并口輸入數(shù)據(jù) */ CH376_RD = 1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_PCS = 1; CH376_A0 = 0; delay1ms(10); return(mData); }