资源简介
ADXL345 3轴加速度传感器, STM32 SPI 代码亲测可用,有问题可以下方评论。
代码片段和文件信息
#include “ADXL345.h“
#include “delay.h“
void SPI_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC 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_PORTADXL345_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_SPIENABLE);
}
void ADXL345_init(void)
{
SPI_init();
ADXL345_write_byte(0x1E0x00);
ADXL345_write_byte(0x1F0x00);
ADXL345_write_byte(0x200x00);
ADXL345_write_byte(0x210x00);
ADXL345_write_byte(0x220x00);
ADXL345_write_byte(0x230x00);
ADXL345_write_byte(0x240x01);
ADXL345_write_byte(0x250x01);
ADXL345_write_byte(0x260x2B);
ADXL345_write_byte(0x270x00);
ADXL345_write_byte(0x280x09);
ADXL345_write_byte(0x290xFF);
ADXL345_write_byte(0x2A0x80);
//ADXL345_read_byte(0x2B);
ADXL345_write_byte(0x2C0x0F);
ADXL345_write_byte(0x2D0x08);
ADXL345_write_byte(0x2E0x80);
ADXL345_write_byte(0x2F0x00);
//ADXL345_read_byte(0x30);
ADXL345_write_byte(0x310X0B);
ADXL345_write_byte(0x380x00);
//ADXL345_read_byte(0x39);
}
uint8_t ADXL345_read_byte(uint8_t 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_SPISPI_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(uint8_t adduint8_t val)
{
GPIO_ResetBits(ADXL34
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4111 2019-04-19 09:33 ADXL345\ADXL345.c
文件 5616 2019-04-19 09:33 ADXL345\ADXL345.h
目录 0 2019-05-07 10:47 ADXL345
----------- --------- ---------- ----- ----
9727 3
- 上一篇:pom-副本.xm
l - 下一篇:智能电子称重系统的设计
相关资源
- STM32无刷直流电机PWM控制
- 图像压缩,使用SPIHT算法进行图像压缩
- STM32封装库和原理图
- 用stm32以软件IIC方式驱动1602 解压密码
- 野火STM32教程之26-计算器(需LCD触摸支
- 兼容超级终端Ymodem协议的stm32程序更新
- Custom_hid 官方STM32f10X例程
- Esp8266程序基于STM32
- Auto Spice CMMI 26262差异对比
- 自己编写的使用Verilog通过SPI读写EEP
- 最完美的STM32读写I2C EEPROM驱动
- STM32掌机代码完成
- STM32掌机驱动OLED屏幕
- stm32库开发实战指南 PDF和光盘源码
- stm32 软件I2C 实验
- STM32实现printf和scanf源代码,保证能运
- AD7793 c程序
- STM32F4XX固件库之DSP库的说明
- STM32上串口与CAN的完整程序,全中断方
- STM32使用GPIO_F的0-7口的4*4矩阵按键
- 基于STM32的三轴加速度传感器代码
- 使用stm32控制彩色级联led WS2818B源程序
- STM32_PID算法源码
- STM32F103 ucLinux 开发BOOT源码
- stm32驱动ov7670 数据转BMP格式再转JPEG存
- Audio_Test for STM32F4Discovery
- stm32f407+FFT浮点运算例程
- C51与ADXL345通信外加上位机显示程序
- max31856官方驱动例程STM32
- Hspice使用详细教程
评论
共有 条评论