コード例 #1
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            // Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open Device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_SCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            //Config timing,Unit of time for microseconds
            ControlI2C.I2C_TimeConfig.tSU_STA = 5;
            ControlI2C.I2C_TimeConfig.tHD_STA = 4;
            ControlI2C.I2C_TimeConfig.tLOW    = 5;
            ControlI2C.I2C_TimeConfig.tHIGH   = 5;
            ControlI2C.I2C_TimeConfig.tSU_DAT = 1;
            ControlI2C.I2C_TimeConfig.tSU_STO = 4;
            ControlI2C.I2C_TimeConfig.tBuf    = 5;
            ret = ControlI2C.VII_TimeConfig(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_TimeConfig);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Set time error!");
                return;
            }
            // Write data
            Byte[] write_buffer = new Byte[8];
            for (int i = 0; i < 8; i++)
            {
                write_buffer[i] = (Byte)i;
            }
            ret = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, 0x00, write_buffer, 8);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            //Delay to ensure write complete
            System.Threading.Thread.Sleep(10);
            // Read data
            Byte[] read_buffer = new Byte[8];
            ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, 0x00, read_buffer, 8);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Int32  read_data;
                String read_data_str = "";
                Console.WriteLine("Get Data:");
                for (int i = 0; i < 8; i++)
                {
                    read_data      = read_buffer[i];
                    read_data_str += read_data.ToString("X2") + " ";
                }
                Console.WriteLine(read_data_str);
            }
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            int ret;

            // Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            //Initialize Device(Hardware control mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 100000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_NONE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            while (true)
            {
                Byte[] write_buffer = new Byte[8];
                // Wake up AM2311 sensor
                ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 1);
                //Send out read temperature and humidity command
                write_buffer[0] = 0x03;
                write_buffer[1] = 0x00;
                write_buffer[2] = 0x04;
                ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 3);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Write data error!");
                    return;
                }
                // Read out temperature and humidity
                Byte[] read_buffer = new Byte[8];
                ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xB8, 0x00, read_buffer, 8);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error!");
                    return;
                }
                else
                {
                    double t = ((read_buffer[4] << 8) | read_buffer[5]) / 10.0;
                    Console.Clear();
                    Console.WriteLine("temperature:" + t.ToString("0.0") + " ℃");
                    double h = ((read_buffer[2] << 8) | read_buffer[3]) / 10.0;
                    Console.WriteLine("humidity:" + h.ToString("0.0") + " %");
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            // Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device(hardware mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }


            Byte[] write_buffer = new Byte[8];
            Byte[] read_buffe   = new Byte[8];
            String cmdString;
            int    pll;
            int    frequency;
            int    dotNum;
            int    i;

            while (true)
            {
                Console.WriteLine("Please input the frequency of radio(The unit is MHz):");
                cmdString = Console.ReadLine();
                if (cmdString == "quit")
                {
                    Console.WriteLine("Exit");
                    break;
                }
                else
                {
                    dotNum = 0;
                    for (i = 0; i < cmdString.Length; i++)
                    {
                        if ((cmdString[i] != '.') && (cmdString[i] < '0') && (cmdString[i] > '9'))
                        {
                            Console.WriteLine("Input error!");
                            return;
                        }
                        if (cmdString.ToCharArray()[i] == '.')
                        {
                            dotNum++;
                        }
                    }
                    if (dotNum > 1)
                    {
                        Console.WriteLine("Input error!");
                        return;
                    }
                    else if (dotNum > 0)
                    {
                        //frequency = atof(cmdString)*1000;
                        frequency = (int)float.Parse(cmdString) * 1000;
                    }
                    else
                    {
                        //frequency = atoi(cmdString)*1000;
                        frequency = int.Parse(cmdString) * 1000;
                    }
                    pll             = (Int32)((float)((frequency + 225)) / (float)8.192);
                    write_buffer[0] = (Byte)(pll >> 8);
                    write_buffer[1] = (Byte)pll;
                    write_buffer[2] = 0x70;
                    write_buffer[3] = 0x16;
                    write_buffer[4] = 0x80;
                    ret             = ControlI2C.VII_WriteBytes(ControlI2C.VII_USBI2C, 0, 0, 0xC0, 0x00, write_buffer, 5);
                    if (ret != ControlI2C.ERROR.SUCCESS)
                    {
                        Console.WriteLine("Set frequency error:{0}", ret);
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Set frequency success!");
                    }
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            //Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            //Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            //Initialize device(Hardware control mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Initialize MPU6050 sensor
            Byte[] write_buffer = new Byte[8];
            write_buffer[0] = 0;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x6B, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            System.Threading.Thread.Sleep(10);
            write_buffer[0] = 0;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x6C, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            System.Threading.Thread.Sleep(10);
            write_buffer[0] = 0x08;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x1B, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            System.Threading.Thread.Sleep(10);
            write_buffer[0] = 0x01;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x1A, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            System.Threading.Thread.Sleep(10);
            write_buffer[0] = 0x08;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x1C, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            System.Threading.Thread.Sleep(10);
            while (true)
            {
                // Get data
                Byte[] read_buffer = new Byte[16];
                ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xD0, 0x3B, read_buffer, 14);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error!");
                    return;
                }
                // Process data
                Int16 AccelX, AccelY, AccelZ;
                AccelX = (Int16)((read_buffer[0] << 8) | read_buffer[1]);
                AccelY = (Int16)((read_buffer[2] << 8) | read_buffer[3]);
                AccelZ = (Int16)((read_buffer[4] << 8) | read_buffer[5]);
                Int16 GyroX, GyroY, GyroZ;
                GyroX = (Int16)((read_buffer[8] << 8) | read_buffer[9]);
                GyroY = (Int16)((read_buffer[10] << 8) | read_buffer[11]);
                GyroZ = (Int16)((read_buffer[12] << 8) | read_buffer[13]);
                // Show data
                Console.Clear();
                Console.WriteLine("AccelX = " + AccelX);
                Console.WriteLine("AccelY = " + AccelY);
                Console.WriteLine("AccelZ = " + AccelZ);
                Console.WriteLine("GyroX = " + GyroX);
                Console.WriteLine("GyroY = " + GyroY);
                Console.WriteLine("GyroZ = " + GyroZ);
                System.Threading.Thread.Sleep(100);
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            //Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            //Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            //Initialize device(Hardware control mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Set all ports to output mode
            Byte[] write_buffer = new Byte[8];
            write_buffer[0] = 0;
            write_buffer[1] = 0;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x40, 0x06, write_buffer, 2);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            while (true)
            {
                //Control all the port output low level
                write_buffer[0] = 0;
                write_buffer[1] = 0;
                ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x40, 0x02, write_buffer, 2);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Write data error!");
                    return;
                }
                System.Threading.Thread.Sleep(500);
                //Control of all port output high level
                write_buffer[0] = 0xFF;
                write_buffer[1] = 0xFF;
                ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x40, 0x02, write_buffer, 2);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Write data error!");
                    return;
                }
                System.Threading.Thread.Sleep(500);
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            int ret;

            //Scan device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }

            //Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            //Initialize device
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            //Write CONTROL register
            UInt32 cmd;

            Byte[] ControlRegister = new Byte[1];
            ControlRegister[0] = 0x03; //POWER = 1,ADC_EN = 1
            cmd = 0x80 | 0x00 | 0x00;  //Select command register,Byte protocol,Register Address.
            ret = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, cmd, ControlRegister, 8);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            //Delay
            System.Threading.Thread.Sleep(10);
            //Get ADC data
            Byte[] adc_buffer = new Byte[2];
            cmd = 0x80 | 0x20 | 0x10;//Select command register,Word protocol,Register Address.
            ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, cmd, adc_buffer, 2);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("Get Color Data:");
                Console.WriteLine("  Green ADC Data : " + String.Format("{0:D}", adc_buffer[0] | (adc_buffer[1] << 8)));
            }
            cmd = 0x80 | 0x20 | 0x12;//Select command register,Word protocol,Register Address.
            ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, cmd, adc_buffer, 2);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Console.WriteLine("    Red ADC Data : " + String.Format("{0:D}", adc_buffer[0] | (adc_buffer[1] << 8)));
            }
            cmd = 0x80 | 0x20 | 0x14;//Select command register,Word protocol,Register Address.
            ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, cmd, adc_buffer, 2);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Console.WriteLine("   Blue ADC Data : " + String.Format("{0:D}", adc_buffer[0] | (adc_buffer[1] << 8)));
            }
            cmd = 0x80 | 0x20 | 0x16;//Select command register,Word protocol,Register Address.
            ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0xA0, cmd, adc_buffer, 2);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Console.WriteLine("  Clear ADC Data : " + String.Format("{0:D}", adc_buffer[0] | (adc_buffer[1] << 8)));
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            int ret;

            //Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            //Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            //Initialize device(Hardware control mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 100000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_NONE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Send wait for measuring instructions
            Byte[] write_buffer = new Byte[8];
            write_buffer[0] = 0x01;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x46, 0x00, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Start measurement in 11x resolution ratio
            write_buffer[0] = 0x10;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x46, 0x00, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Loop measurement
            while (true)
            {
                // Waiting for measurement complete
                System.Threading.Thread.Sleep(1000);
                // Get measurement data
                Byte[] read_buffer = new Byte[8];
                ret = ControlI2C.VII_ReadBytes(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, 0x46, 0x00, read_buffer, 2);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error!");
                    return;
                }
                else
                {
                    double illuminance = ((read_buffer[0] << 8) | read_buffer[1]) / 1.2;
                    Console.Clear();
                    Console.WriteLine("illuminance:" + illuminance.ToString("0.00") + " lx");
                }
            }
        }
