STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据

这篇具有很好参考价值的文章主要介绍了STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据

STM32大部分芯片只有12位的ADC采样性能,如果要实现更高精度的模数转换如24位ADC采样,则需要连接外部ADC实现。AD7791是亚德诺(ADI)半导体一款用于低功耗、24位Σ-Δ型模数转换器(ADC) ,适合低频测量应用,提供50 Hz/60 Hz同步抑制。

这里介绍基于AD7791的24位ADC采样实现。

AD7791控制协议

AD7791的管脚如下所示:
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
AD7791可以工作在2.5V~5.25V供电范围(VDD),而用于模数转换的参考电压可以通过引脚REFIN(+)和REFIN(–)单独设置,从而可以针对特定电压范围更高精度的采样。如果REFIN(+) - REFIN(–) = 1V, 则对应24位采样分辨率率为1/(2^24)=0.00000006V。当然要使得这个级别的电压分辨率有效,对电路噪声的控制要求也很高。
AIN(+)和AIN(-)用于连接输入信号,通过芯片内部寄存器配置,有两种转换模式,即AIN(+)相对AIN(-)是单边电压或双边电压。单边电压模式,采样值0x000000对应AIN(+)-AIN(-)=0,采样值0xFFFFFF对应AIN(+)-AIN(-)=REFIN(+) - REFIN(–) 。双边电压模式,采样值0x000000对应AIN(+)-AIN(-)=-(REFIN(+) - REFIN(–)),采样值0x800000对应AIN(+)-AIN(-)=0,采样值0xFFFFFF对应AIN(+)-AIN(-)=REFIN(+) - REFIN(–) 。
AD7791通过SPI总线进行访问控制,其中数据输出管脚DOUT也是转换完成可读取指示信号/RDY, 访问协议操作主要逻辑如下:

  1. 向通讯寄存器发送指令,设置采样的通道和读操作模式(单次或连续),并且设置下一次访问是对哪个寄存器进行操作,以及进行的是读还是写;
  2. 写模式寄存器,设置采用单次转换还是连续转换模式,单边还是双边转换模式,以及是否buffered模式(buffered模式对应信号接收管脚端接收阻抗高阻模式),以及buffered模式是否在输入端引用100ns电流源;
  3. 在配置模式寄存器后,可以再向通讯寄存器发送指令指示后续读取数据寄存器,然后按照24位格式读取采样到的ADC值;
  4. 芯片支持对供电电压的采样识别,原理为供电电压内部LDO降压到1.17V作为VDD采样电路的参考供电,而VDD本身分压到1/5接到ADC采样端。所以供电电压识别为( VDD采样值 / 16777216 ) * 1.17 * 5 V.
  5. 可配置滤波寄存器以控制采样转换输出频率,影响到噪声抑制级别;
  6. 可以读取状态寄存器获得一些状态信息。

STM32工程配置

这里以STM32F103C6T6及STM32CUBEIDE开发工具为例,介绍AD7791的24位ADC采样实现。

首先建立基本工程并配置时钟:
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
配置一个串口如UART2作为通讯打印输出口
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
再选择4根管脚作为模拟协议用的GPIO, 这里用PA15作为CS,PB3作为CLK,PB4作为MISO, PB5作为MOSI,各管脚上外部电阻上拉到AD7791的供电电压,STM32的管脚配置为Open-drain模式,从而可以兼容不同供电电压的AD7791访问连接, 因此STM32的GPIO也都选择为具有FT(5V耐压)能力的管脚。
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
保存并生成基本工程代码:
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据

STM32工程代码

AD7791的ADC数据读取有5种模式:

  1. 单次转换单次单读
  2. 单次转换多次单读(读到同样的数据)
  3. 连续转换单次单读
  4. 连续转换多次单读
  5. 连续抓换多次连读
    其中单读和连续的区别是,单读的每次读取前要发送一次写操作,连续的所有读取前只发送一次写操作。这里的工程代码对5种模式都做了函数实现。

代码采用的微秒级延时函数实现,参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
代码用到串口打印printf函数的重载及采用的浮点转换函数,参考: STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)

