大家好,欢迎来到IT知识分享网。
本人也是stm32的初学者,以下代码也是由csdn的其它码友改编过来,认为自己学到的想要分享给大家,既要学会接受,也要学会给予,一下代码的引脚等使用的是野火stm32f103rc_mini版的板子与ADS1115模块进行结合。
若有疑问,欢迎大家一起讨论。
(由于挺久了,找不到原作者了,若有码友发现类似,请告知我,谢谢)
主函数:这里的第二个头文件里其实就是包含stm32的i2c和串口等的头文件(因为为了方便自己写程序,将很多文件的头文件都放在这里面了,码友们可以自己根据需求将自己的头文件加上去)
#include “stm32f10x.h”
#include “stm32f10x_gcy.h”
float ads1115_buffer[4];
int main(void)
{
i2c_CfgGpio();
USART_Config();
while (1)
{
for (int i=0;i<4;i++)
{
ads1115_buffer[i] = ADS1115_Read_average_ADC(i,ADS1115_PGA_4096);//,ADS1115_PGA_4096
printf(“\r\n AIN%d=%f\r\n”,i,ads1115_buffer[i]);
}
printf(“\r\n 666\r\n”);
Delay_ms(1000);// 延时100毫秒
}
}
ADS1115的.h文件
#ifndef _ADS1115_H
#define _ADS1115_H
#define ADS1115_DEV_ADDR_GND 0x90//ADDR接GND,ADS1115地址
#define ADS1115_DEV_ADDR_VDD 0x92//ADDR接VDD,ADS1115地址
#define ADS1115_DEV_ADDR_SDA 0x94//ADDR接SDA,ADS1115地址
#define ADS1115_DEV_ADDR_SCL 0x96//ADDR接SCL,ADS1115地址
#define ADS1115_DEV_ADDR_Write_GND 0x90//ADDR接GND,ADS1115地址
#define ADS1115_DEV_ADDR_Read_GND 0x91//ADDR接GND,ADS1115地址
#define ADS1115_PGA_0256 0x0a //110//111
#define ADS1115_PGA_0512 0x08
#define ADS1115_PGA_1024 0x06
#define ADS1115_PGA_2048 0x04
#define ADS1115_PGA_4096 0x02
#define ADS1115_PGA_6144 0x00
#define ADS1115_Conversion 0x00 //转换寄存器地址
#define ADS1115_Config 0x01 //配置寄存器地址
#define ADS1115_Hi_thresh 0x02 //高位预值寄存器地址
#define ADS1115_Lo_thresh 0x03 //低位预值寄存器地址
//#define ADS1115_Config_H_function 0x82 //配置寄存器的功能设置(高8位)(+-4.096V,断电单击模式)
//#define ADS1115_Config_L_function 0x83 //配置寄存器的功能设置(低8位) (每秒采样128次,禁用比较器功能,并将警报/RDY针处于高状态。)
#define ADS1115_I2C_WR 0 /* 写控制bit */
#define ADS1115_I2C_RD 1 /* 读控制bit */
/*参数1:写从机地址;2:寄存器地址;3:配置寄存器的高位设置;4:配置寄存器的低位设置*/
uint8_t ADS1115_Write_Config (uint8_t w_device_addr,uint8_t register_addr,uint8_t H_cmd,uint8_t L_cmd);
uint8_t ADS1115_Write_Pointer(uint8_t w_device_addr,uint8_t register_addr);
uint8_t ADS1115_Read_AD(uint8_t r_device_addr);
float ADS1115_Read_ADC(uint8_t channel,uint8_t PGA);//1:几通道;2:增益(电压范围)[返回值为v电压]
float ADS1115_Read_average_ADC(uint8_t channel,uint8_t PGA);
int16_t ADS1115_Read_ADC1(void);
int16_t ADS1115_Read_ADC2(void);
#endif
ADS1115的函数(注意:i2c的文件由源文件提供,可直接使用)
#include “bsp_i2c_gpio.h”
#include “stm32f10x.h”
#include “ADS1115.h”
#include “stdio.h”
#include “Delay.h”
uint8_t BYTE_BUF[2];
uint8_t ADS1115_CheckDevice (uint8_t addr)
{
uint8_t ucAck;
i2c_Start();
i2c_SendByte(addr);//发送ADS1115设备地址;之后发送其他数据,(这里是利用了写数据顺序前两个【到写设备地址结束】)查看返回的应答
ucAck = i2c_WaitAck();
i2c_NAck();
i2c_Stop();
return ucAck;//i2c_WaitAck()为0为应答,1为非应答
}
//等待ADS1115到准备状态,在写入数据后,必须调用本函数
uint8_t ADS1115_WaitStandby(void)//(返回值为0,成功)
{
uint32_t wait_count = 0;
while(ADS1115_CheckDevice(ADS1115_DEV_ADDR_GND))
{
//若检测超过次数,退出循环
if(wait_count++>0xFFFF)
{
//等待超时
return 1;
}
}
//等待完成
return 0;
}
//步骤1、向配置寄存器0x01写入配置,先写高8位,再写低8位
//H_CMD :命令字高8位(通道,量程,转换模式)
//L_CMD : 命令字低8位(采样率设置 比较模式 有效电平 信号输出锁存)
uint8_t ADS1115_Write_Config (uint8_t w_device_addr,uint8_t register_addr,uint8_t H_cmd,uint8_t L_cmd)
{
if(ADS1115_WaitStandby())
goto w_fail;
i2c_Start();
i2c_SendByte(w_device_addr);//ADS1115_DEV_ADDR_Write_GND
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_SendByte(register_addr);///ADS1115_Config
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_SendByte(H_cmd);//配置寄存器的功能设置 ADS1115_Hi_thresh
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_SendByte(L_cmd);//ADS1115_Lo_thresh
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_Stop();
}
}
}
}
return 0;
w_fail:
i2c_Stop();
return 1;
}
//步骤2、写入指针寄存器0x00,准备读取电压
uint8_t ADS1115_Write_Pointer(uint8_t w_device_addr,uint8_t register_addr)//返回值为0(成功))(这个函数相当于读函数里面的写部分)
{
if(ADS1115_WaitStandby())
goto w_fail;
i2c_Start();
i2c_SendByte(w_device_addr);//ADS1115_DEV_ADDR_GND | ADS1115_I2C_WR
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_SendByte(register_addr);//ADS1115_Conversion
if(i2c_WaitAck())
{
goto w_fail;
}
else
{
i2c_Stop();
}
return 0;
w_fail:
i2c_Stop();
return 1;
}
}
//步骤3、读取电压数据
uint8_t ADS1115_Read_AD(uint8_t r_device_addr)
{
i2c_Start();
i2c_SendByte(r_device_addr);
if(i2c_WaitAck())
{
i2c_Stop();
return 1;
}
else
{
BYTE_BUF[0]= i2c_ReadByte();
if(i2c_WaitAck())
{
i2c_Stop();
return 2;
}
else
{
BYTE_BUF[1]=i2c_ReadByte();
i2c_NAck();
i2c_Stop();
return 0;
}
}
}
float ADS1115_Read_ADC(uint8_t channel,uint8_t PGA)//多通道的单次转换
{
uint8_t H_CMD = 0x00;
uint8_t L_CMD = 0x82;
int16_t tempData;
float voltage;
/*
bits[11:9]:000(6.144);001(4.096);010(2.048);011(1.024);100(0.512);[101|110|111](0.256);
*/
switch (channel)
{
case 0:
H_CMD = (0xC2&0xf1)|PGA; //bit[15]=OS=1:开始单次转换0xC2[单次](0X42)[连续]
//bit[14:12]=MUX[2:0]=100 : AINP = AIN0 and AINN = GND
//bit[11:9]=PGA[2:0]=001 : FSR = ±4.096 V(1)
//bit[8]=MODE=1:单次模式或掉电状态
break;
case 1:
H_CMD =(0xD2&0xf1)|PGA ; //bit[15]=OS=1:开始单次转换0xD2[单次](0X52)[连续]
//bit[14:12]=MUX[2:0]=101 : AINP = AIN1 and AINN = GND
//bit[11:9]=PGA[2:0]=001 : FSR = ±4.096 V(1)
//bit[8]=MODE=1:单次模式或掉电状态
break;
case 2:
H_CMD =(0xE2&0xf1)|PGA; //bit[15]=OS=1:开始单次转换0xE2(0X62 )
//bit[14:12]=MUX[2:0]=110 : AINP = AIN2 and AINN = GND
//bit[11:9]=PGA[2:0]=001 : FSR = ±4.096 V(1)
//bit[8]=MODE=1:单次模式或掉电状态
break;
case 3:
H_CMD =(0xF2&0xf1)|PGA; //bit[15]=OS=1:开始单次转换0xF2[单次](0X72)[连续]
//bit[14:12]=MUX[2:0]=111 : AINP = AIN3 and AINN = GND
//bit[11:9]=PGA[2:0]=001 : FSR = ±4.096 V(1)
//bit[8]=MODE=1:单次模式或掉电状态
break;
}//switch (channel)
L_CMD = 0X82; //bit[7:5]=DR[2:0]=100:128 SPS(默认)0x82
//bit[4]=COMP_MODE=0:传统比较器(默认)
//bit[3]=COMP_POL=0:低电平有效(默认)
//bit[2]=COMP_LAT=0:非锁存比较器。 置位后ALERT / RDY引脚不锁存(默认)
//bit[2:0]=COMP_QUE[1:0]=10:四次转换后置位
ADS1115_Write_Config(ADS1115_DEV_ADDR_Write_GND , ADS1115_Config , H_CMD , L_CMD);
Delay_us(200);// 延时一定时间,防止通道切换互相影响
ADS1115_Write_Pointer(ADS1115_DEV_ADDR_Write_GND , ADS1115_Conversion);
Delay_us(200);
ADS1115_Read_AD(ADS1115_DEV_ADDR_Read_GND);
tempData = (int16_t)(BYTE_BUF[0] << 8) + (int16_t)BYTE_BUF[1];
// return tempData;
switch (PGA)
{
case ADS1115_PGA_0256:
voltage = tempData * 0.0078125;//毫伏mv
break;
case ADS1115_PGA_0512:
voltage = tempData * 0.015625;//毫伏mv
break;
case ADS1115_PGA_1024:
voltage = tempData * 0.03125;//毫伏mv
break;
case ADS1115_PGA_2048:
voltage = tempData * 0.0625;//毫伏mv
break;
case ADS1115_PGA_4096:
voltage = tempData * 0.000125;//伏v
break;
case ADS1115_PGA_6144:
voltage = tempData * 0.0001875;//伏v
break;
default:
voltage = 0;
break;
}
return voltage;
}
float ADS1115_Read_average_ADC(uint8_t channel,uint8_t PGA)
{
float sum[10],average;
for(int i=0;i<10;i++)
{
sum[i]=ADS1115_Read_ADC( channel, PGA);
}
average=(sum[3]+sum[4]+sum[5]+sum[6]+sum[7]+sum[8])/6;
return average;
}
int16_t ADS1115_Read_ADC1(void)//单通道转换(AIN1)(非连续采样)
{
int16_t tempData;
ADS1115_Write_Config(ADS1115_DEV_ADDR_Write_GND , ADS1115_Config ,0xd3 ,0x80);//H:1101 0011最后一个1为非连续
ADS1115_Write_Pointer(ADS1115_DEV_ADDR_Write_GND , ADS1115_Conversion);
ADS1115_Read_AD(ADS1115_DEV_ADDR_Read_GND);
tempData = (int16_t)(BYTE_BUF[0] << 8) + (int16_t)BYTE_BUF[1];
return (tempData);
}
int16_t ADS1115_Read_ADC2(void)//单通道转换(AIN1)(连续采样)
{
int16_t tempData;
ADS1115_Write_Config(ADS1115_DEV_ADDR_Write_GND , ADS1115_Config ,0x52 ,0x80);//H:0101 0010最后一个0为连续
ADS1115_Write_Pointer(ADS1115_DEV_ADDR_Write_GND , ADS1115_Conversion);
ADS1115_Read_AD(ADS1115_DEV_ADDR_Read_GND);
tempData = (int16_t)(BYTE_BUF[0] << 8) + (int16_t)BYTE_BUF[1];
return (tempData);
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/12691.html