본문 바로가기

Nordic/nRF51

[nRF51822 xBee EVM] Arduino 테스트 - I2C VL53L0X, PCF8574

 

 

#include <Wire.h>
#include <VL53L0X.h>

#define PCF_8574_ADDR      0x20
void write8(uint8_t Address, uint8_t Value)
{
   Wire.beginTransmission(Address);
   Wire.write(Value);
   Wire.endTransmission();
}

uint8_t read8(uint8_t Address, uint8_t Value)
{
   uint8_t ret;
    
   Wire.beginTransmission(Address);
   Wire.write(Value);
   Wire.endTransmission();

Serial.println("1");

  Wire.requestFrom(Address, (uint8_t)1);
  ret = Wire.read();

  return ret;   
}


VL53L0X sensor;

void setup()
{
  Serial.begin(115200);

  Serial.println("initialize sensor!");
  Wire.begin();
  
  

  sensor.setTimeout(500);
write8(PCF_8574_ADDR, 0xf0);

  Serial.println(read8(PCF_8574_ADDR, 0));
   Serial.println("start");
  if (!sensor.init(1))
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1) {}
  }

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();
}

int cnt = 0;
void loop()
{
int disp[] = {1,3,7,0xf,0x51f,0x3f,0x7f,0xff};
int i;
  //Serial.println(cnt);
delay(100);

    cnt = sensor.readRangeContinuousMillimeters();
    if(cnt>2000)
    {
      cnt = 2000;
    }
    
  Serial.println(cnt);

  i = cnt/64;
  //Serial.println(i);
  if(i>7)i=7;
  

  write8(PCF_8574_ADDR, ~(disp[i] ));

  //if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  
}

 

 

반응형