#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#引脚,用于查询中断状态 */ 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是否能正常工作,但在执行过程中,出现几个异常情况,请大家帮忙分析一下。谢谢! rev_data的值为08H(8), 而RD_Data的值为0CH(12), 再量八个数据脚,发现全是1。 正常情况下,两个变量的值以及数据脚的值应该是一样的,但结果却如此让人匪夷所思。