📄 upsd_i2c.lst
字号:
202 1 if (prescaler >= 4) prescaler = (prescaler-4)+0x80; // fix bit positions for S2CON
203 1 S2CON|=prescaler;
204 1
205 1 P3SFS |= 0xC0; // Enable P3.7 for SCL, P3.6 for SDA
206 1
207 1 // S2SETUP - sets the START/STOP hold detection time in slave mode for noise filtering.
208 1 S2SETUP = 0x81; // 0x81 is a minimum value that should work for I2C bus clocks
209 1 // up to 833 KHz and system clocks from 40 MHz down to ~ 8 MHz.
210 1
211 1 // See the data sheet for details.
212 1
213 1 // This value should be set appropriately based on the system clock
214 1 // as well as the maximum I2C bus clock to be supported by the
215 1 // system. The amount of noise in the system should be considered
216 1 // when determining this setting. The more noise, the longer the
217 1 // hold time should be.
218 1
219 1 // Important Note: If the hold time is too long, then valid START/STOP
220 1 // bits will be rejected. The shorter the hold time, the less
221 1 // immune the I2C port is to noise. S2SETUP should be adjusted
222 1 // appropriately to the system environment.
223 1
224 1 IPA |= 0x02; // set high priority for EI2C
225 1 IEA |= 0x02; // set EI2C I2C Int. Enable bit
226 1
227 1 S2ADR = Slave_Addr; // set up i2c address
228 1
229 1 i2c_processing = 0; // indicate not processing I2C transaction
230 1
231 1
232 1 S2CON |= ENI; // Set ENI (Enable I2C-2)
233 1
234 1 S2CON &= ~AA; // disable Acknowledge
235 1 return(0);
236 1 }
237
238 /*-------------------------------------------------------------------------
239 unsigned char upsd_i2c_Master_Xmit (unsigned char Slave_Addr,
240 unsigned char* Data_Ptr,
241 unsigned char N)
C51 COMPILER V7.20 UPSD_I2C 07/21/2004 16:31:42 PAGE 5
242
243 This function is used to transmit special length of data to Slave, only for
244 I2C master.
245
246 Slave_Addr - unsigned char
247 - Slave address of I2C device.
248 Data_Ptr - unsigned char*
249 - address pointer of transimt buffer
250 N - unsigned char
251 - the length of data buffer to be transmited.
252
253 return value:
254
255 I2C_MX_END 4 //Indicate a transmitting has been finished in master model
256 I2C_TIME_OUT 9 //Indicate I2C overtime.
257 I2C_NACK 13 //Indicate I2C no acknowledge
258 I2C_BUSLOST 14 //Indicate I2C bus lost
259 I2C_BUSY 16 //Indicate I2C bus is busy
260 -------------------------------------------------------------------------*/
261 unsigned char upsd_i2c_Master_Xmit (unsigned char Slave_Addr,
262 unsigned char* Data_Ptr,
263 unsigned char N){
264 1 EA=0;
265 1 i2c_master = 1; // set up for master
266 1 i2c_xmit_buf=Data_Ptr;
267 1 if(upsd_i2c_Busycheck(1000)==1){
268 2 S2CON&=~STA;
269 2 S2CON|=STO;
270 2 S2DAT=dummy;
271 2 i2c_state=I2C_BUSY;
272 2 return (i2c_state);
273 2 } // Bus busy and return current state of I2C
274 1 i2c_data_len=N; // Initialize i2c_data_len to specify communicated data length
275 1 i2c_master = 1;
276 1 i2c_xmitr = 1; // set up for master transmitter
277 1 S2DAT = Slave_Addr; // set up i2c address
278 1 S2CON |= ENI; // Set ENI (Enable I2C-2)
279 1 S2CON &= ~STO; // Clr STO in S1CON
280 1 S2CON |= STA; // Set STA (Send start bit)
281 1 S2CON &= ~AA; // Clr AA in S2CON, because acknowledge should be provided by receiver
282 1 i2c_data_index=0;
283 1 i2c_state=I2C_MX; // set up i2c current state I2C_MX.
284 1 EA=1;
285 1 if(upsd_i2c_Timeout(I2C_MX,1000)==1){
286 2 EA=0;
287 2 S2CON&=~STA;
288 2 S2CON|=STO;
289 2 S2DAT=dummy;
290 2 S2CON&=~ENI;S2CON|=ENI;
291 2 i2c_state=I2C_TIME_OUT;
292 2 return (i2c_state);
293 2 } // I2C master transmit timeout
294 1 else {
295 2 EA=0;
296 2 return(i2c_state);
297 2 } // return I2C current state
298 1 }
299 /*-------------------------------------------------------------------------
300 unsigned char upsd_i2c_Master_Recv (unsigned char Slave_Addr,
301 unsigned char* Data_Ptr,
302 unsigned char N)
303
C51 COMPILER V7.20 UPSD_I2C 07/21/2004 16:31:42 PAGE 6
304 This function is used to receive special length of data from Slave, only for
305 I2C Master.
306
307 Slave_Addr - unsigned char
308 - Slave address of I2C device.
309 Data_Ptr - unsigned char*
310 - address pointer of transimt buffer
311 N - unsigned char
312 - the length of data buffer to be transmited.
313
314 return value:
315
316 I2C_MR_END 8 //Indicate a receiving has been finished in master model
317 I2C_TIME_OUT 9 //Indicate I2C overtime.
318 I2C_NACK 13 //Indicate I2C no acknowledge
319 I2C_BUSLOST 14 //Indicate I2C bus lost
320 I2C_BUSY 16 //Indicate I2C bus is busy
321 -------------------------------------------------------------------------*/
322 unsigned char upsd_i2c_Master_Recv (unsigned char Slave_Addr,
323 unsigned char* Data_Ptr,
324 unsigned char N){
325 1 EA=0;
326 1 i2c_master = 1; // set up for master
327 1 i2c_rcv_buf= Data_Ptr;
328 1 if(upsd_i2c_Busycheck(1000)==1){
329 2 S2CON&=~STA;
330 2 S2CON|=STO;
331 2 dummybyte=S2DAT;
332 2 i2c_state=I2C_BUSY;
333 2 return (i2c_state);
334 2 } // Bus busy and return current state of I2C
335 1 i2c_data_len=N; // Initialize i2c_data_len to specify communicated data length
336 1 i2c_master = 1;
337 1 i2c_xmitr = 0; // set flags for master receiver
338 1
339 1 S2DAT = (Slave_Addr | 0x01); // set up i2c address (set R/W bit)
340 1 S2CON |= ENI; // Set ENI (Enable I2C-2)
341 1 S2CON &= ~STO; // Clr STO in S1CON
342 1 S2CON |= STA; // Set STA (Send start bit)
343 1 S2CON &= ~AA; // Clr AA in S2CON
344 1 i2c_data_index=0;
345 1 i2c_state=I2C_MR;
346 1 EA=1;
347 1 if(upsd_i2c_Timeout(I2C_MR,1000)==1){
348 2 EA=0;
349 2 S2CON&=~STA;
350 2 S2CON|=STO;
351 2 dummybyte=S2DAT;
352 2 S2CON&=~ENI;S2CON|=ENI;
353 2 i2c_state=I2C_TIME_OUT;
354 2 return (i2c_state);
355 2 } // I2C master receive timeout
356 1 else {
357 2 EA=0;
358 2 return(i2c_state);
359 2 } // return I2C current state
360 1 }
361
362 /*-------------------------------------------------------------------------
363 unsigned char upsd_i2c_Slave_Xmit(unsigned char* Data_Ptr,
364 unsigned char* N_Ptr)
365
C51 COMPILER V7.20 UPSD_I2C 07/21/2004 16:31:42 PAGE 7
366 This function is used to transmit data to master, only for Slave.
367
368 Data_Ptr - unsigned char*
369 - address pointer of transmit buffer
370 N_Ptr - unsigned char*
371 - the length of data buffer transmitted to master.
372
373 return value:
374
375 I2C_SX_END 6 //Indicate a transmitting has been finished in Slave model
376 I2C_TIME_OUT 9 //Indicate I2C overtime.
377 I2C_NACK 13 //Indicate I2C no acknowledge
378 I2C_BUSLOST 14 //Indicate I2C bus lost
379 I2C_BUSY 16 //Indicate I2C bus is busy
380 -------------------------------------------------------------------------*/
381 unsigned char upsd_i2c_Slave_Xmit(unsigned char* Data_Ptr,
382 unsigned char* N_Ptr)
383 {
384 1 EA=0;
385 1 i2c_xmit_buf=Data_Ptr;
386 1 i2c_master = 0;
387 1 i2c_xmitr = 1; // set up for Slave transmitter
388 1 S2CON &= ~AA; // disable ACK and STOP control bit to prepare transmit data
389 1 S2CON &= ~STO;
390 1 i2c_data_index=0;
391 1 S2DAT = i2c_xmit_buf[i2c_data_index]; //Send first data to master
392 1 i2c_data_index++;
393 1 i2c_state=I2C_SX;
394 1 EA=1;
395 1 if(upsd_i2c_Timeout(I2C_SX,1000)==1){
396 2 EA=0;
397 2 i2c_state=I2C_TIME_OUT;
398 2 S2DAT=dummy;
399 2 return(i2c_state);
400 2 } // I2C Slave timeout return
401 1 else {
402 2 EA=0;
403 2 *N_Ptr=i2c_data_index;
404 2 S2DAT=dummy;
405 2 return(i2c_state);
406 2 } // return current I2C state and the length of data
407 1 // buffer transmitted to master.
408 1 }
409 /*-------------------------------------------------------------------------
410 unsigned char upsd_i2c_Slave_Recv(unsigned char* Data_Ptr,
411 unsigned char* N_Ptr)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -