Beispiel #1
0
        static void Main(string[] args)
        {
            usb_device.DEVICE_INFO DevInfo   = new usb_device.DEVICE_INFO();
            USB2SPI.SPI_CONFIG     SPIConfig = new USB2SPI.SPI_CONFIG();
            Int32[] DevHandles = new Int32[20];
            Int32   DevHandle = 0;
            Int32   ADS1256Index = USB2SPI.SPI1_CS0;
            UInt32  ResetPinMask, DrdyPinMask;
            bool    state;
            Int32   DevNum, ret;

            Byte[] WriteBuffer = new Byte[64];
            Byte[] ReadBuffer  = new Byte[20480];
            //扫描查找设备
            DevNum = usb_device.USB_ScanDevice(DevHandles);
            if (DevNum <= 0)
            {
                Console.WriteLine("No device connected!");
                return;
            }
            else
            {
                Console.WriteLine("Have {0} device connected!", DevNum);
            }
            DevHandle = DevHandles[0];
            //打开设备
            state = usb_device.USB_OpenDevice(DevHandle);
            if (!state)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            else
            {
                Console.WriteLine("Open device success!");
            }
            //获取固件信息
            StringBuilder FuncStr = new StringBuilder(256);

            state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr);
            if (!state)
            {
                Console.WriteLine("Get device infomation error!");
                return;
            }
            else
            {
                Console.WriteLine("Firmware Info:");
                Console.WriteLine("    Name:" + Encoding.Default.GetString(DevInfo.FirmwareName));
                Console.WriteLine("    Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate));
                Console.WriteLine("    Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF);
                Console.WriteLine("    Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF);
                Console.WriteLine("    Functions:" + DevInfo.Functions.ToString("X8"));
                Console.WriteLine("    Functions String:" + FuncStr);
                Console.WriteLine("    Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8"));
            }
            //配置RESET引脚和DRADY引脚
            if (ADS1256Index == 0)
            {
                ResetPinMask = 1 << 9;
                DrdyPinMask  = 1 << 8;
            }
            else
            {
                ResetPinMask = 1 << 1;
                DrdyPinMask  = 1 << 0;
            }
            ret = USB2GPIO.GPIO_SetOutput(DevHandle, ResetPinMask, 1);
            if (ret != USB2GPIO.GPIO_SUCCESS)
            {
                Console.WriteLine("Initialize gpio error!");
                return;
            }
            //配置SPI总线相关参数
            SPIConfig.Mode         = USB2SPI.SPI_MODE_HARD_HDX;
            SPIConfig.ClockSpeedHz = 1562500;
            SPIConfig.CPHA         = 1;
            SPIConfig.CPOL         = 0;
            SPIConfig.LSBFirst     = USB2SPI.SPI_MSB;
            SPIConfig.Master       = USB2SPI.SPI_MASTER;
            SPIConfig.SelPolarity  = USB2SPI.SPI_SEL_LOW;
            ret = USB2SPI.SPI_Init(DevHandle, ADS1256Index, ref SPIConfig);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            //RESET引脚输出低脉冲复位ADS1256
            USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, 0);            //输出低电平
            System.Threading.Thread.Sleep(10);
            USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, ResetPinMask); //输出高电平
            //准备配置寄存器数据
            WriteBuffer[0] = 0x50;                                      //ADS1256_CMD_WREG
            WriteBuffer[1] = 0x04;
            WriteBuffer[2] = 0x04;
            WriteBuffer[3] = 0x08; //配置AIN0为单端模式
            WriteBuffer[4] = 0;    //PGA
            WriteBuffer[5] = 0xA1; //1000SPS
            WriteBuffer[6] = 0xFF;
            ret            = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 7, (Int32)DrdyPinMask, 0x00, 1000);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("SPI Event Write Data Error!");
                return;
            }
            //准备读配置寄存器数据,验证配置数据写入是否成功
            WriteBuffer[0] = 0x10;//ADS1256_CMD_RREG;
            WriteBuffer[1] = 4;
            ret            = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 2, ReadBuffer, 5, 10, (Int32)DrdyPinMask, 0x10, 1000);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("SPI Event Write&Read Data Error!");
                return;
            }
            //判断写入配置寄存器的值和读出寄存器的值是否一样
            if ((WriteBuffer[3] != ReadBuffer[1]) || (WriteBuffer[4] != ReadBuffer[2]) || (WriteBuffer[5] != ReadBuffer[3]))
            {
                Console.WriteLine("Config ADS1256 Error!");
                return;
            }
            //发送连续采集数据命令,该命令只适合连续采集一个通道的情况
            WriteBuffer[0] = 0xFC; //ADS1256_CMD_SYNC
            WriteBuffer[1] = 0x00; //ADS1256_CMD_WAKEUP
            WriteBuffer[2] = 0x03; //ADS1256_CMD_RDATAC
            ret            = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 3, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("SPI Event Write&Read Data Error!");
                return;
            }
            Console.WriteLine("Continuous Get ADC Data!");
            //检测DRADY引脚下降沿之后读回数据
            int ReadDataNum = 10;

            ret = USB2SPI.SPI_BlockReadBytesOfEvent(DevHandle, ADS1256Index, ReadBuffer, 3, ReadDataNum, (Int32)DrdyPinMask, 0x10, 100);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("SPI Event BlockRead Data Error!");
                return;
            }
            //将读回的数据转换为实际的电压值
            for (int i = 0; i < ReadDataNum; i++)
            {
                int ADCDataTemp = (ReadBuffer[i * 3 + 0] << 16) | (ReadBuffer[i * 3 + 1] << 8) | ReadBuffer[i * 3 + 2];
                if ((ADCDataTemp & 0x800000) != 0x00)
                {
                    ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp));
                }
                double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000;
                Console.WriteLine("ADC[{0}] = {1} V", i, Volutage);
            }
            //发送停止连续采样数据命令
            WriteBuffer[0] = 0x0F;//ADS1256_CMD_SDATAC
            ret            = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 1, (Int32)DrdyPinMask, 0x10, 1000);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("SPI Event Write Data Error!");
                return;
            }
            Console.WriteLine("Circulate Get ADC Data!");
            //循环采集每个通道数据
            ReadDataNum = 3;
            byte Channel = 0;

            WriteBuffer[0] = 0x50 | 1;//ADS1256_CMD_WREG
            WriteBuffer[1] = 0x00;
            WriteBuffer[2] = (byte)((Channel << 4) | 0x08);
            WriteBuffer[3] = 0xFC; //ADS1256_CMD_SYNC
            WriteBuffer[4] = 0x00; //ADS1256_CMD_WAKEUP
            WriteBuffer[5] = 0x01; //ADS1256_CMD_RDATA
            for (int i = 0; i < ReadDataNum; i++)
            {
                for (Channel = 0; Channel < 8; Channel++)
                {
                    WriteBuffer[2] = (byte)((Channel << 4) | 0x08);
                    ret            = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 6, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000);
                    if (ret != USB2SPI.SPI_SUCCESS)
                    {
                        Console.WriteLine("SPI Event Write&Read Data Error!");
                        return;
                    }
                    else
                    {
                        //丢弃第一次的数据
                        if ((i == 0) && (Channel == 0))
                        {
                            continue;
                        }
                        int ADCDataTemp = (ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2];
                        if ((ADCDataTemp & 0x800000) != 0x00)
                        {
                            ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp));
                        }
                        double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000;
                        Console.WriteLine("ADC_CH[{0}][{1}] = {2} V", i, Channel == 0 ? 7 : Channel - 1, Volutage);
                    }
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Int32[] DevHandles = new Int32[20];
            UInt32  PinValue   = 0;

            Console.WriteLine("扫描设备...");
            int DevCnt = usb_device.USB_ScanDevice(DevHandles);//获取设备连接数,同时获取每个设备的序号

            if (DevCnt <= 0)
            {
                Console.WriteLine("获取设备列表失败!当前可能无设备连接!");
                return;
            }

            Console.Write("扫描到{0}个设备,每个设备的序号分别为:", DevCnt);
            for (int i = 0; i < DevCnt; i++)
            {
                Console.Write(DevHandles[i].ToString("X8") + "  ");
            }

            Console.WriteLine("\n打开第一个设备");
            bool state = usb_device.USB_OpenDevice(DevHandles[0]);

            if (!state)
            {
                Console.WriteLine("Open device error!" + DevHandles[0].ToString("X8"));
            }
            //设置GPIO电平(需要适配器支持)
            usb_device.DEV_SetPowerLevel(DevHandles[0], usb_device.POWER_LEVEL_3V3);
            //输出测试——没上下拉
            USB2GPIO.GPIO_SetOutput(DevHandles[0], 0xFFFF, 0);
            for (int i = 0; i < 10; i++)
            {
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0xAAAA);
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0x5555);
            }
            //输出测试——上拉
            USB2GPIO.GPIO_SetOutput(DevHandles[0], 0xFFFF, 1);
            for (int i = 0; i < 10; i++)
            {
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0xAAAA);
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0x5555);
            }
            //输出测试——下拉
            USB2GPIO.GPIO_SetOutput(DevHandles[0], 0xFFFF, 2);
            for (int i = 0; i < 10; i++)
            {
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0xAAAA);
                USB2GPIO.GPIO_Write(DevHandles[0], 0xFFFF, 0x5555);
            }
            //测试输入——浮空
            USB2GPIO.GPIO_SetInput(DevHandles[0], 0xFFFF, 0);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(Float): " + PinValue.ToString("X4"));
            //测试输入——上拉输入
            USB2GPIO.GPIO_SetInput(DevHandles[0], 0xFFFF, 1);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(Pu): " + PinValue.ToString("X4"));
            //测试输入——下拉输入
            USB2GPIO.GPIO_SetInput(DevHandles[0], 0xFFFF, 2);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(Pd): " + PinValue.ToString("X4"));
            //测试开漏输入——浮空
            USB2GPIO.GPIO_SetOpenDrain(DevHandles[0], 0xFFFF, 0);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(OD-Float): " + PinValue.ToString("X4"));
            //测试开漏输入——上拉输入
            USB2GPIO.GPIO_SetOpenDrain(DevHandles[0], 0xFFFF, 1);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(OD-Pu): " + PinValue.ToString("X4"));
            //测试开漏输入——下拉输入
            USB2GPIO.GPIO_SetOpenDrain(DevHandles[0], 0xFFFF, 2);
            USB2GPIO.GPIO_Read(DevHandles[0], 0xFFFF, ref PinValue);
            Console.WriteLine("READ DATA(OD-Pd): " + PinValue.ToString("X4"));
            //关闭设备
            usb_device.USB_CloseDevice(DevHandles[0]);
        }