HAL库写驱动3个ads1115,并对所得的电压值进行滤波。通过串口发送。STM32F030R8T6
时间: 2024-03-09 13:49:20 浏览: 126
好的,下面是一个示例代码,可以用HAL库驱动三个ADS1115,并对所得的电压值进行滤波,最后通过串口发送。
```c
#include "stm32f0xx_hal.h"
#include "stdio.h"
#define ADS1115_ADDRESS 0x90 // ADS1115器件地址,A0,A1,A2接地
// ADS1115内部寄存器地址
#define ADS1115_REG_POINTER_CONVERT 0x00 // 转换寄存器
#define ADS1115_REG_POINTER_CONFIG 0x01 // 配置寄存器
// ADS1115配置寄存器参数
#define ADS1115_CONFIG_OS_SINGLE 0x8000 // 单次转换模式
#define ADS1115_CONFIG_MUX_DIFF_0_1 0x0000 // 差分输入0-1
#define ADS1115_CONFIG_MUX_DIFF_0_3 0x1000 // 差分输入0-3
#define ADS1115_CONFIG_MUX_DIFF_1_3 0x2000 // 差分输入1-3
#define ADS1115_CONFIG_MUX_DIFF_2_3 0x3000 // 差分输入2-3
#define ADS1115_CONFIG_PGA_6_144V 0x0000 // PGA增益为6.144V
#define ADS1115_CONFIG_MODE_SINGLE 0x0100 // 单次转换模式
#define ADS1115_CONFIG_DR_860SPS 0x0080 // 数据输出速率为860SPS
#define ADS1115_CONFIG_CMODE_TRAD 0x0000 // 传统比较器模式
#define ADS1115_CONFIG_CPOL_ACTVLOW 0x0000 // 比较器输出极性为低
#define ADS1115_CONFIG_CLAT_NONLAT 0x0000 // 非滞后模式
#define ADS1115_CONFIG_CQUE_1CONV 0x0000 // 至少一次转换后触发比较器
// 滤波参数
#define FILTER_SIZE 5 // 滤波器容量
float filter_buffer[FILTER_SIZE][3]; // 滤波器缓存
int filter_index = 0; // 滤波器索引
ADC_HandleTypeDef hadc;
UART_HandleTypeDef huart;
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_USART1_UART_Init(void);
void MX_ADC_Init(void);
void ADS1115_WriteRegister(uint8_t i2c_addr, uint8_t reg_addr,
uint16_t reg_value)
{
uint8_t tx_buf[3];
tx_buf[0] = reg_addr;
tx_buf[1] = reg_value >> 8;
tx_buf[2] = reg_value & 0xFF;
HAL_I2C_Master_Transmit(&hi2c1, i2c_addr, tx_buf, 3, 1000);
}
uint16_t ADS1115_ReadRegister(uint8_t i2c_addr, uint8_t reg_addr)
{
uint8_t tx_buf[1];
uint8_t rx_buf[2];
tx_buf[0] = reg_addr;
HAL_I2C_Master_Transmit(&hi2c1, i2c_addr, tx_buf, 1, 1000);
HAL_I2C_Master_Receive(&hi2c1, i2c_addr, rx_buf, 2, 1000);
return (rx_buf[0] << 8) | rx_buf[1];
}
float ADS1115_ReadVoltage(uint8_t i2c_addr, uint8_t mux_diff, uint8_t pga)
{
uint16_t config = ADS1115_CONFIG_OS_SINGLE |
mux_diff |
pga |
ADS1115_CONFIG_MODE_SINGLE |
ADS1115_CONFIG_DR_860SPS |
ADS1115_CONFIG_CMODE_TRAD |
ADS1115_CONFIG_CPOL_ACTVLOW |
ADS1115_CONFIG_CLAT_NONLAT |
ADS1115_CONFIG_CQUE_1CONV;
ADS1115_WriteRegister(i2c_addr, ADS1115_REG_POINTER_CONFIG, config);
HAL_Delay(1);
uint16_t adc_value = ADS1115_ReadRegister(i2c_addr, ADS1115_REG_POINTER_CONVERT);
float voltage = 0;
switch (pga)
{
case ADS1115_CONFIG_PGA_6_144V:
voltage = (float)adc_value * 6.144 / 32768;
break;
case ADS1115_CONFIG_PGA_4_096V:
voltage = (float)adc_value * 4.096 / 32768;
break;
case ADS1115_CONFIG_PGA_2_048V:
voltage = (float)adc_value * 2.048 / 32768;
break;
case ADS1115_CONFIG_PGA_1_024V:
voltage = (float)adc_value * 1.024 / 32768;
break;
case ADS1115_CONFIG_PGA_0_512V:
voltage = (float)adc_value * 0.512 / 32768;
break;
case ADS1115_CONFIG_PGA_0_256V:
voltage = (float)adc_value * 0.256 / 32768;
break;
default:
break;
}
return voltage;
}
float Filter(float value, int channel)
{
// 添加新数据到滤波器缓存
filter_buffer[filter_index][channel] = value;
// 计算滤波结果
float result = 0;
for (int i = 0; i < FILTER_SIZE; i++)
{
result += filter_buffer[i][channel];
}
result /= FILTER_SIZE;
// 更新滤波器索引
filter_index = (filter_index + 1) % FILTER_SIZE;
return result;
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
// 读取电压值
float voltage1 = ADS1115_ReadVoltage(ADS1115_ADDRESS, ADS1115_CONFIG_MUX_DIFF_0_1, ADS1115_CONFIG_PGA_6_144V);
float voltage2 = ADS1115_ReadVoltage(ADS1115_ADDRESS, ADS1115_CONFIG_MUX_DIFF_0_3, ADS1115_CONFIG_PGA_6_144V);
float voltage3 = ADS1115_ReadVoltage(ADS1115_ADDRESS, ADS1115_CONFIG_MUX_DIFF_1_3, ADS1115_CONFIG_PGA_6_144V);
// 进行滤波
float filtered_voltage1 = Filter(voltage1, 0);
float filtered_voltage2 = Filter(voltage2, 1);
float filtered_voltage3 = Filter(voltage3, 2);
// 发送电压值
char buf[64];
sprintf(buf, "V1: %.2f, V2: %.2f, V3: %.2f\r\n", filtered_voltage1, filtered_voltage2, filtered_voltage3);
HAL_UART_Transmit(&huart, (uint8_t *)buf, strlen(buf), 1000);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC_Init();
HAL_ADC_Start_IT(&hadc);
while (1)
{
}
}
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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void MX_USART1_UART_Init(void)
{
huart.Instance = USART1;
huart.Init.BaudRate = 9600;
huart.Init.WordLength = UART_WORDLENGTH_8B;
huart.Init.StopBits = UART_STOPBITS_1;
huart.Init.Parity = UART_PARITY_NONE;
huart.Init.Mode = UART_MODE_TX_RX;
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart) != HAL_OK)
{
Error_Handler();
}
}
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
```
在代码中,我们使用了HAL库的I2C、UART和ADC库函数。我们首先定义了ADS1115器件地址和内部寄存器地址,并使用ADS1115_WriteRegister和ADS1115_ReadRegister函数操作ADS1115器件。我们还定义了滤波器缓存和滤波器索引,使用Filter函数进行滤波。在ADC转换完成后,我们读取三个差分输入的电压值,并进行滤波和串口发送。最后,在主函数中循环调用ADC转换函数,等待中断触发。
如果需要使用其他电压增益或数据输出速率,可以修改ADS1115配置寄存器参数。如果需要使用其他滤波算法,可以替换Filter函数。
阅读全文