操作ADS129x,SPI读取寄存器总是乱码

2019-07-20 18:03发布

用的407的开发板,AD用的是TI的ADS1299的评估板,SPI都设置好的了,但是每次读寄存器数据都是失败。
源码如下:
#include "ADS1299.h"

/*SPI ReadWriteByte*/
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
uint8_t RxData;
int count;
HAL_SPI_Transmit(&hspi1,&TxData,1,10);
HAL_SPI_Receive(&hspi1,&RxData,1,10);
//HAL_SPI_TransmitReceive(&hspi1,&TxData,&RxData,1,1000);
return RxData;
}
/*SPI Write registers*/
void ADS1299_WriteReg(uint8_t RegAddr,uint8_t RegData)
{
uint8_t WriteAddr;
int count;

ADS1299_CS_Low;
WriteAddr=WREG|RegAddr;
SPI1_ReadWriteByte(WriteAddr);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(0x00);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(RegData);
for(count=0;count<168;count++);
ADS1299_CS_High;
}
/*SPI Read registers*/
uint8_t ADS1299_ReadReg(uint8_t RegAddr)
{
uint8_t ReadAddr;
uint8_t ReadData;
int count;

ADS1299_CS_Low;
ReadAddr=RREG|RegAddr;
SPI1_ReadWriteByte(ReadAddr);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(0x00);
for(count=0;count<168;count++);
ReadData=SPI1_ReadWriteByte(0xff);
for(count=0;count<168;count++);
ADS1299_CS_High;
return ReadData;
}
/*SPI Write command*/
void ADS1299_WriteCommand(uint8_t Command)
{
ADS1299_CS_Low;
SPI1_ReadWriteByte(Command);
ADS1299_CS_High;
}
/*ADS1299 Read ADC data*/
uint8_t ADS1299_ReadAdcData(uint8_t *AdcData)
{
uint8_t i=0;
ADS1299_CS_Low;
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)!=SET);
for(i=0;i<27;i++)
{
  AdcData[i]=SPI1_ReadWriteByte(0xff);
}
ADS1299_CS_High;
return SUCCESS;
}
/*ADS1299 Initial floww at Power-Up*/
void ADS1299_PowerUpInit(void)
{
uint8_t RegValue;
// Delay for Power-On Reset and Oscillator Start-Up;
ADS1299_PDWN_High;
ADS1299_RESET_High;
ADS1299_START_Low;
HAL_Delay(1000);//Delay 1s
//Issue Reset Pulse
ADS1299_RESET_Low;
HAL_Delay(100);//Delay 100ms
ADS1299_RESET_High;
HAL_Delay(100);//Delay 100ms
//Send SDATAC Command
ADS1299_WriteCommand(SDATAC);
//Set Reference
ADS1299_WriteReg(ADS1299_REG_CONFIG3,0xE0);//Using internal Reference
HAL_Delay(10);//Delay 10ms

RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG3);//Debug !!!
printf("CONFIG3 :%04X ",RegValue);
//Write certain Registers
ADS1299_WriteReg(ADS1299_REG_CONFIG1,0x96);//Set output data rate =fMOD/4096(250 SPS)
ADS1299_WriteReg(ADS1299_REG_CONFIG2,0xC0);//Set test signal are generated internally and signal amplitude
                       //is 1x-(VREFP-VREFN)/2400, the frequency is fCLK/2^21
RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG1);//Debug !!!
printf("CONFIG1 :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG2);//Debug !!!
printf("CONFIG2 :%04X ",RegValue);

ADS1299_WriteReg(ADS1299_REG_CH1SET,0x01);//Set All channels to input short
ADS1299_WriteReg(ADS1299_REG_CH2SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH3SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH4SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH5SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH6SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH7SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH8SET,0x01);

RegValue=ADS1299_ReadReg(ADS1299_REG_CH1SET);//Debug !!!
printf("CH1SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH2SET);//Debug !!!
printf("CH2SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH3SET);//Debug !!!
printf("CH3SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH4SET);//Debug !!!
printf("CH4SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH5SET);//Debug !!!
printf("CH5SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH6SET);//Debug !!!
printf("CH6SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH7SET);//Debug !!!
printf("CH7SET :%04X ",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH8SET);//Debug !!!
printf("CH8SET :%04X ",RegValue);

//Active Conversion
ADS1299_START_High;//Set START=1
//Put the device back in RDATAC mode
ADS1299_WriteCommand(RDATAC);
}
















友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
3条回答
ljunbest
2019-07-20 21:35
/**
  ******************************************************************************
  * File Name          : SPI.c
  * Description        : This file provides code for the configuration
  *                      of the SPI instances.
  ******************************************************************************
  *
  * Copyright (c) 2016 STMicroelectronics International N.V.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other
  *    contributors to this software may be used to endorse or promote products
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under
  *    this license is void and will automatically terminate your rights under
  *    this license.
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "spi.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

SPI_HandleTypeDef hspi1;

/* SPI1 init function */
void MX_SPI1_Init(void)
{

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();
  
    /**SPI1 GPIO Configuration   
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }
}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{

  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspDeInit 0 */

  /* USER CODE END SPI1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_SPI1_CLK_DISABLE();
  
    /**SPI1 GPIO Configuration   
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

  }
  /* USER CODE BEGIN SPI1_MspDeInit 1 */

  /* USER CODE END SPI1_MspDeInit 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

一周热门 更多>