AnsweredAssumed Answered

i2c reading problem

Question asked by Aravind palanisamy on Jul 26, 2019
Latest reply on Jul 30, 2019 by Alexis Andalon

Hai

             i am using LPC824 MCUXPRESSO IDE    i need to read 16 bit adc but in lpc824 having only 12 bit so thats why i got external adc ADS1115 module this I2C based module . i try to read this o/p till 1 month but i don't know why i am facing this problem . i asked lot of time in NXP community but didn't got correct answer still . used in ardiuno uno for testing purpose in ardiuno controller ADS1115 O/P getting correctly please let me know the solution what i did the mistake in my code in below provided the code 

 

 

my controller working frequency is 12mhz  AND i2c_freq i set 30mhz and 400khz speed 

 

 

 

 

/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

/* Standard C Included Files */
#include <stdio.h>
#include <string.h>
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_i2c.h"
#include "fsl_i2c.h"
#include <stdlib.h>
#include <string.h>
#include "pin_mux.h"
#include "fsl_device_registers.h"
#include "fsl_common.h"
#include"lpc8xx_i2c.h"
#include"fsl_usart.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define START_BYTE "START_BYTE\n\r"
#define SEPERATE_BYTE "-"
#define STOP_BYTE "STOP_BYTE\n\r"
#define atcmd "AT"
uint8_t F_DEMOCHECK[] = "4096";
uint8_t s_identifier[] = "SPEEDTECH";
uint8_t g_demoInfo[] = " IOT CHECKING............\r\n";
uint8_t A_sensor1[] = " ADC_1 SENSOR_READING:\r\n";
uint8_t A_sensor2[] = " ADC_2 SENSOR_READING:\r\n";
uint8_t A_sensor3[] = " ADC_3 SENSOR_READING:\r\n";
uint8_t A_sensor4[] = " ADC_4 SENSOR_READING:\r\n";
uint8_t g_logInfo[] = "The received characters are:";
uint8_t g_overFlowInfo[] = "\r\nYou have input too many characters, Please try again!\r\n";
uint8_t g_checking[] = "\r\n condition stopped before the while loop ...!\r\n";

#define EXAMPLE_USART USART0
#define EXAMPLE_USART1 USART1
#define EXAMPLE_USART_CLK_SRC kCLOCK_MainClk
#define EXAMPLE_USART_CLK_FREQ CLOCK_GetFreq(EXAMPLE_USART_CLK_SRC)
#define EXAMPLE_USART_IRQn USART0_IRQn

#define EXAMPLE_I2C_MASTER_BASE (I2C0_BASE)
#define I2C_MASTER_CLOCK_FREQUENCY (30000000)
#define WAIT_TIME 10U
#define EXAMPLE_I2C_MASTER ((I2C_Type *)EXAMPLE_I2C_MASTER_BASE)

#define Self_Slave_address 0x48U
#define ADS1015_REG_POINTER_MASK (0x03)
#define ADS1015_REG_POINTER_CONVERT (0x00)
#define ADS1015_REG_POINTER_CONFIG (0x01)
#define ADS1015_REG_POINTER_LOWTHRESH (0x02)
#define ADS1015_REG_POINTER_HITHRESH (0x03)
#define config_high 0xD1
#define config_low 0x83
#define config_line 0x0083


#define BOARD_LED_PORT 0U
#define BOARD_LED_PIN 23U
static void EXAMPLE_USARTInit(void);
void delay_ms(int a);
int main(void)
{
unsigned int msb_byte,lsb_byte,msb=5,lsb=9;
unsigned char * tx_ptr;
unsigned int temp1=5009;
gpio_pin_config_t led_config = {
kGPIO_DigitalOutput,
0,
};

GPIO_PortInit(GPIO, BOARD_LED_PORT);
GPIO_PinInit(GPIO, BOARD_LED_PORT, BOARD_LED_PIN, &led_config);
// i2c_master_config_t masterConfig;


/* Enable clock of uart0. */
CLOCK_EnableClock(kCLOCK_Uart0);
/* Ser DIV of uart0. */
CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);
/* Enable clock of I2C0. */
CLOCK_EnableClock(kCLOCK_Uart1);
/* Ser DIV of uart0. */
CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);
/* Enable clock of I2C0. */
CLOCK_EnableClock( kCLOCK_I2c0 );

