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