久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2806|回復(fù): 1
收起左側(cè)

STM32單片機做一個手勢刷視頻2 APDS9960手勢模塊源程序

[復(fù)制鏈接]
ID:147025 發(fā)表于 2021-7-23 17:02 | 顯示全部樓層 |閱讀模式
三、燒錄程序
把ST-Link插入到電腦上,
通過仿真下載即可!

下載完成后,可以把ST-Link拔了,不需要用到了;然后把KM2模塊的USB通過OTG轉(zhuǎn)換線插到手機上就可以了
51hei圖片_20210723170119.jpg


  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.         //設(shè)置手勢接近進入(手勢狀態(tài)機)閥值為0xA0 = 40
  83.     /* 設(shè)置手勢傳感器寄存器默認值 */
  84.     if( !setGestureEnterThresh(DEFAULT_GPENTH) )
  85.         {
  86.         return false;
  87.     }

  88.         //設(shè)置手勢接近退出(手勢狀態(tài)機)閥值為0xA1 = 30
  89.     if( !setGestureExitThresh(DEFAULT_GEXTH) )
  90.         {
  91.         return false;
  92.     }

  93.         //設(shè)置配置寄存器1 0xA2 = 0x40
  94.         //1.在4個數(shù)據(jù)集被添加到FIFO里后產(chǎn)生中斷
  95.         //2.All UDLR 探測數(shù)據(jù)被包含到集合中
  96.         //3.手勢退出持久性.當(dāng)連續(xù)的手勢結(jié)束發(fā)生稱為比GEXPERS大于或等于的值時,
  97.         //  手勢狀態(tài)機退出(第1個手勢結(jié)束發(fā)生導(dǎo)致手勢狀態(tài)機退出)
  98.     if( !wireWriteDataByte(APDS9960_GCONF1, DEFAULT_GCONF1) )
  99.         {
  100.         return false;
  101.     }

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

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

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

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

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

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

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

  137.         //設(shè)置收勢脈沖數(shù)和脈寬寄存器0xA6 = 0xC9   32us, 10 pulses
  138.     if( !wireWriteDataByte(APDS9960_GPULSE, DEFAULT_GPULSE) )
  139.         {
  140.         return false;
  141.     }

  142.         //設(shè)置配置寄存器3  0xAA 的bit 1:0 = 00  所有光電二極管在手勢期間均有效
  143.     if( !wireWriteDataByte(APDS9960_GCONF3, DEFAULT_GCONF3) )
  144.         {
  145.         return false;
  146.     }

  147.         //設(shè)置配置寄存器4 0xAB 的bit1 = 0 關(guān)閉手勢中斷 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();//復(fù)位手勢變量=0
  256.         
  257.         //設(shè)置等待時間寄存器0x83 = 0xFF (WLONG=1  0.03s)   (WLONG=0   2.78ms)
  258.     if( !wireWriteDataByte(APDS9960_WTIME, 0xFF) ) //
  259.         {
  260.         return false;
  261.     }
  262.         
  263.         //設(shè)置接近脈沖計數(shù)寄存器 0x8E = 0x89 16us, 10 pulses
  264.     if( !wireWriteDataByte(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) )
  265.         {
  266.         return false;
  267.     }
  268.         
  269.         //設(shè)置配置寄存器2 0x90的bit5:4=11  %300   LED驅(qū)動電流
  270.     if( !setLEDBoost(LED_BOOST_300) )
  271.         {
  272.         return false;
  273.     }
  274.         

復(fù)制代碼

全部STM32代碼下載: STM32F103驅(qū)動APDS9960手勢模塊.7z (212.32 KB, 下載次數(shù): 51)

評分

參與人數(shù) 1黑幣 +80 收起 理由
admin + 80 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報

ID:77589 發(fā)表于 2022-11-3 16:47 | 顯示全部樓層
這個怎么樣,沒人評價呢
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 日韩一级| 天天拍天天射 | 爱草视频| 国产精品完整版 | 午夜小视频在线播放 | 日韩在线中文字幕 | 黄色日本片 | 国产视频1| 少妇特黄a一区二区三区88av | 一区中文字幕 | 国产午夜三级一区二区三 | 2019天天干夜夜操 | 国产精品乱码一二三区的特点 | 伊人啪啪网| 97在线播放 | 91看片免费版| 99精品网 | 久久久网| 欧洲亚洲视频 | 免费黄色录像片 | 日日噜噜噜夜夜爽爽狠狠视频97 | 免费观看av网站 | 中文字幕第90页 | 欧美精品一区三区 | 久久综合婷婷 | 在线视频国产一区 | 亚洲国产精品久久久久秋霞不卡 | 亚洲成人福利 | 日韩一区二区三区精品 | xxx视频| 免费精品国产 | 国产乱码精品1区2区3区 | 在线观看国产视频 | 一级中国毛片 | 亚洲精品一区国产精品 | 亚洲一区二区三区视频 | 国产精品一区二区不卡 | 久久久久久国产一区二区三区 | 91社区在线观看高清 | 日日操夜夜操天天操 | 中文字幕91av|