内含ADS111配置代码以及IIC通讯驱动
单片机源程序如下:
- #include "stm32f10x_lib.h"
- #include "ADS1115.h"
- #include "stm32f10x_i2c.h"
- #ifndef __cplusplus
- #endif
- /*--Private Value----------------------------------------------------------*/
- enum TestPort {BatVal,BatCur};
- unsigned char AD_BUF[2]={0};
- #define SCLH GPIOB->BSRR = GPIO_Pin_6
- #define SCLL GPIOB->BRR = GPIO_Pin_6
- #define SDAH GPIOB->BSRR = GPIO_Pin_7
- #define SDAL GPIOB->BRR = GPIO_Pin_7
- #define SCLread GPIOB->IDR & GPIO_Pin_6
- #define SDAread GPIOB->IDR & GPIO_Pin_7
- void I2C_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- // Configure I2C1 pins: SCL and SDA
- 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_4;//assert管脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void I2C_delay(void)
- { unsigned char i=30; //
- while(i)
- {
- i--;
- }
- }
- bool I2C_Start(void)
- {
- SDAH;
- I2C_delay();
- SCLH;
- I2C_delay();
- if(!SDAread)return FALSE; //SDA线为低则总线忙,退出
- SDAL;
- I2C_delay();
- if(SDAread)
- return FALSE; //SDA线为高则总线出错,退出
- SDAL;
- I2C_delay();
- return TRUE;
- }
- void I2C_Stop(void)
- {
- SCLL;
- I2C_delay();
- SDAL;
- I2C_delay();
- SCLH;
- I2C_delay();
- SDAH;
- I2C_delay();
- }
- void I2C_Ack(void)
- {
- SCLL;
- I2C_delay();
- SDAL;
- I2C_delay();
- SCLH;
- I2C_delay();
- SCLL;
- I2C_delay();
- }
- void I2C_NoAck(void)
- {
- SCLL;
- I2C_delay();
- SDAH;
- I2C_delay();
- SCLH;
- I2C_delay();
- SCLL;
- I2C_delay();
- }
- bool I2C_WaitAck(void) //返回1有ACK,返回0无ACK
- {
- SCLL;
- I2C_delay();
- SDAH;
- I2C_delay();
- SCLH;
- I2C_delay();
- if(SDAread)
- { SCLL;
- return FALSE;
- }
- SCLL;
- return TRUE;
- }
-
- void I2C_SendByte(u8 SendByte) //数据从高位到低位//
- {
- u8 i=8;
- while(i--)
- {
- SCLL;
- I2C_delay();
- if(SendByte&0x80)
- SDAH;
- else
- SDAL;
- SendByte<<=1;
- I2C_delay();
- SCLH;
- I2C_delay();
- }
- SCLL;
- }
- u8 I2C_ReceiveByte(void) //数据从高位到低位//
- {
- u8 i=8;
- u8 ReceiveByte=0;
- SDAH;
- while(i--)
- {
- ReceiveByte<<=1;
- SCLL;
- I2C_delay();
- SCLH;
- I2C_delay();
- if(SDAread)
- {
- ReceiveByte|=0x01;
- }
- }
- SCLL;
- return ReceiveByte;
- }
- /****************************************************************************
- * 名 称: ADS1115_Init
- * 功 能: 初始化ADS1115
- * 入口参数:id:设备地址;write_address寄存器 ;byte1:高字节配置;byte2:低字节 最高转换速度
- * 出口参数: 无
- * 说 明: 完成ADS1115的配置寄存器的初始化
- *****************************************************************************/
- void ADS1115_Init(unsigned char id,unsigned char write_address,unsigned char byte1,unsigned char byte2)
- {
- 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内部特定的寄存器
- ****************************************************************************/
- void I2C_AD1115_Point(unsigned char id,unsigned char write_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】
- ****************************************************************************/
- void I2C_Read2(unsigned char id, unsigned char read_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的采样工作
- ****************************************************************************/
- s16 Out,AIN2,AIN1,AIN3,AIN0;
- s16 AD_Buff[4];
- int temp=0;
- float StartChannlel(char Channel)
- {
- float AD_Val=0;
-
- if(Channel==0)
- { //First (1001000)Address+0; Second :Point to the Config resgister BatVal
- ADS1115_Init(0x90,0x01,0xc2,0xe0); //量程4.096;Continuous conversionmode;AIN1
- //860SPS;
- Out=AIN0-AIN1;
- }
- if(Channel==1)
- {
- ADS1115_Init(0x90,0x01,0xd2,0xe0); //量程1.024;Continuous conversionmode;AIN3 BatCur
- //860SPS;
- Out=AIN2-AIN3;//00110110
- }
- if(Channel==2)
- {
- ADS1115_Init(0x90,0x01,0xe2,0xe0); //量程4.096;Continuous conversionmode;GND
- //860SPS;
- }
- if(Channel==3)
- {
- ADS1115_Init(0x90,0x01,0xf2,0xe0); //量程4.096;Continuous conversionmode;GND
- //860SPS;
- }
- while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4));
- I2C_AD1115_Point(0x90,0x00);
- I2C_delay();
- I2C_delay();
- I2C_Read2(0x91,0x00);
- while(!I2C_Start());
- temp=(AD_BUF[0] << 8) | AD_BUF[1];
- // AD_Val=4096*temp/32768.0;
- AD_Val=temp;
- if(Channel==0)
- {
- AD_Buff[0]=AD_Val;
- }
- if(Channel==1)
- {
- AD_Buff[1]=AD_Val;
- }
- if(Channel==2)
- {
- AD_Buff[2]=AD_Val;
- }
- if(Channel==3)
- {
- AD_Buff[3]=AD_Val;
- }
- return AD_Val;
-
-
- }
-
复制代码
所有资料51hei提供下载:
ADS115.rar
(610.13 KB, 下载次数: 39)
|