找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2040|回复: 1
打印 上一主题 下一主题
收起左侧

STM32单片机做一个手势刷视频2 APDS9960手势模块源程序

[复制链接]
跳转到指定楼层
楼主
三、烧录程序
把ST-Link插入到电脑上,
通过仿真下载即可!

下载完成后,可以把ST-Link拔了,不需要用到了;然后把KM2模块的USB通过OTG转换线插到手机上就可以了


  1. #include <math.h>
  2. #include <stdbool.h>

  3. #include "APDS_9960.h"
  4. #include "delay.h"
  5. #include "myiic.h"
  6. #include "LED.h"

  7. #include "usart.h"
  8. #include <stdio.h>
  9. #include "EXTI.h"



  10. extern int abs(int __x);


  11. void Wire_begin_(void);


  12. /* Container for gesture data */
  13. typedef struct gesture_data_type {
  14.     uint8_t u_data[32];
  15.     uint8_t d_data[32];
  16.     uint8_t l_data[32];
  17.     uint8_t r_data[32];
  18.     uint8_t index;
  19.     uint8_t total_gestures;
  20.     uint8_t in_threshold;
  21.     uint8_t out_threshold;
  22. } gesture_data_type;


  23. /* Members */
  24. gesture_data_type gesture_data_;





  25. int gesture_ud_delta_;
  26. int gesture_lr_delta_;
  27. int gesture_ud_count_;
  28. int gesture_lr_count_;
  29. int gesture_near_count_;
  30. int gesture_far_count_;
  31. int gesture_state_;
  32. int gesture_motion_;






  33. /**
  34. * @brief Constructor - Instantiates SparkFun_APDS9960 object
  35. */
  36. void SparkFun_APDS9960(void)
  37. {
  38.     gesture_ud_delta_ = 0;//UP -->DOWN
  39.     gesture_lr_delta_ = 0;//LEFT--> RIGHT
  40.    
  41.     gesture_ud_count_ = 0;
  42.     gesture_lr_count_ = 0;
  43.    
  44.     gesture_near_count_ = 0;
  45.     gesture_far_count_ = 0;
  46.    
  47.     gesture_state_ = 0;
  48.     gesture_motion_ = DIR_NONE;
  49. }


  50. /**
  51. * @brief Configures I2C communications and initializes registers to defaults
  52. *
  53. * @return True if initialized successfully. False otherwise.
  54. */
  55. bool SparkFun_APDS9960_init(void)
  56. {
  57.     uint8_t id=0,*pid;

  58.     /* Initialize I2C */
  59.     Wire_begin_();//初始化I2C
  60.             
  61.     /* 读取器件ID 0x92 = 0xAB */
  62.    if( !wireReadDataByte(APDS9960_ID, pid) )
  63.         {
  64.                 return false;
  65.         }        
  66.    id=*pid;
  67.         printf("ID = %.2X\r\n",id);// 打印器件ID = 0xAB
  68. //    if(!((id == APDS9960_ID_1 || id == APDS9960_ID_2)) )
  69. //        {
  70. //        return false;
  71. //    }
  72.         if(!(id !=0xFF) )
  73.         {
  74.         return false;
  75.     }

  76.     /* 失能失能寄存器0x80 = 0x00 */
  77.     if( !setMode(ALL, OFF) ) //(7,0)
  78.         {
  79.         return false;
  80.     }

  81.         

  82.         //设置手势接近进入(手势状态机)阀值为0xA0 = 40
  83.     /* 设置手势传感器寄存器默认值 */
  84.     if( !setGestureEnterThresh(DEFAULT_GPENTH) )
  85.         {
  86.         return false;
  87.     }

  88.         //设置手势接近退出(手势状态机)阀值为0xA1 = 30
  89.     if( !setGestureExitThresh(DEFAULT_GEXTH) )
  90.         {
  91.         return false;
  92.     }

  93.         //设置配置寄存器1 0xA2 = 0x40
  94.         //1.在4个数据集被添加到FIFO里后产生中断
  95.         //2.All UDLR 探测数据被包含到集合中
  96.         //3.手势退出持久性.当连续的手势结束发生称为比GEXPERS大于或等于的值时,
  97.         //  手势状态机退出(第1个手势结束发生导致手势状态机退出)
  98.     if( !wireWriteDataByte(APDS9960_GCONF1, DEFAULT_GCONF1) )
  99.         {
  100.         return false;
  101.     }

  102.         //设置配置寄存器2 0xA3 的 bit 6:5 = 10  4x增益
  103.     if( !setGestureGain(DEFAULT_GGAIN) )
  104.         {
  105.         return false;
  106.     }   

  107.         //设置配置寄存器2 0xA3 的 bit 4:3 = 00  100ma
  108.     if( !setGestureLEDDrive(DEFAULT_GLDRIVE) )
  109.         {
  110.         return false;
  111.     }     

  112.         //设定配置寄存器2 0xA3 的 bit 2:0=001   2.8ms
  113.     if( !setGestureWaitTime(DEFAULT_GWTIME) )
  114.         {
  115.         return false;
  116.     }

  117.         //设置手势UP偏移寄存器 0xA4 = 0 没有偏移
  118.     if( !wireWriteDataByte(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) )
  119.         {
  120.         return false;
  121.     }   

  122.         //设置手势DOWN偏移寄存器 0xA5 = 0 没有偏移
  123.     if( !wireWriteDataByte(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) )
  124.         {
  125.         return false;
  126.     }

  127.         //设置手势LEFT偏移寄存器 0xA7 = 0 没有偏移
  128.     if( !wireWriteDataByte(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) )
  129.         {
  130.         return false;
  131.     }         

  132.         //设置手势RIGHT偏移寄存器 0xA9 = 0 没有偏移
  133.     if( !wireWriteDataByte(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) )
  134.         {
  135.         return false;
  136.     }

  137.         //设置收势脉冲数和脉宽寄存器0xA6 = 0xC9   32us, 10 pulses
  138.     if( !wireWriteDataByte(APDS9960_GPULSE, DEFAULT_GPULSE) )
  139.         {
  140.         return false;
  141.     }

  142.         //设置配置寄存器3  0xAA 的bit 1:0 = 00  所有光电二极管在手势期间均有效
  143.     if( !wireWriteDataByte(APDS9960_GCONF3, DEFAULT_GCONF3) )
  144.         {
  145.         return false;
  146.     }

  147.         //设置配置寄存器4 0xAB 的bit1 = 0 关闭手势中断 GIEN=0
  148.     if( !setGestureIntEnable(DEFAULT_GIEN) )
  149.         {
  150.         return false;
  151.     }
  152.                
  153.         return true;
  154.                
  155.                
  156. }

  157. /*******************************************************************************
  158. * Public methods for controlling the APDS-9960
  159. ******************************************************************************/

  160. /**
  161. * @brief Reads and returns the contents of the ENABLE register
  162. *
  163. * @return Contents of the ENABLE register. 0xFF if error.
  164. */
  165. uint8_t getMode(void)
  166. {
  167.     uint8_t enable_value;
  168.    
  169.     /* Read current ENABLE register */
  170.     if( !wireReadDataByte(APDS9960_ENABLE, &enable_value) )               
  171.         {
  172.         return ERROR;
  173.     }
  174.    
  175.     return enable_value;
  176. }

  177. /**
  178. * @brief Enables or disables a feature in the APDS-9960
  179. *
  180.         #define POWER                   0
  181.         #define AMBIENT_LIGHT           1
  182.         #define PROXIMITY               2
  183.         #define WAIT                    3
  184.         #define AMBIENT_LIGHT_INT       4
  185.         #define PROXIMITY_INT           5
  186.         #define GESTURE                 6
  187.         #define ALL                     7
  188. * @param[in] mode which feature to enable
  189. * @param[in] enable ON (1) or OFF (0)
  190. * @return True if operation success. False otherwise.
  191.         mode = ALL 7 enable = OFF  0
  192. */
  193. bool setMode(int8_t mode, uint8_t enable)
  194. {
  195.     uint8_t reg_val;

  196.     /* Read current ENABLE register */
  197.     reg_val = getMode();
  198. //        printf("First_setMode_regval = %.2x\n",reg_val);//打印读取到的使能寄存器的值 0x80 = 0x4d
  199.     if( reg_val == ERROR ) {//如果读取到的值为0xFF,则错误
  200.         return false;
  201.     }
  202.    
  203.     /* Change bit(s) in ENABLE register */
  204.     enable = enable & 0x01;
  205.     if((mode >= 0) && (mode <= 6)) //使能或失能某个位
  206.         {
  207.                 if(enable) //使能
  208.                 {
  209.             reg_val |= (1 << mode);
  210.         }
  211.                 else //失能
  212.                 {
  213.             reg_val &= ~(1 << mode);
  214.         }
  215.     }
  216.         else if( mode == ALL ) //使能全部
  217.         {
  218.         if (enable)
  219.                 {
  220.             reg_val = 0x7F;//0x80=0x7F   全部使能
  221.         }
  222.                 else //全部使能
  223.                 {
  224.             reg_val = 0x00;//0x80=0x00
  225. //                        printf("0x80 = 0x00 all disable\n");
  226.         }
  227.     }
  228.       
  229. //        printf("Last_setMode_regval = %.2x\n",reg_val);//打印读取到的使能寄存器的值 0x80 = 0x4d
  230.     /* Write value back to ENABLE register */
  231.     if( !wireWriteDataByte(APDS9960_ENABLE, reg_val) )               
  232.         {
  233.         return false;
  234.     }
  235.         
  236.     return true;
  237. }



  238. /**
  239. * @brief Starts the gesture recognition engine on the APDS-9960
  240. *
  241. * @param[in] interrupts true to enable hardware external interrupt on gesture
  242. * @return True if engine enabled correctly. False on error.
  243. */
  244. bool enableGestureSensor(bool interrupts)
  245. {
  246.    
  247.     /* Enable gesture mode
  248.        Set ENABLE to 0 (power off)
  249.        Set WTIME to 0xFF
  250.        Set AUX to LED_BOOST_300
  251.        Enable PON, WEN, PEN, GEN in ENABLE
  252.     */
  253.           //interrupts = true;
  254.         
  255.     resetGestureParameters();//复位手势变量=0
  256.         
  257.         //设置等待时间寄存器0x83 = 0xFF (WLONG=1  0.03s)   (WLONG=0   2.78ms)
  258.     if( !wireWriteDataByte(APDS9960_WTIME, 0xFF) ) //
  259.         {
  260.         return false;
  261.     }
  262.         
  263.         //设置接近脉冲计数寄存器 0x8E = 0x89 16us, 10 pulses
  264.     if( !wireWriteDataByte(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) )
  265.         {
  266.         return false;
  267.     }
  268.         
  269.         //设置配置寄存器2 0x90的bit5:4=11  %300   LED驱动电流
  270.     if( !setLEDBoost(LED_BOOST_300) )
  271.         {
  272.         return false;
  273.     }
  274.         

复制代码

全部STM32代码下载: STM32F103驱动APDS9960手势模块.7z (212.32 KB, 下载次数: 49)

评分

参与人数 1黑币 +80 收起 理由
admin + 80 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:77589 发表于 2022-11-3 16:47 | 只看该作者
这个怎么样,没人评价呢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表