ads1115 differential readings

108 views
Skip to first unread message

simon

unread,
Apr 19, 2017, 3:27:40 PM4/19/17
to Pi4J
hey

so i'm using the ads1115 example that is provided with pi4j but i'm having a hard time figuring out if and how i can use the differential readings function off the ads1115 chip from the provided code is this possible?


if not is there any good way that i myself can implement this functionality with out reprogramming/remaking the entire communication class to the ads1115?  

Robert Savage

unread,
Apr 19, 2017, 3:46:01 PM4/19/17
to pi...@googlegroups.com
Hi Simon,

Its been a long time since I have looked at that code or that chip.  But I was remembering that only singled ended was implemented and no config support for differential.

I just took a peek and yes, only single ended is implemented.

I would think this class could be adapted to support differential readings.  Perhaps some optional configuration settings passed in as part of the constructor rather than the static configuration that exists now.   I know the chip only supports two inputs in differential mode so something may need to be done to map the correct virtual pins to the appropriate chip registers. 

Thanks, 
-Robert
Message has been deleted

Sachin Singh

unread,
Jun 5, 2019, 2:31:04 PM6/5/19
to Pi4J


unsigned char ADS1115_Read_MiliVolts(float* MiliVolt)  Read 2 Bytes
{
float MVolt = 0 ;
unsigned char Data[] = {0x00 , 0x00};
if( I2C_Read(I2C1 , Data , 2 , AddresDS1115 , 0x00 , 1 ) )  0x00 = Select Data Reg
return 1 ;  Timed Out
      If ADC is NOT connected to voltage , Erratic Readings will Come
  But once voltage is aplied to channel , erratic reading will go away
Data[0] &= 0x7F ;  Make Sign bit of MSByte = 0
MVolt = 0.0625f * (float)(( Data[0] << 8 ) | Data[1] ) ;  Data[0] = MSByte
if( MVolt <= 2000 )  Apply Limits
*MiliVolt = MVolt;
     Else Last value of MiliVolt
return 0 ;  No Time Out
}
unsigned char ADS1115_SetUP(unsigned char chanel)
{
ADS1115_RDY_INTR();  Setup STM32 Interrupt for READY
Alert Interrupt Setup - Gives 8 uSec Pulse  - Use Pull Up Resistor
MSBit Low Threshold = 0          MSBit High Threshold = 1
unsigned char TimeOut = 0x00 ;
unsigned char Threshold[] = {0x00 , 0x00};
TimeOut |= I2C_Write(I2C1 , Threshold, 2, AddresDS1115 ,0x02 , 1);  Low Threshold
Threshold[0] = 0x80 ;
TimeOut |= I2C_Write(I2C1 , Threshold, 2, AddresDS1115 ,0x03 , 1);  High Threshold

Config Reg Setup
unsigned char ConfigReg[] = {0x34 , 0xE3};  C3 = 475 SPS , E3 = 860 SPS
if (chanel == 0)
*ConfigReg = 0x04 ;
return I2C_Write(I2C1 , ConfigReg, 2, AddresDS1115 ,0x01 , 1);
0x01 = Config Reg  , 1 = Config Reg 1 byte address
0x34 / 0x04 Goes to MSByte and 0xC3 goes to LSByte of ConfigReg
}

