用HAL库写驱动3个ads1115,采回的数据求十次的平均值,并用串口输出
时间: 2024-03-05 16:49:34 浏览: 97
DHT11数据采集并且通过串口显示
下面是使用HAL库驱动3个ADS1115芯片进行数据采集、平均值计算并通过串口输出的示例代码:
```c
#include "stm32f1xx_hal.h"
#include "string.h"
#include "stdio.h"
#define ADS1115_I2C_ADDRESS_1 0x90 // 第一个ADS1115的地址
#define ADS1115_I2C_ADDRESS_2 0x92 // 第二个ADS1115的地址
#define ADS1115_I2C_ADDRESS_3 0x94 // 第三个ADS1115的地址
#define I2C_TIMEOUT 1000 // I2C传输超时时间
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_I2C1_Init(void);
void MX_USART1_UART_Init(void);
void ADS1115_Read(uint8_t i2c_address, uint8_t reg_address, uint16_t *data);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
uint16_t data_1[4], data_2[4], data_3[4]; // 用于存储读取的数据
uint32_t sum_1 = 0, sum_2 = 0, sum_3 = 0; // 用于计算平均值
uint16_t avg_1, avg_2, avg_3; // 用于存储平均值
char buffer[50]; // 用于存储输出字符串
while (1)
{
// 读取ADS1115芯片1的4个数据
ADS1115_Read(ADS1115_I2C_ADDRESS_1, 0x00, &data_1[0]);
ADS1115_Read(ADS1115_I2C_ADDRESS_1, 0x01, &data_1[1]);
ADS1115_Read(ADS1115_I2C_ADDRESS_1, 0x02, &data_1[2]);
ADS1115_Read(ADS1115_I2C_ADDRESS_1, 0x03, &data_1[3]);
// 读取ADS1115芯片2的4个数据
ADS1115_Read(ADS1115_I2C_ADDRESS_2, 0x00, &data_2[0]);
ADS1115_Read(ADS1115_I2C_ADDRESS_2, 0x01, &data_2[1]);
ADS1115_Read(ADS1115_I2C_ADDRESS_2, 0x02, &data_2[2]);
ADS1115_Read(ADS1115_I2C_ADDRESS_2, 0x03, &data_2[3]);
// 读取ADS1115芯片3的4个数据
ADS1115_Read(ADS1115_I2C_ADDRESS_3, 0x00, &data_3[0]);
ADS1115_Read(ADS1115_I2C_ADDRESS_3, 0x01, &data_3[1]);
ADS1115_Read(ADS1115_I2C_ADDRESS_3, 0x02, &data_3[2]);
ADS1115_Read(ADS1115_I2C_ADDRESS_3, 0x03, &data_3[3]);
// 计算平均值
for(int i = 0; i < 4; i++)
{
sum_1 += data_1[i];
sum_2 += data_2[i];
sum_3 += data_3[i];
}
avg_1 = sum_1 / 40;
avg_2 = sum_2 / 40;
avg_3 = sum_3 / 40;
// 通过串口输出平均值
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "ADS1115_1: %d, ADS1115_2: %d, ADS1115_3: %d\r\n", avg_1, avg_2, avg_3);
HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), I2C_TIMEOUT);
// 清空计算平均值的变量
sum_1 = 0;
sum_2 = 0;
sum_3 = 0;
HAL_Delay(1000); // 延时1秒钟
}
}
/* 读取ADS1115芯片的数据 */
void ADS1115_Read(uint8_t i2c_address, uint8_t reg_address, uint16_t *data)
{
uint8_t tx_data[1] = {reg_address};
HAL_I2C_Master_Transmit(&hi2c1, i2c_address, tx_data, 1, I2C_TIMEOUT); // 发送读取地址
HAL_I2C_Master_Receive(&hi2c1, i2c_address, (uint8_t*)data, 2, I2C_TIMEOUT); // 读取数据
*data = (*data >> 8) | (*data << 8); // 交换高低字节
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
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(__FILE__, __LINE__);
}
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(__FILE__, __LINE__);
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
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;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void _Error_Handler(char *file, int line)
{
while(1)
{
// 错误处理
}
}
```
说明:
1. 在此示例代码中,使用的是3个ADS1115芯片,它们的I2C地址分别为0x90、0x92和0x94。
2. 使用`ADS1115_Read`函数读取ADS1115芯片的数据,并将读取到的数据存储在`data`数组中。
3. 在主循环中,分别读取3个ADS1115芯片的4个数据,并计算出平均值。然后将平均值通过串口输出。
4. 由于ADS1115芯片的数据是16位的,因此在读取数据后需要将高低字节进行交换。
5. 本示例代码使用的是USART1串口,波特率为115200。如果需要更改串口配置,请修改`MX_USART1_UART_Init`函数。
6. 如果需要使用其他GPIO口,请修改`MX_GPIO_Init`函数。
7. 如果需要修改I2C配置,请修改`MX_I2C1_Init`函数。
阅读全文