为什么初始化CH376时候有时候能成功,有时候失败? 成功起码说明时序和硬件是没有问题的,但又为什么会失败的,返回值不是发送值的取反呢?求解啊
有时候成功有时候失败这种不稳定现象说明时序或者硬件还是有问题的,比如数据之间间隔是否足够?要确保每次测试命令都是通过的。
您能帮我看一下我的代码时序对不? #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; } /* 向并口输出数据 */ #define CH376_DATA_DAT_IN() ( P0 ) /* 从并口输入数据 */ #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(); //系统初始化 data_cunchu(); }
void init(void) //系统初始化 { /*CH376初始化*/ uchar res,m,n; m=0x05; n=0x11; CH376_PORT_INIT(); /* 接口硬件初始化 */ xWriteCH376Cmd(0x06); /* 测试单片机与CH376之间的通讯接口 */ xWriteCH376Data(0x55); //发送数据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的并口输出数据 */ 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( ); /* 禁止数据输出 */ delay1ms(200); /* 延时3uS确保读写周期大于1.5uS */ } void xWriteCH376Data(uchar mData) /* 向CH376写数据 */ { CH376_DATA_DAT_OUT(mData); /* 向CH376的并口输出数据 */ CH376_DATA_DIR_OUT(); /* 设置并口方向为输出 */ CH376_WR=1; //jia CH376_RD=1; //jia CH376_A0 = 0; CH376_WR = 0; /* 输出有效写控制信号, 写CH376芯片的数据端口 */ CH376_PCS = 0; // CH376_CS = 0; /* 该操作无意义,仅作延时,CH376要求读写脉冲宽度大于40nS */ CH376_WR = 1; /* 输出无效的控制信号, 完成操作CH376芯片 */ CH376_PCS = 1; CH376_DATA_DIR_IN(); /* 禁止数据输出 */ delay1ms(200); /* 确保读写周期大于0.6uS */ } uchar xReadCH376Data(void) /* 从CH376读数据 */ { uchar mData; CH376_DATA_DIR_IN(); /* 设置并口方向为输入 */ CH376_A0 = 0; CH376_PCS = 0; CH376_WR = 1; //jia CH376_RD = 0; /* 输出有效读控制信号, 读CH376芯片的数据端口 */ CH376_PCS = 0; /* 该操作无意义,仅作延时,CH376要求读写脉冲宽度大于40nS,强烈建议此处执行一条空指令延时以确保并口有足够时间输入数据 */ mData = CH376_DATA_DAT_IN(); /* 从CH376的并口输入数据 */ CH376_RD = 1; /* 输出无效的控制信号, 完成操作CH376芯片 */ CH376_PCS = 1; delay1ms(10); return(mData); } uchar xReadCH376Cmd(void) /* 从CH376读状态 */ { uchar mData; CH376_DATA_DIR_IN(); /* 设置并口方向为输入 */ CH376_A0 = 1; CH376_PCS = 0; CH376_WR = 1; //JIA CH376_RD = 0; /* 输出有效读控制信号, 读CH376芯片的状态端口 */ CH376_PCS = 0; /* 该操作无意义,仅作延时,CH376要求读写脉冲宽度大于40nS,强烈建议此处执行一条空指令延时以确保并口有足够时间输入数据 */ mData = CH376_DATA_DAT_IN(); /* 从CH376的并口输入数据 */ CH376_RD = 1; /* 输出无效的控制信号, 完成操作CH376芯片 */ CH376_PCS = 1; CH376_A0 = 0; delay1ms(10); return(mData); }