门限滞回设置的C程序代码

编程实验

72人已加入

描述

将接近检测传感器集成到系统后,一个经常遇到的问题是如何正确选择接近检测的门限,以便在用户通话期间打开或关闭屏幕。门限设置须确保出现错误判断的几率非常低,而且能够支持绝大多数使用者的情况。
门限滞回例程

 

#define MAX44000_ADDR	0x94
#define INT_STATUS_REG	0x00
#define OFF_THRESHOLD	4600
#define OFF_DELAY		1
#define ON_THRESHOLD	4000
#define ON_DELAY		3

uint8 screenStatus;	// 0 means off, 1 means on

/*
  i2cWriteBytes()
  
  Arguments:
	uint8 address - device address
	uint8 start_reg - register where the first byte is written
	uint8 *data - data to write
	uint8 nbytes - number of bytes to write

  Consecutively writes several bytes to some i2c device starting at some 
  specified address -- implemented elsewhere
*/
void i2cWriteBytes(uint8 address,uint8 start_reg,uint8 *data,uint8 nbytes);

/*
  MAX44000InterruptHandler()

 

以下代码用于实现MAX44000 INT引脚的中断处理,假设MAX44000的接近检测传感器设置为14位模式,并已使能中断。此外,假设屏幕状态初始化为1或0,详细信息请参阅数据资料的寄存器说明部分。

 

*/
void MAX44000InterruptHandler() {

	uint8 i2cData[3];
	
	i2cRead1Byte(MAX44000_ADDR,INT_STATUS_REG,&i2cData);
	if (i2cData&0x01 != 0)
		return;	// check to make sure interrupt really fired
				// this simultaneously clears the interrupt flag
	
	if (screenStatus) {
		i2cData[0] = ON_DELAY;	
		i2cData[1] = ON_THRESHOLD >> 8 & 0xBF; // set ABOVE = 0
		i2cData[2] = ON_THRESHOLD & 0xFF;
	} else {
		i2cData[0] = OFF_DELAY;	 
		i2cData[1] = OFF_THRESHOLD >> 8 | 0x40; // set ABOVE = 1
		i2cData[2] = OFF_THRESHOLD & 0xFF;
	} // set the new threshold depending on what the screen status was
	
	// set the delay and threshold after each interrupt
	i2cWriteBytes(MAX44000_ADDR,0x0A,i2cData,3);
	
	return;
} // MAX44000InterruptHandler

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分