STM32 ADS1115.docx

上传人:b****1 文档编号:13675602 上传时间:2023-06-16 格式:DOCX 页数:10 大小:15.46KB
下载 相关 举报
STM32 ADS1115.docx_第1页
第1页 / 共10页
STM32 ADS1115.docx_第2页
第2页 / 共10页
STM32 ADS1115.docx_第3页
第3页 / 共10页
STM32 ADS1115.docx_第4页
第4页 / 共10页
STM32 ADS1115.docx_第5页
第5页 / 共10页
STM32 ADS1115.docx_第6页
第6页 / 共10页
STM32 ADS1115.docx_第7页
第7页 / 共10页
STM32 ADS1115.docx_第8页
第8页 / 共10页
STM32 ADS1115.docx_第9页
第9页 / 共10页
STM32 ADS1115.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

STM32 ADS1115.docx

《STM32 ADS1115.docx》由会员分享,可在线阅读,更多相关《STM32 ADS1115.docx(10页珍藏版)》请在冰点文库上搜索。

STM32 ADS1115.docx

STM32ADS1115

STM32ADS1115

#include"stm32f10x.h"

#include"ADS1115.h"

#include"stm32f10x_i2c.h"

#include"LCD12864.h"

#include"core_cm3.h"

#ifndef__cplusplus

typedefenum{FALSE=0,TRUE=!

FALSE}bool;#endif

/*--PrivateValue----------------------------------------------------------*/

enumTestPort{BatVal,BatCur};

unsignedcharAD_BUF[2]={0};

#defineSCLHGPIOB->BSRR=GPIO_Pin_6#defineSCLLGPIOB->BRR=GPIO_Pin_6

#defineSDAHGPIOB->BSRR=GPIO_Pin_7#defineSDALGPIOB->BRR=GPIO_Pin_7

#defineSCLreadGPIOB->IDR&GPIO_Pin_6#defineSDAreadGPIOB->IDR&GPIO_Pin_7

voidI2C_GPIO_Config(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

//ConfigureI2C1pins:

SCLandSDA

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//assert管脚

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;

GPIO_Init(GPIOB,&GPIO_InitStructure);

}

voidI2C_delay(void)

{

__nop();

}

boolI2C_Start(void)

{

SDAH;

SCLH;

I2C_delay();

if(!

SDAread)returnFALSE;//SDA线为低则总线忙,退出

SDAL;

I2C_delay();

if(SDAread)returnFALSE;//SDA线为高则总线出错,退出

SDAL;

I2C_delay();

returnTRUE;}

voidI2C_Stop(void)

{

SCLL;

I2C_delay();

SDAL;

I2C_delay();

SCLH;

I2C_delay();

SDAH;

I2C_delay();}

voidI2C_Ack(void){

SCLL;

I2C_delay();

SDAL;

I2C_delay();

SCLH;

I2C_delay();

SCLL;

I2C_delay();}

voidI2C_NoAck(void)

{

SCLL;

I2C_delay();

SDAH;

I2C_delay();

SCLH;

I2C_delay();

SCLL;

I2C_delay();

}

boolI2C_WaitAck(void)//返回为1有ACK,返回0无ACK

{

SCLL;

I2C_delay();

SDAH;

I2C_delay();

SCLH;

I2C_delay();

if(SDAread)

{

SCLL;

returnFALSE;

}

SCLL;

returnTRUE;

}

voidI2C_SendByte(u8SendByte)//数据从高位到低位//

{

u8i=8;

while(i--)

{

SCLL;

I2C_delay();

if(SendByte&0x80)

SDAH;

else

SDAL;

SendByte<<=1;

I2C_delay();

SCLH;

I2C_delay();

}

SCLL;

}

u8I2C_ReceiveByte(void)//数据从高位到低位//{

u8i=8;

u8ReceiveByte=0;

SDAH;

while(i--)

{

ReceiveByte<<=1;

SCLL;

I2C_delay();

SCLH;

I2C_delay();

if(SDAread)

{

ReceiveByte|=0x01;

}

}

SCLL;

returnReceiveByte;}

/****************************************************************************

*名称:

ADS1115_Init

*功能:

初始化ADS1115

*入口参数:

id:

设备地址;write_address寄存器地址;byte1:

高字节配置;byte2:

低字节最

高转换速度

*出口参数:

*说明:

完成ADS1115的配置寄存器的初始化工作

****************************************************************************/

voidADS1115_Init(unsignedcharid,unsignedcharwrite_address,unsignedcharbyte1,unsigned

charbyte2)

{

while(!

I2C_Start());

I2C_SendByte(id);//写器件地址

while(!

I2C_WaitAck());

I2C_SendByte(write_address);//寄存器地址

while(!

I2C_WaitAck());

I2C_SendByte(byte1);//发送数据

while(!

I2C_WaitAck());

I2C_SendByte(byte2);//发送数据

while(!

I2C_WaitAck());

I2C_Stop();

I2C_delay();I2C_delay();

}

/****************************************************************************

*名称:

I2C_AD1115_Point

*功能:

指向ADS1115内部特定的寄存器

*入口参数:

id:

设备地址;write_address寄存器地址

*出口参数:

*说明:

指向ADS1115内部特定的寄存器

****************************************************************************/

voidI2C_AD1115_Point(unsignedcharid,unsignedcharwrite_address)

{

while(!

I2C_Start());

//产生起始条件

I2C_SendByte(id);//写器件地址

//向设备发送设备地址

while(!

I2C_WaitAck());

//等待ACK

I2C_SendByte(write_address);//发送数据

//寄存器地址

while(!

I2C_WaitAck());

I2C_Stop();

//产生结束信号

}

/****************************************************************************

*名称:

I2C_Read2

*功能:

读ADS1115转换结果

*入口参数:

id:

设备地址;read_address寄存器地址

*出口参数:

*说明:

转换的值放在BUF【2】

****************************************************************************/

voidI2C_Read2(unsignedcharid,unsignedcharread_address)

{

while(!

I2C_Start());

//产生起始条件

I2C_SendByte(id);//写器件地址

//发送地址

while(!

I2C_WaitAck());

//等待ACK

AD_BUF[0]=I2C_ReceiveByte();

I2C_Ack();

AD_BUF[1]=I2C_ReceiveByte();

I2C_NoAck();

I2C_Stop();

}

/****************************************************************************

*名称:

StartChannel

*功能:

选择通道测量

*入口参数:

通道序号0、1、2、3

*出口参数:

采样值

*说明:

完成ADS1115的采样工作

****************************************************************************/

floatStartChannlel(charChannel){

floatAD_Val=0;

inttemp=0;

if(Channel==BatVal)

{

//First(1001000)Address+0;Second:

PointtotheConfigresgister

ADS1115_Init(0x90,0x01,0x02,0xe0);//量程4.096;Continuousconversionmode;860SPS;Out=AIN0-AIN1;

}

if(Channel==BatCur)

{

ADS1115_Init(0x90,0x01,0x36,0xe0);//量程1.024;Continuousconversionmode;860SPS;Out=AIN2-AIN3;00110110

}

//if(Channel==2)

//{

//ADS1115_Init(0x90,0x01,0x62,0xe0);//量程4.096;Continuousconversionmode;860SPS;

//}

//if(Channel==3)

//{

//ADS1115_Init(0x90,0x01,0x72,0xe0);//量程4.096;Continuousconversionmode;860SPS;

//}

while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5));

I2C_AD1115_Point(0x90,0x00);

I2C_Read2(0x91,0x00);

temp=(AD_BUF[0]<<8)|AD_BUF[1];

AD_Val=temp/32768.0;

returnAD_Val;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2