1 | #include <LPC21xx.H>
|
2 | #include <stdarg.h>
|
3 |
|
4 | void I2CISR (void) __irq ;
|
5 | //I2C interrupt
|
6 | void I2CTransferByte(unsigned Addr,unsigned Data); //Background call to start master send and receive byte transfers
|
7 |
|
8 | unsigned I2CAdresse;
|
9 | unsigned I2CData;
|
10 |
|
11 |
|
12 | int main(void)
|
13 | {
|
14 |
|
15 | VICVectCntl1 = 0x00000029; //select a priority slot for a given interrupt
|
16 | VICVectAddr1 = (unsigned)I2CISR; //pass the address of the IRQ into the VIC slot
|
17 | VICIntEnable = 0x00000200; //enable interrupt
|
18 |
|
19 | PINSEL0 = 0x50; //Switch GPIO to I2C pins
|
20 |
|
21 |
|
22 | // Initial I2C Data Rate = 100KHz(VPB=29.4912MHz)
|
23 | // I2C Data Rate = 29.4912MHz / 294
|
24 | // = 100.31KHz
|
25 | I2SCLH = 0x00000093; // Clock High = 147
|
26 | I2SCLL = 0x00000093; // Clock Low = 147
|
27 |
|
28 |
|
29 |
|
30 | while(1)
|
31 | {
|
32 | //Display löschen
|
33 | I2CTransferByte(0xDE,0x11); //DC1
|
34 | I2CTransferByte(0xDE,0x03); //lengh
|
35 | I2CTransferByte(0xDE,0x1B); //ESC
|
36 | I2CTransferByte(0xDE,0x44); //D
|
37 | I2CTransferByte(0xDE,0x4C); //L
|
38 | I2CTransferByte(0xDE,0x11 + 0x03 + 0x1B+ 'D' + 'L');
|
39 | }
|
40 | }
|
41 |
|
42 |
|
43 | void I2CTransferByte(unsigned Addr,unsigned Data)
|
44 | {
|
45 | I2CAdresse = Addr; //Place address and data in Globals to be used by
|
46 | //the interrupt
|
47 | I2CData = Data;
|
48 | I2CONCLR = 0x0000006C; //Clear all I2C settings
|
49 | I2CONSET = 0x00000040; //Enable the I2C interface
|
50 | I2CONSET = 0x00000020; //Start condition
|
51 | }
|
52 |
|
53 |
|
54 | void I2CISR (void) __irq //I2C interrupt routine
|
55 | {
|
56 |
|
57 | switch (I2STAT) //Read result code and switch to next action
|
58 | {
|
59 | // Start and Send byte conditions
|
60 |
|
61 | case ( 0x08): //Start bit
|
62 | I2CONCLR = 0x20; //Clear start bit
|
63 | I2DAT = I2CAdresse; //Send address and write bit
|
64 | break;
|
65 |
|
66 | case (0x18): //Slave address+W, ACK
|
67 | I2DAT = I2CData; //Write Mem,ory start address to tx register
|
68 | break;
|
69 |
|
70 | case (0x20): //Salve address +W, Not ACK
|
71 | I2DAT = I2CAdresse; //Resend address and write bi
|
72 | break;
|
73 |
|
74 | case (0x28):
|
75 | I2CONSET = 0x10; //Stop condition
|
76 | break;
|
77 |
|
78 | case (0x30) : //Data sent, NOT Ack
|
79 | I2DAT = I2CData; //Write data to tx register
|
80 | break;
|
81 |
|
82 |
|
83 | //Receive byte conditions
|
84 |
|
85 | case (0x40) : //Slave Address +R, ACK
|
86 | I2CONSET = 0x04; //Enable ACK for data byte
|
87 | break;
|
88 |
|
89 | case (0x48) : //Slave Address +R, Not Ack
|
90 | I2CONSET = 0x20; //Resend Start condition
|
91 | break;
|
92 |
|
93 | case (0x50) : //Data Received, ACK
|
94 |
|
95 | I2CONSET = 0x10; //Stop condition
|
96 | //Signal end of I2C activity
|
97 |
|
98 | break;
|
99 |
|
100 | case (0x58): //Data Received, Not Ack
|
101 | I2CONSET = 0x20; // Resend Start condition
|
102 | break;
|
103 |
|
104 | default :
|
105 | break;
|
106 |
|
107 | }
|
108 |
|
109 | I2CONCLR = 0x08; //Clear I2C interrupt flag
|
110 | VICVectAddr = 0x00000000; //Clear interrupt in
|
111 |
|
112 | }
|