完整的main.c工程代码如下:

/* 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 "usart.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = HAL_GetTick()+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = HAL_GetTick();
  PY_Delay_us_t(1000000) ;
  secondms = HAL_GetTick();

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}


void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

/*
*Convert float to string type
*Written by Pegasus Yu in 2022
*stra: string address as mychar from char mychar[];
*float: float input like 12.345
*flen: fraction length as 3 for 12.345
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void py_f2s4printf(char * stra, float x, uint8_t flen)
{
	uint32_t base;
	int64_t dn;
	char mc[32];

	base = pow(10,flen);
	dn = x*base;
	sprintf(stra, "%d.", (int)(dn/base));
	dn = abs(dn);
	if(dn%base==0)
	{
		for(uint8_t j=1;j<=flen;j++)
		{
			stra = strcat(stra, "0");
		}
		return;
	}
	else
	{
		if(flen==1){
			sprintf(mc, "%d", (int)(dn%base));
			stra = strcat(stra, mc);
			return;
		}

		for(uint8_t j=1;j<flen;j++)
		{
			if((dn%base)<pow(10,j))
			{
				for(uint8_t k=1;k<=(flen-j);k++)
				{
					stra = strcat(stra, "0");
				}
				sprintf(mc, "%d", (int)(dn%base));
				stra = strcat(stra, mc);
				return;
			}
		}
		sprintf(mc, "%d", (int)(dn%base));
		stra = strcat(stra, mc);
		return;
	}
}
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define   AD7791_CS_LOW      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET)
#define   AD7791_CS_HIGH     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET)
#define   AD7791_CLK_LOW     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET)
#define   AD7791_CLK_HIGH    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET)
#define   AD7791_DIN_LOW     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET)
#define   AD7791_DIN_HIGH    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET)
#define   AD7791_DOUT_nRDY         HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4)

void AD7791_RST(void)
{
/*
 * The serial interface can be reset by writing a series of 1s on the DIN input. If a Logic 1 is written to the AD7791 line for at least
 * 32 serial clock cycles, the serial interface is reset.
 *
 * The DOUT/ RDY pin operates as a Data Ready signal also, the line going low when a new data-word is available in the output register.
 * It is reset high when a read operation from the data register is complete.
 * It also goes high prior to the updating of the data register to indicate when not to read from the device to ensure that a data read is
 * not attempted while the register is being updated.
 */
  AD7791_CS_HIGH;
  AD7791_DIN_HIGH;
  AD7791_CLK_HIGH;

  AD7791_CS_LOW;
  PY_Delay_us_t(1);

  for(uint8_t i=0;i<32;i++)
  {
	AD7791_CLK_LOW;
	PY_Delay_us_t(1);
	AD7791_CLK_HIGH;
	PY_Delay_us_t(1);
  }

  AD7791_DIN_LOW;
  AD7791_CS_HIGH;
}

/*
 * write access to any of the other registers on the part begins with a write operation to the communications register followed by a
 * write to the selected register.
 * A read operation from any other register (except when continuous read mode is selected) starts
 * with a write to the communications register followed by a read operation from the selected register.
 */

void AD7791_WR_1BYTE(uint8_t cmd) //write 8-bit data
{
  for(uint8_t i=0; i<8; i++)
	{
	   AD7791_CLK_LOW;
	   if ((cmd<<i)&0x80) AD7791_DIN_HIGH;
       else AD7791_DIN_LOW;
	   PY_Delay_us_t(1);
       AD7791_CLK_HIGH;
       PY_Delay_us_t(1);
	}

}

uint32_t AD7791_RD_3BYTE(void) //read 24-bit data
{
  uint8_t rbit = 0;
  uint32_t rdata = 0;

  for(uint8_t i=1; i<=24; i++)
  {
	PY_Delay_us_t(1);
	AD7791_CLK_LOW;
	PY_Delay_us_t(1);
	AD7791_CLK_HIGH;
	rbit = AD7791_DOUT_nRDY;
	if (rbit != 0) rdata |= (((uint32_t)rbit)<<(24-i));
  }

  return rdata;
}

