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