i2c terminal ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i2c terminal ?

866 Views
aravindpb5009
Contributor III

hai

 i am using MCUxpresso IDE ,lpc824   how to see the i2c output in mcuxpresso for example in uart i can see the op in terminal but for i2c o/p in which one can see the o/p . plz let me know the solution for my question

Thanks & Regards 

Aravind P

Labels (1)
0 Kudos
6 Replies

708 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Aravind,

As you know that the I2C protocol needs acknowledge signal as a handshaking signal which is provided by slave I2C device, in other words, you need to have both master I2C and slave I2C devices to communicate.

I suggest you get a board on which there is an I2C device for example memory with I2C interface or sensor for example accelerator sensor with I2C interface.

Hope it can help you

BR

XiangJun Rong

0 Kudos

708 Views
aravindpb5009
Contributor III

I am using LPC824 mcuxpresso IDE  , i need 16 bit adc o/p but in lpc824 internally having only 12 bit  for  that i chosen the external adc for 16 bit resolution ADC IC name  (ADS1115   I2C )  . In this ic simply we can connect the adc  to  the controller with sda, scl pin means it can automatically read the values, but i read the i2c adc in i2c interrupt  with controller it doesn't read any value from the ads1115 adc . in MCUXPRESSO some examples are having in that example some i2c LED  coding are available that was working but i2c reading example is not having.  i verified in our datasheet also but i didn't got any solution from datasheet .my LPC824 is master and ads1115 is slave .  please let me know how to read the i2c value in the lpc824 and also provide some example code for read the i2c values. checked my ADS1115  is working are not for that clarification used the arduino uno controller  connect the sda ,scl in arduino uno my ADS1115 O/P was coming but in lpc824 having some problem plz let me know the brief explanation

0 Kudos

708 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I have downloaded the data sheet of ADS1113 from TI home page, firstly, pls determine the slave address based on ADDR pin connection, then you can write the register or read the register based on the Fig30 and Fig 31.

Can you post the code you have written?

BR

XiangJun Rong

0 Kudos

708 Views
aravindpb5009
Contributor III

hai 

I have attached my i2c adc coding plz let me know the solution what i have done in my code or you can edit and send me 

ADC IC (ADS1115)    address 0x48;  . I  need to read 4 sensor values in ads1115 but my code does not read any values

plz give me the solution 

/*
===============================================================================
Name : Example_I2C_Wakeup_Master.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/

//#ifdef __USE_CMSIS
#include "LPC8xx.h"
//#endif

//#include <cr_section_macros.h>

#include <stdio.h>
#include "lpc8xx_i2c.h"
#include "lpc8xx_syscon.h"
#include "lpc8xx_swm.h"
#include "utilities.h"

extern void setup_debug_uart(void);
#define BUFFER_SIZE 35
#define WaitForUART0txRdy while(((LPC_USART0->STAT) & (1<<2)) == 0)

void setup_debug_uart(void);


unsigned char the_prompt[] = "Enter some characters to be transmitted from the I2C master to the slave\n\r";
unsigned char the_massage[] = "Characters received by the I2C slave were \n\r";
unsigned char u0_rx_buffer[BUFFER_SIZE];
unsigned char slave_rx_data[BUFFER_SIZE];
volatile enum {false, true} uart_handshake;

static uint32_t rx_char_counter = 0;
static uint32_t slave_data_counter = 0;
// Program flow
// 1. Configure I2C0 as master (requires external connections with I2C0 on the slave board, don't forget the external pull-ups.
// 2. Prompt the user to type a character ('0', '1', or '2') then [Return]
// 3. Transmit the character on I2C0 to the slave board's I2C0 address to put the slave to sleep.
// 4. Prompt the user to type [Esc] to proceed with waking up the slave.
// 5. Transmit the slave board's address to wake up the slave.

#define slave_board_address 0x48

const char promptstring[] = "IOT I2C ADC list:\n\r0 ADC 0\n\r1 ADC 1\n\r2 ADC 2\n\r3 ADC 3\n\r";

void UART0_IRQHandler() {
unsigned char temp;

temp = LPC_USART0->RXDAT ;
u0_rx_buffer[rx_char_counter] = temp; // Append the current character to the rx_buffer
WaitForUART0txRdy; // Wait for TXREADY
LPC_USART0->TXDAT = temp; // Echo it back to the terminal

if (temp == 0x0D) { // CR (carriage return) is current character. End of string.
u0_rx_buffer[rx_char_counter+1] = 0x0A; // Append a new line character to u0_rx_buffer.
u0_rx_buffer[rx_char_counter+2] = 0x00; // Append a NUL terminator character to u0_rx_buffer to complete the string.
WaitForUART0txRdy; // Wait for TXREADY
LPC_USART0->TXDAT = 0x0A; // Echo a NL (new line) character to the terminal.
uart_handshake = true; // Set handshake for main()
rx_char_counter = 0; // Clear array index counter
}
else { // Current character is not CR, keep collecting them.
rx_char_counter++; // Increment array index counter.

if (rx_char_counter == BUFFER_SIZE) // If the string overruns the buffer, stop here before all hell breaks lose.
while(1);
}
return;
}


/*****************************************************************************
** Function name: I2C0_IRQHandler
**
** Descriptions: I2C0 interrupt service routine.
**
** parameters: None
** Returned value: None
**
*****************************************************************************/
/*void I2C0_IRQHandler(void) {
uint32_t temp;

if ((LPC_I2C0->STAT & SLAVE_STATE_MASK) == STAT_SLVADDR) {
LPC_I2C0->SLVCTL = CTL_SLVCONTINUE; // ACK the address
return;
}

if ((LPC_I2C0->STAT & SLAVE_STATE_MASK) == STAT_SLVRX) {
temp = LPC_I2C0->SLVDAT; // Read the data
slave_rx_data[slave_data_counter++] = temp; // Store it in the array, increment counter
//PutTerminalString(LPC_USART0,slave_rx_data);
LPC_I2C0->SLVCTL = CTL_SLVCONTINUE; // ACK the data
if (temp == 0) // If current char is NUL terminator
slave_data_counter = 0; // clear the counter for next string
return;
}

while(1); // Any other slave state, stop here and debug
}*/


