0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でIoTプログラミング on ESP32 #12 - MMA7660

Posted at

3軸デジタル加速度センサー MMA7660のサンプルプログラムです。

ハードウェア

・Freenove ESP32-S3-WROOM Board or Espressif ESP32-S3-DevKitC
・Grove Digital Accelerometer

配線

MMA7660.PNG

導入パッケージ

・nanoFramework.Hardware.Esp32
・nanoFramework.System.Device.I2c

プログラミング

Program.cs
using nanoFramework.Hardware.Esp32;
using System;
using System.Device.I2c;
using System.Threading;

namespace ESP32S3_MMA7660
{
    public class Program
    {
        public static void Main()
        {
            Configuration.SetPinFunction(8, DeviceFunction.I2C1_DATA);
            Configuration.SetPinFunction(9, DeviceFunction.I2C1_CLOCK);

            var device = I2cDevice.Create(new I2cConnectionSettings(
                1,
                0x4c,
                I2cBusSpeed.FastMode));

            device.Write(new byte[] { 0x07, 0x01 });

            while (true)
            {
                byte[] data = new byte[3];

                device.WriteRead(new byte[] { 0x00 }, data);

                double x = Convert(data[0]);
                double y = Convert(data[1]);
                double z = Convert(data[2]);

                Console.WriteLine($"X: {x.ToString("F4")}, Y: {y.ToString("F4")}, Z: {z.ToString("F4")}");

                Thread.Sleep(1000);
            }
        }

        static double Convert(byte val)
        {
            val &= 0x3f;
            if ((val & 0x20) != 0)
                val |= unchecked((byte)~0x3f);
            int signedVal = (sbyte)val;
            return signedVal * 0.047;
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?