void AD7791_POWER_DOWN(void)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0xc4);

	AD7791_CS_HIGH;
}

uint8_t AD7791_READ_STATUS_REG(void)
{
   uint8_t rbit = 0;
   uint8_t rdata = 0;

    AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x08);

	AD7791_DIN_LOW;
	AD7791_CLK_HIGH;

	for(uint8_t i=1; i<=8; i++)
	{
		PY_Delay_us_t(1);
		AD7791_CLK_LOW;
		PY_Delay_us_t(1);
		AD7791_CLK_HIGH;
		rbit = AD7791_DOUT_nRDY;
		if (rbit != 0) rdata |= ((rbit)<<(8-i));
	}

	AD7791_CS_HIGH;
	return rdata;
}

uint8_t AD7791_SET_FILTER_REG(uint8_t data)
{
	/*
	 * Due to chip fault, 0 or 1 written to lowest bit of filter register will become 1. So to write 0x04 will get back-read value 0x05.
	 * So don't write filter register if you want to keep 0x04 in filter register which will be filled 0x04 when power up.
	 */
	uint8_t mode = 0x04; //default: clock without division, 16.6Hz output rate
	uint8_t rbit = 0;
	uint8_t rdata = 0;

	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x20);

	mode = data;
	AD7791_WR_1BYTE(mode);

	AD7791_CS_HIGH;
	AD7791_RST();
	PY_Delay_us_t(1);

	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x28);

	AD7791_DIN_LOW;
	AD7791_CLK_HIGH;

	for(uint8_t i=1; i<=8; i++)
	{
		PY_Delay_us_t(1);
		AD7791_CLK_LOW;
		PY_Delay_us_t(1);
		AD7791_CLK_HIGH;
		rbit = AD7791_DOUT_nRDY;
		if (rbit != 0) rdata |= ((rbit)<<(8-i));
	}

	AD7791_CS_HIGH;
	return rdata;

}

uint32_t AD7791_SAMPLE_VDD_SINGLE(void)
{
/*
* Along with converting external voltages, the analog input chan-nel can be used to monitor the voltage on the VDD pin.
* When the CH1 and CH0 bits in the communications register are set to 1,
* the voltage on the VDD pin is internally attenuated by 5 and the resultant voltage is applied to the Σ-Δ modulator using an internal 1.17 V reference for analog to digital conversion.
*/
  uint32_t vdd = 0;

  AD7791_CLK_HIGH;
  AD7791_CS_LOW;

  AD7791_WR_1BYTE(0x17);
  AD7791_WR_1BYTE(0x06);

  while(AD7791_DOUT_nRDY) ;

  AD7791_WR_1BYTE(0x3f);
  vdd = AD7791_RD_3BYTE();

  AD7791_CS_HIGH;
  AD7791_RST();

  return vdd;

/*
 * voltage = (vdd/16777216)*1.17*5
 */
}


/* *
 * Mode1: single conversion mode + single read mode : get one-time 24-bit data (write operation advanced before read operation)
 * Mode2: single conversion mode + multi-time single read mode: get multi-time same 24-bit data (write operation advanced before every read operation)
 * Mode3: continuous conversion mode + single read mode: get one-time 24-bit data (write operation advanced before read operation)
 * Mode4: continuous conversion mode + multi-time single read mode: get multi-time individual 24-bit data (write operation advanced before every read operation)
 * Mode5: continuous conversion mode + continuous read mode: get multi-time individual 24-bit data (only once write operation for all read operations)
 */

void AD7791_Mode1_RD(uint32_t *result)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	//AD7791_WR_1BYTE(0x12); //used for AIN(–) – AIN(–) test only
	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0x86);//single conversion mode, no burn-out current, uni-polar and buffered

	while(AD7791_DOUT_nRDY) ;

	AD7791_WR_1BYTE(0x38);//single read
	(*result) =  AD7791_RD_3BYTE();

	AD7791_CS_HIGH;

	AD7791_RST();
}

