embpic
Advanced Member level 3
- Joined
- May 29, 2013
- Messages
- 742
- Helped
- 80
- Reputation
- 160
- Reaction score
- 77
- Trophy points
- 1,308
- Location
- india
- Activity points
- 5,213
void init_i2c(void);
void i2c_start(void);
void i2c_stop(void);
void i2c_tc(void)__irq;
unsigned char read_d[6]={0},a;
unsigned char status;
void init_i2c(void)
{
I2C0CONCLR=0x6C;
I2C0CONSET = 0x40;
I2C0CONCLR = 0x00;
I2C0SCLH=80;
I2C0SCLL=70;
VICIntEnable |= 0x00000200;
VICIntSelect |= 0x00000000;
VICVectCntl0 = 0x00000029;
VICVectAddr0 = (unsigned long)i2c_tc;
PINSEL0&=0xFFFFFF0F;
PINSEL0|=0x00000050;
}
void i2c_start(void)
{
I2C0CONSET = 0x20;
}
void i2c_stop(void)
{
I2C0CONSET = 0x10;
}
void i2c_tc()__irq
{
status = I2C0STAT;
I2C0CONCLR = 0x28;
VICVectAddr = 0xff;
}
void write_eeprom(void)
{
i2c_start();
while(!(status & 0x08));
I2C0DAT = 0xA0;
while(!(status & 0x18));
I2C0DAT = 0x00;
while(!(status & 0x28));
I2C0DAT = 0x00;
while(!(status & 0x28));
I2C0DAT = 'A';
while(!(status & 0x28));
I2C0DAT = 'B';
while(!(status & 0x28));
I2C0DAT = 'C';
while(!(status & 0x28));
I2C0DAT = 'D';
while(!(status & 0x28));
I2C0DAT = 'E';
while(!(status & 0x28));
i2c_stop();
}
void read_eeprom(void)
{
i2c_start();
while(!(status & 0x08));
I2C0DAT = 0xA0;
while(!(status & 0x18));
I2C0DAT = 0x00;
while(!(status & 0x28));
I2C0DAT = 0x00;
while(!(status & 0x28));
i2c_start();
while(!(status & 0x10));
I2C0DAT = 0xA1;
while(!(status & 0x40));
// I2C0CONCLR=0x04; //set hardware to send nack
for(a=0;a<4;a++)
{
read_d[a] = I2C0DAT;
I2C0CONSET=0x04; //set hardware to send ack
while(!(status & 0x50));
}
read_d[4] = I2C0DAT;
I2C0CONCLR=0x04; //set hardware to send nack
while(!(status & 0x58));
i2c_stop();
}
unsigned char status = 0;
Code:void write_eeprom(void) { i2c_start(); while(!(status & 0x08)); //stage 1 I2C0DAT = 0xA0; while(!(status & 0x18)); //stage 2 I2C0DAT = 0x00; while(!(status & 0x28)); //stage 3 I2C0DAT = 0x00; while(!(status & 0x28)); //stage 4 . . . // so on }
while(status != 0x08); //stage 1
status = 0;
I2C0DAT = 0xA0;
while(status != 0x08); //stage 1
I2C0DAT = 0xA0;
while(status != 0x18); //stage 2
I2C0DAT = 0x00;
while(status != 0x28); //stage 3
Hai,
i think u got output for I2C protocol in lpc2148. now i am facing problem in that coding . Can please send me the executing code with project. it will be very useful for me.
thanks in advance
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 unsigned char status=0; void i2c0_stat(void) __irq { status=I2C0STAT; I2C0CONCLR=0x28; VICVectAddr = 0x00; } void i2c0_init() { PINSEL0&=0xFFFFFF0F; PINSEL0|=0x00000050; I2C0CONCLR=0x6C; I2C0CONSET=0x40; I2C0SCLH=80; I2C0SCLL=70; VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored) VICVectCntl2 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot VICVectAddr2 = (unsigned long)i2c0_stat; // Storing vector address of I2C0 VICIntEnable = (1<<9); } void i2c0_start() //used //to send start condition. { I2C0CONSET=0x20; } void i2c0_stop() //used //to send stop condition. { I2C0CONSET=0x10; } void send_i2c0_byte(void) { i2c0_start(); while(status != 0x08); I2C0DAT = 0xA0; while(status != 0x18); I2C0DAT = 0x00; while(status != 0x28); I2C0DAT = 0x00; while(status != 0x28); I2C0DAT = 12; while(status != 0x28); i2c0_stop(); } unsigned char read_i2c0_byte(void) { unsigned char temp; i2c0_start(); while(status != 0x08); I2C0DAT = 0xA0; while(status != 0x18); I2C0DAT = 0x00; while(status != 0x28); I2C0DAT = 0x00; while(status != 0x28); i2c0_start(); while(status != 0x10); /**********HERE I get status as 0x30,..and program stucks here***********/ I2C0DAT = 0xA1; while(status != 0x40); temp = I2C0DAT; I2C0CONCLR=0x04; //set hardware to send nack while(status != 0x58); i2c0_stop(); return temp; }
#include <lpc214x.h> //Includes LPC2148 register definitions
#define MAX_BUFFER_SIZE 16
#define DEVICE_ADDR 0xA0
#define BLK_0 0x00
#define BLK_1 0x02
#define MAX_BLOCK_SIZE 256
#define LED1_ON() IO1SET=(1<<16) //I2C write indicator
#define LED2_ON() IO1SET=(1<<17) //I2C read indicator
#define LED3_ON() IO1SET=(1<<18) //Comm failure indicator
#define LED4_ON() IO1SET=(1<<19) //Comm success indicator
#define LED1_OFF() IO1CLR=(1<<16)
#define LED2_OFF() IO1CLR=(1<<17)
#define LED3_OFF() IO1CLR=(1<<18)
#define LED4_OFF() IO1CLR=(1<<19)
#define Fosc 12000000
#define Fcclk (Fosc * 5)
#define Fcco (Fcclk * 4)
#define Fpclk (Fcclk / 4) * 1
#define UART_BPS 9600 //Set Baud Rate here
void Delay(unsigned char j);
void Init_UART0(void);
void UART0_SendByte(unsigned char data);
void UART0_SendStr(const unsigned char *str);
void Send_Start(void);
void Send_Stop(void);
unsigned char Send_I2C(unsigned char *Data,unsigned char Len);
unsigned char Read_I2C(unsigned char *Data,unsigned char Len);
unsigned char Page_Write(unsigned char BLOCK_NUMBER,unsigned char BLOCK_ADDR);
unsigned char Page_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR);
unsigned char I2C_Status(unsigned char status_code);
unsigned char I2C_WR_Buf[MAX_BUFFER_SIZE]={" Nex Robotics "};
unsigned char I2C_RD_Buf[MAX_BUFFER_SIZE];
unsigned char Status=0;
unsigned char Status_Flag=0;
void Delay(unsigned char j)
{
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}
void Init_UART0(void)
{
unsigned int Baud16;
U0LCR = 0x83; // DLAB = 1
Baud16 = (Fpclk / 16) / UART_BPS;
U0DLM = Baud16 / 256;
U0DLL = Baud16 % 256;
U0LCR = 0x03;
}
void UART0_SendByte(unsigned char data)
{
U0THR = data;
while( (U0LSR&0x40)==0 );
return;
}
void UART0_SendStr(const unsigned char *str) //A function to send a string on UART0
{
while(1)
{
if( *str == '\0' ) break;
UART0_SendByte(*str++);
}
}
void Send_Start()
{
I2C0CONSET=0x20;
}
void Send_Stop()
{
I2C0CONSET=0x10;
}
// This function sends sequential data to the EEPROM 24LC04
// The buffer size for EEPROM 24LC04 is 16 bytes
// The Len parameter should not exceed this value
unsigned char Send_I2C(unsigned char *Data,unsigned char Len)
{
while(Len)
{
I2C0DAT=*Data;
if(I2C_Status(0x28))
{
return 1;
}
Len--;
Data++;
}
return 0;
}
// This function reads random data from the EEPROM 24LC04
unsigned char Read_I2C(unsigned char *Data,unsigned char Len)
{
while(Len)
{
if(Len==1) //Last byte
{
I2C0CONCLR=0x04; //set hardware to send nack
if(I2C_Status(0x58)) //last byte has been received and NACK has been returned
{
return 1;
}
*Data=I2C0DAT;
}
else
{
I2C0CONSET=0x04; //set hardware to send ack
if(I2C_Status(0x50)) //Byte has been received ACK has been returned
{
return 1;
}
*Data=I2C0DAT;
}
Data++;
Len--;
}
return 0;
}
unsigned char I2C_Status(unsigned char status_code)
{
while(Status_Flag==0);
Status_Flag=0;
if(Status!=status_code)
{
return 1;
}
else
{
return 0;
}
}
unsigned char Page_Write(unsigned char BLOCK_NUMBER,unsigned char BLOCK_ADDR)
{
Send_Start();
if(I2C_Status(0x08)) //Start has been transmitted
{
return 1;
}
I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER; // Send Address
if(I2C_Status(0x18)) //Device address, block num and write has been transmitted
{
return 1;
}
I2C0DAT=BLOCK_ADDR; // Send block address
if(I2C_Status(0x28)) //Block address has been transmitted
{
return 1;
}
if(Send_I2C(I2C_WR_Buf,MAX_BUFFER_SIZE)) //Send Data
{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}
unsigned char Page_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR)
{
Send_Start();
if(I2C_Status(0x08)) //Start has been transmitted
{
return 1;
}
I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER; // Send Address
if(I2C_Status(0x18)) //Device address, block num and write has been transmitted
{
return 1;
}
I2C0DAT=BLOCK_ADDR;
if(I2C_Status(0x28)) //Block address has been transmitted
{
return 1;
}
Send_Start(); // Repeat Start
if(I2C_Status(0x10)) //Repeated Start has been transmitted
{
return 1;
}
I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER | 0x01; //Device address, block num and read has been transmitted
if(I2C_Status(0x40)) //
{
return 1;
}
if(Read_I2C(I2C_RD_Buf,MAX_BUFFER_SIZE)) //Receive 16bytes of Data from EEPROM
{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}
void __irq I2C0_Status(void)
{
Status_Flag=0xFF; //update status flag
Status=I2C0STAT; //Read Status byte
I2C0CONCLR=0x28;
VICVectAddr = 0x00; //Acknowledge Interrupt
}
void I2C_Init()
{
PINSEL0&=0xFFFFFF0F;
PINSEL0|=0x00000050;
I2C0CONCLR=0x6C;
I2C0CONSET=0x40;
I2C0SCLH=80;
I2C0SCLL=70;
/* Init VIC for I2C0 */
VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored)
VICVectCntl0 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot
VICVectAddr0 = (unsigned long)I2C0_Status; // Storing vector address of I2C0
VICIntEnable = (1<<9);
}
int main(void)
{
PINSEL0 = 0x00000005; // Enable GPIO on all pins
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P1.16, P1.17, P1.18, P1.19 as Output
LED1_OFF();LED2_OFF();LED3_OFF();LED4_OFF();
Init_UART0();
I2C_Init();
LED1_ON(); //Write Indicator
if(Page_Write(BLK_1,0x00))
{
UART0_SendStr("Write Failed");
LED3_ON();
}
LED1_OFF();
Delay(1);
LED2_ON(); //Read indicator
if(Page_Read(BLK_1,0x00))
{
UART0_SendStr("Read Failed");
LED3_ON();
}
else
{
UART0_SendStr(I2C_RD_Buf);
LED4_ON();
}
LED2_OFF();
while(1)
{
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?