新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2020-10-7
- 在线时间
- 6 小时
|
11金钱
使用STM32F103C8T6的SPI2读取ADXL的器件ID和传感器数据并打印到串口,现在器件ID读取正确,但数据全为0,求助。
串口显示
ADXL375代码如下:
- #include "adxl375.h"
- #include "delay.h"
- //SPI功能引脚初始化
- void adxl375_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOG, ENABLE );//PORTB时钟使能
- //之所以打开其他gpio口是因为SPI2还挂载了其他硬件,所以要将其他的硬件屏蔽。
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PB2 推挽
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_2);//作为ADXL375的片选线
- SPI2_Init(); //初始化SPI
- //SPI2_SetSpeed(SPI_BaudRatePrescaler_256);//256分频
- SPI2_SetSpeed(SPI_BaudRatePrescaler_16);//16分频
- }
- //在给定地址上写一个字节的数 PB2
- void oneBytewrite(u8 add,u8 val)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- SPI2_ReadWriteByte(add|sw);
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- SPI2_ReadWriteByte(val);
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- }
- //从startaddress的地址开始写size长度的数据//PB2
- void multiByteWrite(int startAddress, float* buffer, int size)
- {
- int tx = (startAddress|mw);
- int i;
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- //Send address to start reading from.
- SPI2_ReadWriteByte(tx);
- for ( i = 0; i < size; i++)
- {
- buffer[i] = SPI2_ReadWriteByte(0x00);
- }
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- }
- //adxl375配置
- void adxl375_set()
- {
- oneBytewrite(THRESH_SHOCK,0x03);//冲击阈值2.34g
- oneBytewrite(BW_RATE,0x0d);//800hz 400bw
- oneBytewrite(DATA_FORMAT,0x0f);//MSB
- oneBytewrite(POWER_CTL,0x08);
- oneBytewrite(OFSX,0x00);
- oneBytewrite(OFSY,0x00);
- oneBytewrite(OFSZ,0x00);
- }
- //一个字节的读取
- u8 oneByteRead(u8 address)
- {
- u8 t;
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- SPI2_ReadWriteByte(address|sr);
- t=SPI2_ReadWriteByte(0xff);
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- return t;
- }
-
- //从startadress读取size长度的数据
- void multiByteRead(int startAddress, float* buffer, int size)
- {
- int tx = (startAddress|sr);
- int i;
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- //Send address to start reading from.
- SPI2_ReadWriteByte(tx);
- for ( i= 0; i < size; i++)
- {
- buffer[i] = SPI2_ReadWriteByte(0x00);
- }
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- }
-
- //获取三轴数据
- void getOutput(float* readings)
- {
- float buffer[6];
- multiByteRead(DATAX0, buffer, 6);
-
- readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
- readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
- readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
- }
复制代码
SPI代码如下:
- #include "spi.h"
- void SPI2_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE );//PORTB时钟使能 复用时钟使能
- RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
-
- GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15上拉
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //定义波特率预分频的值:波特率预分频值为4
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
- SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
- SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
- SPI_Cmd(SPI2, ENABLE); //使能SPI外设
-
- SPI2_ReadWriteByte(0xff);//启动传输
- }
复制代码
|
|