void AD7791_Mode2_RD(uint32_t *result, uint32_t times)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0x86);//single conversion mode, no burn-out current, uni-polar and buffered

	while(AD7791_DOUT_nRDY) ;

	for(uint32_t i=0; i<times; i++)
	{
		AD7791_WR_1BYTE(0x38);//single read
		result[i] = AD7791_RD_3BYTE();
		PY_Delay_us_t(1);
	}

	AD7791_CS_HIGH;

	AD7791_RST();
}

void AD7791_Mode3_RD(uint32_t *result)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0x06);//continuous conversion mode, no burn-out current, uni-polar and buffered

	while(AD7791_DOUT_nRDY) ;

	AD7791_WR_1BYTE(0x38);//single read
	(*result) =  AD7791_RD_3BYTE();

	AD7791_CS_HIGH;

	AD7791_RST();
}

void AD7791_Mode4_RD(uint32_t *result, uint32_t times)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0x06);//continuous conversion mode, no burn-out current, uni-polar and buffered

	for(uint32_t i=0; i<times; i++)
	{
		while(AD7791_DOUT_nRDY) ;

		AD7791_WR_1BYTE(0x38);//single read
		result[i] =  AD7791_RD_3BYTE();

		PY_Delay_us_t(1);
	}

	AD7791_CS_HIGH;

	AD7791_RST();
}

void AD7791_Mode5_RD(uint32_t *result, uint32_t times)
{
	AD7791_CLK_HIGH;
	AD7791_CS_LOW;

	AD7791_WR_1BYTE(0x10);
	AD7791_WR_1BYTE(0x06);//continuous conversion mode, no burn-out current, uni-polar and buffered
	AD7791_WR_1BYTE(0x3c);//continuous read

	for(uint32_t i=0; i<times; i++)
	{
		while(AD7791_DOUT_nRDY) ;
		result[i] =  AD7791_RD_3BYTE();
		PY_Delay_us_t(1);
	}

	AD7791_CS_HIGH;

	AD7791_RST();
}
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
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);
/* 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 */
	  uint32_t AData[128];
	  float vdd;
	  char mychar[100];
  /* 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();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		 printf("Status Register Read Value=0x%02x\r\n", AD7791_READ_STATUS_REG());

	     printf("Filter Register Set Value=0x%02x\r\n", AD7791_SET_FILTER_REG(0x05));

		 vdd = ((float)AD7791_SAMPLE_VDD_SINGLE())*1.17*5/16777216;
		 py_f2s4printf(mychar, vdd, 6);
		 printf("VDD Sampling Read Value=%s V\r\n", mychar);

		 AD7791_Mode1_RD(AData);
		 printf("Mode 1 Signal Sampling Read Value=%d\r\n", AData[0]);

		 AD7791_Mode2_RD(AData, 4);
		 printf("Mode 2 Signal Sampling Read Value:\r\n%d\r\n%d\r\n%d\r\n%d\r\n", AData[0], AData[1], AData[2], AData[3]);

		 AD7791_Mode3_RD(AData);
		 printf("Mode 3 Signal Sampling Read Value=%d\r\n", AData[0]);

		 AD7791_Mode4_RD(AData, 4);
		 printf("Mode 4 Signal Sampling Read Value:\r\n%d\r\n%d\r\n%d\r\n%d\r\n", AData[0], AData[1], AData[2], AData[3]);

		 AD7791_Mode5_RD(AData, 4);
		 printf("Mode 5 Signal Sampling Read Value:\r\n%d\r\n%d\r\n%d\r\n%d\r\n", AData[0], AData[1], AData[2], AData[3]);

		 PY_Delay_us_t(1000000);
		 printf("\r\n");
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_SET);

  /*Configure GPIO pin : PA15 */
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB3 PB4 PB5 */
  GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

STM32测试输出

串口测试输出如下:
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据

例程下载

STM32F103C6T6模拟SPI协议读取24位模数转换(24bit ADC)芯片AD7791数据例程

–End–文章来源地址https://www.toymoban.com/news/detail-485300.html

