- #include
- #include
- #include "stdio.h"
- #define debug_printf 1
- void sensor_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- EXti_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//拉高
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource2);
- EXTI_InitStructure.EXTI_Line = EXTI_Line2;//中断线2
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降边沿触发
- EXTI_Init(&EXTI_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = EXTI2_3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
- NVIC_Init(&NVIC_InitStructure);
- }
- void test_RPR0521(void)
- {
- uint8_t buf[256];
- RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x92,buf,1); // check ID
- if(buf[0] == 0xE0)
- {
- #ifdef debug_printf
- printf("rnRPR0521 detected.");
- #endif
- }
- else
- {
- #ifdef debug_printf
- printf("rnNo response.");
- printf("rnbuf[0] = %x",buf[0]);
- #endif
- return;
- }
- buf[0]=0x41;
- buf[1]=0xC6; // enable ALS, PS, 100ms/100ms
- buf[2]=0x02; // ALS Gain x1 ,PS Gain x4
- buf[3]=0x20; //
- RPR_Sensor_Write(RPR0521RS_DEVICE_ADDRESS, buf, 4);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x41,buf,1); // check ID
- // printf("the writing data 0x41 is %xn",buf[0]);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x42,buf,1); // check ID
- // printf("the writing data 0x42 is %xn",buf[0]);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x43,buf,1); // check ID
- // printf("the writing data 0x43 is %xn",buf[0]);
- #ifdef debug_printf
- printf("rnWrite CONTROL regs");
- #endif
- buf[0]=0x4A;
- buf[1]=0x80; // unlatched
- //buf[1]=0x80;
- RPR_Sensor_Write(RPR0521RS_DEVICE_ADDRESS, buf, 2);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x4a,buf,1);
- // printf("the writing data 0x4a is %xn",buf[0]);
- #ifdef debug_printf
- printf("rnWrite INTERRUPT reg");
- #endif
- buf[0]=0x51;
- buf[1]=0xfe;
- buf[2]=0xff;
- RPR_Sensor_Write(RPR0521RS_DEVICE_ADDRESS, buf, 3);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x51,buf,1);
- // printf("the writing data 0x51 is %xn",buf[0]);
- // RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x52,buf,1);
- // printf("the writing data 0x52 is %xn",buf[0]);
- #ifdef debug_printf
- printf("rnWrite ALS TL");
- #endif
- // for(;;)
- // {
- RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x4a,buf,1); // INTERRUPT
- if(buf[0] & 0x40) // ALS status
- {
- uint16_t *d=(uint16_t *)buf;
- RPR_Sensor_Read(RPR0521RS_DEVICE_ADDRESS,0x44,buf,6); // read PS,ALS
- #ifdef debug_printf
- printf("rnPS: %d",d[0]);
- printf(", ALS: %d,%dn",d[1],d[2]);
- #endif
- }
- // }
- }
- //#define debug_printf 1
- //uint8_t _als_data0_gain;
- //uint8_t _als_data1_gain;
- //uint16_t _als_measure_time;
- //uint8_t init_rpr_sensor(void)
- //{
- // uint8_t rc;
- // uint8_t reg;
- // uint8_t index;
- // uint8_t als_gain_table[] = {1, 2, 64, 128};
- // uint16_t als_meas_time_table[20] = {0,0,0,0,0,100,100,100,100,100,400,400,50,0,0,0};
- //
- // rc = sensor_read(RPR0521RS_SYSTEM_CONTROL, ®, sizeof(reg));/*************************************/
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't access RPR0521RSn");
- // #endif
- // return (rc);
- // }
- // reg &= 0x3F;
- // #ifdef debug_printf
- // printf("RPR0521RS Part ID Register Value = %xn",reg);
- // #endif
- // if (reg != RPR0521RS_PART_ID_VAL)
- // {
- // #ifdef debug_printf
- // printf("Can't find RPR0521RSn");
- // #endif
- // return (rc);
- // }
- //
- // rc = sensor_read(RPR0521RS_MANUFACT_ID, ®, sizeof(reg));
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't access RPR0521RS");
- // #endif
- // return (rc);
- // }
- // #ifdef debug_printf
- // printf("RPR0521RS MANUFACT_ID Register Value = %xn",reg);
- // #endif
- // if (reg != RPR0521RS_MANUFACT_ID_VAL)
- // {
- // #ifdef debug_printf
- // printf("Can't find RPR0521RS");
- // #endif
- // return (rc);
- // }
- // reg = RPR0521RS_ALS_PS_CONTROL_VAL;
- // rc = sensor_write(RPR0521RS_ALS_PS_CONTROL, ®, sizeof(reg));
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't write RPR0521RS ALS_PS_CONTROL registern");
- // #endif
- // return (rc);
- // }
- // rc = sensor_read(RPR0521RS_PS_CONTROL, ®, sizeof(reg));
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't read RPR0521RS PS_CONTROL registern");
- // #endif
- // return (rc);
- // }
- // reg |= RPR0521RS_PS_CONTROL_VAL;
- // rc = sensor_write(RPR0521RS_PS_CONTROL, ®, sizeof(reg));
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't write RPR0521RS PS_CONTROL registern");
- // #endif
- // return (rc);/**********************/
- // }
- // reg = RPR0521RS_MODE_CONTROL_VAL;
- // rc = sensor_write(RPR0521RS_MODE_CONTROL, ®, sizeof(reg));
- // if (rc != 0)
- // {
- // #ifdef debug_printf
- // printf("Can't write RPR0521RS MODE CONTROL register");
- // #endif
- // return (rc);
- // }
- // reg = RPR0521RS_ALS_PS_CONTROL_VAL;
- // index = (reg >> 4) & 0x03;
- // _als_data0_gain = als_gain_table[index];
- // index = (reg >> 2) & 0x03;
- // _als_data1_gain = als_gain_table[index];
- // index = RPR0521RS_MODE_CONTROL_VAL & 0x0F;
- // _als_measure_time = als_meas_time_table[index];
- //
- // return (rc);
- //}
- //uint8_t sensor_get_rawpsalsval(uint8_t *data)
- //{
- // uint8_t rc;
- // rc = sensor_read(RPR0521RS_PS_DATA_LSB, data, 6);
- // if (rc != 0)
- // {
- // #ifdef debug_iic
- // printf("Can't get RPR0521RS PS/ALS_DATA valuen");
- // #endif
- // }
- // return (rc);
- //}
- //uint8_t sensor_get_psalsval(uint16_t *ps, float *als)
- //{
- // uint8_t rc;
- // unsigned char val[6];
- // uint16_t rawps;
- // uint16_t rawals[2];
- //
- // rc = sensor_get_rawpsalsval(val);
- // if (rc != 0)
- // {
- // return (rc);
- // }
- // rawps = ((uint16_t)val[1] << 8) | val[0];
- // rawals[0] = ((uint16_t)val[3] << 8) | val[2];
- // rawals[1] = ((uint16_t)val[5] << 8) | val[4];
- // *ps = rawps;
- // *als = sensor_convert_lx(rawals);
- // return (rc);
- //}
- //uint8_t sensor_check_near_far(uint16_t data)
- //{
- // if (data >= RPR0521RS_NEAR_THRESH)
- // {
- // return (RPR0521RS_NEAR_VAL);
- // }
- // else
- // {
- // return (RPR0521RS_FAR_VAL);
- // }
- //}
- //float sensor_convert_lx(uint16_t *data)
- //{
- // float lx;
- // float d0, d1, d1_d0;
- // if (_als_data0_gain == 0)
- // {
- // return (RPR0521RS_ERROR);
- // }
- // if (_als_data1_gain == 0)
- // {
- // return (RPR0521RS_ERROR);
- // }
- // if (_als_measure_time == 0)
- // {
- // return (RPR0521RS_ERROR);
- // }
- // else if (_als_measure_time == 50)
- // {
- // if ((data[0] & 0x8000) == 0x8000)
- // {
- // data[0] = 0x7FFF;
- // }
- // if ((data[1] & 0x8000) == 0x8000)
- // {
- // data[1] = 0x7FFF;
- // }
- // }
- // d0 = (float)data[0] * (100 / _als_measure_time) / _als_data0_gain;
- // d1 = (float)data[1] * (100 / _als_measure_time) / _als_data1_gain;
- // if (d0 == 0)
- // {
- // lx = 0;
- // return (lx);
- // }
- // d1_d0 = d1 / d0;
- // if (d1_d0 < 0.595)
- // {
- // lx = (1.682 * d0 - 1.877 * d1);
- // }
- // else if (d1_d0 < 1.015)
- // {
- // lx = (0.644 * d0 - 0.132 * d1);
- // }
- // else if (d1_d0 < 1.352)
- // {
- // lx = (0.756 * d0 - 0.243 * d1);
- // }
- // else if (d1_d0 < 3.053)
- // {
- // lx = (0.766 * d0 - 0.25 * d1);
- // }
- // else
- // {
- // lx = 0;
- // }
- // return (lx);
- //}
- //uint8_t sensor_write(unsigned char memory_address, unsigned char *data, unsigned char size)
- //{
- // uint8_t i;
- // IIC_Start();
- // if(RPR0521RS_DEVICE_ADDRESS == 0xA0) //eeprom地址大于1字节
- // IIC_Send_Byte(0xA0 + ((memory_address/256)<<1));//发送高地址
- // else
- // IIC_Send_Byte(RPR0521RS_DEVICE_ADDRESS); //发器件地址
- // IIC_Wait_Ack();
- // IIC_Send_Byte(memory_address&0xFF); //发送低地址
- // IIC_Wait_Ack();
- // for ( i = 0; i < size; i++ )
- // {
- // IIC_Send_Byte(*(data+i)); //发送字节
- // IIC_Wait_Ack();
- // }
- // IIC_Stop();//产生一个停止条件
- // if(RPR0521RS_DEVICE_ADDRESS == 0xA0) //
- // Delay_nus(500);
- // else
- // Delay_nus(2);
- // return 0;
- //}
- //uint8_t sensor_read(unsigned char memory_address, unsigned char *data, int size)
- //{
- // uint8_t rc = 0;
- // uint8_t i;
- // IIC_Start();
- // if(RPR0521RS_DEVICE_ADDRESS==0xA0)
- // IIC_Send_Byte(0xA0 + ((memory_address/256)<<1));
- // else
- // IIC_Send_Byte(RPR0521RS_DEVICE_ADDRESS);
- // IIC_Wait_Ack();
- // IIC_Send_Byte(memory_address&0xFF); //发送低地址
- // IIC_Wait_Ack();
- //
- // IIC_Start();
- // IIC_Send_Byte(RPR0521RS_DEVICE_ADDRESS+1); //发器件地址
- // IIC_Wait_Ack();
- //
- // for( i = 0; i < size; i++)
- // {
- // *(data + i) = IIC_Receive_Byte(1);
- // IIC_Wait_Ack();
- // }
- //
- // IIC_Stop();//产生一个停止条件
- // return rc;
- //}
- //byte RPR0521RS::read(unsigned char memory_address, unsigned char *data, int size)
- //{
- // byte rc;
- // unsigned char cnt;
- // Wire.beginTransmission(RPR0521RS_DEVICE_ADDRESS);
- // Wire.write(memory_address);
- // rc = Wire.endTransmission(false);
- // if (rc != 0) {
- // return (rc);
- // }
- // Wire.requestFrom(RPR0521RS_DEVICE_ADDRESS, size, true);
- // cnt = 0;
- // while(Wire.available()) {
- // data[cnt] = Wire.read();
- // cnt++;
- // }
- // return (0);
- //}
复制代码
1
|
|
|
|