用hal库驱动3个ADS1115采集电压,并通过串口发送
时间: 2023-08-04 16:30:25 浏览: 249
首先,需要在STM32CubeMX中配置好串口和I2C外设,并使能对应的GPIO引脚。具体步骤如下:
1. 打开STM32CubeMX软件,选择对应的芯片型号。
2. 在Pinout & Configuration选项卡中,选择对应的引脚模式。对于USART串口,需要选择Alternate Function,对应的引脚根据具体的芯片不同而有所不同。对于I2C外设,需要选择I2C模式,对应的引脚为SDA和SCL。
3. 在Configuration选项卡中,使能对应的外设,例如USART和I2C。
4. 配置ADS1115的寄存器,使其工作在连续采样模式下,并设置采样速率、增益等参数。
5. 使用HAL库中的I2C和USART驱动函数,读取ADS1115的数据,并将数据通过串口发送。
下面是一份参考代码,仅供参考:
```c
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define ADS1115_ADDR 0x90
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
void ADS1115_Init(uint8_t addr);
uint16_t ADS1115_Read(uint8_t addr, uint8_t channel);
void USART_SendString(char* str);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
ADS1115_Init(ADS1115_ADDR);
char buf[32];
while (1)
{
uint16_t val1 = ADS1115_Read(ADS1115_ADDR, 0);
uint16_t val2 = ADS1115_Read(ADS1115_ADDR, 1);
uint16_t val3 = ADS1115_Read(ADS1115_ADDR, 2);
sprintf(buf, "%d %d %d\r\n", val1, val2, val3);
USART_SendString(buf);
HAL_Delay(1000);
}
}
void ADS1115_Init(uint8_t addr)
{
uint8_t config[3];
config[0] = 0x01; // Pointer to Config register
config[1] = 0xC5; // 250SPS, FSR=4.096V
config[2] = 0x83; // Continuous conversion, AIN0
HAL_I2C_Master_Transmit(&hi2c1, addr, config, 3, HAL_MAX_DELAY);
}
uint16_t ADS1115_Read(uint8_t addr, uint8_t channel)
{
uint8_t config[3];
switch (channel)
{
case 0:
config[1] = 0x83; // Continuous conversion, AIN0
break;
case 1:
config[1] = 0xD3; // Continuous conversion, AIN1
break;
case 2:
config[1] = 0xE3; // Continuous conversion, AIN2
break;
case 3:
config[1] = 0xF3; // Continuous conversion, AIN3
break;
}
config[0] = 0x01; // Pointer to Config register
config[2] = 0x83; // Continuous conversion, AIN0
HAL_I2C_Master_Transmit(&hi2c1, addr, config, 3, HAL_MAX_DELAY);
HAL_Delay(10);
uint8_t data[2];
HAL_I2C_Master_Receive(&hi2c1, addr, data, 2, HAL_MAX_DELAY);
uint16_t result = (data[0] << 8) | data[1];
return result;
}
void USART_SendString(char* str)
{
HAL_UART_Transmit(&huart2, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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();
}
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x2000090E;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
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();
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
```
这是一个简单的示例代码,实际应用中需要根据自己的具体需求进行修改和完善。
阅读全文