內(nèi)含ADS111配置代碼以及IIC通訊驅(qū)動
單片機源程序如下:
- #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) //數(shù)據(jù)從高位到低位//
- {
- 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) //數(shù)據(jù)從高位到低位//
- {
- 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
- * 入口參數(shù):id:設備地址;write_address寄存器 ;byte1:高字節(jié)配置;byte2:低字節(jié) 最高轉(zhuǎn)換速度
- * 出口參數(shù): 無
- * 說 明: 完成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);//發(fā)送數(shù)據(jù)
- while(!I2C_WaitAck());
- I2C_SendByte(byte2);//發(fā)送數(shù)據(jù)
- while(!I2C_WaitAck());
- I2C_Stop();
- I2C_delay();
- I2C_delay();
- }
- /****************************************************************************
- * 名 稱: I2C_AD1115_Point
- * 功 能: 指向ADS1115內(nèi)部特定的寄存器
- * 入口參數(shù): id:設備地址;write_address寄存器
- * 出口參數(shù): 無
- * 說 明: 指向ADS1115內(nèi)部特定的寄存器
- ****************************************************************************/
- void I2C_AD1115_Point(unsigned char id,unsigned char write_address)
- {
- while(!I2C_Start());
- //產(chǎn)生起始條件
- I2C_SendByte(id);//寫器件地址
- //向設備發(fā)送設備地址
- while(!I2C_WaitAck());
- //等待ACK
- I2C_SendByte(write_address);//發(fā)送數(shù)據(jù)
- //寄存器地址
- while(!I2C_WaitAck());
- I2C_Stop();
- //產(chǎn)生結(jié)束信號
- }
- /****************************************************************************
- * 名 稱: I2C_Read2
- * 功 能: 讀ADS1115轉(zhuǎn)換結(jié)果
- * 入口參數(shù): id:設備地址;read_address寄存器地址
- * 出口參數(shù): 無
- * 說 明: 轉(zhuǎn)換的值放在BUF【2】
- ****************************************************************************/
- void I2C_Read2(unsigned char id, unsigned char read_address)
- {
- while(!I2C_Start());
- //產(chǎn)生起始條件
- I2C_SendByte(id);//寫器件地址
- //發(fā)送地址
- while(!I2C_WaitAck());
- //等待ACK
- AD_BUF[0] = I2C_ReceiveByte();
- I2C_Ack();
- AD_BUF[1] = I2C_ReceiveByte();
- I2C_NoAck();
- I2C_Stop();
- }
- /****************************************************************************
- * 名 稱: StartChannel
- * 功 能: 選擇通道測量
- * 入口參數(shù): 通道0、1、2、3
- * 出口參數(shù): 采樣值
- * 說 明: 完成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, 下載次數(shù): 39)
2019-9-26 16:19 上傳
點擊文件名下載附件
|