關(guān) 閉

新聞中心

EEPW首頁 > 工控自動化 > 設(shè)計應(yīng)用 > MAX9635環(huán)境光傳感器的接口程序

MAX9635環(huán)境光傳感器的接口程序

作者: 時間:2012-09-19 來源:網(wǎng)絡(luò) 收藏

 C語言例程

// begin definiTION of slave device address
#define _WR_ADDR0x96
#define _RD_ADDR0x97

// begin definition of slave regiSTer addresses for
#define INT_STATUS0x00
#define INT_ENABLE0x01
#define CONFIG_REG0x02
#define HIGH_BYTE0x03
#define LOW_BYTE0x04
#define THRESH_HIGH0x05
#define THRESH_LOW0x06
#define THRESH_TIMER0x07
// 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

稱重傳感器相關(guān)文章:稱重傳感器原理

上一頁 1 2 3 下一頁

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