• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-11
  • 语言: 其他
  • 标签: STM32  ADXL34  SPI  

资源简介

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


评论

共有 条评论