MAX9635环境光传感器的接口程序

出处:xyluck1786 发布于:2011-01-27 11:34:03

     摘要:这篇应用笔记介绍了如何合理使用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

  
关键词:传感器接口

版权与免责声明

凡本网注明“出处:维库电子市场网”的所有作品,版权均属于维库电子市场网,转载请必须注明维库电子市场网,https://www.dzsc.com,违反者本网将追究相关法律责任。

本网转载并注明自其它出处的作品,目的在于传递更多信息,并不代表本网赞同其观点或证实其内容的真实性,不承担此类作品侵权行为的直接责任及连带责任。其他媒体、网站或个人从本网转载时,必须保留本网注明的作品出处,并自负版权等法律责任。

如涉及作品内容、版权等问题,请在作品发表之日起一周内与本网联系,否则视为放弃相关权利。

广告
上传BOM文件: BOM文件
*公司名:
*联系人:
*手机号码:
QQ:
应用领域:

有效期:
OEM清单文件: OEM清单文件
*公司名:
*联系人:
*手机号码:
QQ:
有效期:

扫码下载APP,
一键连接广大的电子世界。

在线人工客服

买家服务:
卖家服务:

0571-85317607

客服在线时间周一至周五
9:00-17:30

关注官方微信号,
第一时间获取资讯。

建议反馈

联系人:

联系方式:

按住滑块,拖拽到最右边
>>
感谢您向阿库提出的宝贵意见,您的参与是维库提升服务的动力!意见一经采纳,将有感恩红包奉上哦!