用HAL库写驱动3个ads1115,并对所得的电压值进行滤波。通过串口发送
时间: 2024-03-09 22:48:57 浏览: 174
基于HAL库的信号源驱动
下面是基于HAL库的ADS1115驱动代码,其中包括了对电压值进行滤波的功能,并通过串口发送:
```c
#include "stm32f4xx_hal.h"
#include "stdio.h"
#define ADS1115_SLAVE_ADDR_1 0x90
#define ADS1115_SLAVE_ADDR_2 0x92
#define ADS1115_SLAVE_ADDR_3 0x94
#define ADS1115_REG_CONFIG 0x01
#define ADS1115_REG_CONVERSION 0x00
#define ADS1115_CONFIG_MUX_SINGLE_0 0x4000
#define ADS1115_CONFIG_MUX_SINGLE_1 0x5000
#define ADS1115_CONFIG_MUX_SINGLE_2 0x6000
#define ADS1115_CONFIG_MUX_SINGLE_3 0x7000
#define ADS1115_CONFIG_PGA_6_144V 0x0000
#define ADS1115_CONFIG_PGA_4_096V 0x0200
#define ADS1115_CONFIG_PGA_2_048V 0x0400
#define ADS1115_CONFIG_PGA_1_024V 0x0600
#define ADS1115_CONFIG_PGA_0_512V 0x0800
#define ADS1115_CONFIG_PGA_0_256V 0x0A00
#define ADS1115_CONFIG_MODE_SINGLE 0x0100
#define ADS1115_CONFIG_DR_860SPS 0x0080
#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 ADS1115_CONFIG_DEFAULT 0x8583
#define FILTER_BUF_LEN 10
typedef struct {
I2C_HandleTypeDef* i2cHandle;
uint16_t slaveAddr;
uint16_t config;
uint16_t conversionValue;
} ADS1115_HandleTypeDef;
void ADS1115_Init(ADS1115_HandleTypeDef* hads, I2C_HandleTypeDef* hi2c,
uint16_t slaveAddr, uint16_t config);
void ADS1115_StartConversion(ADS1115_HandleTypeDef* hads);
void ADS1115_ReadConversionValue(ADS1115_HandleTypeDef* hads);
void ADS1115_FilterConversionValue(ADS1115_HandleTypeDef* hads);
void ADS1115_SendConversionValue(ADS1115_HandleTypeDef* hads, UART_HandleTypeDef* huart);
float filterBuf[FILTER_BUF_LEN];
uint8_t filterBufIndex = 0;
int main(void) {
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
I2C_HandleTypeDef hi2c1 = {0};
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c1);
UART_HandleTypeDef huart2 = {0};
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;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart2);
ADS1115_HandleTypeDef ads1 = {&hi2c1, ADS1115_SLAVE_ADDR_1, ADS1115_CONFIG_DEFAULT, 0};
ADS1115_HandleTypeDef ads2 = {&hi2c1, ADS1115_SLAVE_ADDR_2, ADS1115_CONFIG_DEFAULT, 0};
ADS1115_HandleTypeDef ads3 = {&hi2c1, ADS1115_SLAVE_ADDR_3, ADS1115_CONFIG_DEFAULT, 0};
while (1) {
ADS1115_StartConversion(&ads1);
ADS1115_StartConversion(&ads2);
ADS1115_StartConversion(&ads3);
ADS1115_ReadConversionValue(&ads1);
ADS1115_ReadConversionValue(&ads2);
ADS1115_ReadConversionValue(&ads3);
ADS1115_FilterConversionValue(&ads1);
ADS1115_FilterConversionValue(&ads2);
ADS1115_FilterConversionValue(&ads3);
ADS1115_SendConversionValue(&ads1, &huart2);
ADS1115_SendConversionValue(&ads2, &huart2);
ADS1115_SendConversionValue(&ads3, &huart2);
HAL_Delay(1000);
}
}
void ADS1115_Init(ADS1115_HandleTypeDef* hads, I2C_HandleTypeDef* hi2c,
uint16_t slaveAddr, uint16_t config) {
hads->i2cHandle = hi2c;
hads->slaveAddr = slaveAddr;
hads->config = config;
hads->conversionValue = 0;
uint8_t configBuf[3];
configBuf[0] = ADS1115_REG_CONFIG;
configBuf[1] = (uint8_t)(hads->config >> 8);
configBuf[2] = (uint8_t)(hads->config & 0xFF);
HAL_I2C_Master_Transmit(hads->i2cHandle, hads->slaveAddr, configBuf, 3, 100);
}
void ADS1115_StartConversion(ADS1115_HandleTypeDef* hads) {
uint8_t configBuf[3];
configBuf[0] = ADS1115_REG_CONFIG;
configBuf[1] = (uint8_t)(hads->config >> 8);
configBuf[2] = (uint8_t)(hads->config & 0xFF);
HAL_I2C_Master_Transmit(hads->i2cHandle, hads->slaveAddr, configBuf, 3, 100);
}
void ADS1115_ReadConversionValue(ADS1115_HandleTypeDef* hads) {
uint8_t readBuf[2];
HAL_I2C_Master_Transmit(hads->i2cHandle, hads->slaveAddr, (uint8_t*)&ADS1115_REG_CONVERSION, 1, 100);
HAL_I2C_Master_Receive(hads->i2cHandle, hads->slaveAddr, readBuf, 2, 100);
hads->conversionValue = ((uint16_t)readBuf[0] << 8) | (uint16_t)readBuf[1];
}
void ADS1115_FilterConversionValue(ADS1115_HandleTypeDef* hads) {
float voltage = (hads->conversionValue * 6.144) / 32767.0;
filterBuf[filterBufIndex] = voltage;
filterBufIndex = (filterBufIndex + 1) % FILTER_BUF_LEN;
float filteredVoltage = 0.0;
for (int i = 0; i < FILTER_BUF_LEN; i++) {
filteredVoltage += filterBuf[i];
}
filteredVoltage /= (float)FILTER_BUF_LEN;
hads->conversionValue = (uint16_t)((filteredVoltage / 6.144) * 32767.0);
}
void ADS1115_SendConversionValue(ADS1115_HandleTypeDef* hads, UART_HandleTypeDef* huart) {
float voltage = (hads->conversionValue * 6.144) / 32767.0;
char buf[64];
snprintf(buf, sizeof(buf), "ADS1115 @ 0x%02X: %f V\r\n", hads->slaveAddr, voltage);
HAL_UART_Transmit(huart, (uint8_t*)buf, strlen(buf), 100);
}
```
这段代码初始化了3个ADS1115,使用I2C读取其转换值并进行滤波,最后通过串口发送电压值。在`main()`函数中的`while(1)`循环中,我们每隔1秒读取一次转换值并发送电压值。你可以根据自己的需求更改这些参数。
阅读全文