AnsweredAssumed Answered

lpc824 external adc reading?

Question asked by Aravind palanisamy on Jul 8, 2019
Latest reply on Jul 16, 2019 by Aravind palanisamy

hai 

           I need to read ADS1115 sensor values through the i2c protocol . i am, using lpc824 mcuxpresso i am, try last 2 week referred the ads1115 datasheet and lpc824 datasheet. but i got only junk values , please let me know the solution to correct my faults. 

 plz refer my code and give me somr solution i am asking this assistance last 2 week but till i couldn't get proper o/p  

 

 i refered lpc example coding which provided  in the mcuxpresso ide but in that code also i didn't got any proper solution . in that examples code there is no adc reading code in i2c plz give the solution

 

 

 

//#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;

#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 & 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=0;
unsigned int five=50009;
// 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
PutTerminalchar2(LPC_USART0,slave_rx_data);
// PutTerminalchar2(LPC_USART0, five);
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) {

int temp,temp1=0;
unsigned int abc= 3,abc1=5009,abc2=5,abc3=6;
int 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_SDA0
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);

LPC_I2C0->CFG = CFG_MSTENA;
//LPC_I2C0->MSTDAT = (slave_board_address<<1) | 1;
//NVIC_EnableIRQ(I2C0_IRQn);


while(1) {


WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE);

LPC_I2C0->MSTDAT = (slave_board_address<<1) |1; //// ( read )

LPC_I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction.

WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX );
// LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;

 

temp1 = LPC_I2C0->MSTDAT;
LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
slave_rx_data[slave_data_counter++] = temp1;

PutTerminalchar2(LPC_USART0,(uint8_t*)slave_rx_data);
WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX );
LPC_I2C0->MSTCTL = CTL_MSTSTOP;


if (temp1 == 0) // If current char is NUL terminator
{
slave_data_counter = 0;
}

 


} // end of while(1)

} // end of main

Outcomes