/// <summary> /// Reset the sensor. /// </summary> public void Reset() { Standby = true; _mag3110.WriteRegister((byte)Registers.Control1, 0x00); _mag3110.WriteRegister((byte)Registers.Control2, 0x80); _mag3110.WriteRegisters((byte)Registers.XOffsetMSB, new byte[] { 0, 0, 0, 0, 0, 0 }); }
/// <summary> /// Sets the direction of a particulare port. /// </summary> /// <param name="pin"></param> /// <param name="direction"></param> public void SetPortDirection(byte pin, PortDirectionType direction) { if (IsValidPin(pin)) { // if it's already configured, get out. (1 = input, 0 = output) if (direction == PortDirectionType.Input) { if (BitHelpers.GetBitValue(_iodir, pin)) { return; } //if ((_iodir & (byte)(1 << pin)) != 0) return; } else { if (!BitHelpers.GetBitValue(_iodir, pin)) { return; } //if ((_iodir & (byte)(1 << pin)) == 0) return; } // set the IODIR bit and write the setting _iodir = BitHelpers.SetBit(_iodir, (byte)pin, (byte)direction); _i2cPeripheral.WriteRegister(_IODirectionRegister, _iodir); } else { throw new Exception("Pin is out of range"); } }
/// <summary> /// Reset the sensor. /// </summary> public void Reset() { Standby = true; i2cPeripheral.WriteRegister((byte)Registers.Control1, 0x00); i2cPeripheral.WriteRegister((byte)Registers.Control2, 0x80); i2cPeripheral.WriteRegisters((byte)Registers.XOffsetMSB, new byte[] { 0, 0, 0, 0, 0, 0 }); }
/// <summary> /// Create a new MPL3115A2 object with the default address and speed settings. /// </summary> /// <param name="address">Address of the sensor (default = 0x60).</param> /// <param name="i2cBus">I2cBus (Maximum is 400 kHz).</param> public Mpl3115A2(II2cBus i2cBus, byte address = 0x60) { var device = new I2cPeripheral(i2cBus, address); _mpl3115a2 = device; if (_mpl3115a2.ReadRegister(Registers.WhoAmI) != 0xc4) { throw new Exception("Unexpected device ID, expected 0xc4"); } _mpl3115a2.WriteRegister(Registers.Control1, (byte)(ControlRegisterBits.Active | ControlRegisterBits.OverSample128)); _mpl3115a2.WriteRegister(Registers.DataConfiguration, (byte)(ConfigurationRegisterBits.DataReadyEvent | ConfigurationRegisterBits.EnablePressureEvent | ConfigurationRegisterBits.EnableTemperatureEvent)); }
public void SaveCustomCharacter(byte[] characterMap, byte address) { address &= 0x7; // we only have 8 locations 0-7 Command((byte)(LCD_SETCGRAMADDR | (address << 3))); for (int i = 0; i < 8; i++) { i2cPeripheral.WriteRegister(Rs, characterMap[i]); } }
/// <summary> /// Force the sensor to make a reading and update the relevant properties. /// </summary> async Task Update() { int temp = 0; // // Get the humidity first. // groveTH02.WriteRegister(Registers.Config, StartMeasurement); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // await Task.Delay(40); while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } byte[] data = groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 4; Conditions.Humidity = (((float)temp) / 16) - 24; // // Now get the temperature. // groveTH02.WriteRegister(Registers.Config, StartMeasurement | MeasureTemperature); // // Maximum conversion time should be 40ms but loop just in case // it takes longer. // await Task.Delay(40); while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0) { ; } data = groveTH02.ReadRegisters(Registers.DataHigh, 2); temp = data[0] << 8; temp |= data[1]; temp >>= 2; Conditions.Temperature = (((float)temp) / 32) - 50; }
/// <summary> /// Turn the heater on or off. /// </summary> /// <param name="onOrOff">Heater status, true = turn heater on, false = turn heater off.</param> public void Heater(bool onOrOff) { var register = si7021.ReadRegister(READ_USER_REGISTER); register &= 0xfd; if (onOrOff) { register |= 0x02; } si7021.WriteRegister(WRITE_USER_REGISTER, register); }
public HMC5883L(II2cBus i2CBus, byte address = 0x1E, SamplesToAverage samplesToAverage = SamplesToAverage.One, DataOutputRate dataOutputRate = DataOutputRate._15Hz, MeasurementMode measurementMode = MeasurementMode.Normal, GainConfiguration gainConfiguration = GainConfiguration._1_30, OperatingMode operatingMode = OperatingMode.SingleMeasurement ) { Mode = operatingMode; Gain = gainConfiguration; _device = new I2cPeripheral(i2CBus, address); // Config - Register A byte configRegisterAData = 0b0000_0000; configRegisterAData |= (byte)samplesToAverage; configRegisterAData |= (byte)dataOutputRate; configRegisterAData |= (byte)measurementMode; _device.WriteRegister(CRA, configRegisterAData); Console.WriteLine($"CRA: {Convert.ToString(configRegisterAData, 2).PadLeft(8, '0')}"); // Config - Register A byte configRegisterBData = 0b0000_0000; configRegisterBData |= (byte)gainConfiguration; _device.WriteRegister(CRB, configRegisterBData); Console.WriteLine($"CRB: {Convert.ToString(configRegisterBData, 2).PadLeft(8, '0')}"); // Select Mode byte modeRegisterData = 0b00000000; modeRegisterData |= (byte)operatingMode; _device.WriteRegister(MR, modeRegisterData); Console.WriteLine($"MR: {Convert.ToString(modeRegisterData, 2).PadLeft(8, '0')}"); }
/// <summary> /// Set the PowerControl register (see pages 25 and 26 of the data sheet) /// </summary> /// <param name="linkActivityAndInactivity">Link the activity and inactivity events.</param> /// <param name="autoASleep">Enable / disable auto sleep when the activity and inactivity are linked.</param> /// <param name="measuring">Enable or disable measurements (turn on or off).</param> /// <param name="sleep">Put the part to sleep (true) or run in normal more (false).</param> /// <param name="frequency">Frequency of measurements when the part is in sleep mode.</param> public void SetPowerState(bool linkActivityAndInactivity, bool autoASleep, bool measuring, bool sleep, Frequency frequency) { byte data = 0; if (linkActivityAndInactivity) { data |= 0x20; } if (autoASleep) { data |= 0x10; } if (measuring) { data |= 0x08; } if (sleep) { data |= 0x40; } data |= (byte)frequency; _adxl345.WriteRegister(Registers.PowerControl, data); }
/// <summary> /// Reads the current values of the magnetic field strength /// </summary> /// <returns>A <see cref="Vector"/> with the values of the magnetic field strenth on three axes, expressed in Gaus</returns> public Vector Read() { // In case the sensor is in single measurement mode it is necessary to update the Mode Register, wait at least 6 ms and only then read the data. if (Mode == OperatingMode.SingleMeasurement) { var currentModeRegisterData = (byte)OperatingMode.SingleMeasurement; _device.WriteRegister(MR, currentModeRegisterData); Thread.Sleep(7); } // Read raw sensor data and convert it to Gauss var data = _device.ReadRegisters(DXRA, 6); _currentX = ToGauss((short)((data[0] << 8) + data[1])); _currentY = ToGauss((short)((data[2] << 8) + data[3])); _currentZ = ToGauss((short)((data[4] << 8) + data[5])); return(new Vector(_currentX, _currentY, _currentZ)); }
private void WriteI2cRegister(byte address, byte value) { i2cDevice.WriteRegister(address, value); }
/// <summary> /// Set resolution /// </summary> public void SetResolution(byte resolution) { i2CPeripheral.WriteRegister(MCP_RESOLUTION, resolution); }
/// <summary> /// Initializes the VL53L0X /// </summary> protected async Task Initialize() { if (IsShutdown) { await ShutDown(false); } if (Read(0xC0) != 0xEE || Read(0xC1) != 0xAA || Read(0xC2) != 0x10) { throw new Exception("Failed to find expected ID register values"); } i2cPeripheral.WriteRegister(0x88, 0x00); i2cPeripheral.WriteRegister(0x80, 0x01); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x00, 0x00); stopVariable = Read(0x91); i2cPeripheral.WriteRegister(0x00, 0x01); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x80, 0x00); var configControl = ((byte)(Read(MsrcConfigControl) | 0x12)); var signalRateLimit = 0.25f; i2cPeripheral.WriteRegister(SystemSequenceConfig, 0xFF); var spadInfo = GetSpadInfo(); int spadCount = spadInfo.Item1; bool spad_is_aperture = spadInfo.Item2; byte[] ref_spad_map = new byte[7]; ref_spad_map[0] = GlobalConfigSpadEnablesRef0; i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(DynamicSpadRefEnStartOffset, 0x00); i2cPeripheral.WriteRegister(DynamicSpadNumRequestedRefSpad, 0x2C); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(GlobalConfigRefEnStartSelect, 0xB4); var first_spad_to_enable = (spad_is_aperture) ? 12 : 0; var spads_enabled = 0; for (int i = 0; i < 48; i++) { if (i < first_spad_to_enable || spads_enabled == spadCount) { ref_spad_map[1 + (i / 8)] &= (byte)~(1 << (i % 8)); } else if ((ref_spad_map[1 + (i / 8)] >> (byte)((i % 8)) & 0x1) > 0) { spads_enabled += 1; } } i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x00, 0x00); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x09, 0x00); i2cPeripheral.WriteRegister(0x10, 0x00); i2cPeripheral.WriteRegister(0x11, 0x00); i2cPeripheral.WriteRegister(0x24, 0x01); i2cPeripheral.WriteRegister(0x25, 0xFF); i2cPeripheral.WriteRegister(0x75, 0x00); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x4E, 0x2C); i2cPeripheral.WriteRegister(0x48, 0x00); i2cPeripheral.WriteRegister(0x30, 0x20); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x30, 0x09); i2cPeripheral.WriteRegister(0x54, 0x00); i2cPeripheral.WriteRegister(0x31, 0x04); i2cPeripheral.WriteRegister(0x32, 0x03); i2cPeripheral.WriteRegister(0x40, 0x83); i2cPeripheral.WriteRegister(0x46, 0x25); i2cPeripheral.WriteRegister(0x60, 0x00); i2cPeripheral.WriteRegister(0x27, 0x00); i2cPeripheral.WriteRegister(0x50, 0x06); i2cPeripheral.WriteRegister(0x51, 0x00); i2cPeripheral.WriteRegister(0x52, 0x96); i2cPeripheral.WriteRegister(0x56, 0x08); i2cPeripheral.WriteRegister(0x57, 0x30); i2cPeripheral.WriteRegister(0x61, 0x00); i2cPeripheral.WriteRegister(0x62, 0x00); i2cPeripheral.WriteRegister(0x64, 0x00); i2cPeripheral.WriteRegister(0x65, 0x00); i2cPeripheral.WriteRegister(0x66, 0xA0); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x22, 0x32); i2cPeripheral.WriteRegister(0x47, 0x14); i2cPeripheral.WriteRegister(0x49, 0xFF); i2cPeripheral.WriteRegister(0x4A, 0x00); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x7A, 0x0A); i2cPeripheral.WriteRegister(0x7B, 0x00); i2cPeripheral.WriteRegister(0x78, 0x21); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x23, 0x34); i2cPeripheral.WriteRegister(0x42, 0x00); i2cPeripheral.WriteRegister(0x44, 0xFF); i2cPeripheral.WriteRegister(0x45, 0x26); i2cPeripheral.WriteRegister(0x46, 0x05); i2cPeripheral.WriteRegister(0x40, 0x40); i2cPeripheral.WriteRegister(0x0E, 0x06); i2cPeripheral.WriteRegister(0x20, 0x1A); i2cPeripheral.WriteRegister(0x43, 0x40); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x34, 0x03); i2cPeripheral.WriteRegister(0x35, 0x44); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x31, 0x04); i2cPeripheral.WriteRegister(0x4B, 0x09); i2cPeripheral.WriteRegister(0x4C, 0x05); i2cPeripheral.WriteRegister(0x4D, 0x04); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x44, 0x00); i2cPeripheral.WriteRegister(0x45, 0x20); i2cPeripheral.WriteRegister(0x47, 0x08); i2cPeripheral.WriteRegister(0x48, 0x28); i2cPeripheral.WriteRegister(0x67, 0x00); i2cPeripheral.WriteRegister(0x70, 0x04); i2cPeripheral.WriteRegister(0x71, 0x01); i2cPeripheral.WriteRegister(0x72, 0xFE); i2cPeripheral.WriteRegister(0x76, 0x00); i2cPeripheral.WriteRegister(0x77, 0x00); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x0D, 0x01); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x80, 0x01); i2cPeripheral.WriteRegister(0x01, 0xF8); i2cPeripheral.WriteRegister(0xFF, 0x01); i2cPeripheral.WriteRegister(0x8E, 0x01); i2cPeripheral.WriteRegister(0x00, 0x01); i2cPeripheral.WriteRegister(0xFF, 0x00); i2cPeripheral.WriteRegister(0x80, 0x00); i2cPeripheral.WriteRegister(SystemInterruptConfigGpio, 0x04); var gpio_hv_mux_active_high = Read(GpioHvMuxActiveHigh); i2cPeripheral.WriteRegister(GpioHvMuxActiveHigh, (byte)(gpio_hv_mux_active_high & ~0x10)); i2cPeripheral.WriteRegister(GpioHvMuxActiveHigh, 0x01); i2cPeripheral.WriteRegister(SystemSequenceConfig, 0xE8); i2cPeripheral.WriteRegister(SystemSequenceConfig, 0x01); PerformSingleRefCalibration(0x40); i2cPeripheral.WriteRegister(SystemSequenceConfig, 0x02); PerformSingleRefCalibration(0x00); i2cPeripheral.WriteRegister(SystemSequenceConfig, 0xE8); }
public Max44009(II2cBus i2cBus, byte address = 0x4a) { i2cPeripheral = new I2cPeripheral(i2cBus, address); i2cPeripheral.WriteRegister(0x02, 0x00); }
protected virtual void WriteRegister(byte frame, byte reg, byte data) { SelectPage(frame); i2cPeripheral.WriteRegister(reg, data); }
protected virtual void Write(byte register, byte value) { i2cPeripheral.WriteRegister(register, value); }
/// <summary> /// Turn the TSL2561 off. /// </summary> /// <remarks> /// Reset the power bits in the control register (page 13 of the datasheet). /// </remarks> public void TurnOff() { tsl2561.WriteRegister(Registers.Control, 0x00); }