Created
September 13, 2013 17:36
-
-
Save Embedded-linux/6553722 to your computer and use it in GitHub Desktop.
I2C EEPROM for LPC2148
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I2C with segment display | |
| #include <LPC214x.h> | |
| #include <stdio.h> | |
| #define MAX 6 | |
| //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Declarations >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
| void I2C_ISR(void)__irq; | |
| void Wait (unsigned int); | |
| void I2C_Init (void); | |
| int I2C_Start (unsigned int Slave_Addr); | |
| int I2C_Write (unsigned char *Buff, unsigned int Count); | |
| unsigned char Buff[] = {0x00,0x27,0x3F,0x3F,0x3F,0x3F}; | |
| unsigned char Segment[] = {0x3F/*0*/,0x06/*1*/,0x5B/*2*/,0x4F/*3*/,0x66/*4*/, | |
| 0x6D/*5*/,0x7D/*6*/,0x07/*7*/,0x7F/*8*/,0x6F/*9*/}; | |
| unsigned char index = 0; | |
| unsigned int flag=0; | |
| unsigned char all_chars[] = "0123456789-.ACEFGHIJLPUYbcdfhinortu°"; | |
| char digit[] = { | |
| 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F, | |
| 0x77,0x7C,0x58,0x39,0x5E,0x79,0x71,0x3D,0x76,0x0E, | |
| 0x38,0x54,0x5C,0x73,0x50,0x78,0x3E,0x1C,0x6E | |
| }; | |
| #define AA 2 | |
| #define SI 3 | |
| #define STO 4 | |
| #define STA 5 | |
| #define I2EN 6 | |
| void Delay(void) | |
| { | |
| unsigned int i,j; | |
| for(i=0;i<150;i++) | |
| for(j=0;j<900;j++); | |
| } | |
| void Wait (unsigned int Delay) | |
| { | |
| while(Delay--); | |
| } | |
| void I2C_Init (void) | |
| { | |
| I2C0SCLH = 150; //50%duty cycle ..... I2C Frequency ->100 KHz for PCLK = 30MHz | |
| I2C0SCLL = 150; | |
| I2C0CONSET = 1 << I2EN; //Enable I2C 0 | |
| } | |
| int I2C_Start (unsigned int Slave_Addr) | |
| { | |
| I2C0CONCLR = 1 << STO; | |
| I2C0CONCLR = 1 << AA; | |
| I2C0CONSET = 1 << STA; | |
| return 0; | |
| } | |
| int I2C_Write (unsigned char *Buff, unsigned int Count) | |
| { | |
| while(Count--) | |
| { | |
| I2C0DAT = *Buff++; | |
| } | |
| return 0; | |
| } | |
| //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Main Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
| int main() | |
| { | |
| unsigned int i,j,k,l; // initialize the variables | |
| VPBDIV = 0x02; | |
| PINSEL0 = 0x00000055; // P0.3 - SDA0 and P0.2 - SCL0 | |
| U0LCR = 0x83; | |
| U0DLL = 97; | |
| U0DLM = 0x00; | |
| U0LCR = 0x03; | |
| VICIntSelect = 0<<9; | |
| VICVectCntl0 = 0x020 | 9 ; | |
| VICVectAddr0 = (unsigned long)I2C_ISR; | |
| VICIntEnable = 1<<9; | |
| I2C_Init(); // Initialize I2C in Interrupt mode | |
| I2C_Start (0x70); | |
| for (i=0;i<30;i++) Wait(10000); | |
| I2C0CONCLR = 1 << SI; | |
| i=0; | |
| j=0; | |
| k=0; | |
| l=0; | |
| while(1) | |
| { // display the 4 digits on 4 7-segment display in upcounter manner | |
| Delay(); | |
| Delay(); | |
| flag++; | |
| if (flag > 10) | |
| { | |
| flag = 0; | |
| i++; | |
| if (i>9) | |
| { | |
| i=0; | |
| j++; | |
| if (j>9) | |
| { | |
| j=0; | |
| k++; | |
| if (k>9) | |
| { | |
| k=0; | |
| l++; | |
| if (l>9) | |
| i=j=k=l=0; | |
| } | |
| } | |
| } | |
| } | |
| Buff [4] = Segment[i]; | |
| Buff [5] = Segment[j]; | |
| Buff [3] = Segment[k]; | |
| Buff [2] = Segment[l]; | |
| } | |
| } | |
| void I2C_ISR(void) __irq | |
| { | |
| if (I2C0CONSET & 0x08) | |
| { | |
| switch (I2C0STAT) | |
| { | |
| case (0x08) : I2C0CONCLR = 1 << STA; | |
| I2C0DAT = 0x70; //Slave Addr + W | |
| U0THR = I2C0STAT; | |
| //I2C0CONCLR = 0x08; //Clear SI | |
| break; | |
| case (0x10) : I2C0CONCLR = 1 << STA; //Clear START Bit | |
| I2C0DAT = 0x70; //This Status is recieved if "Repeated START" is txd | |
| U0THR = 'C'; | |
| break; | |
| case (0x18) : I2C0CONCLR = 0x20; //Clear START Bit | |
| I2C0DAT = Buff[index]; | |
| U0THR = 'D'; | |
| index++; | |
| break; | |
| case (0x20) : I2C0CONCLR = 0x20; //Clear START Bit | |
| I2C0DAT = Buff[index]; | |
| U0THR = 'E'; | |
| index++; | |
| break; | |
| case (0x28) : I2C0CONCLR = 0x20; //Clear START Bit | |
| if (index < MAX) | |
| { | |
| I2C0DAT = Buff[index]; | |
| U0THR = 'F'; | |
| index++; | |
| } | |
| else | |
| { | |
| index = 0; | |
| I2C0CONSET = 0x10; //Send STOP Bit | |
| U0THR = 'G'; | |
| I2C_Start(0x70); | |
| I2C0CONCLR = 1 << SI; | |
| } | |
| break; | |
| case (0x30) : I2C0CONCLR = 0x20; //Clear START Bit | |
| if (index < MAX) | |
| { | |
| I2C0DAT = Buff[index]; | |
| U0THR = 'H'; | |
| index++; | |
| } | |
| else | |
| { | |
| index = 0; | |
| I2C0CONSET = 0x10; //Send STOP Bit | |
| U0THR = 'I'; | |
| I2C_Start(0x70); | |
| } | |
| break; | |
| case (0x38) : I2C0CONSET = 0x20; | |
| break; | |
| } | |
| } | |
| I2C0CONCLR = 1 << SI; | |
| VICVectAddr = 0x00; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment