MAX9635環(huán)境光傳感器的接口程序
摘要:這篇應(yīng)用筆記介紹了如何合理使用MAX9635環(huán)境光傳感器的中斷輸出功能,適用于智能手機(jī)、平板電腦等便攜設(shè)備的LCD背光。利用中斷功能可以針對(duì)多個(gè)照明區(qū)間設(shè)置不同的門限值,無需重復(fù)查詢光傳感器。合理使用中斷功能,可以將系統(tǒng)保持在低功耗休眠狀態(tài),或用于執(zhí)行用戶定義的其它任務(wù)。中斷功能極大地提升了系統(tǒng)能效,改善了系統(tǒng)性能和用戶在不同照明條件下的體驗(yàn)。本文還給出了I?C接口的C語言例程,用于中斷操作。
概述
MAX9635環(huán)境光傳感器用于檢測(cè)環(huán)境光的亮度,器件適用于眾多應(yīng)用,其中包括:LCD顯示器背光調(diào)節(jié),用于便攜產(chǎn)品、家電產(chǎn)品以及室內(nèi)照明等。MAX9635具有超低功耗(工作電流僅為0.65uA),工作電壓為1.8V (便于連接微處理器I/O口),這些特性使其成為傳感器和加密應(yīng)用中的理想選擇。背光調(diào)節(jié)和低功耗操作可有效延長電池壽命,提高照明系統(tǒng)的工作效率。
MAX9635最有價(jià)值的特性是具有一個(gè)便利的中斷輸出引腳。這個(gè)引腳可以使系統(tǒng)處于低功耗待機(jī)模式或?qū)⒂邢薜馁Y源用于執(zhí)行其它更重要的任務(wù)。
該應(yīng)用筆記介紹了如何編程中斷輸出功能,優(yōu)化系統(tǒng)性能,并給出了幾個(gè)C語言例程。
寄存器設(shè)置
下表列出了MAX9635的寄存器以及上電復(fù)位(POR)后的狀態(tài)。
可預(yù)設(shè)的寄存器包括:配置寄存器、中斷使能和門限定時(shí)器。
配置寄存器(地址0x02)上電時(shí)設(shè)置為:CONT=0和MANUAL=0,能夠滿足絕大多數(shù)用戶的應(yīng)用。這些設(shè)置通知MAX9635根據(jù)環(huán)境光的強(qiáng)度自動(dòng)調(diào)整其靈敏度。
為了使能中斷功能,主控制器(如微控制器),首先將中斷使能寄存器(地址0x01)置1。
主控制器然后在門限定時(shí)器寄存器(地址0x07)寫入適當(dāng)?shù)难訒r(shí),通常情況下不會(huì)更改這個(gè)設(shè)置。設(shè)置門限延時(shí)需要遵循兩個(gè)基本原則。首先,在該寄存器寫入一個(gè)不為零的數(shù)值,以避免照明條件出現(xiàn)閃變或瞬間發(fā)生變化時(shí)反復(fù)觸發(fā)中斷。當(dāng)用戶的姿勢(shì)發(fā)生變化,或者是在設(shè)備移動(dòng)時(shí),都會(huì)在光傳感器上產(chǎn)生陰影,導(dǎo)致亮度發(fā)生閃變。其次,增加響應(yīng)顯示器亮度調(diào)整的延時(shí),也可以為所定義的用戶接口算法預(yù)留時(shí)間。例如,在iPad™等移動(dòng)設(shè)備中,當(dāng)用戶穿過一個(gè)黑暗的通道時(shí)(比如地鐵軌道),用戶并不希望屏幕的亮度頻繁變化。
門限寄存器設(shè)置
正常操作情況下,用戶會(huì)重復(fù)設(shè)置上限門限寄存器(地址0x05)和下限門限寄存器(地址0x06)。當(dāng)環(huán)境光強(qiáng)超出這些寄存器定義的窗口時(shí),則觸發(fā)中斷(寄存器0x00的INTS位置1,/INT引腳拉低)。該中斷的持續(xù)時(shí)間要比門限定時(shí)寄存器(地址0x07)設(shè)置的延遲時(shí)間長。
設(shè)置門限寄存器時(shí),主控制器必須首先從數(shù)據(jù)寄存器LUX高字節(jié)(地址0x03)和LUX低字節(jié)(地址0x04)讀取流明數(shù)值,以確定當(dāng)前的工作區(qū)域。然后,由主控制器設(shè)置相應(yīng)的上限門限寄存器和下限門限寄存器。
背光控制算法舉例
人眼對(duì)光亮的響應(yīng)特性為對(duì)數(shù)函數(shù),類似于人耳對(duì)聲音的響應(yīng)特性。因此,需要對(duì)背光強(qiáng)度進(jìn)行特殊設(shè)置,使其對(duì)環(huán)境光呈對(duì)數(shù)響應(yīng)。在亮度較低時(shí),背光強(qiáng)度隨亮度變化的幅度較大,而在高亮度環(huán)境下背光強(qiáng)度變化幅度較小。理想情況下,主處理器還會(huì)根據(jù)環(huán)境光強(qiáng)度信息調(diào)整對(duì)比度、色彩等參數(shù),支持高級(jí)圖像處理算法。
典型的亮度控制機(jī)制可以包含五級(jí)控制門限。多數(shù)情況下,屏幕的玻璃和物理尺寸會(huì)使光傳感器測(cè)到的環(huán)境光亮度降低5%至10%,設(shè)置檢測(cè)門限時(shí)應(yīng)該考慮這一因素。
下表給出了一個(gè)背光強(qiáng)度和上限、下限門限的設(shè)置示例。為了把門限的流明值轉(zhuǎn)換成門限計(jì)數(shù)值,簡單地用0.045除目標(biāo)流明值。
隨著外部照明條件的改變調(diào)整背光強(qiáng)度
中斷處理
以下流程圖所示為主機(jī)微控制器處理中斷的典型流程圖。
門限值算法和環(huán)境光測(cè)量:計(jì)數(shù)值與流明值
利用計(jì)數(shù)值(而不是流明值)進(jìn)行計(jì)算更為簡捷,可以省去浮點(diǎn)運(yùn)算并可采用簡單的定點(diǎn)運(yùn)算程序。
從上述表格中獲得所要求的門限,可以計(jì)算門限寄存器字節(jié),以作為每個(gè)流明區(qū)域的偽碼限制。這些門限值可以簡單地從上式計(jì)算出來,與環(huán)境光計(jì)數(shù)值進(jìn)行比較。
需要注意的是,如果光強(qiáng)非常接近所定義的流明區(qū)域的邊界,背光強(qiáng)度會(huì)頻繁擺動(dòng),使用戶感覺很不舒服??紤]到這一因素,可以在一個(gè)流明區(qū)域的上限與下一流明區(qū)域的下限之間建立一個(gè)重疊區(qū),從而形成一個(gè)滯回空間,屏蔽微弱的亮度波動(dòng)。必要時(shí),可以擴(kuò)大該滯回區(qū)域。
這里列舉的算法僅為背光亮度控制提供一些常規(guī)準(zhǔn)則,實(shí)際應(yīng)用中已經(jīng)開發(fā)了多種背光控制算法,滿足終端用戶舒適、透明的背光需求。
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