|
|
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
代码如下:
- private void writeReg()
- {
- //ADS1115(AddressSetting.GND, InputMultiplexeConfig.AIN0, PgaConfig.FS4096, DataRate.SPS860);
- byte adcAddr = (byte)AddressSetting.GND;
- byte adcMux = (byte)InputMultiplexeConfig.AIN0;
- byte adcPga = (byte)PgaConfig.FS6144;
- byte adcRate = (byte)DataRate.SPS64;
- const byte ADC_CONVERSION_REG_ADDR = 0x00;
- const byte ADC_CONFIG_REG_ADDR = 0x01;
- byte configHi = (byte)((0x01 << 7) + (0x04 << 4) +
- (adcPga << 1) +
- (byte)DeviceMode.Continuous);
- byte configLo = (byte)((adcRate << 5) +
- ((byte)(ComparatorMode.Traditional) << 4) +
- ((byte)ComparatorPolarity.Low << 3) +
- ((byte)ComparatorLatching.NonLatching << 2) +
- (byte)ComparatorQueue.Disable);
- byte[] writebuf = new byte[1];
- byte[] buf = new byte[1];
- //writebuf[0] = 0x90;
- writebuf = new byte[] { 0x90 };
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
- Thread.Sleep(10);
- //writebuf[0] = ADC_CONFIG_REG_ADDR;
- writebuf = new byte[] { 0x01 };
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
- Thread.Sleep(10);
- writebuf[0] = configHi;
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
- Thread.Sleep(10);
- writebuf[0] = configLo;
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], 0, ref buf[0]);
- Thread.Sleep(10);
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- //writeReg(); // 写
- const byte ADC_CONVERSION_REG_ADDR = 0x00;
- byte[] writebuf = new byte[2];
- byte[] buf = new byte[2];
- //while (true)
- {
- writebuf = new byte[] { 0x90 };
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
- Thread.Sleep(10);
- writebuf = new byte[] { ADC_CONVERSION_REG_ADDR };
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
- Thread.Sleep(10);
- writebuf = new byte[] { 0x90 + 1 };
- CH341Tclass.CH341StreamI2C(0, writebuf.Length, ref writebuf[0], buf.Length, ref buf[0]);
- Thread.Sleep(10);
- //
- //Array.Reverse(buf);
- //short val = BitConverter.ToInt16(buf, 0);
- //double data = RawToVoltage(val);
- short val;
- double data = 0;
- if ((byte)(buf[0] & 0x80) != 0x00)
- {
- // two's complement conversion (two's complement byte array to int16)
- buf[0] = (byte)~buf[0];
- buf[0] &= 0xEF;
- buf[1] = (byte)~buf[1];
- Array.Reverse(buf);
- val = Convert.ToInt16(-1 * (BitConverter.ToInt16(buf, 0) + 1));
- }
- else
- {
- Array.Reverse(buf);
- val = BitConverter.ToInt16(buf, 0);
- }
- //
- data = RawToVoltage(val);
- //
- lock (_queueLock)
- {
- // 加入队列,超过100个则移除最早的
- _dataQueue.Enqueue(data);
- if (_dataQueue.Count >= 100)
- {
- _dataQueue.Dequeue();
- }
- // 打印电压信号
- this.richTextBox1.AppendText(data.ToString() + "\n");
- Thread.Sleep(10);
- UpdateChart(); // 更新Chart显示
- Thread.Sleep(10);
- }
- }
- }
复制代码
|
|