void I2C0_IRQHandler(void) {
uint32_t temp;

if ((LPC_I2C0->STAT & MASTER_STATE_MASK) == STAT_MSTIDLE) {
LPC_I2C0->MSTCTL = CTL_MSTCONTINUE; // ACK the address
return;
}

if ((LPC_I2C0->STAT & MASTER_STATE_MASK) == STAT_MSTRX) {
temp = LPC_I2C0->MSTDAT; // Read the data
slave_rx_data[slave_data_counter++] = temp; // Store it in the array, increment counter
//PutTerminalString(LPC_USART0,slave_rx_data);
LPC_I2C0->MSTCTL = CTL_SLVCONTINUE; // ACK the data
if (temp == 0) // If current char is NUL terminator
slave_data_counter = 0; // clear the counter for next string
return;
}

while(1); // Any other slave state, stop here and debug
}


int main(void) {

char temp;
int abc= 3,abc1=5009,abc2=5,abc3=6;
char *a="ABCDEFGHIJKLMN1234567/n";
// Configure the debug uart (see Serial.c)
setup_debug_uart();

// Enable clocks to I2C, SWM (see lpc8xx_syscon.h)
LPC_SYSCON->SYSAHBCLKCTRL |= (I2C0|SWM);

// SWM settings for I2C0 (master):
// P0.10 = I2C0_SCL
// P0.11 = I2C0_SDA
LPC_SWM->PINENABLE0 &= ~(I2C0_SCL|I2C0_SDA); // Use for LPC824
// Give I2C a reset (see lpc8xx_syscon.h)
LPC_SYSCON->PRESETCTRL &= (I2C0_RST_N);
LPC_SYSCON->PRESETCTRL |= ~(I2C0_RST_N);

PutTerminalchar(LPC_USART0,(uint8_t *) abc);
PutTerminalString(LPC_USART0, (uint8_t *) the_prompt); // IOT PROJECT STRING

// Configure the I2C clock divider
// Desired bit rate = Fscl = 100,000 Hz
// 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
//
LPC_I2C0->DIV = (75-1);
// LPC_I2C0->DIV = (30-1);
// Configure the I2C CFG register:
// Master enable = true
// Slave enable = false
// Monitor enable = false
// Time-out enable = false
// Monitor function clock stretching = false
//
LPC_I2C0->CFG = CFG_MSTENA;

//Config_LEDs(GREEN|RED|BLUE);
//LPC_I2C0->INTENSET = STAT_SLVPEND;
// LPC_I2C0->INTENSET = STAT_MSTPEND;
NVIC_EnableIRQ(I2C0_IRQn);
while(1){

WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait until master pending and idle

// LPC_I2C0->MSTDAT = (slave_board_address<<1) | 0; // Send the address with 0 for RWn bit (WRITE)

LPC_I2C0->MSTCTL = CTL_MSTSTART; // Start the clk
LPC_I2C0->MSTDAT = (slave_board_address<<1) | 0; // Send the address with 0 for RWn bit (READ)
PutTerminalchar(LPC_USART0,(uint8_t *) abc);

// Wait for the address to be ACK'd
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX);

LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
// PutTerminalString(LPC_USART0, (uint8_t *)a);
// Wait for the data to be ACK'd
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);

// Send a stop to end the transaction
LPC_I2C0->MSTCTL = CTL_MSTSTOP;

// PutTerminalString(LPC_USART0, (uint8_t *)promptstring);


}

}

Thanks & regards 

Aravind P

0 Kudos

708 Views
aravindpb5009
Contributor III

void WaitI2CMasterState(LPC_I2C_TypeDef * ptr_LPC_I2C, uint32_t state) {

while(!(ptr_LPC_I2C->STAT & STAT_MSTPEND)); // Wait for MSTPENDING bit set in STAT register
if((ptr_LPC_I2C->STAT & MASTER_STATE_MASK) != state) { // If master state mismatch ...
LEDs_Off(GREEN);
LEDs_On(RED);
while(1); // die here and debug the problem
}
return; // If no mismatch, return

}

0 Kudos

708 Views
aravindpb5009
Contributor III

still i didn't got the o/p let me know the solution which i send you the my code at above message  

0 Kudos