📄 i2c.lst
字号:
C51 COMPILER V7.02b I2C 05/24/2006 17:59:20 PAGE 4
179 I2L_SDQ = i2ldata & 0x01; //move out the LSB
180 I2L_SCL = 0;
181 i2ldata >>= 1; //shift bit to LSB
182 I2L_SCL = 1;
183 }
184
185 I2L_SCS = 1; //Stop the I2L all signal will set to HI
186 I2L_SDQ = 1;
187 }
188 */
189 /**--------------------------------------------------------------------------
190 * Name BYTE I2L_Read(BYTE addr);
191 *
192 * Description I2L read a byte data from 15xx
193 *
194 * Flow Chart
195 *
196 * Return None
197 *
198 * DATE Author Description
199 * ===========================================================================
200 * 2004-07-09 K.M. Ho This is first time implement
201 **/
202 /*
203 BYTE I2L_Read(BYTE addr)
204 {
205 BYTE i, i2ldata, mask_bit;
206
207 I2L_SCS = 1; //Init
208 I2L_SCL = 1;
209 I2L_SDQ = 1;
210
211 I2L_SCS = 0; //Serial Interface Chip Select
212
213 I2L_SDQ = 1; //Read command
214
215 I2L_SCL = 0;
216 DELAY(1);
217 I2L_SCL = 1; //rising edge latch data
218
219 for (i=0; i<8; i++) //move out 8 bit addr to 15xx from LSB
220 {
221 I2L_SDQ = addr & 0x01; //move out the LSB
222 I2L_SCL = 0;
223 addr >>= 1; //shift bit to LSB
224 I2L_SCL = 1;
225 }
226 I2L_SDQ=1; //tai data bit to Hi
227
228 i2ldata = 0; //clean the reading buffer
229 mask_bit = 0x01; //init set mask bit from 0x01
230 I2L_SCL = 0; // falling edge chage data
231
232 for (i=0; i<8; i++) //move in 8 bit data from 15xx
233 {
234 I2L_SCL = 1; // sample data on rising edge
235 if (I2L_SDQ) i2ldata |= mask_bit;
236 mask_bit <<= 1;
237 I2L_SCL = 0; // falling edge chage data
238 }
239
240 I2L_SCL = 1; //Stop the I2L all signal will set to HI
C51 COMPILER V7.02b I2C 05/24/2006 17:59:20 PAGE 5
241 I2L_SCS = 1;
242 I2L_SDQ = 1;
243 return (i2ldata); //return the data byte
244 }
245 */
246
247 /**--------------------------------------------------------------------------
248 * Name void I2C_Write(BYTE dev_addr, BYTE start_addr,
249 * BYTE count , BYTE *write_buf)
250 *
251 * Description WRITE Control register BY I2C
252 *
253 * Flow Chart 1.Send Module Write ID address
254 * 2.Send Subaddress
255 * 3.Send Data(n byte)
256 *
257 * Return
258 *
259 * DATE Author Description
260 * ===========================================================================
261 **/
262 void I2C_Write(BYTE dev_addr, BYTE start_addr, BYTE count , BYTE *write_buf)
263 {
264 1 BYTE i;
265 1
266 1 I2C_SendByteWithStart(dev_addr); //Write W_ID
267 1 I2C_SendByte(start_addr); //Write Address
268 1 DELAY(1);
269 1 for(i=0;i<count;i++)
270 1 {
271 2 I2C_SendByte(*(write_buf+i)); //Write multi-data
272 2 DELAY(1);
273 2 }
274 1 I2C_SendStop(); //Stop
275 1 DELAY(1);
276 1 I2C_ReleaseBus();
277 1 }
278 void I2C_WriteByte(BYTE dev_addr, BYTE start_addr, BYTE write_byte)
279 {
280 1 I2C_SendByteWithStart(dev_addr); //Write W_ID
281 1 I2C_SendByte(start_addr); //Write Address
282 1 DELAY(1);
283 1
284 1 I2C_SendByte(write_byte);
285 1 DELAY(1);
286 1
287 1 I2C_SendStop(); //Stop
288 1 DELAY(1);
289 1 I2C_ReleaseBus();
290 1 }
291
292 /**--------------------------------------------------------------------------
293 * Name void I2C_Read(BYTE dev_addr, BYTE start_addr,
294 * BYTE count, BYTE *DATA_BUF )
295 *
296 * Description READ Control register BY I2C
297 *
298 * Flow Chart 1.Send Module Write ID address
299 * 2.Send Subaddress
300 * 3.Send Module Read ID address
301 * 4.Read Data(n byte)
302 *
C51 COMPILER V7.02b I2C 05/24/2006 17:59:20 PAGE 6
303 * Return
304 *
305 * DATE Author Description
306 * ===========================================================================
307 **/
308 void I2C_Read(BYTE dev_addr, BYTE start_addr, BYTE count, BYTE *DATA_BUF)
309 {
310 1 BYTE i;
311 1
312 1 I2C_SendByteWithStart(dev_addr); //Write W_ID
313 1 I2C_SendByte(start_addr); //Write Address
314 1 DELAY(1);
315 1 I2C_SendByteWithStart(dev_addr|0x01); //Write R_ID
316 1 DELAY(1);
317 1 for(i=0;i<count-1;i++)
318 1 {
319 2 *(DATA_BUF+i)=I2C_GetByte(I2C_ACK); //Read data from device(burst mode)
320 2 DELAY(1); //After every data need to send ACK,
321 2 // if the reading is not stop
322 2 }
323 1 *(DATA_BUF+i) = I2C_GetByte(I2C_NACK); //Read the last data then send NACK to STOP
324 1 DELAY(1);
325 1
326 1 I2C_SendStop(); //Stop
327 1 DELAY(1);
328 1 I2C_ReleaseBus();
329 1 }
330
331 BYTE I2C_ReadByte(BYTE dev_addr, BYTE start_addr)
332 {
333 1 BYTE rd_byte;
334 1
335 1 I2C_SendByteWithStart(dev_addr); //Write W_ID
336 1 I2C_SendByte(start_addr); //Write Address
337 1 DELAY(1);
338 1 I2C_SendByteWithStart(dev_addr|0x01); //Write R_ID
339 1 DELAY(1);
340 1
341 1 rd_byte = I2C_GetByte(I2C_NACK); //Read the last data then send NACK to STOP
342 1 DELAY(1);
343 1
344 1 I2C_SendStop(); //Stop
345 1 DELAY(1);
346 1 I2C_ReleaseBus();
347 1
348 1 return(rd_byte);
349 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 576 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 1 24
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 3
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -