|
里面有完整的資料
單片機源程序如下:
- /* 包含的頭文件 --------------------------------------------------------------*/
- #include "bsp.h"
- #include "HSJ121.h"
- #include "i2c.h"
- #include "timer.h"
- #include "aboutdata.h"
- uint8_t HSJ121_SendData[HSJ121_SendDataLength]; //命令發送數據
- uint8_t HSJ121_ReciveData[HSJ121_ReciveDataLength]; //數據回復接收
- uint8_t HSJ121_Mode_LowScan[HSJ121ModeSetLength]={0x00,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x80,0x0A}; //低速掃描
- uint8_t HSJ121_Mode_NearInduction[HSJ121ModeSetLength]={0x01,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x80,0x0A}; //臨場接近感應
- uint8_t HSJ121_KeyScanTime[HSJ121_KeyScanTimeLength]={02,05}; //按鍵掃描時間
- //函數聲明(內部調用)
- uint8_t HSJ121_SendAndRecive(uint8_t *SendData,uint8_t *ReciveData); //發送和接收
- uint8_t HSJ121_SendComByIIC(uint8_t sla,uint8_t *s,uint8_t num); //通過IIC發送命令
- uint8_t HSJ121_RecDataByIIC(uint8_t sla,uint8_t *s); //通過IIC接收回復數據
- //函數聲明(外部調用)
- void HSJ121Init(void); //模塊初始化
- uint8_t HSJ121_Set_LowScanMode(uint8_t *Mode); //設置低速掃描模式
- uint8_t HSJ121_Set_NearInductionMode(uint8_t *Mode); //設置臨近感應模式
- uint8_t HSJ121_Get_Config(void); //獲取配置信息
- uint8_t HSJ121_Get_Key(void); //獲取鍵值
- uint8_t HSJ121_Set_ScanTime(uint8_t *ScanTme); //設置按鍵掃描時間
- uint8_t HSJ121_Get_ScanTime(void); //獲取按鍵掃描時間
- uint8_t HSJ121_Set_SleepMode(void); //設置睡眠模式
- uint8_t HSJ121_Into_SleepMode(void); //進入睡眠模式
- uint8_t HSJ121_WakeUp(void); //喚醒芯片
- /*--------------------------內部調用函數---------------------------------------*/
- /************************************************
- 函數名稱 : SendComByIIC
- 功 能 : 通過IIC發送命令數據幀
- 參 數 : uint8_t sla —— 器件地址
- uint8_t *s —— 命令數據緩沖區
- uint8_t num —— 命令長度
- 返 回 值 : TRUE —— 正確
- FALSE —— 錯誤
- 作 者 :
- *************************************************/
- uint8_t HSJ121_SendComByIIC(uint8_t sla,uint8_t *s,uint8_t num)
- {
- uint8_t i,j;
- uint8_t ack=2;
- I2C_Start(); //啟動信號
- //發送器件地址
- ack=I2C_WriteByte(sla|I2C_WR);
- if(ack==I2C_NOACK){
- // printf("send address fail by write\n");
- return FALSE;
- }else if(ack==I2C_ACK){
- // printf("send address success by write\n");
- }
- TIMDelay_Nms(1); //延時
- //循環發送命令數據幀
- for(i=0;i<num;i++)
- {
- ack=I2C_WriteByte(*s);
- if(ack==I2C_NOACK){
- // printf("send command data fail\n");
- return FALSE;
- }
- s++;
- TIMDelay_Nms(1); //延時
- }
- I2C_Stop(); //停止信號
- return TRUE;
- }
- /************************************************
- 函數名稱 : RecDataByIIC
- 功 能 : 通過IIC接收模塊返回數據幀
- 參 數 : uint8_t sla —— 器件地址
- uint8_t *s —— 接收回復數據幀緩沖區
- uint8_t num —— 回復數據幀長度
- 返 回 值 : TRUE —— 正確
- FALSE —— 錯誤
- 作 者 :
- *************************************************/
- uint8_t HSJ121_RecDataByIIC(uint8_t sla,uint8_t *s)
- {
- uint8_t i=0;
- uint8_t HSJ121_Length;
- uint8_t j=0;
- uint8_t ack=2;
- uint8_t num;
-
- j=250;
- while(1)
- {
- I2C_Start(); //啟動信號
- ack=I2C_WriteByte(sla+1); //發送器件地址+寫入命令
- // TIMDelay_Nms(5); //延時
- if(ack==I2C_ACK){
- // printf("Send address success by read\n");
- break; //模塊應答成功,退出循環,準備接收數據
- }
- //模塊超時沒有應答,返回出錯
- if(j--==0){
- I2C_Stop();
- // printf("Send address fail by read\n");
- return FALSE;
- }
- }
-
- // TIMDelay_Nms(1000); //延時
- HSJ121_Length=I2C_ReadByte(I2C_ACK); //接收第一個字節數據,獲取數據幀長度
- *s = HSJ121_Length;
- s++;
-
- if(HSJ121_Length==0x00){
- I2C_Stop();
- return FALSE;
- }
-
- for(i=0;i<HSJ121_Length-1;i++) //循環接受數據(除校驗碼外)
- {
- *s=I2C_ReadByte(I2C_ACK);
- s++;
- }
-
- //接收檢驗碼
- *s=I2C_ReadByte(I2C_NOACK);
- I2C_Stop(); //停止信號
- return TRUE;
- }
- /************************************************
- 函數名稱 : HSJ121_SendAndRecive(uint8_t *SendData,uint8_t *ReciveData)
- 功 能 : 命令發送和回復數據接收
- 參 數 : *SendData -- 發送命令緩沖區
- *ReciveData -- 接收數據緩沖區
- 返 回 值 : TRUE —— 正確
- FALSE —— 錯誤
- 作 者 :
- *************************************************/
- uint8_t HSJ121_SendAndRecive(uint8_t *SendData,uint8_t *ReciveData)
- {
- uint8_t Command;
- uint8_t RepetCnt=5;
-
- Command=SendData[CMD];
-
- SendData[SendData[Length]]=GetBcc(SendData,SendData[Length]); //獲取BCC校驗碼
-
- //發送命令數據幀
- while(RepetCnt) //重復次數,循環幾次,避免模塊沒有及時發出ACK而退出
- {
- RepetCnt--;
- //發送命令數據幀
- if(HSJ121_SendComByIIC(HSJ121_ADD,SendData,(SendData[Length]+1))==TRUE){
- // printf("Send command success\n");
- break;
- }
- //發送命令數據幀出錯
- if(RepetCnt==0){
- // printf("IIC send command timeout\n");
- return FALSE;
- }
- }
-
- TIMDelay_Nms(5);
- RepetCnt=250;
-
- //接收模塊返回數據幀
- while(RepetCnt)
- {
- RepetCnt--;
-
- if(HSJ121_RecDataByIIC(HSJ121_ADD,ReciveData)==TRUE){
- // printf("Receive data success\n");
- break;
- }
-
- //接收模塊返回數據幀出錯
- if(RepetCnt==0){
- // printf("IIC receive data timeout\n");
- return FALSE;
- }
- }
-
- //對模塊返回的數據幀進行校驗
- if(!GetBcc(ReciveData,ReciveData[Length]+1)){
- // printf("Bcc check success\n");
- if(Command==ReciveData[CMD]){
- // printf("Command check success\n");
- return TRUE; //校驗成功
- }else{
- // printf("Command check success\n");
- }
- }else{
- // printf("Bcc check fail\n");
- }
- return FALSE;
- }
- /*--------------------------外部調用函數---------------------------------------*/
- /******************************************/
- //函數原型: void HSJ121Init(void)
- //輸入參數: 無
- //輸出參數: 無
- //函數返回:
- //功能描述: 芯片初始化
- /******************************************/
- void HSJ121Init(void)
- {
- I2C_Initializes(); //I2C通信初始化
- TIMDelay_Nms(10); //上電等待3ms,芯片穩定
- printf("HSJ121 init success\n");
-
- }
- /******************************************/
- //函數原型: HSJ121_Set_LOWScanMode(uint8_t *Mode)
- //輸入參數: *Mode 設置模式碼(10個字節)
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 設置低速掃描模式
- /******************************************/
- uint8_t HSJ121_Set_LowScanMode(uint8_t *Mode)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x0C; //添加數據長度
- HSJ121_SendData[CMD]=0x70; //添加命令碼
- CopyData(&HSJ121_SendData[DataPOS],Mode,10);
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Set_NearInductionMode(uint8_t *Mode)
- //輸入參數: *Mode 設置模式碼(10個字節)
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 設置臨近感應模式
- /******************************************/
- uint8_t HSJ121_Set_NearInductionMode(uint8_t *Mode)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x0C; //添加數據長度
- HSJ121_SendData[CMD]=0x70; //添加命令碼
- CopyData(&HSJ121_SendData[DataPOS],Mode,10);
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: uint8_t HSJ121_Get_Config()
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 獲取配置信息
- /******************************************/
- uint8_t HSJ121_Get_Config(void)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x02; //添加數據長度
- HSJ121_SendData[CMD]=0x80; //添加命令碼
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Get_Key(void)
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 獲取鍵值
- /******************************************/
- uint8_t HSJ121_Get_Key(void)
- {
- uint8_t num;
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x02; //添加數據長度
- HSJ121_SendData[CMD]=0xA0; //添加命令碼
-
- for(num=0;num<3;num++)
- {
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
- if(State==TRUE){
- break;
- }
- }
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Set_ScanTime(uint8_t *ScanTme)
- //輸入參數: *ScanTme 掃描時間
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 設置掃描時間
- /******************************************/
- uint8_t HSJ121_Set_ScanTime(uint8_t *ScanTme) //設置按鍵掃描時間
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x04; //添加數據長度
- HSJ121_SendData[CMD]=0x71; //添加命令碼
- CopyData(&HSJ121_SendData[DataPOS],ScanTme,HSJ121_KeyScanTimeLength);
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Get_ScanTime(void)
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 獲取掃描時間
- /******************************************/
- uint8_t HSJ121_Get_ScanTime(void) //獲取按鍵掃描時間
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x02; //添加數據長度
- HSJ121_SendData[CMD]=0x81; //添加命令碼
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Set_SleepMode(void)
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 設置睡眠模式
- /******************************************/
- uint8_t HSJ121_Set_SleepMode(void)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x03; //添加數據長度
- HSJ121_SendData[CMD]=0x91; //添加命令碼
- // HSJ121_SendData[DataPOS]=0x00; //關閉睡眠模式
- HSJ121_SendData[DataPOS]=0x01; //使能睡眠模式
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_Into_SleepMode(void)
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 進入睡眠模式
- /******************************************/
- uint8_t HSJ121_Into_SleepMode(void)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x02; //添加數據長度
- HSJ121_SendData[CMD]=0x90; //添加命令碼
-
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
- /******************************************/
- //函數原型: HSJ121_WakeUp()
- //輸入參數: 無
- //輸出參數: 無
- //函數返回: TRUE or FALSE
- //功能描述: 喚醒芯片,進入工作模式
- /******************************************/
- uint8_t HSJ121_WakeUp(void)
- {
- uint8_t State;
- EXTI->IMR&=~(1<<13); //屏蔽13線上中斷
-
- ClearData(HSJ121_SendData,HSJ121_SendDataLength); //清零發送數據
- ClearData(HSJ121_ReciveData,HSJ121_ReciveDataLength); //清零接收數據
-
- HSJ121_SendData[Length]=0x02; //添加數據長度
- HSJ121_SendData[CMD]=0xA6; //添加命令碼
- State=HSJ121_SendAndRecive(HSJ121_SendData,HSJ121_ReciveData);
-
- // 將模塊回復的數據從HEX碼轉成Char
- HexToChar(HSJ121_ReciveData,(HSJ121_ReciveData[Length]+1));
-
- EXTI->IMR|=1<<13; //開13線上中斷
- return State;
- }
-
復制代碼
- //////////////////////////////////////////////////////////////////////////////////
- //本程序只供測試使用,不能用作產品代碼
- //////////////////////////////////////////////////////////////////////////////////
- /* 包含的頭文件 --------------------------------------------------------------*/
- #include "bsp.h"
- #include "i2c.h"
- #include "timer.h"
- #include "usart.h"
- #include "aboutdata.h"
- #include "HSJ121.h"
- /************************************************
- 函數名稱 : System_Initializes
- 功 能 : 系統初始化
- 參 數 : 無
- 返 回 值 : 無
- 作 者 :
- *************************************************/
- void System_Initializes(void)
- {
- BSP_Initializes(); //底層初始化
- TIMER_Initializes(); //TIMER初始化
- USART_Initializes(); //串口1初始化
- }
- /************************************************
- 函數名稱 : main
- 功 能 : 主函數入口
- 參 數 : 無
- 返 回 值 : int
- 作 者 :
- *************************************************/
- int main(void)
- {
- System_Initializes(); //系統初始化
- HSJ121Init(); //芯片初始化
- //所有命令都必須在喚醒成功的狀態下執行
- //---------------------芯片喚醒-------------------------//
- #if HSJ_WakeUp_en
- if(HSJ121_WakeUp()==TRUE){
- printf("Wake Up Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- //-----------------------------------------------------//
- //---------------------低速掃描模式---------------------//
- #if HSJ_LowScanMode_en
- if(HSJ121_Set_LowScanMode(HSJ121_Mode_LowScan)==TRUE){
- printf("Set Low Scan Mode Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Set Low Scan Mode Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------接近感應模式---------------------//
- #if HSJ_NearInductionMode_en
- if(HSJ121_Set_NearInductionMode(HSJ121_Mode_NearInduction)==TRUE){
- printf("Set Near Induction Mode Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Set Near Induction Mode Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------獲取配置-------------------------//
- #if HSJ_GetConfig_en
- if(HSJ121_Get_Config()==TRUE){
- printf("Get Config Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Get Config Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------設置掃描時間----------------------//
- #if HSJ_SetScanTime_en
- if(HSJ121_Set_ScanTime(HSJ121_KeyScanTime)==TRUE){
- printf("Set Scan Time Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Set Scan Time Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------獲取掃描時間----------------------//
- #if HSJ_GetScanTime_en
- if(HSJ121_Get_ScanTime()==TRUE){
- printf("Get Scan Time Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Get Scan Time Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------設置睡眠模式----------------------//
- #if HSj_SetSleepMode_en
- if(HSJ121_Set_SleepMode()==TRUE){
- printf("Set Sleep Mode Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Set Sleep Mode Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //---------------------進入睡眠模式---------------------//
- #if HSJ_IntoSleepMode_en
- if(HSJ121_Into_SleepMode()==TRUE){
- printf("Into Sleep Mode Success\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }else{
- printf("Into Sleep Mode Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
- //-----------------------------------------------------//
- }else{
- printf("Wake Up Fail\n");
- USART1_PrintfNChar(CharData,strlen(CharData));
- }
- #endif
-
- while(1);
- }
- /******************************* end ************************************/
復制代碼
所有資料51hei提供下載:
HSJ121-STM32F103RCT6.7z
(391.06 KB, 下載次數: 15)
2019-3-2 17:40 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|