程序已经通过
0、定义通道
#define TongDao0 0xc2e3
#define TongDao1 0xd2e3
#define TongDao2 0xe2e3
#define TongDao3 0xf2e3
1、STM32F103的IIC端口初始化
void ads1115_io_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_Init(GPIOB,&GPIO_InitStruct);
}
2、IIC起始位
void I2C_Start(void)
{
//SCL_LOW;
SDAOUT();
//SDA_LOW;
SDA_HIGH;
SCL_HIGH;
ads1115_Delay();
SDA_LOW;
ads1115_Delay();
SCL_LOW;
}
3、IIC停止位
void I2C_Stop(void)
{
/*SCL_LOW;
SDAOUT();
SDA_HIGH;
SCL_HIGH;
ads1115_Delay();
SDA_LOW;
ads1115_Delay();
//SCL_LOW;*/
SCL_LOW;
SDAOUT();
SDA_LOW;
SCL_HIGH;
ads1115_Delay();
SDA_HIGH;
ads1115_Delay();
//ads1115_Delay();
//ads1115_Delay();
}
4、IIC校验位
void I2C_ACK()
{
SCL_LOW;
SDAOUT();
SDA_LOW;
//ads1115_Delay();
//systick_us(2);
ads1115_Delay();
SCL_HIGH;
//systick_us(2);
ads1115_Delay();
SCL_LOW;
}
5、IIC等待校验位
u8 I2C_Wait_ACK()
{
u8 count=0;
SDAIN();
SCL_HIGH;//ÊÍ·Å×ÜÏß
//systick_us(1);
ads1115_Delay();
SDA_HIGH; //À¸ßSDA
// systick_us(1);
ads1115_Delay();
while(SDA) //¶ÁÈ¡SDA״̬£¬0ΪӦ´ð
{
count++;
if(count>50)
{
return 1;
}
}
SCL_LOW;
// ACK++; //¼Ç¼оƬ¸øµÄÓ¦¸Ã´ÎÊý£¬µ÷ÊÔÓÃ
return 0;
}
6、IIC写数据
void I2C_WriteByte(u8 cmd)
{
u8 i;
SDAOUT();
SCL_LOW; //scl=0,ÔÊÐíÊý¾Ý±ä»¯
// SysTick_us(5);
for(i=0;i<8;i++)
{
if(cmd&0x80)SDA_HIGH;
else SDA_LOW;
cmd<<=1;
//systick_us(2);
ads1115_Delay();
SCL_HIGH; //SCL=1,Êý¾Ý±£³Ö
//systick_us(2);
ads1115_Delay();
SCL_LOW;
//systick_us(1);
//ads1115_Delay();
}
}
7、IIC读数据
u8 I2C_ReadByte()
{
u8 i,ans=0;
SDAIN();
SDA_HIGH;
for(i=0;i<8;i++)
{
SCL_LOW; //ÔÊÐíÊý¾Ý±ä»¯
//systick_us(2);
ads1115_Delay();
SCL_HIGH; //±£³ÖÊý¾Ý²»±ä
//systick_us(1);
ads1115_Delay();
ans<<=1; //
if(SDA)ans++;
//systick_us(1);
//ads1115_Delay();
}
SCL_LOW;
I2C_ACK();
ads1115_Delay();
return ans;
}
8、ADS1115配置参数
void ads1115_config(u16 config)
{
I2C_Start();
I2C_WriteByte(0x90); //дÐźÅ
I2C_Wait_ACK();
I2C_WriteByte(0x01); //Ö¸ÏòÅäÖüĴæÆ÷
I2C_Wait_ACK();
I2C_WriteByte(config>>8);
I2C_Wait_ACK();
I2C_WriteByte(config);
I2C_Wait_ACK();
I2C_Stop();
}
9、ADS1115
s16 ads1115_readreg(u8 reg)
{
s16 value_h=0;
//s16 x=0;
s16 ConversionResult=0;
u8 value_l=0;
//int count = 0;
I2C_Start();
I2C_WriteByte(0x90); //дÐźÅ
ads1115_Delay();
I2C_Wait_ACK();
I2C_WriteByte(reg); //Ö¸ÏòÅäÖüĴæÆ÷
ads1115_Delay();
ads1115_Delay();
I2C_Wait_ACK();
ads1115_Delay();
I2C_Stop();//////////
ads1115_Delay();
I2C_Start();
I2C_WriteByte(0x91); //¶ÁÐźÅ
ads1115_Delay();
I2C_Wait_ACK();
ads1115_Delay();
value_h=I2C_ReadByte();
value_l=I2C_ReadByte();
//I2C_Wait_ACK();
I2C_Stop();//////////
ConversionResult=value_h*256+value_l;
return ConversionResult;
}
10、ADS1115获取数据
s16 ads1115_getvalue()
{
s16 value=0;
value=ads1115_readreg(CONVER_REG); //CONVER_REG==0;
//delay_uss(100);/////////////////////////////
return value;
}
11、ADS1115切换采集通道
s16 Get_ADS1115_Reture(u8 channel)
{
ADS1115_Reture=0;
switch(channel)
{
case 0:
//ads1115_config(TongDao0);
//Delay_Ms(5);
//ADS1115_Reture=ads1115_getvalue();
//Delay_Ms(5);
break;
case 1:
ads1115_config(TongDao1);//TongDao1
Delay_Ms(7);
//delay_uss(10000);
ADS1115_Reture=ads1115_getvalue();
//Delay_Ms(3);
break;
case 2:
ads1115_config(TongDao2);//TongDao2
Delay_Ms(7);
//delay_uss(10000);
ADS1115_Reture=ads1115_getvalue();
//Delay_Ms(3);
break;
case 3:
ads1115_config(TongDao3);//TongDao3
Delay_Ms(7);
//delay_uss(10000);
ADS1115_Reture=ads1115_getvalue();
//Delay_Ms(3);//ÑÓʱһ¶¨Òª¼Ó
ads1115counter ++;
break;
default:
break;
}
//ADS1115_Reture=0x8000;
//channel=0;
return ADS1115_Reture;
}
12、数据量转换为模拟量
V1=volt*(8.192/65536.0);
上一篇:stm32F103状态机矩阵键盘
下一篇:STM32F103单片机RTC结合time.h使用
推荐阅读最新更新时间:2024-03-16 15:38