李欣 姚英 王淑芬 桑宝晶
摘 要 本文介绍利用Visual Basic 6.0设计PCS-05型程控直流恒流电源控制软件,以达到控制该电流源输出用户所需各种电流方波信号。
关键词 VB,GPIB,接口控制
一、引言 PCS-05型程控直流恒流电源用于输出用户所需的电流信号,但手动进行输出电流调节时往往比较麻烦,还可能造成输出波动。为了解决手动操作仪表所造成的不便,本文基于GPIB总线技术,利用Visual Basic 6.0设计了一种便于操作的PCS-05型程控直流恒流电源控制软件。 二、GPIB总线 GPIB总线(General Purpose Interface Bus),即IEEE-488通用接口总线,是HP公司在70年代推出的台式仪器接口总线,因此又叫HPIB(HP Interface Bus)。该标准总线在仪器、仪表及测控领域得到了广泛的应用。GPIB系统在PC出现的初期问世,存在一定的局限性,但目前仍是仪器、仪表及测控系统与计算机互连的主流并行总线,因为装有GPIB接口的台式仪器的品种和数量都明显超过高速的VXI仪器,以PCI为基础的PXI系统,也都具有GPIB接口。所以,在相当长的时间内,GPIB系统仍将在实际应用中,特别是中、低速范围内的计算机外总线应用中占有一定的市场。 三、软件设计 控制软件包括量程设置、输出方向设置、输出电流设置、输出电压限设置及波形设置等功能。软件界面如下图所示。下面将分别描述各部分功能代码。
图 软件界面
1.标准模块代码 在标准模块(Module1.bas),定义全局变量如下: Public l1, l2 As Long '存放量程 Public f1, f2 As Long '存放方向 Public defrm As Long '存放接口信息 Public vi As Long Public u As Long Public v As Long Public y1, y2 As Long '存放参数设置 Public X1, X3 As Long Public X2, X4 As Long Public D1, D2 As Long Public N1, N2 '时间延时变量 Public start1, start2 2.GPIB编程 本系统采用Agilent 82357A GPIB接口卡,在VB中应包含VISA32.bas模块,其中包括总线控制相关函数,可以完成总线控制和数据采集。 (1)系统初始化 系统初始化即向总线发送一系列预定的总线命令并且向PCS-05发送初始化命令。本系统GPIB总线地址为17,PCS-05地址为1,程序代码如下: Private Sub Command1_Click() ' 打开一个GPIB对话 Call viOpenDefaultRM(defrm) Call viOpen(defrm, "GPIB0::1::INSTR", 0, 0, vi) ' 系统复位 Call viVPrintf(vi, "*RST" + Chr$(10), 0) '系统输出置0,选择正向输出,输出电压限置为127.5V u = 0 v = 1 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 3 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 0 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 6 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 255 v = 10 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 255 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) ' 关闭GPIB对话 Call viClose(vi) Call viClose(defrm) End Sub (2)参数及波形选择 主要的窗体控件:四个ComboBox控件,名称分别为:Combo1、Combo2、Combo3及Combo4。七个TextBox控件,分别是:Text1、Text2、Text3、Text4、Text5、Text6及Text7。一个CommandButton:Command2。 量程及输出方向选择的实现代码如下: Private Sub Combo1_Click() If Combo1.ListIndex = 0 Then l1 = 0 End If If Combo1.ListIndex = 1 Then l1 = 1 End If If Combo1.ListIndex = 2 Then l1 = 2 End If If Combo1.ListIndex = 3 Then l1 = 3 End If If Combo1.ListIndex = 4 Then l1 = 4 End If If Combo1.ListIndex = 5 Then l1 = 5 End If End Sub Private Sub Combo2_Click() If Combo2.ListIndex = 0 Then f1 = 0 End If If Combo2.ListIndex = 1 Then f1 = 8 End If End Sub Private Sub Combo4_Click() If Combo4.ListIndex = 0 Then l2 = 0 End If If Combo4.ListIndex = 1 Then l2 = 1 End If If Combo4.ListIndex = 2 Then l2 = 2 End If If Combo4.ListIndex = 3 Then l2 = 3 End If If Combo4.ListIndex = 4 Then l2 = 4 End If If Combo4.ListIndex = 5 Then l2 = 5 End If End Sub Private Sub Combo3_Click() If Combo3.ListIndex = 0 Then f2 = 0 End If If Combo3.ListIndex = 1 Then f2 = 8 End If End Sub 输出参数设置及波形时间设置的实现代码如下: Private Sub Command2_Click() ' 打开一个GPIB对话 Call viOpenDefaultRM(defrm) Call viOpen(defrm, "GPIB0::1::INSTR", 0, 0, vi) ' 系统复位 Call viVPrintf(vi, "*RST" + Chr$(10), 0) ' 输出置0 u = 0 v = 1 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 3 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 0 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) '设置输出参数及输出波形循环次数 For i = Val(Text7.Text) To 1 Step -1 '设置量程及输出方向 y1 = l1 + f1 u = y1 v = 6 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = y1 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) '设置电压限制 D1 = Val(Text2.Text) * 2 If Text2.Text = "" Then D1 = 255 End If u = D1 v = 10 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = D1 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) '设置输出电流值 X1 = Int(Val(Text1.Text) * 125) X2 = Val(Text1.Text) * 2000 - X1 * 16 u = X1 v = 1 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X1 v = 3 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X2 v = 0 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X2 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) ' 输出波形输出延时1 N1 = Val(Text3.Text) start1 = Timer ' 设置开始暂停的时刻。 Do While Timer < start1 + N1 DoEvents ' 将控制让给其他程序。 Loop '设置量程及输出方向 y2 = l2 + f2 u = y2 v = 6 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = y2 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) '设置电压限制 D1 = Val(Text4.Text) * 2 If Text4.Text = "" Then D1 = 255 End If u = D2 v = 10 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = D2 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) '设置输出电流值 X3 = Int(Val(Text5.Text) * 125) X4 = Val(Text5.Text) * 2000 - X1 * 16 u = X3 v = 1 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X3 v = 3 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X4 v = 0 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = X4 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) ' 输出波形输出延时2 N2 = Val(Text6.Text) start2 = Timer ' 设置开始暂停的时刻 Do While Timer < start2 + N2 DoEvents ' 将控制让给其他程序 Loop Next i '恢复初始化状态 u = 0 v = 1 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 3 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 0 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 6 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 0 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 255 v = 10 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) u = 255 v = 2 WRT$ = Chr$(u) + Chr$(v) Call viWrite(vi, WRT$, 2, actual) Text1.Text = "" Text2.Text = "" Text3.Text = "" Combo1.Text = "" Combo2.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" Combo3.Text = "" Combo4.Text = "" Text7.Text = "" ' 关闭GPIB对话 Call viClose(vi) Call viClose(defrm) End Sub 主窗体实现的核心代码如下: Private Sub Form_Load() Combo1.AddItem "1uA" Combo1.AddItem "10uA" Combo1.AddItem "100uA" Combo1.AddItem "1mA" Combo1.AddItem "10mA" Combo1.AddItem "100mA" Combo1.Text = Combo1.List(0) Combo2.AddItem "正" Combo2.AddItem "负" Combo2.Text = Combo2.List(0) Combo4.AddItem "1uA" Combo4.AddItem "10uA" Combo4.AddItem "100uA" Combo4.AddItem "1mA" Combo4.AddItem "10mA" Combo4.AddItem "100mA" Combo4.Text = Combo1.List(0) Combo3.AddItem "正" Combo3.AddItem "负" Combo3.Text = Combo2.List(0) End Sub 四、结语 利用上述方法,实现了PCS-05型程控直流恒流电源的计算机控制,使用户能简便地操作并使用该电源进行指定电流方波信号的输出,避免了因手动操作引起的误差,保证了电源输出的准确性和可靠性。本程序在VB6.0下调试通过,并能达到所有操作功能。
参考文献 [1] 黄淼云,张学忠,王福成.Visual Basic 控件应用编程实例教程.北京希望电子出版社,2002. [2] Agilent 82357A USB/GPIB Interface for Windows User’s Guide.Agilent Tec-hnologies.
|