コード例 #8
0
        static void Main(string[] args)
        {
            int ret;

            // Scan connected device
            ret = ControlI2C.VII_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlI2C.VII_OpenDevice(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device(hardware mode)
            ControlI2C.I2C_Config.AddrType     = ControlI2C.INIT_CONFIG.VII_ADDR_7BIT;
            ControlI2C.I2C_Config.ClockSpeed   = 400000;
            ControlI2C.I2C_Config.ControlMode  = ControlI2C.INIT_CONFIG.VII_HCTL_MODE;
            ControlI2C.I2C_Config.MasterMode   = ControlI2C.INIT_CONFIG.VII_MASTER;
            ControlI2C.I2C_Config.SubAddrWidth = ControlI2C.INIT_CONFIG.VII_SUB_ADDR_1BYTE;
            ret = ControlI2C.VII_InitI2C(ControlI2C.INIT_CONFIG.VII_USBI2C, 0, 0, ref ControlI2C.I2C_Config);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }

            //Put the HMC5883 IC into the correct operating mode
            UInt16 Address = 0x1E;                              //0011110b, I2C 7bit address of HMC5883

            Address = (UInt16)(Address << 1);
            Byte[] write_buffer = new Byte[8];
            Byte[] read_buffer  = new Byte[8];
            write_buffer[0] = 0x00;
            ret             = ControlI2C.VII_WriteBytes(ControlI2C.VII_USBI2C, 0, 0, Address, 0x02, write_buffer, 1);
            if (ret != ControlI2C.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            while (true)
            {
                //Read status
                ret = ControlI2C.VII_ReadBytes(ControlI2C.VII_USBI2C, 0, 0, Address, 0x09, read_buffer, 1);
                if (ret != ControlI2C.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error!");
                    return;
                }
                //Data ready
                if ((read_buffer[0] & 0x01) > 0)
                {
                    //Tell the HMC5883 where to begin reading data
                    ret = ControlI2C.VII_ReadBytes(ControlI2C.VII_USBI2C, 0, 0, Address, 0x03, read_buffer, 6);
                    if (ret != ControlI2C.ERROR.SUCCESS)
                    {
                        Console.WriteLine("Read data error!\n");
                        return;
                    }
                    int x, y, z;
                    x = (read_buffer[0] << 8) | read_buffer[1];
                    y = (read_buffer[2] << 8) | read_buffer[3];
                    z = (read_buffer[4] << 8) | read_buffer[5];
                    Console.WriteLine("------------------------------------------------------\n");
                    Console.WriteLine("x = {0:d}", x);
                    Console.WriteLine("y = {0:d}", y);
                    Console.WriteLine("z = {0:d}", z);
                    Console.WriteLine("------------------------------------------------------\n");
                }
                //Console.ReadLine();
            }
        }