到了这里,关于STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STM32读取24位模数转换(24bit ADC)芯片HX711数据

    HX711是一款国产低成本24位ADC芯片,常用于与称重传感器配合实现体重计的应用。这里介绍STM32读取HX711的电路和代码实现。 HX711的内部原理如下图所示: 市面上有普通和带屏蔽的两种模块: STM32可直接与HX711进行连接,选择2个具有FT(5V耐压)的管脚,将其中对应时钟输出的管

    2023年04月08日
    浏览(69)
  • STM32配置读取双路24位模数转换(24bit ADC)芯片CS1238数据

    CS1238是一款国产双路24位ADC芯片,与CS1238对应的单路24位ADC芯片是CS1237,功能上相当于HX711和TM7711的组合。其功能如下所示: 市面上的模块: CS1238内部原理如下所示, VDD是DVDD和AVDD的合并: 有单独的参考电压输入设置管脚,以及内部输出与VDD同电压的参考输出电压,可选连接到

    2024年02月16日
    浏览(42)
  • STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

    STM32部分芯片具有12位DAC输出能力,要实现16位及以上DAC输出需要外挂DAC转换ASIC。 DAC8552是双路16位DAC输出芯片,通过SPI三线总线进行配置控制输出。这里介绍通过GPIO管脚模拟时序进行控制的方式。 DAC8552支持2.7V~5.5V的供电,根据需要提供电源电压,对于STM32可能面对不同供电电

    2023年04月09日
    浏览(40)
  • STM32-ADC模数转换

    ADC的概念 Analog-to-Digital Converter的缩写。指模/数转换器或者模拟/数字转换器。是指将连续变量的模拟信号转换为离散的数字信号的器件。 ADC的作用 采集传感器的数据,测量输入电压,检查电池电量剩余,监测温湿度等。 典型的模拟数字转换器将模拟信号转换为表示一定

    2024年01月20日
    浏览(37)
  • STM32--ADC模数转换

    STM32的ADC(Analog-Digital Converter)模拟-数字转换器 , 是一种逐次逼近型模拟数字转换器,可以将引脚上连续变化的模拟电压转换为内存中存储的数字变量,建立模拟电路到数字电路的桥梁 。拥有18个输入通道,可测量16个外部通道和2个内部信号源。各通道的A/D转换可以单次、

    2024年02月12日
    浏览(34)
  • STM-32:ADC模数转换器—ADC单通道转换/ADC多通道转换

    ADC(Analog-Digital Converter),意即模拟-数字转换器,简称模数转换器。ADC可以将引脚上连续变化的模拟电压转换为内存中存储的数字变量,建立模拟电路到数字电路的桥梁。与ADC相对应,从数字电路到模拟电路的桥梁即DAC(Digital-Analog Convertor),数模转换器。 DAC不是唯一可以

    2024年02月09日
    浏览(44)
  • STM32笔记(1)———ADC模数转换器原理及单、双通道转换

    ADC(Analog-Digital Converter),意即模拟-数字转换器,简称模数转换器。 ADC可以将引脚上连续变化的模拟电压转换为内存中存储的数字变量,建立模拟电路到数字电路的桥梁。 DAC:数字到模拟的桥梁(PWM控制灯的亮度和电机旋转的速度,DAC的使用只要是在信号发生器、音频解码

    2024年02月04日
    浏览(42)
  • 【第五章】STM32-ADC模数转换(2.AD多通道+DMA转运实验)

    我们在上一节已经了解了ADC以及AD单通道采集的过程,那么既然有AD单通道,那么必然有AD多通道,上一节也已经铺垫了一下: 【问】如果一个规则组同时用多个通道采集数据,那么数据如何读取?                                              --DMA--    

    2024年04月26日
    浏览(36)
  • 【STM32】ADC(模拟/数字转换)

    PWM---》实际上就是使用DAC(数字--模拟)。因为PWM只有完全断开和完全接通,所以在大功率电机中损耗很小,所以使用PWM比较合适。 1)将【电信号】--【电压】--【数字量】 2)ADC可以将引脚上 连续变化的模拟电压转换为内存中存储的数字量 ,建立模拟电路到数字电路的桥梁

    2024年02月01日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包