BOARD_InitPins();
BOARD_BootClockIRC12M();
BOARD_InitDebugConsole();
EXAMPLE_USARTInit();
SYSCON->PRESETCTRL &= (I2C0_RST_N);
SYSCON->PRESETCTRL |= ~(I2C0_RST_N);


// Configure the I2C0 clock divider
// Desired bit rate = Fscl = 100,000 Hz (1/Fscl = 10 us, 5 us low and 5 us high)
// Use default clock high and clock low times (= 2 clocks each)
// So 4 I2C_PCLKs = 100,000/second, or 1 I2C_PCLK = 400,000/second
// I2C_PCLK = SystemClock = 30,000,000/second, so we divide by 30/.4 = 75
// Remember, value written to DIV divides by value+1

I2C0->CLKDIV = (75-1);
I2C0->CFG = CFG_MSTENA ;


WaitI2CMasterState(STAT_MSTIDLE ); // CHECKING MASTER IS IDLE
// I2C0->INTENSET = STAT_MSTPEND|STAT_SLVPEND ;

 

//////////////////////////// configuration bits write to ads1115 //////////////////////////////////////////////////////
I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
I2C0->MSTDAT = (Self_Slave_address<<1) | 0; // Address with 0 for RWn bit (WRITE)
WaitI2CMasterState(I2C_STAT_MSTST_TX ); // Wait for the address to be ACK'd
delay_ms(200);
I2C0->MSTDAT = ADS1015_REG_POINTER_CONFIG; // Address with 0 for RWn bit (WRITE)
I2C0->MSTCTL = CTL_MSTCONTINUE; // MASTER CONTINUE THE TRANSCATION
WaitI2CMasterState( I2C_STAT_MSTST_TX ); // Wait for the address to be ACK'd
delay_ms(200);
I2C0->MSTDAT = config_high; // Address with 0 for RWn bit (WRITE) config
I2C0->MSTCTL = CTL_MSTCONTINUE; // MASTER CONTINUE THE TRANSCATION
delay_ms(200);
WaitI2CMasterState( I2C_STAT_MSTST_TX ); // Wait for the address to be ACK'd
I2C0->MSTDAT =config_low; // Address with 0 for RWn bit (WRITE) config
I2C0->MSTCTL = CTL_MSTCONTINUE; // MASTER CONTINUE THE TRANSCATION
delay_ms(200);
WaitI2CMasterState( I2C_STAT_MSTST_TX ); // Wait for the address to be ACK'd
I2C0->MSTCTL = CTL_MSTSTOP; // Send a stop to end the transaction
WaitI2CMasterState(STAT_MSTIDLE ); // checking idle state


// //////////////////////////// convertion bits write to ads1115 //////////////////////////////////////////////////////
I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
I2C0->MSTDAT = (Self_Slave_address<<1) | 0; // Address with 0 for RWn bit (write)
WaitI2CMasterState( I2C_STAT_MSTST_TX); // Wait for the address to be ACK'd
delay_ms(200);
I2C0->MSTDAT = ADS1015_REG_POINTER_CONVERT; // SENDING THE COVERSION REG BITS
I2C0->MSTCTL = CTL_MSTCONTINUE; // MASTER CONTINUE THE TRANSCATION
delay_ms(200);
WaitI2CMasterState( I2C_STAT_MSTST_TX); // Wait for the address to be ACK'd
I2C0->MSTCTL = CTL_MSTSTOP; // Send a stop to end the transaction
WaitI2CMasterState(STAT_MSTIDLE ); // CHECKING MASTER IS IDLE

