hai
I am using LPC824 mcuxpresso IDE , I need 4 sensor value in 16 bit resolution for that i choosen the ADS 1115 adc sensor i2c protocol. I couldn't read the value from the sensor i dont know why? . i provided my code which used to take my o/p . plz let me know the solution for my below code
/*
===============================================================================
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\r ADC 0:\n\r ADC 1:\n\r ADC 2:\n\r 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 & 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);
//PutTerminalchar1(LPC_USART0, (uint8_t*)slave_rx_data);
LPC_I2C0->MSTCTL = CTL_SLVCONTINUE; // ACK the data
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX);
PutTerminalchar1(LPC_USART0, (uint8_t*)slave_rx_data);
// Send a stop to end the transaction
// LPC_I2C0->MSTCTL = CTL_MSTSTOP;
// PutTerminalchar1(LPC_USART0, (uint8_t*)slave_rx_data);
if (temp == 0) // If current char is NUL terminator
slave_data_counter = 0; // clear the counter for next string
}
// Any other slave state, stop here and debug
}
int main(void) {
int temp;
unsigned int abc= 3,abc1=5009,abc2=5,abc3=6;
int a="ABCDEFGHIJKLMN1234567/n";
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, 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;
// LPC_I2C0->MSTDAT = (slave_board_address<<1) | 1;
//Config_LEDs(GREEN|RED|BLUE);
//LPC_I2C0->INTENSET = STAT_SLVPEND;
LPC_I2C0->INTENSET = STAT_MSTPEND;
//NVIC_EnableIRQ(I2C0_IRQn);
while(1) {
// LEDs_On(GREEN);
// Prompt user to select a low-power mode for the slave (see utilities_lib)
temp = GetConsoleCharacter((const char *)&promptstring);
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX );
NVIC_EnableIRQ(I2C0_IRQn);
LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
LPC_I2C0->MSTCTL = CTL_MSTSTOP;
}
}
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
}
*/
} // end of while(1)
} // end of main
Hello Aravind palanisamy,
First of all, I recommend you use a Logic analyzer to analysis the two singles I2C_SDA and I2C_SCL,
check whether it is right.
Then there is I2C demo under LPCopen about lpc824, you can refer to:
LPCOpen Software for LPC8XX | NXP
Have a great day,
TIC
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------