HAL库驱动3个ADS1115
时间: 2023-08-04 09:30:51 浏览: 158
如果需要驱动3个ADS1115芯片,则可以使用STM32的多路I2C功能,同时连接3个ADS1115芯片,使用不同的从机地址来区分。以下是一个基于STM32 HAL库的驱动3个ADS1115芯片的示例代码:
```c
#include "stm32f4xx_hal.h"
#define ADS1115_0_ADDRESS (0x90) //ADS1115芯片0的I2C地址
#define ADS1115_1_ADDRESS (0x92) //ADS1115芯片1的I2C地址
#define ADS1115_2_ADDRESS (0x94) //ADS1115芯片2的I2C地址
/* ADS1115寄存器定义 */
#define ADS_REG_POINTER_CONVERT 0x00
#define ADS_REG_POINTER_CONFIG 0x01
#define ADS_REG_POINTER_LOWTHRESH 0x02
#define ADS_REG_POINTER_HITHRESH 0x03
/* ADS1115配置寄存器定义 */
#define ADS_OS_SINGLE 0x8000
#define ADS_MUX_DIFF_0_1 0x0000
#define ADS_MUX_DIFF_0_3 0x1000
#define ADS_MUX_DIFF_1_3 0x2000
#define ADS_MUX_DIFF_2_3 0x3000
#define ADS_MUX_SINGLE_0 0x4000
#define ADS_MUX_SINGLE_1 0x5000
#define ADS_MUX_SINGLE_2 0x6000
#define ADS_MUX_SINGLE_3 0x7000
#define ADS_PGA_6_144V 0x0000
#define ADS_PGA_4_096V 0x0200
#define ADS_PGA_2_048V 0x0400
#define ADS_PGA_1_024V 0x0600
#define ADS_PGA_0_512V 0x0800
#define ADS_PGA_0_256V 0x0A00
#define ADS_MODE_CONTIN 0x0000
#define ADS_MODE_SINGLE 0x0100
#define ADS_DR_128SPS 0x0000
#define ADS_DR_250SPS 0x0020
#define ADS_DR_490SPS 0x0040
#define ADS_DR_920SPS 0x0060
#define ADS_DR_1600SPS 0x0080
#define ADS_DR_2400SPS 0x00A0
#define ADS_DR_3300SPS 0x00C0
#define ADS_CMODE_TRAD 0x0000
#define ADS_CMODE_WINDOW 0x0010
#define ADS_CPOL_ACTVLOW 0x0000
#define ADS_CPOL_ACTVHI 0x0008
#define ADS_CLAT_NONLAT 0x0000
#define ADS_CLAT_LATCH 0x0004
#define ADS_CQUE_1CONV 0x0000
#define ADS_CQUE_2CONV 0x0001
#define ADS_CQUE_4CONV 0x0002
#define ADS_CQUE_NONE 0x0003
/**
* @brief ADS1115读取指定通道的数据
* @param hi2c: I2C句柄
* @param address: ADS1115芯片的I2C地址
* @param channel: 要读取的通道
* ADS_MUX_SINGLE_0: 读取AIN0通道
* ADS_MUX_SINGLE_1: 读取AIN1通道
* ADS_MUX_SINGLE_2: 读取AIN2通道
* ADS_MUX_SINGLE_3: 读取AIN3通道
* @retval 读取的数据
*/
int16_t ADS1115_Read(I2C_HandleTypeDef *hi2c, uint8_t address, uint16_t channel) {
uint8_t config[2];
uint8_t data[2];
int16_t result;
config[0] = ADS_REG_POINTER_CONFIG;
config[1] = ADS_OS_SINGLE | channel | ADS_PGA_6_144V | ADS_MODE_SINGLE |
ADS_DR_128SPS | ADS_CMODE_TRAD | ADS_CPOL_ACTVLOW |
ADS_CLAT_NONLAT | ADS_CQUE_NONE;
/* 向ADS1115写入配置信息 */
HAL_I2C_Master_Transmit(hi2c, address, config, 2, 1000);
/* 等待转换完成 */
HAL_Delay(10);
/* 读取结果 */
config[0] = ADS_REG_POINTER_CONVERT;
HAL_I2C_Master_Transmit(hi2c, address, config, 1, 1000);
HAL_I2C_Master_Receive(hi2c, address, data, 2, 1000);
result = (int16_t)((data[0] << 8) | data[1]);
return result;
}
int main(void) {
I2C_HandleTypeDef hi2c1;
/* 初始化I2C1 */
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) {
/* 初始化失败 */
while (1);
}
/* 读取ADS1115芯片0的AIN0通道数据 */
int16_t data0 = ADS1115_Read(&hi2c1, ADS1115_0_ADDRESS, ADS_MUX_SINGLE_0);
/* 读取ADS1115芯片1的AIN1通道数据 */
int16_t data1 = ADS1115_Read(&hi2c1, ADS1115_1_ADDRESS, ADS_MUX_SINGLE_1);
/* 读取ADS1115芯片2的AIN2通道数据 */
int16_t data2 = ADS1115_Read(&hi2c1, ADS1115_2_ADDRESS, ADS_MUX_SINGLE_2);
while(1) {
/* 循环读取数据 */
data0 = ADS1115_Read(&hi2c1, ADS1115_0_ADDRESS, ADS_MUX_SINGLE_0);
data1 = ADS1115_Read(&hi2c1, ADS1115_1_ADDRESS, ADS_MUX_SINGLE_1);
data2 = ADS1115_Read(&hi2c1, ADS1115_2_ADDRESS, ADS_MUX_SINGLE_2);
/* 处理数据 */
//...
}
}
```
在上述代码中,通过初始化I2C1句柄,然后调用ADS1115_Read函数来读取3个不同的ADS1115芯片的数据,最后将数据存储在data0、data1和data2中。在循环中,可以不断读取数据并进行处理。注意要根据不同的ADS1115芯片使用不同的I2C地址。
阅读全文