在STM32F103VC下采用并口的方式訪問(wèn)CH376,可以確認(rèn)硬件鏈路沒(méi)有問(wèn)題,從STM32輸出的數(shù)據(jù)和控制信號(hào)都測(cè)試正確,但是給CH376寫(xiě)入0x06命令后,再寫(xiě)入一個(gè)0x65,讀出數(shù)據(jù)恒定為0xFF,請(qǐng)技術(shù)支持幫忙分析一下,非常感謝!
以下是測(cè)試代碼: /* 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 /* 未知錯(cuò)誤,不應(yīng)該發(fā)生的情況,需檢查硬件或者程序錯(cuò)誤 */
#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) //--------------------------------------------------------------------------------------------------------// /* 延時(shí)指定微秒時(shí)間,根據(jù)單片機(jī)主頻調(diào)整,不精確 */ void mDelayuS( UINT8 us ) { UINT8 i; while (us) /* 24MHz MCS51 */ { for(i=0;i<10;i++); us -- ; } }
/* 延時(shí)指定毫秒時(shí)間,根據(jù)單片機(jī)主頻調(diào)整,不精確 */ 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) /* 寫(xiě)并口低八位輸出數(shù)據(jù) */ { unsigned int D_Temp = 0; D_Temp = D_Temp + d; GPIO_Write(GPIOD,D_Temp); } //--------------------------------------------------------------------------------------------------------// UINT8 CH376_DATA_DAT_IN(void ) /* 讀并口低八位輸入數(shù)據(jù) */ { unsigned int D_Temp; UINT8 d; D_Temp = GPIO_ReadInputData(GPIOD); d = (UINT8)D_Temp; return d; } //--------------------------------------------------------------------------------------------------------// void CH376_DATA_DIR_OUT(void ) /* 設(shè)置并口低八位方向?yàn)檩敵?*/ { 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 ) /* 設(shè)置并口低八位方向?yàn)檩斎?*/ { 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的控制引腳和數(shù)據(jù)引腳 { 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#引腳,如果未連接那么也可以通過(guò)查詢狀態(tài)端口實(shí)現(xiàn) */ //--------------------------------------------------------------------------------------------------------// void CH376_PORT_INIT( void ) /* 由于使用通用I/O模擬并口讀寫(xiě)時(shí)序,所以進(jìn)行初始化 */ { CH376_CS_1; mDelayuS(50); CH376_WR_1; mDelayuS(50); CH376_RD_1; mDelayuS(50); CH376_A0_0; mDelayuS(50); CH376_DATA_DIR_OUT( ); /* 設(shè)置并口輸入 */ }
//void mDelay0_5uS( void ) /* 至少延時(shí)0.5uS,根據(jù)單片機(jī)主頻調(diào)整 */ //{ //}
void xEndCH376Cmd( void ) /* 結(jié)束CH376命令,僅用于SPI接口方式 */ {
}
UINT8 xReadCH376Status( void ) /* 從CH376讀狀態(tài),僅用于并口方式 */ { UINT8 mData; CH376_A0_1; CH376_DATA_DIR_IN( ); /* 設(shè)置并口方向?yàn)檩斎?*/ // mDelayuS(10); CH376_CS_0; CH376_RD_0; /* 輸出有效讀控制信號(hào), 讀CH376芯片的狀態(tài)端口 */ mDelayuS(10); CH376_RD_1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH376芯片 */ mDelayuS(10); mData = CH376_DATA_DAT_IN( ); /* 從CH376的并口輸入數(shù)據(jù) */ CH376_CS_1; // mDelayuS(10); CH376_A0_0; return( mData ); }
void xWriteCH376Cmd( UINT8 mCmd ) /* 向CH376寫(xiě)命令 */ { CH376_A0_1; CH376_DATA_DIR_OUT( ); /* 設(shè)置并口方向?yàn)檩敵?*/ CH376_DATA_DAT_OUT( mCmd ); /* 向CH376的并口輸出數(shù)據(jù) */ mDelayuS(10); CH376_CS_0; CH376_WR_0; /* 輸出有效寫(xiě)控制信號(hào), 寫(xiě)CH376芯片的命令端口 */ mDelayuS(50); CH376_WR_1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH376芯片 */ CH376_CS_1; mDelayuS(10); CH376_A0_0; mDelayuS(50); // CH376_DATA_DIR_IN( ); /* 禁止數(shù)據(jù)輸出 */ }
void xWriteCH376Data( UINT8 mData ) /* 向CH376寫(xiě)數(shù)據(jù) */ { CH376_A0_0; mDelayuS(10); CH376_DATA_DIR_OUT( ); /* 設(shè)置并口方向?yàn)檩敵?*/ CH376_DATA_DAT_OUT( mData ); /* 向CH376的并口輸出數(shù)據(jù) */ mDelayuS(10); CH376_CS_0; CH376_WR_0; /* 輸出有效寫(xiě)控制信號(hào), 寫(xiě)CH376芯片的數(shù)據(jù)端口 */ mDelayuS(50); CH376_WR_1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH376芯片 */ CH376_CS_1; mDelayuS(10); CH376_A0_0; // CH376_DATA_DIR_IN( ); /* 禁止數(shù)據(jù)輸出 */ mDelayuS(50); }
UINT8 xReadCH376Data( void ) /* 從CH376讀數(shù)據(jù) */ { UINT8 mData; CH376_A0_0; // mDelayuS(10); CH376_DATA_DIR_IN( ); /* 設(shè)置并口方向?yàn)檩斎?*/ // mDelayuS(10); CH376_CS_0; CH376_RD_0; /* 輸出有效讀控制信號(hào), 讀CH376芯片的數(shù)據(jù)端口 */ mDelayuS(20); CH376_RD_1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH376芯片 */ mDelayuS(20); mData = CH376_DATA_DAT_IN( ); /* 從CH376的并口輸入數(shù)據(jù) */ CH376_CS_1; // mDelayuS(10); return( mData ); }
/* 查詢CH376中斷(INT#低電平) */
UINT8 Query376Interrupt( void ) { return( xReadCH376Status( ) & PARA_STATE_INTB ? FALSE : TRUE ); // 如果未連接CH376的中斷引腳則查詢狀態(tài)端口 }
UINT8 mInitCH376Host( void ) /* 初始化CH376 */ { UINT8 res; CH376_RST_1; mDelayuS( 250 ); mDelayuS( 250 ); CH376_RST_0; CH376_PORT_INIT( ); /* 接口硬件初始化 */ xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 測(cè)試單片機(jī)與CH376之間的通訊接口 */ xWriteCH376Data( 0x65 ); res = xReadCH376Data( );
if ( res != 0x9A ) return( ERR_USB_UNKNOWN ); /* 通訊接口不正常,可能原因有:接口連接異常,其它設(shè)備影響(片選不唯一),串口波特率,一直在復(fù)位,晶振不工作 */ xWriteCH376Cmd( CMD11_SET_USB_MODE ); /* 設(shè)備USB工作模式 */ xWriteCH376Data( 0x06 ); mDelayuS( 20 ); res = xReadCH376Data( ); // xEndCH376Cmd( ); // 并口方式不需要 if ( res == CMD_RET_SUCCESS ) return( USB_INT_SUCCESS ); else return( ERR_USB_UNKNOWN ); /* 設(shè)置模式錯(cuò)