#include #include #include #include
#define CH375_DATA_PORT P0 /* CH375端口的I/O地址 */ sbit CH375_CMD_DAT = P2^2; /* CH375地址線輸入A0,A0=1時寫命令,A0=0時讀寫數據 */ sbit CH375_RD = P3^7; /* CH375讀選通輸入,低電平有效 */ sbit CH375_WR = P3^6; /* CH375寫選通輸入,低電平有效 */ sbit CH375_CS = P2^3; /* CH375片選,低電平有效 */ sbit led = P2^5; /* 指示燈 */ sbit CH375_INT_WIRE = P3^2; /* CH375中斷請求輸出,低電平有效;P3.2, INT0, 連接CH375的INT#引腳,用于查詢中斷狀態(tài) */ unsigned char RD_Data,p,q,i,j2,length,rev_data; #define CMD_CHECK_EXIST 0x06
void delay50ms() { unsigned char i, j; unsigned char p=0; for( i=50; i!=0; i-- ) { for( j=25; j!=0; j-- ) {p++; p--;} } }
void flash_led() { unsigned char i; for( i=20;i>0;i-- ) { led=!led; delay50ms(); delay50ms(); } }
unsigned char CH375_WR_CMD_PORT(unsigned char cmd) { CH375_DATA_PORT=cmd; CH375_CMD_DAT = 1; CH375_WR = 0; CH375_CS =0; CH375_RD=1; _nop_(); _nop_(); CH375_CS =1; CH375_WR = 0; }
void CH375_WR_DAT_PORT( unsigned char dat ) { _nop_(); CH375_DATA_PORT = dat; CH375_CMD_DAT = 0; /* 數據 */ CH375_WR=0; CH375_CS=0; CH375_RD=1; _nop_(); CH375_CS=1; CH375_WR = 1; }
unsigned char CH375_RD_DAT_PORT(void) { CH375_DATA_PORT = 0xFF; CH375_CMD_DAT = 0; /* 數據 */ CH375_WR = 1; CH375_RD = 0; CH375_CS =0; rev_data = CH375_DATA_PORT; _nop_(); CH375_RD =1; CH375_CS =1; CH375_RD = 1; return( rev_data ); }
main() { i=0; led=i; CH375_WR_CMD_PORT(CMD_CHECK_EXIST); /* 測試CH375是否正常工作 */ CH375_WR_DAT_PORT( 0x5A ); /* 寫入測試數據 */ RD_Data = CH375_RD_DAT_PORT(); while(1) { } }
以上程序僅是用來檢測CH375是否能正常工作,但在執(zhí)行過程中,出現(xiàn)幾個異常情況,請大家?guī)兔Ψ治鲆幌?。謝謝! rev_data的值為08H(8), 而RD_Data的值為0CH(12), 再量八個數據腳,發(fā)現(xiàn)全是1。 正常情況下,兩個變量的值以及數據腳的值應該是一樣的,但結果卻如此讓人匪夷所思。