赞
踩
STM32大部分芯片只有12位的ADC采样性能,如果要实现更高精度的模数转换如24位ADC采样,则需要连接外部ADC实现。AD7791是亚德诺(ADI)半导体一款用于低功耗、24位Σ-Δ型模数转换器(ADC) ,适合低频测量应用,提供50 Hz/60 Hz同步抑制。
这里介绍基于AD7791的24位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, 访问协议操作主要逻辑如下:
这里以STM32F103C6T6及STM32CUBEIDE开发工具为例,介绍AD7791的24位ADC采样实现。
首先建立基本工程并配置时钟:
配置一个串口如UART2作为通讯打印输出口
再选择4根管脚作为模拟协议用的GPIO, 这里用PA15作为CS,PB3作为CLK,PB4作为MISO, PB5作为MOSI,各管脚上外部电阻上拉到AD7791的供电电压,STM32的管脚配置为Open-drain模式,从而可以兼容不同供电电压的AD7791访问连接, 因此STM32的GPIO也都选择为具有FT(5V耐压)能力的管脚。
保存并生成基本工程代码:
AD7791的ADC数据读取有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 */
串口测试输出如下:
STM32F103C6T6模拟SPI协议读取24位模数转换(24bit ADC)芯片AD7791数据例程
–End–
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。