I2C0->MSTCTL = CTL_MSTCONTINUE;
while(1)
{

GPIO_PortToggle(GPIO, BOARD_LED_PORT, 1u << BOARD_LED_PIN);
USART_WriteBlocking(EXAMPLE_USART, START_BYTE , (sizeof( START_BYTE ))-1); // SEPERATE BYTE '-
USART_WriteBlocking(EXAMPLE_USART1, atcmd , (sizeof( atcmd ))-1); // SEPERATE BYTE '-

 

/////////////////////////// convertion bits read from ads1115 //////////////////////////////////////////////////////
I2C0->MSTDAT = (Self_Slave_address<<1) | 1; // Address with 0 for RWn bit (read)
I2C0->MSTCTL = CTL_MSTSTART;
delay_ms(500);
WaitI2CMasterState( I2C_STAT_MSTST_RX);
msb_byte = I2C0->MSTDAT; // Read the data
I2C0->MSTCTL = CTL_MSTCONTINUE;
WaitI2CMasterState( I2C_STAT_MSTST_RX);
delay_ms(500);
lsb_byte = I2C0->MSTDAT; // Read the data
I2C0->MSTCTL = CTL_MSTCONTINUE;
WaitI2CMasterState( I2C_STAT_MSTST_RX);
delay_ms(500);
USART_WriteBlocking1(EXAMPLE_USART, msb_byte, lsb_byte ); // SEPERATE BYTE '-
USART_WriteBlocking2(EXAMPLE_USART, lsb_byte ); // SEPERATE BYTE '-
delay_ms(500);
USART_WriteBlocking2(EXAMPLE_USART, msb_byte ) ; // SEPERATE BYTE '-
delay_ms(500);
USART_WriteBlocking(EXAMPLE_USART, STOP_BYTE , (sizeof( STOP_BYTE ))-1); // SEPERATE BYTE '-
I2C0->MSTCTL = CTL_MSTSTOP;

// delay_ms(100);

// slave_rx_data[slave_data_counter++] = temp; // Store it in the array, increment counter

// WaitI2CMasterState(I2C0, I2C_STAT_MSTST_RX); // Wait for the data to be ACK'd
// NVIC_EnableIRQ(I2C0_IRQn);
// Send a stop to end the transaction
//PutTerminalString(LPC_USART0, (uint8_t *)stop_bit);// Print a massage
//delay_ms(100);
} // end of while 1

} // end of main

 

 


void delay_ms(int a)
{
int i,j;
for(i=0;i<a;i++)
for(j=0;j<a;j++);
}

/////////////////////////////////////

 

static void EXAMPLE_USARTInit(void)
{
usart_config_t config;
/* Default config by using USART_GetDefaultConfig():
* config.baudRate_Bps = 9600U;
* config.parityMode = kUSART_ParityDisabled;
* config.stopBitCount = kUSART_OneStopBit;
* config.bitCountPerChar = kUSART_8BitsPerChar;
* config.loopback = false;
* config.enableRx = false;
* config.enableTx = false;
* config.syncMode = kUSART_SyncModeDisabled;
*/
USART_GetDefaultConfig(&config);
config.enableRx = true;
config.enableTx = true;
config.baudRate_Bps = BOARD_DEBUG_USART_BAUDRATE;

/* Initialize the USART with configuration. */
USART_Init(EXAMPLE_USART, &config, EXAMPLE_USART_CLK_FREQ);
USART_Init(EXAMPLE_USART1, &config, EXAMPLE_USART_CLK_FREQ);
/* Send demo info and prompt info out to terminal in blocking way. */
USART_WriteBlocking(EXAMPLE_USART, g_demoInfo, (sizeof(g_demoInfo) / sizeof(g_demoInfo[0])) - 1);
USART_WriteBlocking(EXAMPLE_USART1, g_demoInfo, (sizeof(g_demoInfo) / sizeof(g_demoInfo[0])) - 1);

EnableIRQ(EXAMPLE_USART_IRQn);
}

 

 

 

 

Thanks& Regards

Aravind P

Outcomes