我的12位ADC SPI代码有什么问题?
因此,我正在尝试使用INA128P放大器和LTC1296CCN ADC读取具有2MV/V输出的应变负载单元的数据。 ADC数据表指出它在半双层操作中起作用,并建议使用3线双向连接。要配置ADC,在设置CS低之后,我应该向ADC的DIN发送一个8位单词,然后从Dout a a null Bit,12位数据,然后将零传输到CS再次变高。我尝试了完整的双工和半复式,但我一直从代码中收到零。下面是我的代码,电路(零件在面包板上)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
char uart_buf[100];
char boofer[100];
uint8_t ADC_buf[2];
uint8_t input_word[2];
int uart_buf_len;
int length;
uint16_t testdata[100];
uint8_t i = 0;
uint16_t sample;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
// CS pin should default high
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
uart_buf_len = sprintf(uart_buf, "SPI test\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
//input word is 10100110
input_word[0] = 167;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
//HAL_SPI_Transmit(&hspi1, input_word, 1, 100);
//HAL_SPI_Receive(&hspi1, ADC_buf, 2, 100);
HAL_SPI_TransmitReceive(&hspi1, input_word, ADC_buf, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
sample = (((uint16_t) ADC_buf[0] << 9)|(((uint16_t)ADC_buf[1] << 1))) >> 4;
testdata[i] = sample;
//uart_buf_len = sprintf(uart_buf, testdata[i]);
//HAL_UART_Transmit_IT(&huart2, (uint16_t *)uart_buf, 2);
length =sprintf(boofer,"%d\n",testdata[i]) + 1;
HAL_UART_Transmit(&huart2, boofer, length, 100);
i++;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
So I am trying to read data from a strain load cell, which has a 2mV/V output, using the INA128P amplifier and LTC1296CCN ADC. The ADC datasheet states it works in half duplex operation and suggests a 3 wire bidirectional connection. To configure the ADC, after setting the CS low I should send an 8 bit word to the ADC's Din and then from the Dout a null bit, the 12 bit data and then zeros are transmitted until the CS goes high again. I have tried full duplex and half duplex but i keep receiving zeros from my code. Below is my code and the circuit (the parts are on a breadboard)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
char uart_buf[100];
char boofer[100];
uint8_t ADC_buf[2];
uint8_t input_word[2];
int uart_buf_len;
int length;
uint16_t testdata[100];
uint8_t i = 0;
uint16_t sample;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
// CS pin should default high
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
uart_buf_len = sprintf(uart_buf, "SPI test\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
//input word is 10100110
input_word[0] = 167;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
//HAL_SPI_Transmit(&hspi1, input_word, 1, 100);
//HAL_SPI_Receive(&hspi1, ADC_buf, 2, 100);
HAL_SPI_TransmitReceive(&hspi1, input_word, ADC_buf, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
sample = (((uint16_t) ADC_buf[0] << 9)|(((uint16_t)ADC_buf[1] << 1))) >> 4;
testdata[i] = sample;
//uart_buf_len = sprintf(uart_buf, testdata[i]);
//HAL_UART_Transmit_IT(&huart2, (uint16_t *)uart_buf, 2);
length =sprintf(boofer,"%d\n",testdata[i]) + 1;
HAL_UART_Transmit(&huart2, boofer, length, 100);
i++;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论