EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Problem on LPC2148 I2C

Problem on LPC2148 I2C

|

Miscellaneous › Others › Problem on LPC2148 I2C

  • This topic has 1 reply, 2 voices, and was last updated 8 years, 10 months ago by Prabakaran P M.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • June 13, 2016 at 11:54 am #4486
    Rajavarman
    Participant
      **


    CODE 1


    ** 
     
        #include<lpc214x.h>
        
        void ser_init(void);
        void tx(unsigned char c);
        unsigned char rx(void);
        void tx_string(unsigned char *s);
        
        void i2c_init(void);
        void write(unsigned char data,unsigned char addr);
        unsigned char read(unsigned char addr);
        
        int main()
        {
        unsigned char temp;
        ser_init();
        i2c_init();
        tx_string("Writingnr");
        write('A',0x00);
        tx_string("Write successfully…nr");
        tx_string("Reading…nr");
        temp=read(0x00);
        tx_string("Received data is : ");
        tx(temp);
        while(1);
        }
        
        void i2c_init()
        {
        PINSEL0|=0x50;
        I2C0SCLH=75;
        I2C0SCLL=75;
        }
        
        void write(unsigned char data,unsigned char addr)
        {
        unsigned char addL,addH;
        
        addL=addr&0xff;
        addH=(addr&0xff) >>8;
        
        I2C0CONCLR=0x000000FF; //Clear all I2C settings
        I2C0CONSET=1<<6; //I2EN=1
        I2C0CONSET=1<<5; //Start
        while(I2C0STAT != 0x08);
        I2C0CONCLR=(1<<5)|(1<<3); //Clear Start and Interrupt
        
        I2C0DAT = 0xA0;
        I2C0CONSET=1<<2;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x18);
        
        I2C0DAT = addH;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x28);
        I2C0DAT = addL;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x28);  
        
        I2C0DAT = data;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x28);
        I2C0CONCLR=(1<<3); //Clear Interrupt
        I2C0CONSET=1<<4; //Stop
        }
        
        unsigned char read(unsigned char addr)
        {
        unsigned char read=0,addL,addH;
        addL=addr&0xff;
        addH=(addr&0xff00)>>8;
        
        I2C0CONCLR=0x000000FF; //Clear all I2C settings
        I2C0CONSET=1<<6; //I2EN=1
        I2C0CONSET=1<<5; //Start
        while(I2C0STAT != 0x08);
        I2C0CONCLR=(1<<5)|(1<<3); //Clear Start and Interrupt
        
        I2C0ADR = 0xA0;
        I2C0DAT = 0xA0;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x18);
        
        I2C0DAT = addH;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x28);
        I2C0DAT = addL;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x28);
        
        I2C0CONSET=(1<<2)|(1<<5); //Start
        I2C0CONCLR=(1<<3); //Clear Interrupt
        while(I2C0STAT != 0x10);
        
        I2C0ADR = 0xA1;
        I2C0DAT = 0xA1;
        I2C0CONCLR=(1<<3) | (1<<5); //Clear Interrupt , Start
        while(I2C0STAT != 0x40);
        I2C0CONCLR=(1<<3); //Clear Interrupt
        
        while(I2C0STAT != 0x50);
        read=I2C0DAT;
        I2C0CONCLR=(1<<3); //Clear Interrupt
        
        I2C0CONCLR=(1<<3); //Clear Interrupt
        I2C0CONSET=1<<4;
        
        return read;
        }
        
        void ser_init()
        {
        VPBDIV=0x02;
        PINSEL0|=0x05;
        U0LCR=0x83;
        U0DLL=195;
        U0DLM=0;
        U0LCR=0x03;
        U0TER=(1<<7);
        
        }
        
        void tx(unsigned char c)
        {
        U0THR=c;
        while((U0LSR&(1<<5))==0);
        }
        
        void tx_string(unsigned char *s)
        {
        while(*s)
        {
        tx(*s++);
        }
        }
        
        unsigned char rx()
        {
        while((U0LSR&(1<<0))==0);
        return U0RBR;
        }
     
    **


    CODE 2


    **
     
        #include<lpc214x.h>
        
        #define delay for(i=0;i<100000;i++)
        #define FALSE 0
        #define TRUE 1
        unsigned long int i;
        
        void i2c_init(void);
        unsigned char check_status(unsigned char status);
        unsigned char i2c_write(unsigned char data,unsigned char status);
        unsigned char i2c_read(void);
        unsigned char send_byte(unsigned char data,unsigned char address);
        unsigned char read_byte(unsigned char address);
        void i2c_delay(void);
        
        void ser_init(void);
        void tx(unsigned char c);
        unsigned char rx(void);
        void tx_string(unsigned char *s);
        
        int main(void)
        {
        ser_init();
        tx_string("I2C INTERFACINGnr");
        send_byte('w',0x00);
        delay;
        tx_string("I2C Writenr");
        delay;
        tx(read_byte(0x01));
        while(1);
        }
        
        void ser_init()
        {
        VPBDIV=0x02;
        PINSEL0|=0x05;
        U0LCR=0x83;
        U0DLL=195;
        U0DLM=0;
        U0LCR=0x03;
        U0TER=(1<<7);
        
        }
        
        void tx(unsigned char c)
        {
        U0THR=c;
        while((U0LSR&(1<<5))==0);
        }
        
        void tx_string(unsigned char *s)
        {
        while(*s)
        {
        tx(*s++);
        }
        }
        
        unsigned char rx()
        {
        while((U0LSR&(1<<0))==0);
        return U0RBR;
        }
        
        void i2c_init(void)
        {
        PCONP |= 0x00000080; // Power on I2C0 peripheral
        PINSEL0 |= 0x50; /*Initialize Pin Connect Block P0.2 as SCL0 P0.3 as SDA0*/
        I2C0CONCLR = 0x6C; /*Clear AA, I2C Interrupt Flag, Start Bit*/
        I2C0CONSET = 0x40; /*Enable I2C0*/
        /* I2C0SCLH = 75;
        I2C0SCLL = 75; */
        I2C0SCLH = 14745600/(2*50000);
        I2C0SCLL = 14745600/(2*50000);
        }
        
        
        
        unsigned char check_status(unsigned char status)
        {
        unsigned long int time=0;
        while(time<300000)
        {
        if(I2C0CONSET & 1<<3)
        {
        if (I2C0STAT == status)
        {
        return TRUE;
        }
        }
        time++;
        }
        return FALSE;
        }
        
        unsigned char i2c_write(unsigned char data,unsigned char status)
        {
        I2C0DAT = data & 0xFF;;
        I2C0CONCLR = 0X2C;
        if(!check_status(0x08)) /*Wait for the Status Set*/
        return FALSE;
        else
        return TRUE;
        }
        
        unsigned char i2c_read(void)
        {
        I2C0CONCLR = 0x2C; // clear all except I2EN 
        if(!check_status(0x58)) /* Wait for Status Set – 0x58 */
        return FALSE;
        i2c_delay();
        return (char)I2C0DAT;
        }
        
        unsigned char send_byte(unsigned char data,unsigned char address)
        {
        //


    START


        
        I2C0CONSET = 0x20; /*Set the Start Bit*/
        if(!check_status(0x08)) /*Wait for the Status Set*/
        return FALSE;
        
        //


    SEND SLAVE ADDRESS


        
            i2c_delay();
        if(!i2c_write(0xA0,0x18))
        return FALSE;
        
        //


    SEND ADDRESS


        
        i2c_delay();
        if(!i2c_write(address,0x28))
        return FALSE;
       
        //


    SEND DATA


        
        i2c_delay();
        if(!i2c_write(data,0x28))
        return FALSE;
        
        //


    STOP


        
        i2c_delay();
        I2C0CONSET = 0x10; // generate stop condition
        I2C0CONCLR = 0x2C;
        
        return TRUE;
        }
        
        unsigned char read_byte(unsigned char address)
        {
        unsigned char read;
        i2c_init();
        //


    START


        
        I2C0CONSET = 0x20; /*Set the Start Bit*/
        if(!check_status(0x08)) /*Wait for the Status Set*/
        return FALSE;
        
        //


    SEND SLAVE ADDRESS


        
            i2c_delay();
        if(!i2c_write(0xA0,0x18))
        return FALSE;
       
        //


    SEND ADDRESS


        
        i2c_delay();
        if(!i2c_write(address,0x28))
        return FALSE;
        
        I2C0CONCLR = 0x08; // clear SI flag
        I2C0CONSET = 0x10; // generate stop condition
        
        //


    START


        
        I2C0CONSET = 0x20; /*Set the Start Bit*/
        if(!check_status(0x08)) /*Wait for the Status Set*/
        return FALSE;
        
        //


    SEND READ ADDRESS


        
        I2C0DAT = 0xA1;
        I2C0CONCLR = 0x28; // clear all except I2EN and AA
        if(!check_status(0x40)) /*Wait for the Status Set*/
        return FALSE;
        
        //


    READ


        
        read = i2c_read();
        //


    STOP


        
        i2c_delay();
        I2C0CONSET = 0x10; // generate stop condition
        I2C0CONCLR = 0x2C;
        
        return read;
        }
        
        
        void i2c_delay()
        {
        unsigned long int i2c_del;
        for(i2c_del=0;i2c_del<=100000;i2c_del++);
        }
     
     
    This is my I2C code with LPC2148. But these codes are not working. Please help me in this problem. Thanks in advance
     
    June 27, 2016 at 11:07 am #14020
    Prabakaran P M
    Participant

    Refer the Library for I2C in NXP website 

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • 100uF bypass Caps? May 19, 2025
  • how to work on pcbs that are thick May 19, 2025
  • Fuel Auto Shutoff May 18, 2025
  • Actin group needed for effective PCB software tutorials May 18, 2025
  • compatible eth ports for laptop May 18, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise