MAX9635环境光传感器的接口程序
扫描二维码
随时随地手机看文章
摘要:这篇应用笔记介绍了如何合理使用MAX9635环境光传感器的中断输出功能,适用于智能手机、平板电脑等便携设备的LCD背光。利用中断功能可以针对多个照明区间设置不同的门限值,无需重复查询光传感器。合理使用中断功能,可以将系统保持在低功耗休眠状态,或用于执行用户定义的其它任务。中断功能极大地提升了系统能效,改善了系统性能和用户在不同照明条件下的体验。本文还给出了I?C接口的C语言例程,用于中断操作。
概述
MAX9635环境光传感器用于检测环境光的亮度,器件适用于众多应用,其中包括:LCD显示器背光调节,用于便携产品、家电产品以及室内照明等。MAX9635具有超低功耗(工作电流仅为0.65uA),工作电压为1.8V (便于连接微处理器I/O口),这些特性使其成为传感器和加密应用中的理想选择。背光调节和低功耗操作可有效延长电池寿命,提高照明系统的工作效率。
MAX9635最有价值的特性是具有一个便利的中断输出引脚。这个引脚可以使系统处于低功耗待机模式或将有限的资源用于执行其它更重要的任务。
该应用笔记介绍了如何编程中断输出功能,优化系统性能,并给出了几个C语言例程。
寄存器设置
下表列出了MAX9635的寄存器以及上电复位(POR)后的状态。
可预设的寄存器包括:配置寄存器、中断使能和门限定时器。
配置寄存器(地址0x02)上电时设置为:CONT=0和MANUAL=0,能够满足绝大多数用户的应用。这些设置通知MAX9635根据环境光的强度自动调整其灵敏度。
为了使能中断功能,主控制器(如微控制器),首先将中断使能寄存器(地址0x01)置1。
主控制器然后在门限定时器寄存器(地址0x07)写入适当的延时,通常情况下不会更改这个设置。设置门限延时需要遵循两个基本原则。首先,在该寄存器写入一个不为零的数值,以避免照明条件出现闪变或瞬间发生变化时反复触发中断。当用户的姿势发生变化,或者是在设备移动时,都会在光传感器上产生阴影,导致亮度发生闪变。其次,增加响应显示器亮度调整的延时,也可以为所定义的用户接口算法预留时间。例如,在iPad™等移动设备中,当用户穿过一个黑暗的通道时(比如地铁轨道),用户并不希望屏幕的亮度频繁变化。
门限寄存器设置
正常操作情况下,用户会重复设置上限门限寄存器(地址0x05)和下限门限寄存器(地址0x06)。当环境光强超出这些寄存器定义的窗口时,则触发中断(寄存器0x00的INTS位置1,/INT引脚拉低)。该中断的持续时间要比门限定时寄存器(地址0x07)设置的延迟时间长。
设置门限寄存器时,主控制器必须首先从数据寄存器LUX高字节(地址0x03)和LUX低字节(地址0x04)读取流明数值,以确定当前的工作区域。然后,由主控制器设置相应的上限门限寄存器和下限门限寄存器。
背光控制算法举例
人眼对光亮的响应特性为对数函数,类似于人耳对声音的响应特性。因此,需要对背光强度进行特殊设置,使其对环境光呈对数响应。在亮度较低时,背光强度随亮度变化的幅度较大,而在高亮度环境下背光强度变化幅度较小。理想情况下,主处理器还会根据环境光强度信息调整对比度、色彩等参数,支持高级图像处理算法。
典型的亮度控制机制可以包含五级控制门限。多数情况下,屏幕的玻璃和物理尺寸会使光传感器测到的环境光亮度降低5%至10%,设置检测门限时应该考虑这一因素。
下表给出了一个背光强度和上限、下限门限的设置示例。为了把门限的流明值转换成门限计数值,简单地用0.045除目标流明值。
随着外部照明条件的改变调整背光强度
中断处理
以下流程图所示为主机微控制器处理中断的典型流程图。
门限值算法和环境光测量:计数值与流明值
利用计数值(而不是流明值)进行计算更为简捷,可以省去浮点运算并可采用简单的定点运算程序。
从上述表格中获得所要求的门限,可以计算门限寄存器字节,以作为每个流明区域的伪码限制。这些门限值可以简单地从上式计算出来,与环境光计数值进行比较。
需要注意的是,如果光强非常接近所定义的流明区域的边界,背光强度会频繁摆动,使用户感觉很不舒服。考虑到这一因素,可以在一个流明区域的上限与下一流明区域的下限之间建立一个重叠区,从而形成一个滞回空间,屏蔽微弱的亮度波动。必要时,可以扩大该滞回区域。
这里列举的算法仅为背光亮度控制提供一些常规准则,实际应用中已经开发了多种背光控制算法,满足终端用户舒适、透明的背光需求。
C语言例程
// begin definiTION of slave device address
#define MAX9635_WR_ADDR 0x96
#define MAX9635_RD_ADDR 0x97
// begin definition of slave regiSTer addresses for MAX9635
#define INT_STATUS 0x00
#define INT_ENABLE 0x01
#define CONFIG_REG 0x02
#define HIGH_BYTE 0x03
#define LOW_BYTE 0x04
#define THRESH_HIGH 0x05
#define THRESH_LOW 0x06
#define THRESH_TIMER 0x07
// end definition of slave addresses for MAX9635
// define some lookup tables for the upper and lower thresholds as well as the
// brightness. All tables values are taken from text of application notes
#define NUM_REGI* 5
uint8 upperThresholds[NUM_REGI*] = {0x01, 0x06, 0x29, 0x48, 0xEF};
uint8 lowerThresholds[NUM_REGI*] = {0x00, 0x01, 0x06, 0x29, 0x48};
uint8 backlightBrightness[NUM_REGI*] = {0x40, 0x73, 0xA6, 0xD9, 0xFF};
/**
Function: SetPWMDutyCycle
Arguments: uint8 dc - desired duty cycle
Returns: none
Description: sets the duty cycle of a 8-bit PWM, assuming that in this
architecture, 0x00 = 0% duty cycle 0x7F = 50% and 0xFF = 100%
**/
extern void SetPWMDutyCycle(uint8 dc);
extern void SetupMicro(void);
extern void Idle(void);
/**
Function: I2C_WriteByte
Arguments: uint8 slaveAddr - address of the slave device
uint8 regAddr - destination register in slave device
uint8 data - data to write to the register
Returns: ACK bit
Description: performs necessary functions to send one byte of data to a
specified register in a specific device on the I²C bus
**/
extern uint8 I2C_WriteByte(uint8 slaveAddr, uint8 regAddr, uint8 data);
/**
Function: I2C_ReadByte
Arguments: uint8 slaveAddr - address of the slave device
uint8 regAddr - destination register in slave device
uint8 *data - pointer data to read from the register
Returns: ACK bit
Description: performs necessary functions to get one byte of data from a
specified register in a specific device on the I²C bus
**/
extern uint8 I2C_ReadByte(uint8 slaveAddr, uint8 regAddr, uint8* data);
/**
Function: findNewThresholdsAndBrightness
Arguments: uint8 luxCounts - light counts High Byte
uint8 *highThresh - pointer to memory storing upper threshold byte
uint8 *lowThresh - pointer to memory storing lower threshold byte
Returns: none
Description: Based on what the lux reading was (in counts), this routine
determines the current operating illumination zone. The zones
are defined by upper and lower bounds in a lookup table. After
knowing the operating zone, this function may set new interrupt
thresholds and a backlight brightness. Since the interrupt only
fires when the lux reading is outside the defined region, these
threshold and brightness settings are not overwritten with the
same data repeatedly.
**/
void findNewThresholdsAndBrightness(uint8 luxCounts, uint8 *highThresh,
uint8 *lowThresh);
void main() {
uint8 *highThresholdByte; // upper and lower threshold bytes
uint8 *lowThresholdByte;
uint8 *timerByte;
uint8 max9635Interrupt = 0; // status of MAX9635 interrupt register
uint8 luxCounts; // computed as shown below
SetupMicro(); // some subroutine which initializes this CPU
*highByte = 0;
*lowByte = 0;
*highThresholdByte = 0xEF; // upper threshold counts
// initially = POR setting (maximum possible = 0xEF)
*lowThresholdByte = 0x00; // lower threshold counts
// initially POR setting (minimum possible = 0x00)
*timerByte = 0x14; // initial timer delay for thresholds:
// 0x14 * 100ms = 2 seconds
// initialize MAX9635 threshold and timer registers
I2C_WriteByte(MAX9635_WR_ADDR, THRESH_HIGH, *highThresholdByte);
I2C_WriteByte(MAX9635_WR_ADDR, THRESH_LOW, *lowThresholdByte);
I2C_WriteByte(MAX9635_WR_ADDR, THRESH_TIMER, *timerByte);
I2C_WriteByte(MAX9635_WR_ADDR, INT_ENABLE, 0x01);// enable sensor interrupts
while(1) {
// do other tasks until an interrupt fires
// assume that this function waits for the status of a GPIO-type pin to
// change states
while (! GPIO_StatusChanged() ) {
// some idling subroutine, shown with polling a port for
// simplicity - but alternate interrupt-based routines are more
// efficient
Idle();
} // loop until an interrupt occurs
// ok... an interrupt fired! was it from the MAX9635?
I2C_ReadByte(MAX9635_RD_ADDR, INT_STATUS, max9635Interrupt);
/**
Place code to check other devices here, if desired
**/
if (max9635Interrupt) {
// get the current lux reading from the MAX9635
I2C_ReadByte(MAX9635_RD_ADDR, HIGH_BYTE, luxCounts);
findNewThresholdsAndBrightness(luxCounts, highThresholdByte,
lowThresholdByte);
// write to the threshold and timer registers with new data
I2C_WriteByte(MAX9635_WR_ADDR, THRESH_HIGH, *highThresholdByte);
I2C_WriteByte(MAX9635_WR_ADDR, THRESH_LOW, *lowThresholdByte);
max9635Interrupt = 0; // interrupt serviced, clear the bits
} // only executes if the MAX9635's interrupt fired
// perform. other tasks which are only done after change of a GPIO pin
} // loop forever
} // main routine
void findNewThresholdsAndBrightness(uint8 luxCounts, uint8 *highThresh, uint8 *lowThresh) {
uint8 i;
for (i=0; i < NUM_REGI*; ++i) {
if ((luxCounts >= lowerThresholds[i]) && (luxCounts <= upperThresholds[i])){
*highThresh = upperThresholds[i];
*lowThresh = lowerThresholds[i];
// PWM duty cycle sets the brightness of the backlight
SetPWMDutyCycle(backlightBrightness[i]);
return; // found the region -- no point in continuing the loop
} // found the right region
} // check where the lux reading lies in terms of threshold regions
} // findNewThresholdsAndBrightness