📄 iic.lst
字号:
243 2 if(SDA)
244 2 tmData |= 0x01;
245 2 SCL = 0;
246 2
247 2 _nop_ ();
248 2 _nop_ ();
249 2 _nop_ ();
250 2 _nop_ ();
251 2 _nop_ ();
252 2 }
253 1
254 1 /* SCL = 0;*/
255 1
256 1 return tmData;
257 1 }
258 /*******************************************************************************
259 * 名 称: TSL2561_write_byte()
260 * 功 能: 启动I2C操作
261 * 入口参数: 无
262 * 出口参数: 无
263 *******************************************************************************/
264 uchar TSL2561_write_byte( unsigned char addr, unsigned char dat)
265 {
266 1
267 1 uchar status=0;
268 1 I2C_Start(); //开始
269 1 status=Send_Byte_I2C(TSL2561_ADDR|TSL2561_WR);//写TSL2561地址
270 1 status=Send_Byte_I2C(0x80|addr); //写命令
271 1 // status=Send_Byte_I2C(0x01); //写Byte Count
272 1 status=Send_Byte_I2C(dat); //写数据
273 1 I2C_Stop( ); //停止
274 1 delay_1ms(2); //延时2 ms
275 1 return 0;
276 1
277 1 }
278 /*******************************************************************************
279 * 名 称: TSL2561_read_byte()
280 * 功 能: 启动I2C操作
281 * 入口参数: 无
282 * 出口参数: 无
283 *******************************************************************************/
284 uchar TSL2561_read_byte( uchar addr)
285 {
286 1
287 1 uchar status=0;
288 1
289 1 I2C_Start(); //开始
290 1 status=Send_Byte_I2C(TSL2561_ADDR|TSL2561_WR);//写TSL2561地址
291 1 status=Send_Byte_I2C(0x80|addr); //写命令
292 1
293 1 I2C_Start(); //Re开始
294 1 status=Send_Byte_I2C(TSL2561_ADDR|TSL2561_RD); //写TSL2561地址
295 1 status=Read_Byte_I2C_ACK(); //读数据
296 1 I2C_Stop( ); //停止
297 1
298 1 delay_1ms(10);
299 1
300 1 return status;
301 1
C51 COMPILER V7.50 IIC 01/01/2002 04:01:32 PAGE 6
302 1 }
303
304 /*******************************************************************************
305 * 名 称: InitTsl256x()
306 * 功 能: 启动I2C操作
307 * 入口参数: 无
308 * 出口参数: 无
309 *******************************************************************************/
310 void InitTsl256x(void)
311 {
312 1 uchar status;
313 1 status=TSL2561_write_byte(0x00,PWUP); //寄存器00H
314 1 status=TSL2561_write_byte(0x01,0x12); //寄存器00H
315 1 // status=TSL2561_write_byte(0x02,0x00); //寄存器00H
316 1 // status=TSL2561_write_byte(0x03,0x00); //寄存器00H
317 1 // status=TSL2561_write_byte(0x04,0x00); //寄存器00H
318 1 // status=TSL2561_write_byte(0x05,0x00); //寄存器00H
319 1 status=TSL2561_write_byte(0x06,0x00); //寄存器00H
320 1 /*
321 1 I2C_Start(); //开始
322 1
323 1 status=Send_Byte_I2C(TSL2561_ADDR|TSL2561_WR); //写TSL2561地址
324 1 //jumpingtest
325 1 while(!status)//test the Tsl2561 ack
326 1 LED=0;
327 1 LED=1;
328 1 status=Send_Byte_I2C(0x90|REG_CTR); //写命令
329 1 // I2C_Start(); //开始
330 1 // status=Send_Byte_I2C(TSL2561_ADDR|TSL2561_RD);//写TSL2561地址
331 1
332 1 status=Send_Byte_I2C(0x07);//写n ge ????????
333 1
334 1 status=Send_Byte_I2C(PWUP); //寄存器00H
335 1 status=Send_Byte_I2C(0x12); //寄存器01H -the gain=1 16x -the timer=10 401ms
336 1
337 1 status=Send_Byte_I2C(0x00); //寄存器02H
338 1 status=Send_Byte_I2C(0x00); //寄存器03H
339 1 status=Send_Byte_I2C(0x00); //寄存器04H
340 1 status=Send_Byte_I2C(0x00); //寄存器05H
341 1
342 1 status=Send_Byte_I2C(0x00); //寄存器06H no interrupt
343 1 I2C_Stop( ); //停止 */
344 1 delay_1ms(10); //延时10 ms
345 1
346 1 }
347
348 //****************************************************************************
349 //
350 // Copyright -Handson Jumping, Inc.
351 //
352 // lux equation approximation without floating point calculations
353 //////////////////////////////////////////////////////////////////////////////
354 // Routine: unsigned int CalculateLux(unsigned int ch0, unsigned int ch0, int iType)
355 //
356 // Description: Calculate the approximate illuminance (lux) given the raw
357 // channel values of the TSL2560. The equation if implemented
358 // as a piece.wise linear approximation.
359 //
360 // Arguments: unsigned int iGain . gain, where 0:1X, 1:16X
361 // unsigned int tInt . integration time, where 0:13.7mS, 1:100mS, 2:402mS,
362 // 3:Manual
363 // unsigned int ch0 . raw channel value from channel 0 of TSL2560
C51 COMPILER V7.50 IIC 01/01/2002 04:01:32 PAGE 7
364 // unsigned int ch1 . raw channel value from channel 1 of TSL2560
365 // unsigned int iType . package type (T or CS)
366 //
367 // Return: unsigned int . the approximate illuminance (lux)
368 //
369 //////////////////////////////////////////////////////////////////////////////
370 unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0, unsigned int ch1, int i
-Type)
371 {
372 1 unsigned long ratio1;
373 1 unsigned long ratio;
374 1 unsigned long temp;
375 1 unsigned long lux;
376 1 unsigned long chScale; //TSL2561
377 1 unsigned long channel1; //TSL2561
378 1 unsigned long channel0; //TSL2561
379 1 unsigned int b, m;
380 1 //........................................................................
381 1 // first, scale the channel values depending on the gain and integration time
382 1 // 16X, 402mS is nominal.
383 1 // scale if integration time is NOT 402 msec
384 1 switch (tInt)
385 1 {
386 2 case 0: // 13.7 msec
387 2 chScale = CHSCALE_TINT0;
388 2 break;
389 2 case 1: // 101 msec
390 2 chScale = CHSCALE_TINT1;
391 2 break;
392 2 default: // assume no scaling
393 2 chScale = (1 << CH_SCALE);
394 2 break;
395 2 }
396 1 // scale if gain is NOT 16X
397 1 if (!iGain) chScale = chScale << 4; // scale 1X to 16X
398 1 // scale the channel values
399 1 channel0 = (ch0 * chScale) >> CH_SCALE;
400 1 channel1 = (ch1 * chScale) >> CH_SCALE;
401 1 //........................................................................
402 1 // find the ratio of the channel values (Channel1/Channel0)
403 1 // protect against divide by zero
404 1 ratio1 = 0;
405 1 if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
406 1 // round the ratio value
407 1 ratio = (ratio1 + 1) >> 1;
408 1 // is ratio <= eachBreak ?
409 1 switch (iType)
410 1 {
411 2 case 0: // T package
412 2 if ((ratio >= 0) && (ratio <= K1T))
413 2 {b=B1T; m=M1T;}
414 2 else if (ratio <= K2T)
415 2 {b=B2T; m=M2T;}
416 2 else if (ratio <= K3T)
417 2 {b=B3T; m=M3T;}
418 2 else if (ratio <= K4T)
419 2 {b=B4T; m=M4T;}
420 2 else if (ratio <= K5T)
421 2 {b=B5T; m=M5T;}
422 2 else if (ratio <= K6T)
423 2 {b=B6T; m=M6T;}
424 2 else if (ratio <= K7T)
C51 COMPILER V7.50 IIC 01/01/2002 04:01:32 PAGE 8
425 2 {b=B7T; m=M7T;}
426 2 else if (ratio > K8T)
427 2 {b=B8T; m=M8T;}
428 2 break;
429 2 case 1:// CS package
430 2 /*
431 2 if ((ratio >= 0) && (ratio <= K1C))
432 2 {b=B1C; m=M1C;}
433 2 else if (ratio <= K2C)
434 2 {b=B2C; m=M2C;}
435 2 else if (ratio <= K3C)
436 2 {b=B3C; m=M3C;}
437 2 else if (ratio <= K4C)
438 2 {b=B4C; m=M4C;}
439 2 else if (ratio <= K5C)
440 2 {b=B5C; m=M5C;}
441 2 else if (ratio <= K6C)
442 2 {b=B6C; m=M6C;}
443 2 else if (ratio <= K7C)
444 2 {b=B7C; m=M7C;}
445 2 else if (ratio > K8C)
446 2 {b=B8C; m=M8C;}
447 2 */
448 2 break;
449 2 }
450 1
451 1 temp = ((channel0 * b)- (channel1 * m));
452 1 // do not allow negative lux value
453 1 if (temp < 0) temp = 0;
454 1 // round lsb (2^(LUX_SCALE.1))
455 1 temp += (1 << (LUX_SCALE-1));
456 1 // strip off fractional portion
457 1 lux = temp >> LUX_SCALE;
458 1 return(lux);
459 1 }
460
461 /*******************************************************************************
462 * 名 称: GetLux()
463 * 功 能: 启动I2C操作
464 * 入口参数: 无
465 * 出口参数: 无
466 *******************************************************************************/
467 INT GetLux(void)
468 {
469 1 INT i;
470 1 i=0;
471 1 CH0=TSL2561_read_byte(REG_CH0_L);
472 1 CH0=CH0+TSL2561_read_byte(REG_CH0_H)*256;
473 1
474 1 CH1=TSL2561_read_byte(REG_CH1_L);
475 1 CH1=CH1+TSL2561_read_byte(REG_CH1_H)*256;
476 1
477 1 i=CalculateLux(GAIN_16X,ITIME_400MS,CH0,CH1,FootType_T);
478 1 return (i);
479 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1065 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
C51 COMPILER V7.50 IIC 01/01/2002 04:01:32 PAGE 9
DATA SIZE = 4 43
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -