-
Notifications
You must be signed in to change notification settings - Fork 0
/
M24LC64F.c
197 lines (148 loc) · 6.18 KB
/
M24LC64F.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "M24LC64F.h"
#include "libpic30.h"
#include "stepper.h"
void ByteWriteI2C(unsigned char HighAdd, unsigned char LowAdd, unsigned char data, unsigned char address);
unsigned char ByteReadI2C(unsigned char HighAdd, unsigned char LowAdd, unsigned char address);
void M24LC64FWriteWord(unsigned int register_location, unsigned int data, unsigned char address) {
unsigned int temp;
unsigned char adr_high_byte;
unsigned char adr_low_byte;
unsigned char data_low_byte;
unsigned char data_high_byte;
if (register_location <= M24LC64F_MAX_16BIT_REGISTERS) {
// The register_location is in "word" locations and if greater than M24LC64F_MAX_16BIT_REGISTERS it will extended beyond the device
data_high_byte = (data >> 8);
data_low_byte = (data & 0x00FF);
temp = (register_location*2);
adr_high_byte = (temp >> 8);
adr_low_byte = (temp & 0x00FF);
ByteWriteI2C(adr_high_byte, adr_low_byte, data_high_byte, address);
temp = (register_location*2+1);
adr_high_byte = (temp >> 8);
adr_low_byte = (temp & 0x00FF);
ByteWriteI2C(adr_high_byte, adr_low_byte, data_low_byte, address);
}
}
unsigned int M24LC64FReadWord(unsigned int register_location, unsigned char address) {
unsigned int temp;
unsigned char adr_high_byte;
unsigned char adr_low_byte;
unsigned char data_low_byte;
unsigned char data_high_byte;
if (register_location <= M24LC64F_MAX_16BIT_REGISTERS) {
// The register_location is in "word" locations and if greater than M24LC64F_MAX_16BIT_REGISTERS it will extended beyond the device
temp = (register_location*2);
adr_high_byte = (temp >> 8);
adr_low_byte = (temp & 0x00FF);
data_high_byte = ByteReadI2C(adr_high_byte, adr_low_byte, address);
temp = (register_location*2+1);
adr_high_byte = (temp >> 8);
adr_low_byte = (temp & 0x00FF);
data_low_byte = ByteReadI2C(adr_high_byte, adr_low_byte, address);
} else {
data_low_byte = 0;
data_high_byte = 0;
}
temp = data_high_byte*256 + data_low_byte;
return temp;
}
void ByteWriteI2C(unsigned char HighAdd, unsigned char LowAdd, unsigned char data, unsigned char address) {
unsigned char eeprom_busy;
TMR3 = 0;
T3CONbits.TON = 1;
eeprom_busy = 1;
while (I2CSTATbits.TRSTAT); //Wait for bus Idle
I2CCONbits.SEN = 1; //Generate Start COndition
while (I2CCONbits.SEN); //Wait for Start COndition
TMR3 = 0;
I2CTRN = (address | M24LC64F_WRITE_CONTROL_BIT); //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CTRN = HighAdd; //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CTRN = LowAdd; //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CTRN = data; //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CCONbits.PEN = 1; //Generate Stop Condition
while (I2CCONbits.PEN); //Wait for Stop Condition
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Wait for bus Idle
TMR3 = 0;
T3CONbits.TON = 0;
__delay32(EEPROM_DELAY);
T3CONbits.TON = 1;
while(eeprom_busy) {
I2CCONbits.SEN = 1; //Generate Start COndition
TMR3 = 0;
while (I2CCONbits.SEN); //Wait for Start COndition
TMR3 = 0;
I2CTRN = (address | M24LC64F_WRITE_CONTROL_BIT); //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
if (!I2CSTATbits.ACKSTAT) { //ACK means that the write process is complete
eeprom_busy = 0;
I2CCONbits.PEN = 1; //Generate Stop Condition
while (I2CCONbits.PEN); //Wait for Stop Condition
TMR3 = 0;
T3CONbits.TON = 0;
}
}
}
unsigned char ByteReadI2C(unsigned char HighAdd, unsigned char LowAdd, unsigned char address) {
unsigned char data;
TMR3 = 0;
T3CONbits.TON = 1;
while (I2CSTATbits.TRSTAT); //Wait for bus Idle
TMR3 = 0;
I2CCONbits.SEN = 1; //Generate Start COndition
while (I2CCONbits.SEN); //Wait for Start COndition
TMR3 = 0;
I2CTRN = (address | M24LC64F_WRITE_CONTROL_BIT); //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CTRN = HighAdd; //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CTRN = LowAdd; //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Cleared at end of Slave ACK
TMR3 = 0;
I2CCONbits.RSEN = 1; //Generate Re-Start Condition
while (I2CCONbits.RSEN); //Wait for Re-Start Condition
TMR3 = 0;
I2CTRN = (address | M24LC64F_READ_CONTROL_BIT); //Load byte to I2C Transmit buffer
while (!I2CSTATbits.TRSTAT); //Set when transmit process starts
TMR3 = 0;
while (I2CSTATbits.TRSTAT); //Wait for bus Idle
TMR3 = 0;
I2CCONbits.RCEN = 1; //Start Master receive
while(I2CCONbits.RCEN);
TMR3 = 0;
I2CCONbits.PEN = 1; //Generate Stop Condition
while (I2CCONbits.PEN); //Wait for Stop Condition
TMR3 = 0;
while(!I2CSTATbits.RBF); //Wait for receive bufer to be full
TMR3 = 0;
data = I2CRCV;
T3CONbits.TON = 0;;
return data;
}