在STM32F103VC下采用并口的方式访问CH376,可以确认硬件链路没有问题,从STM32输出的数据和控制信号都测试正确,但是给CH376写入0x06命令后,再写入一个0x65,读出数据恒定为0xFF,请技术支持帮忙分析一下,非常感谢!
以下是测试代码: /* CH376芯片 软件模拟8位并口连接的硬件抽象层 V1.0 */ /* 提供I/O接口子程序 */
#include "stm32f10x_lib.h" #include "stm32f10x_gpio.h" #include "CH376INC.H"
#define UINT8 unsigned char #define UINT16 unsigned int
#define ERR_USB_UNKNOWN 0xFA /* 未知错误,不应该发生的情况,需检查硬件或者程序错误 */
#define CH376_RST_1 GPIO_SetBits(GPIOC,GPIO_Pin_7) #define CH376_RST_0 GPIO_ResetBits(GPIOC,GPIO_Pin_7)
#define CH376_A0_1 GPIO_SetBits(GPIOC,GPIO_Pin_11) #define CH376_A0_0 GPIO_ResetBits(GPIOC,GPIO_Pin_11)
#define CH376_CS_1 GPIO_SetBits(GPIOB,GPIO_Pin_7) #define CH376_CS_0 GPIO_ResetBits(GPIOB,GPIO_Pin_7)
#define CH376_WR_1 GPIO_SetBits(GPIOC,GPIO_Pin_8) #define CH376_WR_0 GPIO_ResetBits(GPIOC,GPIO_Pin_8)
#define CH376_RD_1 GPIO_SetBits(GPIOC,GPIO_Pin_9); #define CH376_RD_0 GPIO_ResetBits(GPIOC,GPIO_Pin_9) //--------------------------------------------------------------------------------------------------------// /* 延时指定微秒时间,根据单片机主频调整,不精确 */ void mDelayuS( UINT8 us ) { UINT8 i; while (us) /* 24MHz MCS51 */ { for(i=0;i<10;i++); us -- ; } }
/* 延时指定毫秒时间,根据单片机主频调整,不精确 */ void mDelaymS( UINT8 ms ) { while ( ms -- ) { mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); mDelayuS( 250 ); } } //--------------------------------------------------------------------------------------------------------// void CH376_DATA_DAT_OUT (UINT8 d) /* 写并口低八位输出数据 */ { unsigned int D_Temp = 0; D_Temp = D_Temp + d; GPIO_Write(GPIOD,D_Temp); } //--------------------------------------------------------------------------------------------------------// UINT8 CH376_DATA_DAT_IN(void ) /* 读并口低八位输入数据 */ { unsigned int D_Temp; UINT8 d; D_Temp = GPIO_ReadInputData(GPIOD); d = (UINT8)D_Temp; return d; } //--------------------------------------------------------------------------------------------------------// void CH376_DATA_DIR_OUT(void ) /* 设置并口低八位方向为输出 */ { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7;; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); } //--------------------------------------------------------------------------------------------------------// void CH376_DATA_DIR_IN( void ) /* 设置并口低八位方向为输入 */ { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7;; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); } //--------------------------------------------------------------------------------------------------------// void CH376_GPIOD_Init (void) //CH376的控制引脚和数据引脚 { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7;; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure);
//rst,wr,rd,a0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure);
//cs GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure); // GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6); //set ext interrupt line } //--------------------------------------------------------------------------------------------------------// #define CH376_INT_WIRE GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6); /* 假定CH376的INT#引脚,如果未连接那么也可以通过查询状态端口实现 */ //--------------------------------------------------------------------------------------------------------// void CH376_PORT_INIT( void ) /* 由于使用通用I/O模拟并口读写时序,所以进行初始化 */ { CH376_CS_1; mDelayuS(50); CH376_WR_1; mDelayuS(50); CH376_RD_1; mDelayuS(50); CH376_A0_0; mDelayuS(50); CH376_DATA_DIR_OUT( ); /* 设置并口输入 */ }
//void mDelay0_5uS( void ) /* 至少延时0.5uS,根据单片机主频调整 */ //{ //}
void xEndCH376Cmd( void ) /* 结束CH376命令,仅用于SPI接口方式 */ {
}
UINT8 xReadCH376Status( void ) /* 从CH376读状态,仅用于并口方式 */ { UINT8 mData; CH376_A0_1; CH376_DATA_DIR_IN( ); /* 设置并口方向为输入 */ // mDelayuS(10); CH376_CS_0; CH376_RD_0; /* 输出有效读控制信号, 读CH376芯片的状态端口 */ mDelayuS(10); CH376_RD_1; /* 输出无效的控制信号, 完成操作CH376芯片 */ mDelayuS(10); mData = CH376_DATA_DAT_IN( ); /* 从CH376的并口输入数据 */ CH376_CS_1; // mDelayuS(10); CH376_A0_0; return( mData ); }
void xWriteCH376Cmd( UINT8 mCmd ) /* 向CH376写命令 */ { CH376_A0_1; CH376_DATA_DIR_OUT( ); /* 设置并口方向为输出 */ CH376_DATA_DAT_OUT( mCmd ); /* 向CH376的并口输出数据 */ mDelayuS(10); CH376_CS_0; CH376_WR_0; /* 输出有效写控制信号, 写CH376芯片的命令端口 */ mDelayuS(50); CH376_WR_1; /* 输出无效的控制信号, 完成操作CH376芯片 */ CH376_CS_1; mDelayuS(10); CH376_A0_0; mDelayuS(50); // CH376_DATA_DIR_IN( ); /* 禁止数据输出 */ }
void xWriteCH376Data( UINT8 mData ) /* 向CH376写数据 */ { CH376_A0_0; mDelayuS(10); CH376_DATA_DIR_OUT( ); /* 设置并口方向为输出 */ CH376_DATA_DAT_OUT( mData ); /* 向CH376的并口输出数据 */ mDelayuS(10); CH376_CS_0; CH376_WR_0; /* 输出有效写控制信号, 写CH376芯片的数据端口 */ mDelayuS(50); CH376_WR_1; /* 输出无效的控制信号, 完成操作CH376芯片 */ CH376_CS_1; mDelayuS(10); CH376_A0_0; // CH376_DATA_DIR_IN( ); /* 禁止数据输出 */ mDelayuS(50); }
UINT8 xReadCH376Data( void ) /* 从CH376读数据 */ { UINT8 mData; CH376_A0_0; // mDelayuS(10); CH376_DATA_DIR_IN( ); /* 设置并口方向为输入 */ // mDelayuS(10); CH376_CS_0; CH376_RD_0; /* 输出有效读控制信号, 读CH376芯片的数据端口 */ mDelayuS(20); CH376_RD_1; /* 输出无效的控制信号, 完成操作CH376芯片 */ mDelayuS(20); mData = CH376_DATA_DAT_IN( ); /* 从CH376的并口输入数据 */ CH376_CS_1; // mDelayuS(10); return( mData ); }
/* 查询CH376中断(INT#低电平) */
UINT8 Query376Interrupt( void ) { return( xReadCH376Status( ) & PARA_STATE_INTB ? FALSE : TRUE ); // 如果未连接CH376的中断引脚则查询状态端口 }
UINT8 mInitCH376Host( void ) /* 初始化CH376 */ { UINT8 res; CH376_RST_1; mDelayuS( 250 ); mDelayuS( 250 ); CH376_RST_0; CH376_PORT_INIT( ); /* 接口硬件初始化 */ xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 测试单片机与CH376之间的通讯接口 */ xWriteCH376Data( 0x65 ); res = xReadCH376Data( );
if ( res != 0x9A ) return( ERR_USB_UNKNOWN ); /* 通讯接口不正常,可能原因有:接口连接异常,其它设备影响(片选不唯一),串口波特率,一直在复位,晶振不工作 */ xWriteCH376Cmd( CMD11_SET_USB_MODE ); /* 设备USB工作模式 */ xWriteCH376Data( 0x06 ); mDelayuS( 20 ); res = xReadCH376Data( ); // xEndCH376Cmd( ); // 并口方式不需要 if ( res == CMD_RET_SUCCESS ) return( USB_INT_SUCCESS ); else return( ERR_USB_UNKNOWN ); /* 设置模式错