找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32 ST LIS3DH三轴加速度传感器参考代码

[复制链接]
跳转到指定楼层
楼主
ID:480813 发表于 2019-2-26 11:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32 ST LIS3DH 三轴加速度传感器参考代码

单片机源程序如下:
  1. /******************** (C) COPYRIGHT 2012 STMicroelectronics ********************
  2. * File Name          : example_main.c
  3. * Author             : MSH Application Team
  4. * Author             : Abhishek Anand,Fabio Tota
  5. * Revision           : $Revision: 1.5 $
  6. * Date               : $Date: 16/06/2011 12:19:08 $
  7. * Description        : Example main file for MKI109V1 board
  8. * HISTORY:
  9. * Date        | Modification                                | Author
  10. * 16/06/2011  | Initial Revision                            | Fabio Tota
  11. * 11/06/2012  |        Support for multiple drivers in the same program |        Abhishek Anand

  12. ********************************************************************************
  13. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  14. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  15. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  16. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  17. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  18. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  19. *
  20. * THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS.
  21. *
  22. *******************************************************************************/


  23. /* Includes ------------------------------------------------------------------*/
  24. //include files for MKI109V1 board
  25. #include "stm32f10x.h"
  26. #include "usb_lib.h"
  27. #include "usb_desc.h"
  28. #include "hw_config.h"
  29. #include "usb_pwr.h"
  30. #include "led.h"
  31. #include "button.h"
  32. #include "adc_mems.h"
  33. #include "string.h"
  34. #include "spi_mems.h"
  35. #include <stdio.h>

  36. //include MEMS driver
  37. #include "lis3dh_driver.h"

  38. /* Private typedef -----------------------------------------------------------*/
  39. /* Private define ------------------------------------------------------------*/
  40. /* Private macro -------------------------------------------------------------*/
  41. /* Private variables ---------------------------------------------------------*/
  42. uint8_t response;
  43. uint8_t USBbuffer[64];

  44. /* Extern variables ----------------------------------------------------------*/
  45. /* Private function prototypes -----------------------------------------------*/
  46. /* Private functions ---------------------------------------------------------*/

  47. //define for example1 or example2
  48. //#define __EXAMPLE1__H
  49. #define __EXAMPLE2__H

  50. /*******************************************************************************
  51. * Function Name  : main.
  52. * Description    : Main routine.
  53. * Input          : None.
  54. * Output         : None.
  55. * Return         : None.
  56. *******************************************************************************/
  57. int main(void)
  58. {
  59.   uint8_t buffer[26];
  60.   uint8_t position=0, old_position=0;
  61.   AxesRaw_t data;
  62.   //Initialize your hardware here
  63.   
  64.   //function for MKI109V1 board
  65.   InitHardware();
  66.   SPI_Mems_Init();
  67.   
  68.   EKSTM32_LEDOff(LED1);
  69.   EKSTM32_LEDOff(LED2);
  70.   EKSTM32_LEDOff(LED3);   

  71.   //wait until the USB is ready (MKI109V1 board)
  72.   while(bDeviceState != CONFIGURED);

  73.   //Inizialize MEMS Sensor
  74.   //set ODR (turn ON device)
  75.   response = LIS3DH_SetODR(LIS3DH_ODR_100Hz);
  76.   if(response==1){
  77.         sprintf((char*)buffer,"\n\rSET_ODR_OK    \n\r\0");
  78.         USB_SIL_Write(EP1_IN, buffer, 19);
  79.         SetEPTxValid(ENDP1);
  80.   }
  81.   //set PowerMode
  82.   response = LIS3DH_SetMode(LIS3DH_NORMAL);
  83.   if(response==1){
  84.         sprintf((char*)buffer,"SET_MODE_OK     \n\r\0");
  85.         USB_SIL_Write(EP1_IN, buffer, 19);
  86.         SetEPTxValid(ENDP1);
  87.   }
  88.   //set Fullscale
  89.   response = LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);
  90.   if(response==1){
  91.         sprintf((char*)buffer,"SET_FULLSCALE_OK\n\r\0");
  92.         USB_SIL_Write(EP1_IN, buffer, 19);
  93.         SetEPTxValid(ENDP1);
  94.   }
  95.   //set axis Enable
  96.   response = LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);
  97.   if(response==1){
  98.         sprintf((char*)buffer,"SET_AXIS_OK     \n\r\0");
  99.         USB_SIL_Write(EP1_IN, buffer, 19);
  100.         SetEPTxValid(ENDP1);
  101.   }

  102.   
  103. /*********************/  
  104. /******Example 1******/
  105. #ifdef __EXAMPLE1__H
  106.   while(1){
  107.   //get Acceleration Raw data  
  108.   response = LIS3DH_GetAccAxesRaw(&data);
  109.   if(response==1){
  110.     //print data values
  111.     //function for MKI109V1 board
  112.     EKSTM32_LEDToggle(LED1);
  113.     sprintf((char*)buffer, "X=%6d Y=%6d Z=%6d \r\n", data.AXIS_X, data.AXIS_Y, data.AXIS_Z);
  114.     USB_SIL_Write(EP1_IN, buffer, 29);
  115.     SetEPTxValid(ENDP1);  
  116.     old_position = position;
  117.   }
  118. }
  119. #endif /* __EXAMPLE1__H  */


  120. /*********************/
  121. /******Example 2******/
  122. #ifdef __EXAMPLE2__H
  123. //configure Mems Sensor
  124. //set Interrupt Threshold
  125. response = LIS3DH_SetInt1Threshold(20);
  126. if(response==1){
  127.         sprintf((char*)buffer,"SET_THRESHOLD_OK\n\r\0");
  128.         USB_SIL_Write(EP1_IN, buffer, 19);
  129.         SetEPTxValid(ENDP1);
  130.   }
  131. //set Interrupt configuration (all enabled)
  132. response = LIS3DH_SetIntConfiguration(LIS3DH_INT1_ZHIE_ENABLE | LIS3DH_INT1_ZLIE_ENABLE |
  133.                                                                            LIS3DH_INT1_YHIE_ENABLE | LIS3DH_INT1_YLIE_ENABLE |
  134.                                                                            LIS3DH_INT1_XHIE_ENABLE | LIS3DH_INT1_XLIE_ENABLE );
  135. if(response==1){
  136.         sprintf((char*)buffer,"SET_INT_CONF_OK \n\r\0");
  137.         USB_SIL_Write(EP1_IN, buffer, 19);
  138.         SetEPTxValid(ENDP1);
  139.   }
  140. //set Interrupt Mode
  141. response = LIS3DH_SetIntMode(LIS3DH_INT_MODE_6D_POSITION);
  142. if(response==1){
  143.         sprintf((char*)buffer,"SET_INT_MODE    \n\r\0");
  144.         USB_SIL_Write(EP1_IN, buffer, 19);
  145.         SetEPTxValid(ENDP1);
  146.   }

  147. while(1){
  148.   //get 6D Position
  149.   response = LIS3DH_Get6DPosition(&position);
  150.   if((response==1) && (old_position!=position)){
  151.     EKSTM32_LEDToggle(LED1);
  152.     switch (position){
  153.     case LIS3DH_UP_SX:   sprintf((char*)buffer,"\n\rposition = UP_SX  \n\r\0");   break;
  154.     case LIS3DH_UP_DX:   sprintf((char*)buffer,"\n\rposition = UP_DX  \n\r\0");   break;
  155.     case LIS3DH_DW_SX:   sprintf((char*)buffer,"\n\rposition = DW_SX  \n\r\0");   break;              
  156.     case LIS3DH_DW_DX:   sprintf((char*)buffer,"\n\rposition = DW_DX  \n\r\0");   break;
  157.     case LIS3DH_TOP:     sprintf((char*)buffer,"\n\rposition = TOP    \n\r\0");   break;
  158.     case LIS3DH_BOTTOM:  sprintf((char*)buffer,"\n\rposition = BOTTOM \n\r\0");   break;
  159.     default:      sprintf((char*)buffer,"\n\rposition = unknown\n\r\0");   break;
  160.     }
  161.   
  162.   //function for MKI109V1 board   
  163.   USB_SIL_Write(EP1_IN, buffer, 23);
  164.   SetEPTxValid(ENDP1);  
  165.   old_position = position;
  166.   }
  167. }
  168. #endif /*__EXAMPLE2__H */

  169. } // end main


  170. #ifdef USE_FULL_ASSERT
  171. ……………………

  172. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
LIS3DH_MCU_Driver.7z (9.55 KB, 下载次数: 92)



评分

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

查看全部评分

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

使用道具 举报

沙发
ID:480813 发表于 2019-2-26 12:01 | 只看该作者
本帖最后由 136739843 于 2019-2-26 13:08 编辑

希望对大家有帮助。
回复

使用道具 举报

板凳
ID:481690 发表于 2020-11-15 21:57 | 只看该作者
没啥用。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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