unsigned char ADS1115_Set_Chanel(unsigned char chanel)
 Set Chanel = Mux , Full Scale = PGA 2.048 ,  Data Rate = 475 SPS
{
Timer7SetupMicro();  setup timer in micro sec mode to use in WhileTimed

WhileTimed( I2C_GetFlagStatus(I2C1 , I2C_FLAG_BUSY)   ) ;

I2C_GenerateSTART(I2C1 , ENABLE);
WhileTimed( ! I2C_CheckEvent(I2C1 , I2C_EVENT_MASTER_MODE_SELECT)  ) ;

I2C_Send7bitAddress(I2C1 , AddresDS1115 << 1  ,I2C_Direction_Transmitter);
WhileTimed( ! I2C_CheckEvent(I2C1 ,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ;
Device has returned Ack => Address Ok , Device Ok

I2C_SendData(I2C1 , 0x01 );  Select Config Reg = write 1st byte
WhileTimed( ! I2C_GetFlagStatus(I2C1 , I2C_FLAG_BTF)  ) ; wait on BTF

if(chanel)
I2C_SendData(I2C1 , 0x34 ) ;  MSB Config Register
else                               Volt = 2.048  Continous
I2C_SendData(I2C1 , 0x04 ) ;  MSB Config Reg
WhileTimed( ! I2C_GetFlagStatus(I2C1 , I2C_FLAG_BTF)  ) ; wait on BTF

I2C_SendData(I2C1 , 0xC3 ) ;  Sample Per Sec
WhileTimed( ! I2C_GetFlagStatus(I2C1 , I2C_FLAG_BTF)  ) ; wait on BTF

I2C_GenerateSTOP(I2C1 , ENABLE);
WhileTimed( I2C_GetFlagStatus(I2C1 , I2C_FLAG_STOPF) );

/
TIM_Cmd(TIM7, DISABLE);  Desable WhileTimed Timer 7
return 0;  No Time Out

ErrorTimeOut :
TIM_Cmd(TIM7, DISABLE);
return 1 ;  Yes Timed Out

}


unsigned char ADS1115_Read_MiliVolts(unsigned char chanel , float *MiLVolts)  Read 2 Bytes
{
ADS1115_Set_Chanel(chanel) ;
Delay( 0 , 100 );

Timer7SetupMicro();  setup timer in micro sec mode to use in WhileTimed

unsigned char MSB = 0 , LSB = 0 ;
WhileTimed( I2C_GetFlagStatus(I2C1 , I2C_FLAG_BUSY) ) ;

I2C_GenerateSTART(I2C1 , ENABLE);
WhileTimed( ! I2C_CheckEvent(I2C1 , I2C_EVENT_MASTER_MODE_SELECT)  ) ;

I2C_Send7bitAddress(I2C1 , AddresDS1115 << 1  ,I2C_Direction_Transmitter);
while( ! I2C_CheckEvent(I2C1 ,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ;
Ack From Slave
WhileTimed (!(I2C_CheckEvent (I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED & ~((uint32_t) I2C_SR1_ADDR)))) ;

I2C_SendData(I2C1 , 0x00 );  Select Data Reg
WhileTimed( ! I2C_GetFlagStatus(I2C1 , I2C_FLAG_BTF)  ) ; wait on BTF

I2C_GenerateSTOP(I2C1 , ENABLE);
WhileTimed( I2C_GetFlagStatus(I2C1 , I2C_FLAG_STOPF) );
Pointer at data reg set , Start Reading 2 bytes


I2C_AcknowledgeConfig(I2C1 , ENABLE); Enable Ack
I2C_NACKPositionConfig(I2C1 , I2C_NACKPosition_Current);  clear POS flag

Last Communication Complete , New Comms To Be Started

I2C_GenerateSTART(I2C1 , ENABLE);  Start  Bit
WhileTimed (! I2C_CheckEvent(I2C1 , I2C_EVENT_MASTER_MODE_SELECT));  wait for EV5

I2C_Send7bitAddress(I2C1 , AddresDS1115 << 1 , I2C_Direction_Receiver);  Send Address
WhileTimed (! I2C_GetFlagStatus(I2C1 , I2C_FLAG_ADDR)); EV6

I2C_NACKPositionConfig(I2C1 , I2C_NACKPosition_Next); Set POS flag
EV6_1 --> must be atomic and in this order
__disable_irq ();
(void) I2C1 ->SR2;                       Clear ADDR flag
I2C_AcknowledgeConfig(I2C1 , DISABLE);   Clear Ack bit
__enable_irq ();
EV7_3 -- Wait for BTF , program stop , read data twice
WhileTimed (! I2C_GetFlagStatus(I2C1 , I2C_FLAG_BTF));
__disable_irq ();
I2C_GenerateSTOP(I2C1 ,ENABLE);
MSB = I2C1 ->DR;          MSB First
__enable_irq ();
LSB = I2C1 ->DR;
WhileTimed(I2C_GetFlagStatus(I2C1 , I2C_FLAG_STOPF)); Wait for stop Flag

return (( MSB << 8 ) | LSB)  ;
*MiLVolts =  (float)( 0.0625f * (float)(( MSB << 8 ) | LSB ) );

/
TIM_Cmd(TIM7, DISABLE);  Desable WhileTimed Timer 7
return 0;  No Time Out

ErrorTimeOut :
TIM_Cmd(TIM7, DISABLE);
return 1 ;  Yes Timed Out
}
void I2C_Start(void)  I2C1   PB6 = SCL        PB7 = SDA
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_StructInit (& GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;  Out Open Drain
GPIO_Init(GPIOB , &GPIO_InitStructure);

I2C1 Reset = Peripheral Reset Register
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1 , ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1 , DISABLE);

Configure I2C1
I2C_StructInit (& I2C_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0 ;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;  100 K Hz - 400 K Hz
I2C_Init(I2C1 , &I2C_InitStructure);
I2C_Cmd(I2C1 , ENABLE);
}

void ADS1115_RDY_INTR(void)
{
GPIO Config
RCC->APB2ENR |= (1<<3) | (1<<0) ;  AFIO is must for EXT  + PB
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;  Weak Pull Up
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 ;  PB0
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);

EXT Line Config
EXTI_InitTypeDef EXTI_InitStruct;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);  PB0
EXTI_InitStruct.EXTI_Line = EXTI_Line0 ;  EXTI_Line0
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);

NVIC Config
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;  EXTI0_IRQn = EXTI_Line0 = P - ABCD - 0
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0F;  lowest priority
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0F;  lowest subpriority
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
       NVIC_EnableIRQ(EXTI0_IRQn);
}

Reply all
Reply to author
Forward
0 new messages