|
三、燒錄程序 把ST-Link插入到電腦上, 通過仿真下載即可!
下載完成后,可以把ST-Link拔了,不需要用到了;然后把KM2模塊的USB通過OTG轉(zhuǎn)換線插到手機上就可以了

- #include <math.h>
- #include <stdbool.h>
- #include "APDS_9960.h"
- #include "delay.h"
- #include "myiic.h"
- #include "LED.h"
- #include "usart.h"
- #include <stdio.h>
- #include "EXTI.h"
- extern int abs(int __x);
- void Wire_begin_(void);
- /* Container for gesture data */
- typedef struct gesture_data_type {
- uint8_t u_data[32];
- uint8_t d_data[32];
- uint8_t l_data[32];
- uint8_t r_data[32];
- uint8_t index;
- uint8_t total_gestures;
- uint8_t in_threshold;
- uint8_t out_threshold;
- } gesture_data_type;
- /* Members */
- gesture_data_type gesture_data_;
-
- int gesture_ud_delta_;
- int gesture_lr_delta_;
- int gesture_ud_count_;
- int gesture_lr_count_;
- int gesture_near_count_;
- int gesture_far_count_;
- int gesture_state_;
- int gesture_motion_;
- /**
- * @brief Constructor - Instantiates SparkFun_APDS9960 object
- */
- void SparkFun_APDS9960(void)
- {
- gesture_ud_delta_ = 0;//UP -->DOWN
- gesture_lr_delta_ = 0;//LEFT--> RIGHT
-
- gesture_ud_count_ = 0;
- gesture_lr_count_ = 0;
-
- gesture_near_count_ = 0;
- gesture_far_count_ = 0;
-
- gesture_state_ = 0;
- gesture_motion_ = DIR_NONE;
- }
-
- /**
- * @brief Configures I2C communications and initializes registers to defaults
- *
- * @return True if initialized successfully. False otherwise.
- */
- bool SparkFun_APDS9960_init(void)
- {
- uint8_t id=0,*pid;
- /* Initialize I2C */
- Wire_begin_();//初始化I2C
-
- /* 讀取器件ID 0x92 = 0xAB */
- if( !wireReadDataByte(APDS9960_ID, pid) )
- {
- return false;
- }
- id=*pid;
- printf("ID = %.2X\r\n",id);// 打印器件ID = 0xAB
- // if(!((id == APDS9960_ID_1 || id == APDS9960_ID_2)) )
- // {
- // return false;
- // }
- if(!(id !=0xFF) )
- {
- return false;
- }
- /* 失能失能寄存器0x80 = 0x00 */
- if( !setMode(ALL, OFF) ) //(7,0)
- {
- return false;
- }
-
- //設(shè)置手勢接近進入(手勢狀態(tài)機)閥值為0xA0 = 40
- /* 設(shè)置手勢傳感器寄存器默認值 */
- if( !setGestureEnterThresh(DEFAULT_GPENTH) )
- {
- return false;
- }
- //設(shè)置手勢接近退出(手勢狀態(tài)機)閥值為0xA1 = 30
- if( !setGestureExitThresh(DEFAULT_GEXTH) )
- {
- return false;
- }
- //設(shè)置配置寄存器1 0xA2 = 0x40
- //1.在4個數(shù)據(jù)集被添加到FIFO里后產(chǎn)生中斷
- //2.All UDLR 探測數(shù)據(jù)被包含到集合中
- //3.手勢退出持久性.當(dāng)連續(xù)的手勢結(jié)束發(fā)生稱為比GEXPERS大于或等于的值時,
- // 手勢狀態(tài)機退出(第1個手勢結(jié)束發(fā)生導(dǎo)致手勢狀態(tài)機退出)
- if( !wireWriteDataByte(APDS9960_GCONF1, DEFAULT_GCONF1) )
- {
- return false;
- }
- //設(shè)置配置寄存器2 0xA3 的 bit 6:5 = 10 4x增益
- if( !setGestureGain(DEFAULT_GGAIN) )
- {
- return false;
- }
- //設(shè)置配置寄存器2 0xA3 的 bit 4:3 = 00 100ma
- if( !setGestureLEDDrive(DEFAULT_GLDRIVE) )
- {
- return false;
- }
- //設(shè)定配置寄存器2 0xA3 的 bit 2:0=001 2.8ms
- if( !setGestureWaitTime(DEFAULT_GWTIME) )
- {
- return false;
- }
- //設(shè)置手勢UP偏移寄存器 0xA4 = 0 沒有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //設(shè)置手勢DOWN偏移寄存器 0xA5 = 0 沒有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //設(shè)置手勢LEFT偏移寄存器 0xA7 = 0 沒有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //設(shè)置手勢RIGHT偏移寄存器 0xA9 = 0 沒有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //設(shè)置收勢脈沖數(shù)和脈寬寄存器0xA6 = 0xC9 32us, 10 pulses
- if( !wireWriteDataByte(APDS9960_GPULSE, DEFAULT_GPULSE) )
- {
- return false;
- }
- //設(shè)置配置寄存器3 0xAA 的bit 1:0 = 00 所有光電二極管在手勢期間均有效
- if( !wireWriteDataByte(APDS9960_GCONF3, DEFAULT_GCONF3) )
- {
- return false;
- }
- //設(shè)置配置寄存器4 0xAB 的bit1 = 0 關(guān)閉手勢中斷 GIEN=0
- if( !setGestureIntEnable(DEFAULT_GIEN) )
- {
- return false;
- }
-
- return true;
-
-
- }
- /*******************************************************************************
- * Public methods for controlling the APDS-9960
- ******************************************************************************/
- /**
- * @brief Reads and returns the contents of the ENABLE register
- *
- * @return Contents of the ENABLE register. 0xFF if error.
- */
- uint8_t getMode(void)
- {
- uint8_t enable_value;
-
- /* Read current ENABLE register */
- if( !wireReadDataByte(APDS9960_ENABLE, &enable_value) )
- {
- return ERROR;
- }
-
- return enable_value;
- }
- /**
- * @brief Enables or disables a feature in the APDS-9960
- *
- #define POWER 0
- #define AMBIENT_LIGHT 1
- #define PROXIMITY 2
- #define WAIT 3
- #define AMBIENT_LIGHT_INT 4
- #define PROXIMITY_INT 5
- #define GESTURE 6
- #define ALL 7
- * @param[in] mode which feature to enable
- * @param[in] enable ON (1) or OFF (0)
- * @return True if operation success. False otherwise.
- mode = ALL 7 enable = OFF 0
- */
- bool setMode(int8_t mode, uint8_t enable)
- {
- uint8_t reg_val;
- /* Read current ENABLE register */
- reg_val = getMode();
- // printf("First_setMode_regval = %.2x\n",reg_val);//打印讀取到的使能寄存器的值 0x80 = 0x4d
- if( reg_val == ERROR ) {//如果讀取到的值為0xFF,則錯誤
- return false;
- }
-
- /* Change bit(s) in ENABLE register */
- enable = enable & 0x01;
- if((mode >= 0) && (mode <= 6)) //使能或失能某個位
- {
- if(enable) //使能
- {
- reg_val |= (1 << mode);
- }
- else //失能
- {
- reg_val &= ~(1 << mode);
- }
- }
- else if( mode == ALL ) //使能全部
- {
- if (enable)
- {
- reg_val = 0x7F;//0x80=0x7F 全部使能
- }
- else //全部使能
- {
- reg_val = 0x00;//0x80=0x00
- // printf("0x80 = 0x00 all disable\n");
- }
- }
-
- // printf("Last_setMode_regval = %.2x\n",reg_val);//打印讀取到的使能寄存器的值 0x80 = 0x4d
- /* Write value back to ENABLE register */
- if( !wireWriteDataByte(APDS9960_ENABLE, reg_val) )
- {
- return false;
- }
-
- return true;
- }
- /**
- * @brief Starts the gesture recognition engine on the APDS-9960
- *
- * @param[in] interrupts true to enable hardware external interrupt on gesture
- * @return True if engine enabled correctly. False on error.
- */
- bool enableGestureSensor(bool interrupts)
- {
-
- /* Enable gesture mode
- Set ENABLE to 0 (power off)
- Set WTIME to 0xFF
- Set AUX to LED_BOOST_300
- Enable PON, WEN, PEN, GEN in ENABLE
- */
- //interrupts = true;
-
- resetGestureParameters();//復(fù)位手勢變量=0
-
- //設(shè)置等待時間寄存器0x83 = 0xFF (WLONG=1 0.03s) (WLONG=0 2.78ms)
- if( !wireWriteDataByte(APDS9960_WTIME, 0xFF) ) //
- {
- return false;
- }
-
- //設(shè)置接近脈沖計數(shù)寄存器 0x8E = 0x89 16us, 10 pulses
- if( !wireWriteDataByte(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) )
- {
- return false;
- }
-
- //設(shè)置配置寄存器2 0x90的bit5:4=11 %300 LED驅(qū)動電流
- if( !setLEDBoost(LED_BOOST_300) )
- {
- return false;
- }
-
復(fù)制代碼
全部STM32代碼下載:
STM32F103驅(qū)動APDS9960手勢模塊.7z
(212.32 KB, 下載次數(shù): 51)
2021-7-23 23:27 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|