|
本帖最后由 gxg0000 于 2020-2-8 11:20 编辑
闲来无事,写上一段用c#编写的应用程序盼望交流,实际应用见本坛《精确测量二极管RD的方法》一贴
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.InteropServices;
- using Microsoft.Win32;
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Int32 V1, V2, V3, V4;
- Double Id1, Id2;
- Int32 Index; //
- Int64[] R = new Int64[7]; //限流电阻R1, R2, R3, R4, R5, R6, R7
- NumericUpDown[] numericUpDown = new NumericUpDown[7];
- String[] str1 ={ "R1", "R2", "R3", "R4", "R5", "R6", "R7" }; //电阻的编号数组
- Decimal[] str2 ={ 4700.0M, 1000.0M, 330.0M, 100.0M, 33.0M, 10.0M, 10000M }; //电阻初值数组
- Int32 T=25; //温度
- Double IS = 171.34388778833204; //饱和电流
- Double RD = 160.40886801322117; //零电阻
- Double N = 1.0693751430533303; //理想因子
- Double VT = 0.025679647254953728; //热电压
- Double[] set1 ={ 1.0, 1.0, 1.0, 1.0 }; //IS,RD,N,VT
- Double[] set2 ={ 1.0, 1.0, 1.0, 1.0 };
- Double[] set3 ={ 1.0, 1.0, 1.0, 1.0 };
- Double[] set4 ={ 1.0, 1.0, 1.0, 1.0 };
- Double[] set5 ={ 1.0, 1.0, 1.0, 1.0 };
- Label[] lb1 = new Label[7];
- Label[] lb2 = new Label[7];
- Label[] lb3 = new Label[6];
- Bitmap tup1;
- Graphics g1;
- private void Form1_Load(object sender, EventArgs e)
- {
- this.listBox1.LostFocus += new System.EventHandler(this.listBox1_LostFocus); //添加失去焦点事件
- this.Controls.Add(panel1);
- panel1.BringToFront();
- panel1.Left = 522;
- panel1.Top = 270;
- panel1.Visible = false;
- for (int i = 0; i < 7; i++)
- {
- numericUpDown[i] = new NumericUpDown();
- numericUpDown[i].Cursor = Cursors.Hand;
- numericUpDown[i].Width = 57;
- numericUpDown[i].Height = 21;
- numericUpDown[i].Top = 33 + 27 * i;
- numericUpDown[i].Left = 102;
- numericUpDown[i].Minimum = 0M;
- if (i == 6)
- {
- numericUpDown[i].DecimalPlaces = 0;
- numericUpDown[i].Maximum = 20000M;
- numericUpDown[i].Increment = 1M;
- }
- else
- {
- numericUpDown[i].DecimalPlaces = 1;
- numericUpDown[i].Maximum =10000M;
- numericUpDown[i].Increment = 0.1M;
- }
- this.panel1.Controls.Add(numericUpDown[i]); //添加到控件集合
- numericUpDown[i].BringToFront();
- }
- //读取注册表保存的设置
- if (null == Registry.GetValue(@"HKEY_CURRENT_USER\Software\二极管参数计算\Settings", "R1", ""))
- {
- //第1次创建注册表子项,并将初值数据写入内存和注册表
- for (Int32 i = 0; i < 7; i++)
- {
- R[i] = Convert.ToInt64(str2[i]);
- numericUpDown[i].Value = str2[i];
- Registry.SetValue(@"HKEY_CURRENT_USER\Software\二极管参数计算\Settings", str1[i], str2[i]);
- }
- }
- else
- {
- //读取注册表保存的子项内数据,并写入界面
- for (Int32 i = 0; i < 7; i++)
- {
- numericUpDown[i].Value = Convert.ToDecimal(Registry.GetValue(@"HKEY_CURRENT_USER\Software\二极管参数计算\Settings", str1[i], ""));
- }
- }
- Index = 3;
- comboBox1.SelectedIndex = Index;
- label6.Text = "电阻" + str1[Index] + ":";
- listBox1.SelectedIndex = 1;
- listBox1.Visible = false;
- Drawing_Transfer_Characteristic(); //绘制曲线图形
- System.IO.Directory.CreateDirectory("C:\\Documents and Settings\\User\\My Documents\\二极管参数表"); //创建保存数据的文件夹
- }
- /// <summary>
- /// 计算Is,N,Rd
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- V1 = Convert.ToInt32(1000 * numericUpDown1.Value);
- V2 = Convert.ToInt32(1000 * numericUpDown2.Value);
- V3 = Convert.ToInt32(1000 * numericUpDown3.Value);
- V4 = Convert.ToInt32(1000 * numericUpDown4.Value);
- for (Int32 i = 0; i < 7; i++)
- {
- R[i] = Convert.ToInt64(1000 * numericUpDown[i].Value);
- }
- for (Int32 i = 0; i < 6; i++)
- {
- R[i] = (R[i] * R[6]) / (R[i] + R[6]); //并联10M电阻后,修正DVM带来的误差
- }
- T = Convert.ToInt32(numericUpDown5.Value);
- VT = ((273 + T) * 1.380649 / 1.602177) / 10000; //计算热电压
- Id1 = 1000.0 * V2 / R[comboBox1.SelectedIndex] - 1000.0 * V1 / R[6]; //计算二极管电流(单位nA)
- Id2 = 1000.0 * V4 / R[comboBox1.SelectedIndex] - 1000.0 * V3 / R[6];
- Int32 n = 0;
- Double Is1=0, Is2=0;
- for (Int32 i = 0; i < 16; i++) //16位逐次逼近法
- {
- n |= (1 << (15 - i)); //预置转换位
- N = 0.5 + n / 21845.0; //N(0.5~3.5)
- Is1 = Id1 / (Math.Exp(V1 / (VT * 1000000 * N)) - 1);
- Is2 = Id2 / (Math.Exp(V3 / (VT * 1000000 * N)) - 1);
- if (Is1 > Is2) //
- {
- n &= ~(1 << (15 - i)); //清零该位
- N = 0.5 + n / 21845.0;
- }
- }
- if (Is1 > Is2)
- {
- if ((Is1 - Is2)/Is1 > 0.001)
- MessageBox.Show("方程无解!"); //误差超过0.1%,表示方程无法解出
- }
- else
- {
- if ((Is2 - Is1) /Is2> 0.001)
- MessageBox.Show("方程无解!"); //显示错误
- }
- IS = (Is1 + Is2) / 2;
- RD=1000000 * (VT * N / IS);
- label26.Text = Convert.ToString(Convert.ToUInt32(IS));
- label27.Text = Convert.ToString(Convert.ToUInt32(RD));
- label28.Text = String.Format("{0:0.00}", N);
- label16.Text = String.Format("{0:样本数据1: Vd1=0.0 mV}", Convert.ToDouble(numericUpDown1.Value));
- label38.Text = String.Format("{0:Id1=0 nA}", Id1);
- label17.Text = String.Format("{0:样本数据2: Vd2=0.0 mV}", Convert.ToDouble(numericUpDown3.Value));
- label39.Text = String.Format("{0:Id2=0 nA}", Id2);
- label18.Text = String.Format("{0:热电压: VT=0.0 mV}", VT * 1000);
- label29.Text = Convert.ToString(numericUpDown5.Value) + "℃";
- Drawing_Transfer_Characteristic(); //绘制转移特性曲线
- }
- /// <summary>
- /// 绘制二极管转移特性函数
- /// </summary>
- /// <param name="x">电压</param>
- /// <param name="y">电流</param>
- private void Drawing_Transfer_Characteristic()
- {
- tup1 = new Bitmap(201, 201);
- g1 = Graphics.FromImage(tup1);
- String[] str = new String[] { "正", "向", "电", "流", "IF", " " };
- //写垂直数字刻度
- for (int i = 0; i < 6; i++)
- {
- lb1[i] = new Label();
- lb1[i].BackColor = this.tabPage5.BackColor; //背景颜色(取底色)
- lb1[i].Width = 30;
- lb1[i].Height = 15;
- lb1[i].TextAlign = ContentAlignment.MiddleRight;
- if (i == 5)
- lb1[i].Top = 20 + (40 * i - 3);
- else
- lb1[i].Top = 20 + 40 * i;
- lb1[i].Left = pictureBox1.Left - lb1[i].Width;
- lb1[i].Font = new Font("宋体", 9, FontStyle.Regular);
- lb1[i].Text = Convert.ToString((5 - i) * Convert.ToUInt32(numericUpDown6.Value) / 5);
- this.tabPage5.Controls.Add(lb1[i]); //添加到控件集合
- lb1[i].BringToFront();
- }
- for (int i = 0; i < 6; i++)
- {
- lb3[i] = new Label();
- lb3[i].BackColor = this.tabPage5.BackColor; //背景颜色(取底色)
- lb3[i].Width = 35;
- lb3[i].Height = 15;
- lb3[i].TextAlign = ContentAlignment.MiddleCenter;
- lb3[i].ForeColor = Color.Green;
- lb3[i].Top = 80 + 15 * i;
- lb3[i].Left = 0;
- lb3[i].Font = new Font("宋体", 9, FontStyle.Bold);
- if (i == 5)
- lb3[i].Text = "(" + button2.Text.Substring(0, 2) + ")";
- else
- lb3[i].Text = str[i];
- this.tabPage5.Controls.Add(lb3[i]); //添加到控件集合
- lb3[i].BringToFront();
- }
- //写水平数字刻度
- for (int i = 0; i < 7; i++)
- {
- lb2[i] = new Label();
- lb2[i].BackColor = this.tabPage5.BackColor; //背景颜色(取底色)
- if (i != 6)
- {
- lb2[i].Width = 30;
- lb2[i].Height = 15;
- lb2[i].Top = 235;
- lb2[i].Left = 47 + 40 * i;
- lb2[i].Text = Convert.ToString(i *Convert.ToUInt32(numericUpDown7.Value) / 5);
- lb2[i].Font = new Font("宋体", 9, FontStyle.Regular);
- lb2[i].ForeColor = Color.Black;
- }
- else
- {
- lb2[i].Width = 100;
- lb2[i].Height = 15;
- lb2[i].Top = 255;
- lb2[i].Left =120;
- lb2[i].Text = "正向电压VF(mV)";
- lb2[i].Font = new Font("宋体", 9, FontStyle.Bold);
- lb2[i].ForeColor = Color.Green;
- }
- lb2[i].TextAlign = ContentAlignment.MiddleCenter;
- this.tabPage5.Controls.Add(lb2[i]); //添加到控件集合
- lb2[i].BringToFront();
- }
- g1.Clear(Color.AliceBlue);
- //横线
- for (int j = 0; j <= 200; j += 20)
- {
- g1.DrawLine(new Pen(Color.Gainsboro), 0, j, 200, j);
- }
- //竖线
- for (int j = 0; j <= 200; j += 20)
- {
- g1.DrawLine(new Pen(Color.Gainsboro), j, 0, j, 200);
- }
- if (checkBox1.Checked == true) //绘制叠加1
- {
- Draw_Line(set1[0], set1[2], set1[3], button5.BackColor);
- }
- if (checkBox2.Checked == true) //绘制叠加2
- {
- Draw_Line(set2[0], set2[2], set2[3], button6.BackColor);
- }
- if (checkBox3.Checked == true) //绘制叠加3
- {
- Draw_Line(set3[0], set3[2], set3[3], button7.BackColor);
- }
- if (checkBox4.Checked == true) //绘制叠加4
- {
- Draw_Line(set4[0], set4[2], set4[3], button8.BackColor);
- }
- if (checkBox5.Checked == true) //绘制叠加5
- {
- Draw_Line(set5[0], set5[2], set5[3], button9.BackColor);
- }
- Draw_Line(IS, N, VT, Color.Green); //绘制当前二极管伏安曲线
- pictureBox1.Image = tup1;
- }
- private void Draw_Line(Double Is, Double n, Double vt, Color color) //绘制二极管伏安线函数
- {
- g1.DrawImage(tup1, 0, 0);
- Double I1, I2, Vd; ;
- Int32 k = Convert.ToInt32(Convert.ToString(listBox1.SelectedItem).Substring(4));
- for (int V = 0; V < 200; V++)
- {
- Vd = (V / 200.0) * Convert.ToUInt32(numericUpDown7.Value); //mV
- I1 = 200 * (Is * (Math.Exp(Vd / (1000 * vt * n)) - 1)) / (Convert.ToInt32(numericUpDown6.Value) * k); //nA
- if (I1 > 200)
- I1 = 200;
- Vd = ((V + 1) / 200.0) * Convert.ToUInt32(numericUpDown7.Value); //mV
- I2 = 200 * (Is * (Math.Exp(Vd / (1000 * vt * n)) - 1)) / (Convert.ToInt32(numericUpDown6.Value) * k); //nA
- if (I2 > 200)
- I2 = 200;
- g1.DrawLine(new Pen(color), (float)V, (float)(200 - I1), (float)(V + 1), (float)(200 - I2));
- if ((I1 == 200) | (I2 == 200))
- break;
- }
- }
- /// <summary>
- /// 绘图XY坐标单位调整
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- listBox1.Visible = true; //显示电流单位列表
- listBox1.Focus(); //设置listBox1控件焦点
- }
- private void listBox1_LostFocus(object sender, EventArgs e)
- {
- listBox1.Visible = false; //失去焦点隐藏
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- button2.Text = listBox1.Text;
- listBox1.Visible = false;
- Drawing_Transfer_Characteristic();
- }
- private void numericUpDown6_ValueChanged(object sender, EventArgs e)
- {
- Drawing_Transfer_Characteristic();
- }
- private void numericUpDown7_ValueChanged(object sender, EventArgs e)
- {
- Drawing_Transfer_Characteristic();
- }
复制代码
公开源代码;
补充内容 (2020-2-8 17:12):
谢谢老师们点赞!
补充内容 (2021-2-24 12:26):
13#是升级版 |
评分
-
3
查看全部评分
-
|