adxl345的STM32驱动程序和硬件设计
一、硬件电路接口图片
1.ADXL345硬件接口图片使用的是SPI端口进行通信,这样读取数据比较快且后续也可以转化为IIC通信接口。
在网上找一些发现IIC接口的比较多,所以本人就DIY做SPI的通信。
2.STM32F103T系列单片机作为MCU 资源比较丰富、本人比较熟悉开发速度较快
硬件电路首先是为了实现功能,所以设计比较简单。后续小编想做无线蓝牙的数据传输,所以硬件上也留了蓝牙串口通信的硬件接口和3.3V电源管理。
暂且将硬件这样设计,设计为双层PCB 这样减少了空间。电容、电阻为0805的易焊接。器件都选为贴片。
二、单片机驱动代码
1.ADXL345的端口配置函数
#define ADXL345_FLAG_TIMEOUT ((uint32_t)0x1000)
#define ADXL345_SPI SPI1
#define ADXL345_SPI_CLK RCC_APB2Periph_SPI1
#define ADXL345_SPI_SCK_PIN GPIO_Pin_5
#define ADXL345_SPI_SCK_GPIO_PORT GPIOA
#define ADXL345_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_SCK_SOURCE GPIO_PinSource5
#define ADXL345_SPI_SCK_AF GPIO_AF_5
#define ADXL345_SPI_MISO_PIN GPIO_Pin_6
#define ADXL345_SPI_MISO_GPIO_PORT GPIOA
#define ADXL345_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_MISO_SOURCE GPIO_PinSource6
#define ADXL345_SPI_MISO_AF GPIO_AF_5
#define ADXL345_SPI_MOSI_PIN GPIO_Pin_7
#define ADXL345_SPI_MOSI_GPIO_PORT GPIOA
#define ADXL345_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_MOSI_SOURCE GPIO_PinSource7
#define ADXL345_SPI_MOSI_AF GPIO_AF_5
#define ADXL345_SPI_CS_PIN GPIO_Pin_2
#define ADXL345_SPI_CS_GPIO_PORT GPIOB
#define ADXL345_SPI_CS_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT1_PIN GPIO_Pin_0
#define ADXL345_SPI_INT1_GPIO_PORT GPIOB
#define ADXL345_SPI_INT1_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT1_EXTI_LINE EXTI_Line0
#define ADXL345_SPI_INT1_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
#define ADXL345_SPI_INT1_EXTI_PIN_SOURCE EXTI_PinSource0
#define ADXL345_SPI_INT1_EXTI_IRQn EXTI0_IRQn
#define ADXL345_SPI_INT2_PIN GPIO_Pin_1
#define ADXL345_SPI_INT2_GPIO_PORT GPIOB
#define ADXL345_SPI_INT2_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT2_EXTI_LINE EXTI_Line1
#define ADXL345_SPI_INT2_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
#define ADXL345_SPI_INT2_EXTI_PIN_SOURCE EXTI_PinSource1
#define ADXL345_SPI_INT2_EXTI_IRQn EXTI1_IRQn
#define ADXL345_WHO_AM_I_ADDR 0x0F
#define ADXL345_CTRL_REG1_ADDR 0x20
#define ADXL345_CTRL_REG2_ADDR 0x21
#define ADXL345_CTRL_REG3_ADDR 0x22
#define ADXL345_CTRL_REG4_ADDR 0x23
#define ADXL345_CTRL_REG5_ADDR 0x24
#define ADXL345_REFERENCE_REG_ADDR 0x25
#define ADXL345_OUT_TEMP_ADDR 0x26
#define ADXL345_STATUS_REG_ADDR 0x27
#define ADXL345_OUT_X_L_ADDR 0x28
#define ADXL345_OUT_X_H_ADDR 0x29
#define ADXL345_OUT_Y_L_ADDR 0x2A
#define ADXL345_OUT_Y_H_ADDR 0x2B
#define ADXL345_OUT_Z_L_ADDR 0x2C
#define ADXL345_OUT_Z_H_ADDR 0x2D
#define ADXL345_FIFO_CTRL_REG_ADDR 0x2E
#define ADXL345_FIFO_SRC_REG_ADDR 0x2F
#define ADXL345_INT1_CFG_ADDR 0x30
#define ADXL345_INT1_SRC_ADDR 0x31
#define ADXL345_INT1_TSH_XH_ADDR 0x32
#define ADXL345_INT1_TSH_XL_ADDR 0x33
#define ADXL345_INT1_TSH_YH_ADDR 0x34
#define ADXL345_INT1_TSH_YL_ADDR 0x35
#define ADXL345_INT1_TSH_ZH_ADDR 0x36
#define ADXL345_INT1_TSH_ZL_ADDR 0x37
#define ADXL345_INT1_DURATION_ADDR 0x38
#define I_AM_ADXL345 ((uint8_t)0xD4)
#define ADXL345_MODE_POWERDOWN ((uint8_t)0x00)
#define ADXL345_MODE_ACTIVE ((uint8_t)0x08)
#define ADXL345_OUTPUT_DATARATE_1 ((uint8_t)0x00)
#define ADXL345_OUTPUT_DATARATE_2 ((uint8_t)0x40)
#define ADXL345_OUTPUT_DATARATE_3 ((uint8_t)0x80)
#define ADXL345_OUTPUT_DATARATE_4 ((uint8_t)0xC0)
#define ADXL345_X_ENABLE ((uint8_t)0x02)
#define ADXL345_Y_ENABLE ((uint8_t)0x01)
#define ADXL345_Z_ENABLE ((uint8_t)0x04)
#define ADXL345_AXES_ENABLE ((uint8_t)0x07)
#define ADXL345_AXES_DISABLE ((uint8_t)0x00)
#define ADXL345_BANDWIDTH_1 ((uint8_t)0x00)
#define ADXL345_BANDWIDTH_2 ((uint8_t)0x10)
#define ADXL345_BANDWIDTH_3 ((uint8_t)0x20)
#define ADXL345_BANDWIDTH_4 ((uint8_t)0x30)
#define ADXL345_FULLSCALE_250 ((uint8_t)0x00)
#define ADXL345_FULLSCALE_500 ((uint8_t)0x10)
#define ADXL345_FULLSCALE_2000 ((uint8_t)0x20)
#define ADXL345_BlockDataUpdate_Continous ((uint8_t)0x00)
#define ADXL345_BlockDataUpdate_Single ((uint8_t)0x80)
#define ADXL345_BLE_LSB ((uint8_t)0x00)
#define ADXL345_BLE_MSB ((uint8_t)0x40)
#define ADXL345_HIGHPASSFILTER_DISABLE ((uint8_t)0x00)
#define ADXL345_HIGHPASSFILTER_ENABLE ((uint8_t)0x10)
#define ADXL345_INT1INTERRUPT_DISABLE ((uint8_t)0x00)
#define ADXL345_INT1INTERRUPT_ENABLE ((uint8_t)0x80)
#define ADXL345_INT2INTERRUPT_DISABLE ((uint8_t)0x00)
#define ADXL345_INT2INTERRUPT_ENABLE ((uint8_t)0x08)
#define ADXL345_INT1INTERRUPT_LOW_EDGE ((uint8_t)0x20)
#define ADXL345_INT1INTERRUPT_HIGH_EDGE ((uint8_t)0x00)
#define ADXL345_BOOT_NORMALMODE ((uint8_t)0x00)
#define ADXL345_BOOT_REBOOTMEMORY ((uint8_t)0x80)
#define ADXL345_HPM_NORMAL_MODE_RES ((uint8_t)0x00)
#define ADXL345_HPM_REF_SIGNAL ((uint8_t)0x10)
#define ADXL345_HPM_NORMAL_MODE ((uint8_t)0x20)
#define ADXL345_HPM_AUTORESET_INT ((uint8_t)0x30)
#define ADXL345_HPFCF_0 0x00
#define ADXL345_HPFCF_1 0x01
#define ADXL345_HPFCF_2 0x02
#define ADXL345_HPFCF_3 0x03
#define ADXL345_HPFCF_4 0x04
#define ADXL345_HPFCF_5 0x05
#define ADXL345_HPFCF_6 0x06
#define ADXL345_HPFCF_7 0x07
#define ADXL345_HPFCF_8 0x08
#define ADXL345_HPFCF_9 0x09
#define ADXL345_CS_LOW() GPIO_ResetBits(ADXL345_SPI_CS_GPIO_PORT, ADXL345_SPI_CS_PIN)
#define ADXL345_CS_HIGH() GPIO_SetBits(ADXL345_SPI_CS_GPIO_PORT, ADXL345_SPI_CS_PIN)
void ADXL345_Init(ADXL345_InitTypeDef *ADXL345_InitStruct);
void ADXL345_RebootCmd(void);
void ADXL345_INT1InterruptCmd(uint8_t InterruptState);
void ADXL345_INT2InterruptCmd(uint8_t InterruptState);
void ADXL345_INT1InterruptConfig(ADXL345_InterruptConfigTypeDef *ADXL345_IntConfigStruct);
uint8_t ADXL345_GetDataStatus(void);
void ADXL345_FilterConfig(ADXL345_FilterConfigTypeDef *ADXL345_FilterStruct);
void ADXL345_FilterCmd(uint8_t HighPassFilterState);
void ADXL345_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
void ADXL345_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
2.ADXL345的SPI配置函数
void SPI_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(ADXL345_SPI_CLK ,ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =ADXL345_SPI_CS_PIN ;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADXL345_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
GPIO_SetBits(ADXL345_SPI_CS_GPIO_PORT,ADXL345_SPI_CS_PIN);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =ADXL345_SPI_SCK_PIN|ADXL345_SPI_MISO_PIN|ADXL345_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADXL345_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(ADXL345_SPI, &SPI_InitStructure);
SPI_Cmd(ADXL345_SPI,ENABLE);
}
3.ADXL345初始化配置函数
void ADXL345_init(void)
{
SPI_init(); //ADXL345¶Ë¿Ú³õʼ»¯ÅäÖÃSPIͨÐÅ
ADXL345_write_byte(0x1E,0x00); //X Æ«ÒÆÁ¿ ¸ù¾Ý²âÊÔ´«¸ÐÆ÷µÄ״̬дÈëpdf29Ò³ 0X00 (15.6mg/LSB)
ADXL345_write_byte(0x1F,0x00); //Y Æ«ÒÆÁ¿ ¸ù¾Ý²âÊÔ´«¸ÐÆ÷µÄ״̬дÈëpdf29Ò³ 0X00 (15.6mg/LSB)
ADXL345_write_byte(0x20,0x00); //Z Æ«ÒÆÁ¿ ¸ù¾Ý²âÊÔ´«¸ÐÆ÷µÄ״̬дÈëpdf29Ò³ 0X00 (15.6mg/LSB)
ADXL345_write_byte(0x21,0x00); //Çû÷ÑÓʱ0:½ûÓÃ; (1.25ms/LSB)
ADXL345_write_byte(0x22,0x00); //¼ì²âµÚÒ»´ÎÇû÷ºóµÄÑÓʱ0:½ûÓÃ; (1.25ms/LSB)
ADXL345_write_byte(0x23,0x00); //Çû÷´°¿Ú0:½ûÓÃ; (1.25ms/LSB)
ADXL345_write_byte(0x24,0x01); //±£´æ¼ì²â»î¶¯·§Öµ; (62.5mg/LSB)
ADXL345_write_byte(0x25,0x01); //±£´æ¼ì²â¾²Ö¹·§Öµ; (62.5mg/LSB)
ADXL345_write_byte(0x26,0x2B); //¼ì²â»î¶¯Ê±¼ä·§Öµ; (1s/LSB)
ADXL345_write_byte(0x27,0x00); //
ADXL345_write_byte(0x28,0x09); //×ÔÓÉÂäÌå¼ì²âÍƼö·§Öµ; (62.5mg/LSB)
ADXL345_write_byte(0x29,0xFF); //×ÔÓÉÂäÌå¼ì²âʱ¼ä·§Öµ,ÉèÖÃΪ×î´óʱ¼ä; (5ms/LSB)
ADXL345_write_byte(0x2A,0x80); //
//ADXL345_read_byte(0x2B); //Ö»¶Á¼Ä´æÆ÷,״̬¶ÁÈ¡
ADXL345_write_byte(0x2C,0x0F); //ËÙÂÊÉ趨Ϊ3200HZ²Î¿¼pdf13Ò³ 0X0A 1OO,0X0E 1600HZ
ADXL345_write_byte(0x2D,0x08); //Ñ¡ÔñµçԴģʽ¹Ø±Õ×Ô¶¯ÐÝÃß,ÐÝÃß,»½Ðѹ¦Äܲο¼pdf24Ò³
ADXL345_write_byte(0x2E,0x80); //ʹÄÜ DATA_READY ÖжÏ
ADXL345_write_byte(0x2F,0x00);
//ADXL345_read_byte(0x30); //Ö»¶Á¼Ä´æÆ÷,״̬¶ÁÈ¡
ADXL345_write_byte(0x31,0X0B); //Êý¾ÝͨПñʽ;ÉèÖÃΪ×Լ칦ÄܽûÓÃ,4ÏßÖÆSPI½Ó¿Ú,µÍµçƽÖжÏÊä³ö,13λȫ·Ö±æÂÊ,Êä³öÊý¾ÝÓÒ¶ÔÆë,16gÁ¿³Ì
ADXL345_write_byte(0x38,0x00); //FIFOģʽÉ趨,Streamģʽ£¬´¥·¢Á¬½ÓINT1,31¼¶Ñù±¾»º³å
//ADXL345_read_byte(0x39); //Ö»¶Á¼Ä´æÆ÷,״̬¶ÁÈ¡
}
4.ADXL345的读写函数
u8 ADXL345_read_byte(u8 add)
{
GPIO_ResetBits(ADXL345_SPI_CS_GPIO_PORT ,ADXL345_SPI_CS_PIN);
SPI_I2S_SendData(ADXL345_SPI,(add|0x80)<<8|0x00);
while(SPI_I2S_GetFlagStatus(ADXL345_SPI,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(ADXL345_SPI, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(ADXL345_SPI_CS_GPIO_PORT ,ADXL345_SPI_CS_PIN);
return SPI_I2S_ReceiveData(ADXL345_SPI)&0xff;
}
void ADXL345_write_byte(u8 add,u8 val)
{
GPIO_ResetBits(ADXL345_SPI_CS_GPIO_PORT ,ADXL345_SPI_CS_PIN);
SPI_I2S_SendData(ADXL345_SPI,add<<8|val);
while(SPI_I2S_GetFlagStatus(ADXL345_SPI,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(ADXL345_SPI, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(ADXL345_SPI_CS_GPIO_PORT ,ADXL345_SPI_CS_PIN);
SPI_I2S_ReceiveData(ADXL345_SPI)&0xff;
}
void ADXL345_ReadXYZ(float *g)
{
uint8_t BUF[6]; // ´æ·ÅX,Y,ZÖáµÄÊý¾Ý
int16_t temp;
BUF[0] = ADXL345_read_byte(0x32);
BUF[1] = ADXL345_read_byte(0x33);
delay_ms(1);
BUF[2] = ADXL345_read_byte(0x34);
BUF[3] = ADXL345_read_byte(0x35);
delay_ms(1);
BUF[4] = ADXL345_read_byte(0x36);
BUF[5] = ADXL345_read_byte(0x37);
delay_ms(1);
temp = (BUF[1] << 8) + BUF[0];
if(temp < 0)
temp = -temp;
g[0] = (float)(temp * 3.9); //¼ÆËãÊý¾ÝºÍÏÔʾ,²é¿¼ADXL345¿ìËÙÈëÃŵÚ4Ò³
temp = (BUF[3] << 8) + BUF[2];
if(temp < 0)
temp = -temp;
g[1] = (float)(temp * 3.9); //¼ÆËãÊý¾ÝºÍÏÔʾ,²é¿¼ADXL345¿ìËÙÈëÃŵÚ4Ò³
temp = (BUF[5] << 8) + BUF[4];
if(temp < 0)
temp = -temp;
g[2] = (float)(temp * 3.9); //¼ÆËãÊý¾ÝºÍÏÔʾ,²é¿¼ADXL345¿ìËÙÈëÃŵÚ4Ò³
}
http://blog.sina.com.cn/zhuifxk
adxl345的STM32驱动程序和硬件设计的更多相关文章
- SLAM+语音机器人DIY系列:(四)差分底盘设计——1.stm32主控硬件设计
摘要 运动底盘是移动机器人的重要组成部分,不像激光雷达.IMU.麦克风.音响.摄像头这些通用部件可以直接买到,很难买到通用的底盘.一方面是因为底盘的尺寸结构和参数是要与具体机器人匹配的:另一方面是因为 ...
- stm32串口USART 硬件流控 --学习笔记
流控的概念源于 RS232 这个标准,在 RS232 标准里面包含了串口.流控的定义.大家一定了解,RS232 中的"RS"是Recommend Standard 的缩写,即&qu ...
- 基于STM32+华为云IOT设计智能称重系统
摘要:选择部署多个重量传感器和必要的算法.通过WiFi 通信模块.GPS定位模块,采集车辆称重数据一地理位置信息,并通过网络发送至云平台,设计图形化UI界面展示称重.地图位置等重要信息,实现对称重系统 ...
- 关于AD9516芯片的硬件设计和FPGA程序编写心得
最近在做一个项目,其中有涉及时钟芯片AD9516的硬件设计和软件编程,有些使用心得,供大家参考讨论. AD9516,这是一个由ADI公司设计的14路输出时钟发生器,具有亚皮秒级抖动性能,还配有片内集成 ...
- Android之 看“马达”如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI --> Framework --> Application)
在Android 2.3(Gingerbread) 系统的时候,我写过一篇关于“Android 震动马达系统“的文章,当时的Linux内核还是2.6版本的.写那篇文章的目的,是想彻底的了解从硬件到驱动 ...
- USB2.0学习笔记连载(六):USB2.0硬件设计需要注意事项
笔者在设计USB2.0时找到了一个官方给的硬件设计正确设计指南,其中有些内容还挺nice的.不单单只是USB的设计,其中有些思想可以应用到其他的场合中. 对于USB2.0而言,全速状态下可以达到480 ...
- W5100硬件设计和调试要点
文章来源:成都浩然 与MCU的接口 W5100与MCU接口採用并行总线方式(假设要使用SPI接口,建议採用W5200),因此W5100与MCU的接口设计相对简单.以AT89C52为例,例如以下图所看到 ...
- HDMI接口基础知识及硬件设计
参考资料:http://blog.csdn.net/u013625961/article/details/53434189: http://blog.csdn.net/u014276460/artic ...
- 实时人工智能:微软发布Project Brainwave预览版 现场可编程门阵列(Field Programmable Gate Array,简称FPGA) 硬件设计可以迅速演进
https://mp.weixin.qq.com/s/bAPiPURZd-YsbV5PbzwpQQ 编者按:随着各大公司对于数据计算的要求越来越高,实时AI成为了研究者们关注的重点.在美国西雅图举行的 ...
随机推荐
- WINDOWS硬件通知应用程序的常方法
摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口.为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,并 ...
- 分布式配置管理平台 Disconf
Distributed Configuration Management Platform(分布式配置管理平台) 专注于各种 分布式系统配置管理 的通用组件/通用平台, 提供统一的配置管理服务. 包括 ...
- HDU - 2276 Kiki & Little Kiki 2
Description There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and ...
- Objective-c 方法的调用
在书写了类的声明和实现后,应用程序如何去调用它呢? 在Objective-c中,调用方法的简单格式如下: 1⃣ [实例 方法]; 如: [person setAge:32]; 其中 pe ...
- 积跬步,聚小流------关于UML类图
UML的存在 类图是使用频率比較高的UML图,它用于描写叙述系统中所含的类以及它们之间的相互关系,帮助人们简化对系统的理解,也是系统分析和设计阶段的重要产物,也是系统编码和測试的重要类型根据. UML ...
- AIX LVM学习笔记
LVM: LOGIC VOLUMN MANAGEMENT (逻辑卷管理器) 通过将数据在存储空间的 逻辑视图 与 实际的物理磁盘 之间进行映射,来控制磁盘资源.实现方式是在传统的物理设备驱动层之上加载 ...
- oracle 开发笔记“跨数据库查询复制”
1.方法一:创建DBL(data base link) CREATE PUBLIC DATABASE LINK 数据链名称 CONNECT TO 登陆用户名 IDENTIFIED BY 密码 USIN ...
- Django问题汇总
Q1.运行python manage.py runserver后,出现如下错误(CentOS6.5x86,Python2.7.12,Django1.10): django.core.exception ...
- uva 11077 置换
/** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...
- grawlew 编译
http://somefuture.iteye.com/blog/2002052 Gradlew behind a proxy systemProp.http.proxyHost=www.someho ...