CubeMX的IIC配置
时钟串口见之前文章;IIC配置只需修改IIC时钟频率,ADS115频率范围如下。
.h文件
#ifndef __ADS1115_H__
#define __ADS1115_H__
#include "main.h"
extern I2C_HandleTypeDef hi2c1;
#define Accuracy 32768
#define ADS1115_ADDRESS_W ADS1115_ADDRESS|0x00
#define ADS1115_ADDRESS_R ADS1115_ADDRESS|0x01
#define ADS1115_Pointer_ConverReg 0x00
#define ADS1115_Pointer_ConfigReg 0x01
#define ADS1115_Pointer_LoThreshReg 0x02
#define ADS1115_Pointer_HiThreshReg 0x03
#define ADS1115_OS_OperationalStatus 0x0000
#define ADS1115_OS_SingleConverStart 0x8000
#define ADS1115_MUX_Differ_01 0x0000
#define ADS1115_MUX_Differ_03 0x1000
#define ADS1115_MUX_Differ_13 0x2000
#define ADS1115_MUX_Differ_23 0x3000
#define ADS1115_MUX_Channel_0 0x4000
#define ADS1115_MUX_Channel_1 0x5000
#define ADS1115_MUX_Channel_2 0x6000
#define ADS1115_MUX_Channel_3 0x7000
#define ADS1115_PGA_6144 0x0000
#define ADS1115_PGA_4096 0x0200
#define ADS1115_PGA_2048 0x0400
#define ADS1115_PGA_1024 0x0600
#define ADS1115_PGA_0512 0x0800
#define ADS1115_PGA_0256 0x0A00
#define ADS1115_MODE_ContinuConver 0x0000
#define ADS1115_MODE_SingleConver 0x0100
#define ADS1115_DataRate_8 0x0000
#define ADS1115_DataRate_16 0x0020
#define ADS1115_DataRate_32 0x0040
#define ADS1115_DataRate_64 0x0060
#define ADS1115_DataRate_128 0x0080
#define ADS1115_DataRate_250 0x00A0
#define ADS1115_DataRate_475 0x00C0
#define ADS1115_DataRate_860 0x00E0
#define ADS1115_COMP_MODE_0 0x0000
#define ADS1115_COMP_MODE_1 0x0010
#define ADS1115_COMP_POL_0 0x0000
#define ADS1115_COMP_POL_1 0x0008
#define ADS1115_COMP_LAT_0 0x0000
#define ADS1115_COMP_LAT_1 0x0004
#define ADS1115_COMP_QUE_0 0x0000
#define ADS1115_COMP_QUE_1 0x0001
#define ADS1115_COMP_QUE_2 0x0002
#define ADS1115_COMP_QUE_3 0x0003
typedef enum
{
ADS1115_ADDRESS_GND = 0x90,
ADS1115_ADDRESS_VDD = 0x92,
ADS1115_ADDRESS_SDA = 0x94,
ADS1115_ADDRESS_SCL = 0x96
}ADS1115_ADDRESS;
typedef enum
{
ADS1115_CHANNEL0 = 0U,
ADS1115_CHANNEL1,
ADS1115_CHANNEL2,
ADS1115_CHANNEL3,
ADS1115_MAX_CHANNEL
}ADS1115_CHANNEL;
typedef struct
{
uint16_t OS;
uint16_t MUX;
uint16_t PGA;
uint16_t MODE;
uint16_t DataRate;
uint16_t COMP_MODE;
uint16_t COMP_POL;
uint16_t COMP_LAT;
uint16_t COMP_QUE;
ADS1115_CHANNEL CHANNEL;
ADS1115_ADDRESS ADDRESS;
int16_t ADS1115_RawData[4];
float ADS1115_Vol[4];
} ADS1115_InitTypeDefine;
void ADS1115_Init(void);
void ADS1115_UserConfig_SingleConver(ADS1115_InitTypeDefine* hADS1115 , ADS1115_ADDRESS ADDRESS);
void ADS1115_UserConfig_ContinuConver(ADS1115_InitTypeDefine* ADS1115_InitStruct,ADS1115_ADDRESS ADDRESS);
void ADS1115_Config(ADS1115_InitTypeDefine* ADS1115_InitStruct);
void ADS1115_ReadRawData(ADS1115_InitTypeDefine *ADS1115_InitStruct);
void ADS1115_ScanChannel(ADS1115_InitTypeDefine *ADS1115_InitStruct);
void ADS1115_RawDataToVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct);
void ADS1115_GetVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct);
void ADS1115_GetAverageVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct);
void ADS1115_RefreshAllChannel(ADS1115_InitTypeDefine *ADS1115_InitStruct);
extern ADS1115_InitTypeDefine ADS1115_ADDR_VDD;
extern ADS1115_InitTypeDefine ADS1115_ADDR_GND;
#endif
.c文件
#include "ads1115.h"
ADS1115_InitTypeDefine ADS1115_ADDR_GND;
ADS1115_InitTypeDefine ADS1115_ADDR_VDD;
float ADS1115_CHn_Vol[4];
int16_t ADS1115_RawData[4] = {0};
void ADS1115_Init()
{
}
void ADS1115_UserConfig_SingleConver(ADS1115_InitTypeDefine* ADS1115_InitStruct,ADS1115_ADDRESS ADDRESS)
{
ADS1115_InitStruct->COMP_LAT = ADS1115_COMP_LAT_0;
ADS1115_InitStruct->COMP_MODE = ADS1115_COMP_MODE_0;
ADS1115_InitStruct->COMP_POL = ADS1115_COMP_POL_0;
ADS1115_InitStruct->DataRate = ADS1115_DataRate_475;
ADS1115_InitStruct->MODE = ADS1115_MODE_SingleConver;
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_0;
ADS1115_InitStruct->OS = ADS1115_OS_SingleConverStart;
ADS1115_InitStruct->PGA = ADS1115_PGA_4096;
ADS1115_InitStruct->ADDRESS = ADDRESS;
ADS1115_InitStruct->CHANNEL = ADS1115_CHANNEL0;
ADS1115_Config(ADS1115_InitStruct);
}
void ADS1115_UserConfig_ContinuConver(ADS1115_InitTypeDefine* ADS1115_InitStruct,ADS1115_ADDRESS ADDRESS)
{
ADS1115_InitStruct->COMP_LAT = ADS1115_COMP_LAT_0;
ADS1115_InitStruct->COMP_MODE = ADS1115_COMP_MODE_0;
ADS1115_InitStruct->COMP_POL = ADS1115_COMP_POL_0;
ADS1115_InitStruct->DataRate = ADS1115_DataRate_475;
ADS1115_InitStruct->MODE = ADS1115_MODE_ContinuConver;
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_0;
ADS1115_InitStruct->OS = ADS1115_OS_OperationalStatus;
ADS1115_InitStruct->PGA = ADS1115_PGA_4096;
ADS1115_Config(ADS1115_InitStruct);
}
void ADS1115_Config(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
uint16_t Config;
uint8_t Writebuff[3];
Config = ADS1115_InitStruct->OS + ADS1115_InitStruct->MUX + ADS1115_InitStruct->PGA + ADS1115_InitStruct->MODE
+ ADS1115_InitStruct->DataRate + ADS1115_InitStruct->COMP_MODE + ADS1115_InitStruct->COMP_POL
+ ADS1115_InitStruct->COMP_LAT + ADS1115_InitStruct->COMP_QUE;
Writebuff[0] = ADS1115_Pointer_ConfigReg;
Writebuff[1] = (unsigned char) ((Config >> 8) & 0xFF);
Writebuff[2] = (unsigned char) (Config & 0xFF);
HAL_I2C_Master_Transmit(&hi2c1, ADS1115_InitStruct->ADDRESS, Writebuff, 3, 100);
}
void ADS1115_ReadRawData(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
unsigned char Result[2];
uint8_t Writebuff[1];
Writebuff[0] = ADS1115_Pointer_ConverReg;
HAL_I2C_Master_Transmit(&hi2c1, ADS1115_InitStruct->ADDRESS, Writebuff, 3, 100);
HAL_I2C_Master_Receive(&hi2c1, ADS1115_InitStruct->ADDRESS , Result, 2, 100);
ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL] = (int16_t) (((Result[0] << 8) & 0xFF00) | (Result[1] & 0xFF));
}
void ADS1115_ScanChannel(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
switch (ADS1115_InitStruct->CHANNEL)
{
case ADS1115_CHANNEL0:
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_0;
break;
case ADS1115_CHANNEL1:
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_1;
break;
case ADS1115_CHANNEL2:
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_2;
break;
case ADS1115_CHANNEL3:
ADS1115_InitStruct->MUX = ADS1115_MUX_Channel_3;
break;
default:
break;
}
ADS1115_Config(ADS1115_InitStruct);
}
void ADS1115_RawDataToVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
switch (ADS1115_InitStruct->PGA)
{
case ADS1115_PGA_0256:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.0078125;
break;
case ADS1115_PGA_0512:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.015625;
break;
case ADS1115_PGA_1024:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.03125;
break;
case ADS1115_PGA_2048:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.0625;
break;
case ADS1115_PGA_4096:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.125;
break;
case ADS1115_PGA_6144:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = ADS1115_InitStruct->ADS1115_RawData[ADS1115_InitStruct->CHANNEL]* 0.1875;
break;
default:
ADS1115_InitStruct->ADS1115_Vol[ADS1115_InitStruct->CHANNEL] = 0;
break;
}
}
void ADS1115_GetVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
ADS1115_ReadRawData(ADS1115_InitStruct);
ADS1115_RawDataToVoltage(ADS1115_InitStruct);
}
void ADS1115_GetAverageVoltage(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
}
void ADS1115_RefreshAllChannel(ADS1115_InitTypeDefine *ADS1115_InitStruct)
{
ADS1115_ReadRawData(ADS1115_InitStruct);
ADS1115_ReadRawData(ADS1115_InitStruct);
ADS1115_ReadRawData(ADS1115_InitStruct);
ADS1115_InitStruct->CHANNEL++;
if(ADS1115_InitStruct->CHANNEL > ADS1115_MAX_CHANNEL-1)
ADS1115_InitStruct->CHANNEL = ADS1115_CHANNEL0;
ADS1115_ScanChannel(ADS1115_InitStruct);
}
main.c
ADS1115_UserConfig_SingleConver(&ADS1115_ADDR_GND,ADS1115_ADDRESS_GND);
ADS1115_UserConfig_SingleConver(&ADS1115_ADDR_VDD,ADS1115_ADDRESS_VDD);
for(;;)
{
ADS1115_GetVoltage(&ADS1115_ADDR_GND);
ADS1115_RefreshAllChannel(&ADS1115_ADDR_GND);
printf("ADS1115 GND CH %d vol = %.3f \n\n",ADS1115_ADDR_GND.CHANNEL,ADS1115_ADDR_GND.ADS1115_Vol[ADS1115_ADDR_GND.CHANNEL]);
ADS1115_GetVoltage(&ADS1115_ADDR_VDD);
ADS1115_RefreshAllChannel(&ADS1115_ADDR_VDD);
printf("ADS1115 VDD CH %d vol = %.3f \n\n",ADS1115_ADDR_VDD.CHANNEL,ADS1115_ADDR_VDD.ADS1115_Vol[ADS1115_ADDR_VDD.CHANNEL]);
osDelay(100);
}
|