ADS1115 STM32F103C8T6代码
时间: 2023-11-12 11:09:39 浏览: 111
以下是使用STM32F103C8T6控制ADS1115进行模拟转换的基本代码示例:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "i2c.h"
#define ADS1115_ADDR 0x90
#define ADS1115_REG_CONV 0x00
#define ADS1115_REG_CONFIG 0x01
#define ADS1115_REG_LO_THRESH 0x02
#define ADS1115_REG_HI_THRESH 0x03
#define ADS1115_CONFIG_OS_SINGLE 0x8000
#define ADS1115_CONFIG_MUX_DIFF_0_1 0x0000
#define ADS1115_CONFIG_MUX_DIFF_2_3 0x1000
#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_144 0x0000
#define ADS1115_CONFIG_PGA_4_096 0x0200
#define ADS1115_CONFIG_PGA_2_048 0x0400
#define ADS1115_CONFIG_PGA_1_024 0x0600
#define ADS1115_CONFIG_PGA_0_512 0x0800
#define ADS1115_CONFIG_PGA_0_256 0x0A00
#define ADS1115_CONFIG_MODE_SINGLE 0x0100
#define ADS1115_CONFIG_DR_128 0x0000
#define ADS1115_CONFIG_DR_250 0x0020
#define ADS1115_CONFIG_DR_490 0x0040
#define ADS1115_CONFIG_DR_920 0x0060
#define ADS1115_CONFIG_DR_1600 0x0080
#define ADS1115_CONFIG_DR_2400 0x00A0
#define ADS1115_CONFIG_DR_3300 0x00C0
#define ADS1115_CONFIG_CMODE_TRAD 0x0000
#define ADS1115_CONFIG_CPOL_ACTVLOW 0x0000
#define ADS1115_CONFIG_CLAT_NONLAT 0x0000
#define ADS1115_CONFIG_CQUE_NONE 0x0003
void ADS1115_Init(void)
{
I2C_Configuration();
}
void ADS1115_Convert(uint16_t config)
{
uint8_t txBuf[3];
txBuf[0] = ADS1115_REG_CONFIG;
txBuf[1] = config >> 8;
txBuf[2] = config & 0xFF;
I2C_WriteBytes(ADS1115_ADDR, txBuf, 3);
while (1)
{
I2C_ReadByte(ADS1115_ADDR, ADS1115_REG_CONFIG, txBuf);
if ((txBuf[0] & ADS1115_CONFIG_OS_SINGLE) == 0)
break;
}
}
int16_t ADS1115_Read(void)
{
uint8_t txBuf[2], rxBuf[2];
txBuf[0] = ADS1115_REG_CONV;
I2C_WriteBytes(ADS1115_ADDR, txBuf, 1);
I2C_ReadBytes(ADS1115_ADDR, txBuf, 2, rxBuf);
return ((int16_t)(rxBuf[0] << 8 | rxBuf[1]));
}
```
该示例代码需要使用I2C通信,需要先初始化I2C和延时函数。同时,需要先对ADS1115进行配置,然后才能进行模拟转换和读取转换结果。可以根据自己的需求修改配置参数。
阅读全文