-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C.H
112 lines (88 loc) · 2.76 KB
/
I2C.H
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
void i2c_delay(unsigned int val) //DELAY FUNCTION FOR I2C
{
unsigned int i2c_i,i2c_j;
for(i2c_i=0;i2c_i<val;i2c_i++)
for(i2c_j=0;i2c_j<1000;i2c_j++);
}
/*void delay(unsigned long int count1)
{
while(count1 > 0) {count1--;} // Loop Decrease Counter
}*/
void i2c_lpc_init(int Mode) //I2C INITILIZATION
{
PINSEL0 |=0x50;
IODIR0 = 0x0C; /* set port 0.2 and port 0.3 to output, high */
IOSET0 = 0x0C;
if(Mode == '1') //200
{
//--- I2C Timing for 58 MHz (t = 16.954 ns) ---
I2C0SCLH = 75; //-- more then 0.6 us - 0.8
I2C0SCLL = 75; //-- more then 1.3 us - 1.4
}
else //Slow
{
I2C0SCLH = 150;//SCHL*4;
I2C0SCLL = 150;//SCHL*4;
}
I2C0CONCLR = 0xFF; //-- Clear all flags
I2C0CONSET = 0x40; //-- Set Master Mode --- Enable I2C
}
//---------------------------------------------------------------------------
static int i2c_lpc_ctrl(int ctrl)
{
//-- Set START
I2C0CONCLR = 0xFF; // Clear all bits
I2C0CONSET |= (1<<6)| (1<<5);
while(!(I2C0CONSET & (1<<3))); //--- End START
//-- Set ADDRESS
I2C0DAT = ctrl;
I2C0CONCLR = (1<<5) | (1<<3); //-- Clear START & SI
while(!(I2C0CONSET & (1<<3))); //-- End CTRL
return 0;
}
//---------------------------------------------------------------------------
static void i2c_lpc_wr_byte(int byte)
{
I2C0DAT = byte;
I2C0CONCLR = (1<<3); //-- Clear SI
while(!(I2C0CONSET & (1<<3))); //-- End wr POINT
}
//---------------------------------------------------------------------------
static void i2c_lpc_stop()
{
//-- Set STOP condition
I2C0CONCLR = (1<<3); //-- Clear SI
I2C0CONSET |= (1<<2) | (1<<4); //-- Clear NO ASK
}
//----------------------------------------------------------------------------
static int eeprom_write(unsigned char add, unsigned char dat) //-- Bytes to write qty
{
unsigned char rc;
//--- wr START + CONTROL
rc=i2c_lpc_ctrl(0xa0); //-- Now WR (RD/WI = 0)
//--- wr ADDRESS
i2c_lpc_wr_byte(add);
//--- Write data
i2c_lpc_wr_byte(dat);
delay(100000);
//-----------------------
i2c_lpc_stop();
//i2c_lpc_ask_polling_op(0xa0); //-- wait until write finished
i2c_delay(100);
i2c_lpc_stop();
return 0;
}
//----------------------------------------------------------------------------
unsigned char eeprom_read(unsigned char add) //-- Bytes to read qty
{
unsigned char val;
i2c_lpc_ctrl(0xa0); //-- Now WR (RD/WI = 0)
i2c_lpc_wr_byte(add);
i2c_lpc_ctrl(0xa1); //-- Now RD (RD/WI = 1)
I2C0CONCLR = (1<<3);
I2C0CONSET |= (1<<2);
while(!(I2C0CONSET & (1<<3))); //-- End Data from slave;
val = (unsigned char)I2C0DAT;
i2c_lpc_stop(); //---- Set STOP ---
return(val);
}