AOI

 找回密码
 立即注册
查看: 37|回复: 0

USBI2C实现模拟信号控制

[复制链接]

124

主题

157

帖子

845

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
845
发表于 5 天前 | 显示全部楼层 |阅读模式
USBI2C实现模拟信号控制:
[1]CH341T二合一模块 USB转I2C IIC UART USB转TTL 单片机串口下载器
[2]ADS1115 16位 860SPS 4通道 ADC模数转换模块_紫板_蓝板
[3]心率/心跳速率传感器 脉搏生物模拟监测感应器模块 pulsesensor
技术参考:
【1】51 驱动 ADS1115 AD采集
【2】CH341PAR.ZIP--32/64bit下载
github代码参考:
【1】https://github.com/ZhangGaoxing/windows-iot-demo
【2】https://cloud.tencent.com/developer/article/1130230
【3】https://github.com/webspiderteam/TestCH341
代码如下:
  1.   private void writeReg()
  2.   {
  3.       //ADS1115(AddressSetting.GND, InputMultiplexeConfig.AIN0, PgaConfig.FS4096, DataRate.SPS860);
  4.       byte adcAddr = (byte)AddressSetting.GND;
  5.       byte adcMux = (byte)InputMultiplexeConfig.AIN0;
  6.       byte adcPga = (byte)PgaConfig.FS6144;
  7.       byte adcRate = (byte)DataRate.SPS64;

  8.       const byte ADC_CONVERSION_REG_ADDR = 0x00;
  9.       const byte ADC_CONFIG_REG_ADDR = 0x01;

  10.       byte configHi = (byte)((0x01 << 7) + (0x04 << 4) +
  11.           (adcPga << 1) +
  12.           (byte)DeviceMode.Continuous);

  13.       byte configLo = (byte)((adcRate << 5) +
  14.                       ((byte)(ComparatorMode.Traditional) << 4) +
  15.                       ((byte)ComparatorPolarity.Low << 3) +
  16.                       ((byte)ComparatorLatching.NonLatching << 2) +
  17.                       (byte)ComparatorQueue.Disable);

  18.       byte[] writebuf = new byte[1];
  19.       byte[] buf = new byte[1];
  20.       //writebuf[0] = 0x90;
  21.       writebuf = new byte[] { 0x90 };
  22.       CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
  23.       Thread.Sleep(10);
  24.       //writebuf[0] = ADC_CONFIG_REG_ADDR;
  25.       writebuf = new byte[] { 0x01 };
  26.       CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
  27.       Thread.Sleep(10);
  28.       writebuf[0] = configHi;
  29.       CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
  30.       Thread.Sleep(10);
  31.       writebuf[0] = configLo;
  32.       CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
  33.       Thread.Sleep(10);
  34.   }

  35.   private void timer1_Tick(object sender, EventArgs e)
  36.   {
  37.       //writeReg(); // 写
  38.       const byte ADC_CONVERSION_REG_ADDR = 0x00;
  39.       byte[] writebuf = new byte[2];
  40.       byte[] buf = new byte[2];
  41.       //while (true)
  42.       {
  43.           writebuf = new byte[] { 0x90 };
  44.           CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
  45.           Thread.Sleep(10);
  46.           writebuf = new byte[] { ADC_CONVERSION_REG_ADDR };
  47.           CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
  48.           Thread.Sleep(10);
  49.           writebuf = new byte[] { 0x90 + 1 };
  50.           CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
  51.           Thread.Sleep(10);
  52.           //
  53.           //Array.Reverse(buf);
  54.           //short val = BitConverter.ToInt16(buf, 0);
  55.           //double data = RawToVoltage(val);
  56.           short val;
  57.           double data = 0;
  58.           if ((byte)(buf[0] & 0x80) != 0x00)
  59.           {
  60.               // two's complement conversion (two's complement byte array to int16)
  61.               buf[0] = (byte)~buf[0];
  62.               buf[0] &= 0xEF;
  63.               buf[1] = (byte)~buf[1];
  64.               Array.Reverse(buf);
  65.               val = Convert.ToInt16(-1 * (BitConverter.ToInt16(buf, 0) + 1));
  66.           }
  67.           else
  68.           {
  69.               Array.Reverse(buf);
  70.               val = BitConverter.ToInt16(buf, 0);
  71.           }
  72.           //
  73.           data = RawToVoltage(val);
  74.           //
  75.           lock (_queueLock)
  76.           {
  77.               // 加入队列,超过100个则移除最早的
  78.               _dataQueue.Enqueue(data);
  79.               if (_dataQueue.Count >= 100)
  80.               {
  81.                   _dataQueue.Dequeue();
  82.               }
  83.               // 打印电压信号
  84.               this.richTextBox1.AppendText(data.ToString() + "\n");
  85.               Thread.Sleep(10);
  86.               UpdateChart(); // 更新Chart显示
  87.               Thread.Sleep(10);
  88.           }
  89.       }

  90.   }
复制代码






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Opencv|MATLAB|Python|WiTiAi ( 蜀ICP备2021019707号 )

GMT+8, 2025-12-3 17:18 , Processed in 0.155910 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表