Miscellaneous › Others › Problem on LPC2148 I2C
- This topic has 1 reply, 2 voices, and was last updated 8 years, 3 months ago by Prabakaran P M.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
June 13, 2016 at 11:54 am #4486RajavarmanParticipant**
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 settingsI2C0CONSET=1<<6; //I2EN=1I2C0CONSET=1<<5; //Startwhile(I2C0STAT != 0x08);I2C0CONCLR=(1<<5)|(1<<3); //Clear Start and InterruptI2C0DAT = 0xA0;I2C0CONSET=1<<2;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x18);I2C0DAT = addH;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x28);I2C0DAT = addL;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x28);I2C0DAT = data;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x28);I2C0CONCLR=(1<<3); //Clear InterruptI2C0CONSET=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 settingsI2C0CONSET=1<<6; //I2EN=1I2C0CONSET=1<<5; //Startwhile(I2C0STAT != 0x08);I2C0CONCLR=(1<<5)|(1<<3); //Clear Start and InterruptI2C0ADR = 0xA0;I2C0DAT = 0xA0;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x18);I2C0DAT = addH;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x28);I2C0DAT = addL;I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x28);I2C0CONSET=(1<<2)|(1<<5); //StartI2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x10);I2C0ADR = 0xA1;I2C0DAT = 0xA1;I2C0CONCLR=(1<<3) | (1<<5); //Clear Interrupt , Startwhile(I2C0STAT != 0x40);I2C0CONCLR=(1<<3); //Clear Interruptwhile(I2C0STAT != 0x50);read=I2C0DAT;I2C0CONCLR=(1<<3); //Clear InterruptI2C0CONCLR=(1<<3); //Clear InterruptI2C0CONSET=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 1unsigned 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 peripheralPINSEL0 |= 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;elsereturn TRUE;}unsigned char i2c_read(void){I2C0CONCLR = 0x2C; // clear all except I2ENif(!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 conditionI2C0CONCLR = 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 flagI2C0CONSET = 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 AAif(!check_status(0x40)) /*Wait for the Status Set*/return FALSE;//
READ
read = i2c_read();//
STOP
i2c_delay();I2C0CONSET = 0x10; // generate stop conditionI2C0CONCLR = 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 advanceJune 27, 2016 at 11:07 am #14020Prabakaran P MParticipantRefer the Library for I2C in NXP website
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.