ADS1115程序代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// I2C_delay(20); ReadBuffer[(cnt * 8) + 4] = TWI_ReceiveByte(); TWI_SendACK();
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 5] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[58]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) << count--);
delay(20);
}
}
TWI_STOP();
}
/******************************************************************************
*
* Function Name : I2C_delay * Description : 延迟时间
: None
* Output
: None
* Return
: None
* Attention
:获取 ADS1115 模拟转换结果
*******************************************************************************
/
void Get_ATOD (unsigned char channel,unsigned char cnt)
/
static void PointRegister (void)
{
unsigned char i;
WritepointBuf[0] = CMD_Write;
Hale Waihona Puke Baidu
WritepointBuf[1] = CMD_POINT_REG;
if(!(TWI_START()))
{
for(i=0;i<2;i++)
{
TWI_SendByte(WritepointBuf[i]);
/
static void Confige1115 (unsigned char port)
{
static unsigned char chnel, i;
switch (port)
{
case 0:
//0 通道
chnel=0xC2;
break;
case 1: chnel=0xD2;
break;
//1 通道
for(i=0;i<4;i++) {
TWI_SendByte(WriteIntBuf[i]); delay(20); } } TWI_STOP();
} /******************************************************************************
*
* Function Name : PointRegister
* Description : 延迟时间
* Input
: None
* Output
: None
* Return
: None
* Attention
:指向 ADS1115 指针寄存器用于准备读取数据
*******************************************************************************
TWI_STOP(); } ReadBuffer[56]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << count); } if(chn == 1) { // if(!(TWI_START())) {
TWI_SendByte(CMD_Read);
ReadBuffer[(cnt * 8) + 2] = TWI_ReceiveByte(); TWI_SendACK();
default: break; } /* if(chn == 0) {
if(count == 0xff) count = 6;
// if(!(TWI_START())) { TWI_SendByte(CMD_Read); ReadBuffer[cnt * 8] = TWI_ReceiveByte(); TWI_SendACK(); ReadBuffer[(cnt * 8) + 1] = TWI_ReceiveByte(); TWI_SendACK();
#define CMD_Write 0x90
#define CMD_Read
0x91
#define CMD_CONF_REG 0x01
#define CMD_POINT_REG 0x00
#define CONF_L
0xe3
void Get_ATOD (unsigned char channel,unsigned char cnt);
#include "ads1115.h" #include "ap_i2c.h" static void Confige1115 (unsigned char port); static void PointRegister (void); static void ReadData (unsigned char chn, unsigned char cnt); unsigned char WriteIntBuf[4], WritepointBuf[2], count = 6,ReadBuffer[64];; void delay (unsigned int delay) {
ReadBuffer[cnt * 8] = TWI_ReceiveByte(); TWI_SendACK();
ReadBuffer[(cnt * 8) + 1] = TWI_ReceiveByte(); TWI_SendACK();
TWI_STOP();
} break;
ReadBuffer[56]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << count);
/
static void ReadData (unsigned char chn,unsigned char cnt)
{
switch(chn)
{
case 0:
//0 通道
if(!(TWI_START()))
{ if(count == 0xff)
count = 6;
TWI_SendByte(CMD_Read); delay(20);
}*/
}
//=================================================================///
#ifndef __ADS1115_H
#define __ADS1115_H
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
*
* Function Name : Confige1115 * Description : 延迟时间
* Input
: None
* Output
: None
* Return * Attention
: None :配置 ADS1115
*******************************************************************************
TWI_SendACK();
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 5] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
ReadBuffer[59]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << count); }
* Input
: None
* Output
: None
* Return
: None
* Attention
: CH1 PA4 CH2 PA7 CH3 PA6 CH4 PA5
*******************************************************************************
ReadBuffer[(cnt * 8) + 7] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
} break;
ReadBuffer[58]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << count--);
break;
case 3:
//3 通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay(20);
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 6] = TWI_ReceiveByte();
TWI_SendACK();
// I2C_delay(20);
case 2: chnel=0xE2;
break;
//2 通道
case 3: chnel=0xF2;
break;
//3 通道
default: break; } WriteIntBuf[0] = CMD_Write; WriteIntBuf[1] = CMD_CONF_REG; WriteIntBuf[2] = chnel; WriteIntBuf[3] = CONF_L; if(!(TWI_START())) {
ReadBuffer[(cnt * 8) + 7] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[59]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << count); } if(chn == 3) { // if(!(TWI_START())) { TWI_SendByte(CMD_Read);
} break;
ReadBuffer[57]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) << count);
case 2:
//2 通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay(20);
ReadBuffer[(cnt * 8) + 4] = TWI_ReceiveByte();
case 1:
//1 通道
if(!(TWI_START()))
{ TWI_SendByte(CMD_Read);
delay(20);
ReadBuffer[(cnt * 8) + 2] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[(cnt * 8) + 3] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
{
static unsigned char chn;
chn = channel;
Confige1115(channel);
delay(1000);
PointRegister();
delay(1000);
ReadData(chn,cnt);
delay(1000);
}
/******************************************************************************
while(delay--); }
/******************************************************************************
*
* Function Name : Confige1115
* Description : 延迟时间
* Input
ReadBuffer[(cnt * 8) + 3] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[57]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << count); } if(chn == 2) { // if(!(TWI_START())) { TWI_SendByte(CMD_Read); ReadBuffer[(cnt * 8) + 6] = TWI_ReceiveByte(); TWI_SendACK(); // I2C_delay(20);
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 5] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[58]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) << count--);
delay(20);
}
}
TWI_STOP();
}
/******************************************************************************
*
* Function Name : I2C_delay * Description : 延迟时间
: None
* Output
: None
* Return
: None
* Attention
:获取 ADS1115 模拟转换结果
*******************************************************************************
/
void Get_ATOD (unsigned char channel,unsigned char cnt)
/
static void PointRegister (void)
{
unsigned char i;
WritepointBuf[0] = CMD_Write;
Hale Waihona Puke Baidu
WritepointBuf[1] = CMD_POINT_REG;
if(!(TWI_START()))
{
for(i=0;i<2;i++)
{
TWI_SendByte(WritepointBuf[i]);
/
static void Confige1115 (unsigned char port)
{
static unsigned char chnel, i;
switch (port)
{
case 0:
//0 通道
chnel=0xC2;
break;
case 1: chnel=0xD2;
break;
//1 通道
for(i=0;i<4;i++) {
TWI_SendByte(WriteIntBuf[i]); delay(20); } } TWI_STOP();
} /******************************************************************************
*
* Function Name : PointRegister
* Description : 延迟时间
* Input
: None
* Output
: None
* Return
: None
* Attention
:指向 ADS1115 指针寄存器用于准备读取数据
*******************************************************************************
TWI_STOP(); } ReadBuffer[56]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << count); } if(chn == 1) { // if(!(TWI_START())) {
TWI_SendByte(CMD_Read);
ReadBuffer[(cnt * 8) + 2] = TWI_ReceiveByte(); TWI_SendACK();
default: break; } /* if(chn == 0) {
if(count == 0xff) count = 6;
// if(!(TWI_START())) { TWI_SendByte(CMD_Read); ReadBuffer[cnt * 8] = TWI_ReceiveByte(); TWI_SendACK(); ReadBuffer[(cnt * 8) + 1] = TWI_ReceiveByte(); TWI_SendACK();
#define CMD_Write 0x90
#define CMD_Read
0x91
#define CMD_CONF_REG 0x01
#define CMD_POINT_REG 0x00
#define CONF_L
0xe3
void Get_ATOD (unsigned char channel,unsigned char cnt);
#include "ads1115.h" #include "ap_i2c.h" static void Confige1115 (unsigned char port); static void PointRegister (void); static void ReadData (unsigned char chn, unsigned char cnt); unsigned char WriteIntBuf[4], WritepointBuf[2], count = 6,ReadBuffer[64];; void delay (unsigned int delay) {
ReadBuffer[cnt * 8] = TWI_ReceiveByte(); TWI_SendACK();
ReadBuffer[(cnt * 8) + 1] = TWI_ReceiveByte(); TWI_SendACK();
TWI_STOP();
} break;
ReadBuffer[56]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << count);
/
static void ReadData (unsigned char chn,unsigned char cnt)
{
switch(chn)
{
case 0:
//0 通道
if(!(TWI_START()))
{ if(count == 0xff)
count = 6;
TWI_SendByte(CMD_Read); delay(20);
}*/
}
//=================================================================///
#ifndef __ADS1115_H
#define __ADS1115_H
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
*
* Function Name : Confige1115 * Description : 延迟时间
* Input
: None
* Output
: None
* Return * Attention
: None :配置 ADS1115
*******************************************************************************
TWI_SendACK();
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 5] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
ReadBuffer[59]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << count); }
* Input
: None
* Output
: None
* Return
: None
* Attention
: CH1 PA4 CH2 PA7 CH3 PA6 CH4 PA5
*******************************************************************************
ReadBuffer[(cnt * 8) + 7] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
} break;
ReadBuffer[58]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << count--);
break;
case 3:
//3 通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay(20);
// I2C_delay(20);
ReadBuffer[(cnt * 8) + 6] = TWI_ReceiveByte();
TWI_SendACK();
// I2C_delay(20);
case 2: chnel=0xE2;
break;
//2 通道
case 3: chnel=0xF2;
break;
//3 通道
default: break; } WriteIntBuf[0] = CMD_Write; WriteIntBuf[1] = CMD_CONF_REG; WriteIntBuf[2] = chnel; WriteIntBuf[3] = CONF_L; if(!(TWI_START())) {
ReadBuffer[(cnt * 8) + 7] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[59]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << count); } if(chn == 3) { // if(!(TWI_START())) { TWI_SendByte(CMD_Read);
} break;
ReadBuffer[57]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) << count);
case 2:
//2 通道
if(!(TWI_START()))
{
TWI_SendByte(CMD_Read);
delay(20);
ReadBuffer[(cnt * 8) + 4] = TWI_ReceiveByte();
case 1:
//1 通道
if(!(TWI_START()))
{ TWI_SendByte(CMD_Read);
delay(20);
ReadBuffer[(cnt * 8) + 2] = TWI_ReceiveByte();
TWI_SendACK();
ReadBuffer[(cnt * 8) + 3] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP();
{
static unsigned char chn;
chn = channel;
Confige1115(channel);
delay(1000);
PointRegister();
delay(1000);
ReadData(chn,cnt);
delay(1000);
}
/******************************************************************************
while(delay--); }
/******************************************************************************
*
* Function Name : Confige1115
* Description : 延迟时间
* Input
ReadBuffer[(cnt * 8) + 3] = TWI_ReceiveByte(); TWI_SendACK(); TWI_STOP(); } ReadBuffer[57]|=(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << count); } if(chn == 2) { // if(!(TWI_START())) { TWI_SendByte(CMD_Read); ReadBuffer[(cnt * 8) + 6] = TWI_ReceiveByte(); TWI_SendACK(); // I2C_delay(20);