LPC1111 I2C frezes

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

LPC1111 I2C frezes

441 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by audriusmerfeldas on Mon Nov 30 14:26:04 MST 2015
I am strugling with I2C issue. I am trying to get I2C communication working. I will use it for reading and controlling accelerometer, but for beginning I would like to get at least some data transfer during timmer interrupt. When I was searching for simple example I have found: https://eewiki.net/display/microcontroller/Getting+Started+with+NXP's+LPC11XX+Cortex-M0+ARM+Microcon...

It looks like me program stucks at this line:  LPC_I2C->CONCLR     = 0x28;  
I am newbie in programming so thank you very much for any help.

Here is me code:



#include "LPC11xx.h"                        /* LPC11xx definitions */
#include "gpio.h"
#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

int i=0;
unsigned int status = 0;

void timer16_0_setup(void)
{
//Set up 16 bit timer
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);//enable timer clock
LPC_TMR16B0->PR  = 48000-1; //prescaler 48000 = 1ms
LPC_TMR16B0->MCR = (3<<3); //interrupt and reset counter match1
LPC_TMR16B0->CCR = 0; //timer mode
LPC_TMR16B0->MR1 = 1000-1; //set match1 time in us
NVIC_EnableIRQ(TIMER_16_0_IRQn); //enable interrupt
LPC_TMR16B0->TCR = 1; //start timer
}

void I2C_setup(void)
{
//INITIALIZE I2C pins, system components, and enable master transmit mode
     LPC_IOCON->PIO0_4         &= ~(0x303); //clear "FUNC" and "I2CMODE" bits (sec 7.4.11)
     LPC_IOCON->PIO0_4         |= 0x1;      //select I2C function SCL (sec 7.4.11)
     LPC_IOCON->PIO0_5         &= ~(0x303); //clear "FUNC" and "I2CMODE" bits (sec 7.4.12)
     LPC_IOCON->PIO0_5         |= 0x1;      //select I2C function SDA (sec 7.4.12)
     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);   //enable clock to I2C block (sec 3.5.14)
     LPC_SYSCON->PRESETCTRL    |= (0x2);    //disable reset to I2C unit (sec 3.5.2)
     LPC_I2C->CONSET           |= (1<<6);   //put I2C unit in master transmit mode (sec 15.8.1 and 15.7.1)
     LPC_I2C->SCLH             = 0xFFF;     //set clk dividers (sec 15.7.5) set arbitrarily long
     LPC_I2C->SCLL             = 0xFFF;     //set clk dividers (sec 15.7.5) set arbitrarily long
}


//Timer interrupt
void TIMER16_0_IRQHandler(void)
{
if(LPC_TMR16B0->IR & (1<<1))//match1 interrupt?
{
  if (i==0) {GPIOSetValue( 1, 7, 1 ); i=1;}
  else {GPIOSetValue( 1, 7, 0 ); i=0;}

//I2C TRANSMIT
            LPC_I2C->CONSET |= (1<<5);             //set start bit to initiate transmission (sec 15.7.1)
           do{                                    //wait for start condition to be sent
            status = LPC_I2C->STAT & 0xF8;          //store current state (sec 15.7.2)
            }while(status != 0x08);                //compare current state with possible states (sec 15.10.1 table 235)
             //TRANSMIT DEVICE ADDRESS
            LPC_I2C->DAT        = 0x1D;            //transmit device address (sec 15.7.3)
          [color=#f00] LPC_I2C->CONCLR     = 0x28;   [/color]         //clear STA and SI bit (sec 15.7.6)
            //TRANSMIT CONTROL BYTE
            while(LPC_I2C->STAT != 0x18);          //wait for address byte to be sent (sec 15.10.1 table 235)
            LPC_I2C->DAT        = 0x06;            //send data (sec 15.7.3)
             LPC_I2C->CONCLR     = (1<<3);          //clear SIC (sec 15.7.6)
            //TRANSMIT DATA BYTE
            while(LPC_I2C->STAT != 0x28);          //wait for address byte to be sent (sec 15.10.1 table 235)
            LPC_I2C->DAT        = ++i;             //send data (sec 15.7.3)
            LPC_I2C->CONCLR     = (1<<3);          //clear SIC (sec 15.7.6)
            //INITIATE STOP CONDITION
            while((LPC_I2C->CONSET & 0x8) != 0x8); //set STOP bit (sec 15.7.1)
            LPC_I2C->CONSET     = 0x10;            //set stop bit (sec 15.7.1)
            LPC_I2C->CONCLR     = (1<<3);          //clear SIC (sec 15.7.6)


}
  LPC_TMR16B0->IR = (1<<1); //reset flag

}//end match1 interrupt

int main (void) {

  //Set up timer
  timer16_0_setup();
  I2C_setup();
  GPIOInit();

  // Set port for LED to output
  GPIOSetDir( 1, 7, 1 );

  while (1)                                   // Loop forever
  {

  }
   }
Labels (1)
0 Kudos
4 Replies

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by audriusmerfeldas on Tue Dec 08 00:07:12 MST 2015
Any solutions?
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by audriusmerfeldas on Fri Dec 04 06:52:51 MST 2015
Jep, I have. At the moment I have made PCB and solder only LPC1111 and 3.3V LDO. I suppose it is nothing special to hardware, it should be software issues. While checking on the scope I can see something like trying to start there is a pulse down on both SDA ans SCL.
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tschnagel on Fri Dec 04 04:44:09 MST 2015
Just to ask the (not always  ;-) ) obvious question...do you have any pull up resistors on SDA and SCL?  Can you provide us with a schematic of your circuit?
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by audriusmerfeldas on Thu Dec 03 00:30:56 MST 2015
I have tried one more example. This doesn't freeze processor but still I dont get any signal on the SCL, SDA pins on the scope.

