大家好,欢迎来到IT知识分享网。
#include "./bsp/bsp.h"
uint8_t Channel_Value;
uint8_t ShowChannel[] = "No.- Channel:";
uint16_t D_ADS=0; //ת»»µÄÊý×ÖÁ¿
uint16_t Config=0;
uint8_t Writebuff[4]={0},Readbuff[3]={0};
uint32_t Result[2]={0};
uint16_t Format[5]={0}; //ת»»µÄÊý×ÖÁ¿×ª»»³ÉÊ®½øÖÆ
/***************¹¦ÄÜ:ADS1115¶Ë¿Ú³õʼ»¯****************/
void ADS1115_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //¸´ÓÃIO¿ÚʱÖÓ
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹÄÜPB¶Ë¿ÚʱÖÓ
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_2; //SCL-->PA.3, SDA-->PA.2 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOB
PBout(4)=1; PBout(3)=1; //PA.2, PA.3 Êä³ö¸ß
}
void ADS1115_GPIO_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //SDA-->PB.9 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOB.9
}
void ADS1115_GPIO_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //SDA-->PB.9 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOB.9
}
/*******************************************
º¯ÊýÃû³Æ£ºStart
¹¦ ÄÜ£ºÍê³ÉIICµÄÆðʼÌõ¼þ²Ù×÷
²Î Êý£ºÎÞ
·µ»ØÖµ £ºÎÞ
********************************************/
void Start(void)
{
SCL=1;
SDA=1;
Delay_us(15);
SDA=0;
Delay_us(15);
SCL=0;
Delay_us(15);
}
/*******************************************
º¯ÊýÃû³Æ£ºStop
¹¦ ÄÜ£ºÍê³ÉIICµÄÖÕÖ¹Ìõ¼þ²Ù×÷
²Î Êý£ºÎÞ
·µ»ØÖµ £ºÎÞ
********************************************/
void Stop(void)
{
SDA=0;
Delay_us(15);
SCL=1;
Delay_us(15);
SDA=1;
Delay_us(15);
}
/*******************************************
º¯ÊýÃû³Æ£ºACK
¹¦ ÄÜ£ºÍê³ÉIICµÄÖ÷»úÓ¦´ð²Ù×÷
²Î Êý£ºÎÞ
·µ»ØÖµ £ºÎÞ
********************************************/
void ACK(void)
{
SDA=0;
Delay_us(1);
SCL=1;
Delay_us(15);
SCL=0;
Delay_us(15);
SDA=1;
Delay_us(15);
}
/*******************************************
º¯ÊýÃû³Æ£ºNACK
¹¦ ÄÜ£ºÍê³ÉIICµÄÖ÷»úÎÞÓ¦´ð²Ù×÷
²Î Êý£ºÎÞ
·µ»ØÖµ £ºÎÞ
********************************************/
void NACK(void)
{
SDA=1;
Delay_us(1);
SCL=1;
Delay_us(15);
SCL=0;
Delay_us(1);
SDA=0;
Delay_us(15);
}
/**********¼ì²éÓ¦´ðÐźź¯Êý******************
Èç¹û·µ»ØֵΪ1ÔòÖ¤Ã÷ÓÐÓ¦´ðÐźţ¬·´Ö®Ã»ÓÐ
*******************************************
º¯ÊýÃû³Æ£ºCheck
¹¦ ÄÜ£º¼ì²é´Ó»úµÄÓ¦´ð²Ù×÷
²Î Êý£ºÎÞ
·µ»ØÖµ £º´Ó»úÊÇ·ñÓÐÓ¦´ð£º1--ÓУ¬0--ÎÞ
********************************************/
uint8_t Check(void)
{
uint8_t slaveack;
SDA=1;
Delay_us(1);
SCL=1;
// ADS1115_GPIO_IN(); // SDAÉèÖÃΪÊäÈë
Delay_us(15);
slaveack = SDA_IN; //¶ÁÈëSDAÊýÖµ
SCL=0;
Delay_us(15);
// ADS1115_GPIO_OUT(); // SDAÉèÖÃΪÊä³ö
if(slaveack) return FALSE;
else return TRUE;
}
/***************дÈëÒ»¸ö×Ö½Ú****************/
void Write_1_Byte(uint8_t DataByte)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(DataByte&0x80) //if((DataByte<<i)&0x80)
SDA=1;
else
SDA=0;
Delay_us(15);
SCL=1;
Delay_us(15);
SCL=0;
Delay_us(15);
DataByte <<= 1;
}
SDA=1;
Delay_us(5);
}
/***************дÈëN¸ö×Ö½Ú****************/
uint8_t Write_N_Byte(uint8_t *writebuffer,uint8_t n)
{
uint8_t i;
for(i=0;i<n;i++)
{
Write_1_Byte(*writebuffer);
if(Check())
{
writebuffer ++;
}
else
{
Stop();
return FALSE;
}
}
Stop();
return TRUE;
}
/***************¶ÁÈ¡Ò»¸ö×Ö½Ú****************/
uint8_t Read_1_Byte(void)
{
uint8_t data_Value = 0, FLAG, i;
for(i=0;i<8;i++)
{
SDA=1;
Delay_us(15);
SCL=1;
ADS1115_GPIO_IN(); // SDAÉèÖÃΪÊäÈë
Delay_us(15);
FLAG=SDA_IN;
data_Value <<= 1;
if( FLAG )
data_Value |= 0x01;
SCL=0;
Delay_us(15);
ADS1115_GPIO_OUT(); // SDAÉèÖÃΪÊä³ö
}
return data_Value;
}
/***************¶ÁÈ¡N¸ö×Ö½Ú****************/
void Read_N_Byte(uint32_t *readbuff, uint8_t n)
{
uint8_t i;
for(i=0;i<n;i++)
{
readbuff[i]=Read_1_Byte();
if(i==n-1)
NACK(); //²»Á¬Ðø¶Á×Ö½Ú
else
ACK(); //Á¬Ðø¶Á×Ö½Ú
}
Stop();
}
/*****************³õʼ»¯ADS1115******************/
void InitADS1115(uint8_t S_MUX_0, uint8_t S_MUX_1, uint8_t S_MUX_2)
{
if (S_MUX_0 == 0 && S_MUX_1 == 0 && S_MUX_2 == 0) Config = OS_1+MUX_A0+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 0 && S_MUX_1 == 0 && S_MUX_2 == 1) Config = OS_1+MUX_A1+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 0 && S_MUX_1 == 1 && S_MUX_2 == 0) Config = OS_1+MUX_A2+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 0 && S_MUX_1 == 1 && S_MUX_2 == 1) Config = OS_1+MUX_A3+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 1 && S_MUX_1 == 0 && S_MUX_2 == 0) Config = OS_1+MUX_A4+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 1 && S_MUX_1 == 0 && S_MUX_2 == 1) Config = OS_1+MUX_A5+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 1 && S_MUX_1 == 1 && S_MUX_2 == 0) Config = OS_1+MUX_A6+PGA+DRX+COMP_QUE+MODE;
else if (S_MUX_0 == 1 && S_MUX_1 == 1 && S_MUX_2 == 1) Config = OS_1+MUX_A7+PGA+DRX+COMP_QUE+MODE;
Writebuff[0]=ADDRESS_W;
Writebuff[1]=Pointer_1;
Writebuff[2]=Config/256;
Writebuff[3]=Config%256;
Readbuff[0]=ADDRESS_W;
Readbuff[1]=Pointer_0;
Readbuff[2]=ADDRESS_R;
}
/***************дÈë×Ö½Ú***********************/
void WriteWord(void)
{
int t=0;
Start(); //дÈë4¸ö×Ö½Ú
do
{
t=Write_N_Byte(Writebuff,4);
}while(t==0);
}
/***************¶ÁÈ¡×Ö½Ú***********************/
void ReadWord(void)
{
int t=0;
Start(); //дÈë2¸ö×Ö½Ú
do
{
t=Write_N_Byte(Readbuff,2);
}while(t==0);
Start(); //дÈë2¸ö×Ö½Ú
do
{
t=Write_N_Byte(&Readbuff[2],1);
}while(t==0);
Read_N_Byte(Result,2); //¶Á³ö2¸ö×Ö½Ú
}
/***************¶ÁÈ¡ADS1115¸÷ͨµÀÖµ********************/
unsigned int ADS1115(uint8_t Channel)
{
uint8_t S_MUX_0, S_MUX_1, S_MUX_2;
switch(Channel)
{
case 0: S_MUX_0 = 1;S_MUX_1 = 0;S_MUX_2 = 0;break;
case 1: S_MUX_0 = 1;S_MUX_1 = 0;S_MUX_2 = 1;break;
case 2: S_MUX_0 = 1;S_MUX_1 = 1;S_MUX_2 = 0;break;
case 3: S_MUX_0 = 1;S_MUX_1 = 1;S_MUX_2 = 1;break;
default:
break;
// Channel_Value = Channel + '0';
}
InitADS1115(S_MUX_0, S_MUX_1, S_MUX_2);
WriteWord();
Delay_ms(1);
ReadWord();
D_ADS=Result[0]*256+Result[1]; //ת»»µÄÊý×ÖÁ¿
return D_ADS;
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24569.html