三、烧录程序 把ST-Link插入到电脑上, 通过仿真下载即可!
下载完成后,可以把ST-Link拔了,不需要用到了;然后把KM2模块的USB通过OTG转换线插到手机上就可以了
- #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;
- }
-
- //设置手势接近进入(手势状态机)阀值为0xA0 = 40
- /* 设置手势传感器寄存器默认值 */
- if( !setGestureEnterThresh(DEFAULT_GPENTH) )
- {
- return false;
- }
- //设置手势接近退出(手势状态机)阀值为0xA1 = 30
- if( !setGestureExitThresh(DEFAULT_GEXTH) )
- {
- return false;
- }
- //设置配置寄存器1 0xA2 = 0x40
- //1.在4个数据集被添加到FIFO里后产生中断
- //2.All UDLR 探测数据被包含到集合中
- //3.手势退出持久性.当连续的手势结束发生称为比GEXPERS大于或等于的值时,
- // 手势状态机退出(第1个手势结束发生导致手势状态机退出)
- if( !wireWriteDataByte(APDS9960_GCONF1, DEFAULT_GCONF1) )
- {
- return false;
- }
- //设置配置寄存器2 0xA3 的 bit 6:5 = 10 4x增益
- if( !setGestureGain(DEFAULT_GGAIN) )
- {
- return false;
- }
- //设置配置寄存器2 0xA3 的 bit 4:3 = 00 100ma
- if( !setGestureLEDDrive(DEFAULT_GLDRIVE) )
- {
- return false;
- }
- //设定配置寄存器2 0xA3 的 bit 2:0=001 2.8ms
- if( !setGestureWaitTime(DEFAULT_GWTIME) )
- {
- return false;
- }
- //设置手势UP偏移寄存器 0xA4 = 0 没有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //设置手势DOWN偏移寄存器 0xA5 = 0 没有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //设置手势LEFT偏移寄存器 0xA7 = 0 没有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //设置手势RIGHT偏移寄存器 0xA9 = 0 没有偏移
- if( !wireWriteDataByte(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) )
- {
- return false;
- }
- //设置收势脉冲数和脉宽寄存器0xA6 = 0xC9 32us, 10 pulses
- if( !wireWriteDataByte(APDS9960_GPULSE, DEFAULT_GPULSE) )
- {
- return false;
- }
- //设置配置寄存器3 0xAA 的bit 1:0 = 00 所有光电二极管在手势期间均有效
- if( !wireWriteDataByte(APDS9960_GCONF3, DEFAULT_GCONF3) )
- {
- return false;
- }
- //设置配置寄存器4 0xAB 的bit1 = 0 关闭手势中断 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();//复位手势变量=0
-
- //设置等待时间寄存器0x83 = 0xFF (WLONG=1 0.03s) (WLONG=0 2.78ms)
- if( !wireWriteDataByte(APDS9960_WTIME, 0xFF) ) //
- {
- return false;
- }
-
- //设置接近脉冲计数寄存器 0x8E = 0x89 16us, 10 pulses
- if( !wireWriteDataByte(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) )
- {
- return false;
- }
-
- //设置配置寄存器2 0x90的bit5:4=11 %300 LED驱动电流
- if( !setLEDBoost(LED_BOOST_300) )
- {
- return false;
- }
-
复制代码
全部STM32代码下载:
STM32F103驱动APDS9960手势模块.7z
(212.32 KB, 下载次数: 51)
|