#include <reg52.h>
#include <intrins.h>
#include <i2c.h>
/////////////////////////////////////////////////////
#define PS1_THRESHOLD 2000
#define PS2_THRESHOLD 2000
#define PS3_THRESHOLD 3000
#define UP_SIGN 1
#define DOWN_SIGN 2
#define LEFT_SIGN 3
#define RIGHT_SIGN 4
sfr AUXR = 0x8E;
typedef union UU16
{
unsigned int U16;
unsigned char U8[2];
};
typedef union UU32
{
unsigned long U32;
unsigned int U16[2];
unsigned char U8[4];
};
sbit led4 =P0^7;
sbit led3 = P0^6;
sbit led2 = P0^5;
sbit led1 = P0^4;
unsigned char i2cDataNew[11] = {0};
unsigned char irqStatus;
unsigned char firstEnter = 0;
unsigned char ifEnter = 0;
unsigned char tt = 0;
union UU32 maxValueTime[3] = {0};
union UU16 maxValue[3] = {0};
union UU16 ALS_VIS;
union UU16 ALS_IR;
union UU16 PS1, PS2, PS3;
/****************************串口传送一个8位数据****************************/
void send_char(unsigned char txd)
// 传送一个字符
{
SBUF = txd;
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
}
/****************************1ms基准延时程序****************************/
void delay1ms(unsigned int i)
{
unsigned char j;
while(i --)
{
for(j= 0; j < 115; j++)
{
;
}
}
}
void deviceInit() {
I2C_Init();
/*********定时器初始化*********/
TMOD = 0x21; // 定时器1工作于8位自动重载模式, 用于产生波特率
TH1 = TL1 = 0xFD; // 波特率9600
SCON = 0x40; // 设定串行口工作方式1 允许接收
PCON =0x00; // 波特率不倍增
TR1 = 1; // 启动定时器1
TL0 = (65536-50000) ;
TH0 = (65536-50000) >> 8;
TR0 = 1;
ET0 = 1;
EA = 1;
delay1ms(2000);
/*********I2C复位命令*********/
I2C_Start();
I2C_Write(0x00);
if ( I2C_GetAck() )
{
led1=0;
}
I2C_Write(0x06);
if ( I2C_GetAck() )
{
led2=0;
}
I2C_Stop();
delay1ms(2000);
}
void deviceConfig() {
unsigned char cmd;
unsigned char *p;
/*********HW_KEY*********/
cmd = 0x17;
p = &cmd;
I2C_Puts(0x5A, 0x07, 1, p, 1);
/*********PS_LED21*********/
// cmd = 0xFF;
cmd = 0xF6;
I2C_Puts(0x5A, 0x0F, 1, p, 1);
/*********PS_LED3*********/
cmd = 0x0F;
I2C_Puts(0x5A, 0x10, 1, p, 1);
/*********INT_CFG*********/
cmd = 0x01;
I2C_Puts(0x5A, 0x03, 1, p, 1);
/*********IRQ_ENABLE*********/
cmd = 0x1D;
I2C_Puts(0x5A, 0x04, 1, p, 1);
/*********IRQ_MODE1*********/
cmd = 0x00;
I2C_Puts(0x5A, 0x05, 1, p, 1);
/*********IRQ_MODE2*********/
cmd = 0x00;
I2C_Puts(0x5A, 0x06, 1, p, 1);
/*********MEAS_RATE*********/
cmd = 0x84;
I2C_Puts(0x5A, 0x08, 1, p, 1);
/*********PS_RATE*********/
cmd = 0x08;
I2C_Puts(0x5A, 0x0A, 1, p, 1);
/*********ALS_RATE*********/
cmd = 0x08;
I2C_Puts(0x5A, 0x09, 1, p, 1);
/*********唤醒内部定序器*********/
cmd = 0x01;
I2C_Puts(0x5A, 0x17, 1, p, 1);
cmd = 0xBB;
I2C_Puts(0x5A, 0x18, 1, p, 1);
/*********CHLIST*********/
cmd = 0x37;
I2C_Puts(0x5A, 0x17, 1, p, 1);
cmd = 0xA1;
I2C_Puts(0x5A, 0x18, 1, p, 1);
/*********PSALS_AUTO*********/
cmd = 0x0F;
I2C_Puts(0x5A, 0x18, 1, p, 1);
}
unsigned char defineDirection() {
union UU32 deltaTime13;
union UU32 deltaTime12;
union UU32 deltaTime23;
deltaTime13.U32 = maxValueTime[0].U32 > maxValueTime[2].U32? maxValueTime[0].U32 - maxValueTime[2].U32: maxValueTime[2].U32 - maxValueTime[0].U32;
deltaTime12.U32 = maxValueTime[0].U32 > maxValueTime[1].U32? maxValueTime[0].U32 - maxValueTime[1].U32: maxValueTime[1].U32 - maxValueTime[0].U32;
deltaTime23.U32 = maxValueTime[1].U32 > maxValueTime[2].U32? maxValueTime[1].U32 - maxValueTime[2].U32: maxValueTime[2].U32 - maxValueTime[1].U32;
if(maxValueTime[0].U32 < maxValueTime[1].U32 && maxValueTime[0].U32 < maxValueTime[2].U32) firstEnter = 1;
else if (maxValueTime[1].U32 < maxValueTime[0].U32 && maxValueTime[1].U32 < maxValueTime[2].U32) firstEnter = 2;
else if (maxValueTime[2].U32 < maxValueTime[0].U32 && maxValueTime[2].U32 < maxValueTime[1].U32) firstEnter = 3;
/* send_char(deltaTime12.U8[0]);
send_char(deltaTime12.U8[1]);
send_char(deltaTime12.U8[2]);
send_char(deltaTime12.U8[3]);
send_char(deltaTime13.U8[0]);
send_char(deltaTime13.U8[1]);
send_char(deltaTime13.U8[2]);
send_char(deltaTime13.U8[3]);
send_char(deltaTime23.U8[0]);
send_char(deltaTime23.U8[1]);
send_char(deltaTime23.U8[2]);
send_char(deltaTime23.U8[3]);
send_char(firstEnter); */
if (firstEnter == 1) {
if (deltaTime12.U32 * 2 < deltaTime13.U32){
return RIGHT_SIGN;
}
else return DOWN_SIGN;
}
else if (firstEnter == 2){
if (deltaTime12.U32 * 2 < deltaTime13.U32){
return RIGHT_SIGN;
}
else return UP_SIGN;
}
else if (firstEnter == 3) {
if (deltaTime12.U32 * 2 < deltaTime13.U32){
return LEFT_SIGN;
}
else if(deltaTime23.U32 < deltaTime13.U32){
return UP_SIGN;
}
else return DOWN_SIGN;
}
else return 0;
}
void tm0_isr() interrupt 1 using 1 {
TL0 = (65536 - 50000) ;
TH0 = (65536 - 50000) >> 8;
tt ++;
}
void exint1() interrupt 2 {
unsigned char *p;
EX0 = 0;
led4 = ! led4;
/*********读取数据*********/
p = i2cDataNew;
I2C_Gets_ForInt(0x5A, 0x22, 1, p, 10);
ALS_VIS.U16 = i2cDataNew[0] + i2cDataNew[1] * 256;
ALS_IR.U16 = i2cDataNew[2] + i2cDataNew[3] * 256;
PS1.U16 = i2cDataNew[4] + i2cDataNew[5] * 256;
PS2.U16 = i2cDataNew[6] + i2cDataNew[7] * 256;
PS3.U16 = i2cDataNew[8] + i2cDataNew[9] * 256;
/*
send_char(PS1.U8[0]);
send_char(PS1.U8[1]);
send_char(PS2.U8[0]);
send_char(PS2.U8[1]);
send_char(PS3.U8[0]);
send_char(PS3.U8[1]);
*/
if (tt >= 10) {
ifEnter = 0;
maxValueTime[0].U32 = 0;
maxValueTime[1].U32 = 0;
maxValueTime[2].U32 = 0;
maxValue[0].U16 = 0;
maxValue[1].U16 = 0;
maxValue[2].U16 = 0;
tt = 0;
}
if (ifEnter == 0 && (PS1.U16 > PS1_THRESHOLD || PS2.U16 > PS2_THRESHOLD || PS3.U16 > PS3_THRESHOLD)){
TL0 = (65536-50000) ;
TH0 = (65536-50000) >> 8;
tt = 0;
maxValueTime[0].U32 = 0;
maxValueTime[1].U32 = 0;
maxValueTime[2].U32 = 0;
maxValue[0].U16 = 0;
maxValue[1].U16 = 0;
maxValue[2].U16 = 0;
ifEnter = 1;
}
if (ifEnter == 1) {
if (PS1.U16 < 2000 && PS2.U16 < 2000 && PS3.U16 < 3000){
ifEnter = 0;
/* send_char(maxValueTime[0].U8[0]);
send_char(maxValueTime[0].U8[1]);
send_char(maxValueTime[0].U8[2]);
send_char(maxValueTime[0].U8[3]);
send_char(maxValueTime[1].U8[0]);
send_char(maxValueTime[1].U8[1]);
send_char(maxValueTime[1].U8[2]);
send_char(maxValueTime[1].U8[3]);
send_char(maxValueTime[2].U8[0]);
send_char(maxValueTime[2].U8[1]);
send_char(maxValueTime[2].U8[2]);
send_char(maxValueTime[2].U8[3]); */
send_char(defineDirection());
maxValueTime[0].U32 = 0;
maxValueTime[1].U32 = 0;
maxValueTime[2].U32 = 0;
maxValue[0].U16 = 0;
maxValue[1].U16 = 0;
maxValue[2].U16 = 0;
}
else{
if (maxValue[0].U16 < PS1.U16){
maxValue[0].U16 = PS1.U16;
maxValueTime[0].U32 = TH0 * 256 + TL0 + tt * 50000;
}
if (maxValue[1].U16 < PS2.U16){
maxValue[1].U16 = PS2.U16;
maxValueTime[1].U32 = TH0 * 256 + TL0 + tt * 50000;
}
if (maxValue[2].U16 < PS3.U16){
maxValue[2].U16 = PS3.U16;
maxValueTime[2].U32 = TH0 * 256 + TL0 + tt * 50000;
}
}
}
/*********清除中断*********/
p = &irqStatus;
I2C_Gets_ForInt(0x5A, 0x21, 1, p, 1);
I2C_Puts_ForInt(0x5A, 0x21, 1, p, 1);
EX0 = 1;
}
void main() {
deviceInit();
deviceConfig();
IT1 = 1; //
EX1 = 1; //
EA = 1; //
led1 = led2 = led3 = led4 = 0;
while(1) {
}