Here is the code I am using now:

#include "LPC11xx.h"                        /* LPC11xx definitions */
#include "gpio.h"
#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

int i=0;
unsigned int status = 0;

void timer16_0_setup(void)
{
//Set up 16 bit timer
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);    //enable timer clock
LPC_TMR16B0->PR  = 48000-1;             //prescaler 48000 = 1ms
LPC_TMR16B0->MCR = (3<<3);             //interrupt and reset counter match1
LPC_TMR16B0->CCR = 0;                     //timer mode
LPC_TMR16B0->MR1 = 1000-1;                 //set match1 time in us
NVIC_EnableIRQ(TIMER_16_0_IRQn);         //enable interrupt
LPC_TMR16B0->TCR = 1;                     //start timer
}

void I2C_Init(void)
{
        LPC_SYSCON->PRESETCTRL |= (1<<1);      //De-Asserts Reset Signal to I2C
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);   //Enable I2C Clock

        LPC_IOCON->PIO0_4 &= ~0x3F;
        LPC_IOCON->PIO0_4 |= 0x01;   //SCL

        LPC_IOCON->PIO0_5 &= ~0x3F;
        LPC_IOCON->PIO0_5 |= 0x01;   //SDA

        //I2C 100
        LPC_I2C->SCLH = 360;         //I2PCLK is 72MHz
        LPC_I2C->SCLL = 360;         //I2PCLK is 72MHz

        LPC_I2C->CONCLR = 0xFF;        //Clear All Flags
        LPC_I2C->CONSET = (1<<6);      //I2C Interface Enable
}
void I2C_Start(void)
{
        LPC_I2C->CONCLR = 0X28;
        LPC_I2C->CONSET |= 0x20;                     //Set the Start Bit
        while(LPC_I2C->STAT!=0x08);          //Wait for the Status Bit
}
void I2C_Restart(void)
{
        LPC_I2C->CONCLR = 0X28;
        LPC_I2C->CONSET |= 0x20;                     //Set the Start Bit
        while(LPC_I2C->STAT!=0x10);          //Wait for the Status Bit
}
void I2C_Stop(void)
{
        LPC_I2C->CONSET |= 0x14;     //Stop I2C
        LPC_I2C->CONCLR = 0x08;
}
void I2C_Write(unsigned char data,unsigned char status)
{
        LPC_I2C->CONCLR = 0X28;
        LPC_I2C->DAT = data;
  LPC_I2C->CONCLR = 0X28;                                            //Clear Start Flag and SI Interrupt
  //while(LPC_I2C->STAT!=status);                //Wait for the Status Byte
}
unsigned char I2C_Read(void)
{
  LPC_I2C->CONCLR = 0X2C;            //Cleat SI and Asset Acknowledgment Flag
  while(LPC_I2C->STAT!=0x58);
  return(LPC_I2C->DAT);
}


//Timer interrupt
void TIMER16_0_IRQHandler(void)
{
if(LPC_TMR16B0->IR & (1<<1))            //match1 interrupt?
{
  if (i==0) {GPIOSetValue( 1, 7, 1 ); i=1;}
  else {GPIOSetValue( 1, 7, 0 ); i=0;}

  I2C_Start();
  I2C_Write(100,1);
  I2C_Stop();


}
  LPC_TMR16B0->IR = (1<<1);             //reset flag

}                    //end match1 interrupt

int main (void) {

  //Set up timer
  timer16_0_setup();
  I2C_Init();
  GPIOInit();

  // Set port for LED to output
  GPIOSetDir( 1, 7, 1 );

  while (1)                                   // Loop forever
  {

  }
   }



Any hints?
0 Kudos