0nism

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

树莓派3Python驱动TSL2561

TSL2561文档翻译

操作原则
  • 模数转换模块
    TSL256x包含两个内部积分的adc模块。积分adc模块的电流来自于channel 0 和 1 的光电二极管。两个通道的测量同时发生,当一次测量即将完成的时候,测量结果被存储在channel 0 与 1的数据寄存器中。为了确保无效数据不被读取,测量被记录两次。在一次transfer结束后,就开始下次(integrate)积分周期。

  • 数字接口
    地址如下表所示。
    Note:slave地址位7bits,一个读写位应该被附加在slave address后。

ADDR SEL TERMINAL LEVEL SLAVE ADDRESS SMB ALERT ADDRESS
GND 0101001 0001100
Float 0111001 0001100
VDD 1001001 0001100
  • 寄存器设置
    TSL256X被16个寄存器(3个被保留)控制。一个命令寄存器控制串行接口。这些寄存器提供大量功能,能被读取用于确定adc转换的结果。
    通常情况下,为了指定要指定的寄存器,command寄存器首先被写
ADDRESS RESISTER NAME REGISTER FUNCTION
−− COMMAND Specifies register address
0h CONTROL Control of basic functions
1h TIMING Integration time/gain control
2h THRESHLOWLOW Low byte of low interrupt threshold
3h THRESHLOWHIGH High byte of low interrupt threshold
4h THRESHHIGHLOW Low byte of high interrupt threshold
5h THRESHHIGHHIGH High byte of high interrupt threshold
6h INTERRUPT Interrupt control
7h −− Reserved
8h CRC Factory test — not a user register
9h −− Reserved
Ah ID Part number/ Rev ID
Bh −− Reserved
Ch DATA0LOW Low byte of ADC channel 0
Dh DATA0HIGH High byte of ADC channel 0
Eh DATA1LOW Low byte of ADC channel 1
Fh DATA1HIGH High byte of ADC channel 1
  • Command Register
    命令寄存器指定目标寄存器的地址,为了接下来的读写操作。send byte协议被用于配置命令寄存器。
    启动时命令寄存器默认为0x00
FIELD BIT DESCRIPTION
CMD 7 Select command register. Must write as 1.
CLEAR 6 Interrupt clear. Clears any pending interrupt. This bit is a write-one-to-clear bit. It is self clearing.
WORD 5 SMB Write/Read Word Protocol. 1 indicates that this SMB transaction is using either the SMB Write Word or

Read Word protocol.
BLOCK | 4 | Block Write/Read Protocol. 1 indicates that this transaction is using either the Block Write or the Block Read
protocol. See Note below.
ADDRESS | 3:0 | Register Address. This field selects the specific control or status register for following write and read
commands according to Table 2.

  • Control Register
FIELD BIT DESCRIPTION
Resv 7:2 Reserved. Write as 0.
POWER 1:0 Power up/power down. By writing a 03h to this register, the device is powered up. By writing a 00h to this register, the device is powered down. NOTE: If a value of 03h is written, the value returned during a read cycle will be 03h. This feature can be used to verify that the device is communicating properly.
  • Timing Register
    控制积分时间和adc通道值的获取
    积分时间依赖于INTEG以及内部时钟频率
FIELD BIT DESCRIPTION
Resv 7−5 Reserved. Write as 0.
GAIN 4 Switches gain between low gain and high gain modes. Writing a 0 selects low gain (1×); writing a 1 selects high gain (16×).
Manual 3 Manual timing control. Writing a 1 begins an integration cycle. Writing a 0 stops an integration cycle.NOTE: This field only has meaning when INTEG = 11. It is ignored at all other times.
Resv 2 Reserved. Write as 0.
INTEG 1:0 Integrate time. This field selects the integration time for each conversion.
INTEG FIELD VALUE SCALE NOMINAL INTEGRATION TIME
00 0.034 13.7 ms
01 0.252 101 ms
10 1 402 ms
11 −− N/A
  • ID Register
    ID寄存器有一个功能:
    [7:4]表示Part Number Identification
    向其通过IIC总线发送0x8a时,返回值为0x05,即为TSL2561T/FN/CL型

  • ADCChannel Data Registers(Ch-Fh)
    为数据位,按协议读取即可。

REGISTER ADDRESS BITS DESCRIPTION
DATA0LOW Ch 7:0 ADC
DATA0HIGH Dh 7:0 ADC
DATA1LOW Eh 7:0 ADC
DATA1HIGH Fh 7:0 ADC

python代码

#! /usr/bin/python

"""
        File name:  tsl2561.py
        Time:       2019/3/11
"""

import wiringpi as wp
import time

TSL2561_IIC_ADDR=   0x39
POWER_UP        =   0x03
POWER_DOWN      =   0x00
CONTROL_REG     =   0x80
DATA0_LOW       =   0x8c
DATA0_HIGH      =   0x8d
DATA1_LOW       =   0x8e
DATA1_HIGH      =   0x8f

#   初始化IIC设备
fd = wp.wiringPiSetUp(TSL2561_IIC_ADDR)

#   启动采集
wp.wiringPiI2CWrite(fd, CONTROL_REG)
wp.wiringPiI2CWrite(fd, POWER_UP)
time.sleep(0.5)

#   读取数据
wp.wiringPiI2CWrite(fd, DATA0_LOW)
data0_low = wp.wiringPiI2CRead(fd)

wp.wiringPiI2CWrite(fd, DATA0_HIGH)
data0_high = wp.wiringPiI2CRead(fd)

wp.wiringPiI2CWrite(fd, DATA1_LOW)
data1_low = wp.wiringPiI2CRead(fd)

wp.wiringPiI2CWrite(fd, DATA1_LOW)
data1_low = wp.wiringPiI2CRead(fd)

#   数据处理
chnn0 = (data0_high << 8) + data0_low
chnn1 = (data1_high << 8) + data1_low

if chnn0 == 0:
    lux = 0
elif chnn0 !=0:
    div = float(chnn1)/float(chnn0)
    if(div>0 and div<=0.5):
        lux = (0.304*chnn0 - 0.062 * chnn0 * (div*1.4))
    if(div>0.5 and div<=0.61):
        lux = (0.022*chnn0 - 0.031 * chnn1)
    if(div>0.61 and div<=0.8):
        lux = (0.8 and div<=1.3)
    if(div>0.8 and div<=1.3):
        lux = (0.00146*chnn0 - 0x00112*chnn1)
    if(div > 1.3)
        lux = 0

#   显示数据
print '%.3f' % lux
posted on 2019-03-12 18:41  0nism  阅读(626)  评论(0编辑  收藏  举报