读参数是正常的,就是值跟管脚对不上,程序也是论友发布的[mw_shl_code=c,true]
#ifndef __ADS1115_H
#define __ADS1115_H
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
#define CMD_Write 0x90 //10010000
#define CMD_Read 0x91 //10010001
#define CMD_CONF_REG 0x01 //00000001
#define CMD_POINT_REG 0x00
#define CONF_L 0x83 //11100011 10000011
#define channel_0 0 //表示选择通道0
#define channel_1 1 //表示选择通道1
#define channel_2 2 //表示选择通道2
#define channel_3 3 //表示选择通道2
u16 Get_ATOD (unsigned char channel);
void Confige1115(u8 channel);
void PointRegister (void) ;
u16 ReadData (unsigned char chn);
float display_ADS1(u8 cnt);
float display_ADS2(u8 cnt);
#endif[/mw_shl_code][mw_shl_code=c,true]#include "ads1115.h"
//#include "AD5420_IO.h"
#include "TWI.h"
#include "delay.h"
u16 value=0,value1=0;
u32 Limit_Avoid_filter(u32 last_value,u32 new_value);
//void Confige1115 (unsigned char port);
//void PointRegister (void);
//u16 ReadData (unsigned char chn);
unsigned char WriteIntBuf[4], WritepointBuf[2],ReadBuffer[2];
/*******************************************************************************
* Function Name : Confige1115
* Description : 延迟时间
* Input : None
* Output : None
* Return : None
* Attention :获取ADS1115模拟转换结果
*******************************************************************************/
u16 data_get;
u16 dianya_value;
/*******************************************************************************
* Function Name : Confige1115
* Description : 延迟时间
* Input : None
* Output : None
* Return : None
* Attention :配置ADS1115
*******************************************************************************/
void Confige1115 (unsigned char port)
{
static unsigned char chnel, i;
switch (port)
{
case 0: //0通道
chnel=0xC2; //11000010
break;
case 1: //1通道
chnel=0xD2; //11010010
break;
case 2: //2通道
chnel=0xE2; //11100010
break;
case 3: //3通道
chnel=0xF2; //11110010
break;
default:
break;
}
WriteIntBuf[0] = CMD_Write;
WriteIntBuf[1] = CMD_CONF_REG;
WriteIntBuf[2] = chnel;
WriteIntBuf[3] = CONF_L;
if(!(TWI_START()))
{
for(i=0;i<4;i++)
{
TWI_SendByte(WriteIntBuf);
delay_us(1);
}
}
TWI_STOP();
}
/*******************************************************************************
* Function Name : PointRegister
* Description : 延迟时间
* Input : None
* Output : None
* Return : None
* Attention :指向ADS1115指针寄存器用于准备读取数据
*******************************************************************************/
void PointRegister (void)
{
unsigned char i;
WritepointBuf[0] = CMD_Write; //90
WritepointBuf[1] = CMD_POINT_REG; //00
if(!(TWI_START()))
{
for(i=0;i<2;i++)
{
TWI_SendByte(WritepointBuf);
delay_us(1);
}
}
TWI_STOP();
}
/*******************************************************************************
* Function Name : I2C_delay
* Description : 延迟时间
* Input : None
* Output : None
* Return : None
* Attention : CH1 PA4 CH2 PA7 CH3 PA6 CH4 PA5
*******************************************************************************/
u16 ReadData (unsigned char chn)
{
u16 data_rece;
switch(chn)
{
case 0: //0通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay_us(1);
ReadBuffer[0] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[1] = TWI_ReceiveByte();
TWI_SendACK();
TWI_STOP();
data_rece=ReadBuffer[0]*256+ReadBuffer[1];
}
break;
case 1: //1通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay_us(1);
ReadBuffer[0] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[1] = TWI_ReceiveByte();
TWI_SendACK();
TWI_STOP();
data_rece=ReadBuffer[0]*256+ReadBuffer[1];
}
break;
case 2: //2通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay_us(1);
ReadBuffer[0] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[1] = TWI_ReceiveByte();
TWI_SendACK();
TWI_STOP();
data_rece=ReadBuffer[0]*256+ReadBuffer[1];
}
break;
case 3: //3通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay_us(1);
ReadBuffer[0] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[1] = TWI_ReceiveByte();
TWI_SendACK();
TWI_STOP();
data_rece=ReadBuffer[0]*256+ReadBuffer[1];
}
break;
default:
break;
}
return data_rece;
}
u16 Get_ATOD (unsigned char channel)
{
unsigned char chn;
chn = channel;
Confige1115(channel);
delay_us(20);
PointRegister();
delay_us(20);
data_get=ReadData(chn);
delay_us(20);
if(data_get>=0x8000)
dianya_value=0x8000;
else
dianya_value=data_get;
return dianya_value;
}
float display_ADS1(u8 cnt)
{
u32 temp_val=0;
u8 t=0;
float temp;
value= Get_ATOD(channel_0);
for(t=0;t<cnt;t++)
{
temp_val+=value;
delay_ms(5);
}
value = temp_val / cnt;
temp= ((float)(value)/32768.0)*4096;
return temp;
}
float display_ADS2(u8 cnt)
{
u32 temp_val2=0;
u8 t=0;
float temp;
value1= Get_ATOD(channel_2);
for(t=0;t<cnt;t++)
{
temp_val2+=value1;
delay_ms(5);
}
value1 = temp_val2 / cnt;
temp= ((float)(value1)/32768.0)*4096;
return temp;
}
#define A_N 8
#define A_A 50
//限幅值+消抖滤波
u32 Limit_Avoid_filter(u32 last_value,u32 new_value)
{
static u32 result_value;
if((new_value-last_value)>A_A||(last_value-new_value)>A_A) //消除局部小跳动
{
result_value = last_value;
}
else
result_value = new_value;
return result_value;
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>