Signal Generators

cancel
Showing results for 
Search instead for 
Did you mean: 

How come the waveform generation example for Visual Basic 6 in ".\NI-DAQ\Examples\VBasic\Ao" take 100% CPU while generating a simple sine wave?

Hello, I am trying to generate some simple infinite waves such as sine, cosine, square wave, etc by using PCI-6711 AO card. I've just tried the waveform generation example for Visual Basic 6 from "\VBasic\Ao" But when I ran the compiled Win32 program, the PIII-CPU was 100% in use for generating just a simple sine wave. Can anyone tell me what's going on? Is it because I did't use DMA? or what?
Here's the VB code written by NI. How do I modify it to make it run with less or little CPU consumption? Thank a lot in advance.
' ********************************************************************
'
' Example Program:
' WFMsingleBufAsync.FRM
'
' Description:
' Output a waveform from one analog output channel, using internal
' timing (uses low-level NI-DAQ functions)
'
' Example Category:
' AO
'
' Example Task Types:
' BUF, 1CH, BURST, ASYNC, INTTIM, INTTRIG
'
' List of key parameters:
' iGroup, ulIterations, iWFMstopped, iOpSTART, iOpCLEAR
'
' [Since variables are hardcoded, there is no guarantee that this
' program will work for your setup. This example is simply
' presented as a code snippet of how you can use NI-DAQ functions
' to perform a task.]
'
' List of NI-DAQ Functions used in this example:
' NIDAQMakeBuffer, WFM_Group_Setup, NIDAQErrorHandler, WFM_Scale,
' WFM_Load, WFM_Rate, WFM_ClockRate, WFM_Group_Control, WFM_Check,
' NIDAQYield, AO_VWrite
'
' [NOTE: For further details on each NI-DAQ function, please refer
' to the NI-DAQ On-Line Help (NIDAQPC.HLP).]
'
' Pin Connection Information:
' The analog output signal will be available at AO channel 0. The
' default analog output mode for the DAQ device will be used.
'
' [For further I/O connection details, please refer to your hardware
' User Manual.]
'
' [For further details on how to run this example, please refer to
' the NI-DAQ Examples On-Line Help (NIDAQEx.HLP).]
'
' ********************************************************************
Option Explicit
Option Base 0
'
' Constant for PrintText
'
Const LEN_PRINTTEXT = 4096


' ************************************************************************
' SUBROUTINE: PrintText
' DESCRIPTION: PrintText to desired TextBox (upto 4096 characters)
' INPUTS: txtBox - TextBox to print on
' strText - Text to print
' ************************************************************************
Sub PrintText(txtBox As TextBox, strText As String)

txtBox.Text = Right$(txtBox.Text + strText$ + Chr$(13) + Chr$(10), LEN_PRINTTEXT)

txtBox.SelStart = Len(CStr(txtBox.Text))

DoEvents

End Sub


' ************************************************************************
' SUBROUTINE: cmdExit_Click
' DESCRIPTION: Clean up and exit
' ************************************************************************
Sub cmdExit_Click()

End

End Sub

' ************************************************************************
' SUBROUTINE: Form_Load
' DESCRIPTION: Gets automatically called at startup
' ************************************************************************
Sub Form_Load()


End Sub


' ************************************************************************
' SUBROUTINE: cmdDoOperation_Click
' DESCRIPTION: The main NI-DAQ operations are here
' ************************************************************************
Sub cmdDoOperation_Click()

'
' Local Variable Declarations:


Dim iStatus As Integer
Dim iRetVal As Integer
Dim iDevice As Integer
Dim iNumChans As Integer
Dim iChan As Integer
Static piChanVect(1) As Integer
Dim iGroup As Integer
Static pdBuffer(5000) As Double
Static piBuffer(5000) As Integer
Dim ulCount As Long
Dim ulIterations As Long
Dim iFIFOMode As Integer
Dim iDelayMode As Integer
Dim dUpdateRate As Double
Dim iUpdateTB As Integer
Dim ulUpdateInt As Long
Dim iWhichClock As Integer
Dim iUnits As Integer
Dim iWFMstopped As Integer
Dim ulItersDone As Long
Dim ulPtsDone As Long
Dim iOpSTART As Integer
Dim iOpCLEAR As Integer
Dim iIgnoreWarning As Integer
Dim iYieldON As Integer
Dim i As Long

iDevice% = 1
iNumChans% = 1
iGroup% = 1
ulCount& = 5000
ulIterations& = 1
dUpdateRate# = 1000#
iOpSTART% = 1
iYieldON% = 1

' Temporarily disable buttons for protection from 'DoEvents'
cmdDoOperation.Enabled = False
cmdExit.Enabled = False

iStatus% = NIDAQMakeBuffer(pdBuffer#(0), ulCount&, WFM_DATA_F64)

If (iStatus% = 0) Then

' If buffer was made correctly, then output it.

iStatus% = WFM_Group_Setup(iDevice%, iNumChans%, piChanVect%(0), iGroup%)

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Group_Setup", iIgnoreWarning%)

iStatus% = WFM_Scale(iDevice%, iChan%, ulCount&, 1#, pdBuffer#(0), piBuffer%(0))

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Scale", iIgnoreWarning%)

' NOTE FOR DSA devices... DSA devices can format samples in a
' left-justified format in 32-bit data words. This means that
' the most significant bits of the data word contain the bits
' generated by the converter. When allocating data buffers, be
' sure to account for the 32-bit data width. Even though a
' number of DAQ/SCAN/WFM functions are declared to accept
' pointers to 16-bit data buffers, you should pass pointers to
' 32-bit data buffers.

' Also for DSA devices, remember to call AO_Change_Parameter to
' enable the analog output.

iStatus% = WFM_Load(iDevice%, iNumChans%, piChanVect%(0), piBuffer%(0), ulCount&, ulIterations&, iFIFOMode%)

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Load", iIgnoreWarning%)

' NOTE: If you are using a DSA device, call WFM_Set_Clock
' instead. Refer to NI-DAQ Function Reference Manual for
' details.

iStatus% = WFM_Rate(dUpdateRate#, iUnits%, iUpdateTB%, ulUpdateInt&)

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Rate", iIgnoreWarning%)

iStatus% = WFM_ClockRate(iDevice%, iGroup%, iWhichClock%, iUpdateTB%, ulUpdateInt&, iDelayMode%)

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_ClockRate", iIgnoreWarning%)

Call PrintText(txtStatusBox, "A " + Trim$(Str$(ulCount&)) + "point waveform should be output at a rate of " + Trim$(Str$(dUpdateRate#)) + "updates/sec. ")

iStatus% = WFM_Group_Control(iDevice%, iGroup%, iOpSTART%)

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Group_Control/START", iIgnoreWarning%)

While ((iWFMstopped% = 0) And (iStatus% = 0))

iStatus% = WFM_Check(iDevice%, iChan%, iWFMstopped%, ulItersDone&, ulPtsDone&)

DoEvents

Wend

iRetVal% = NIDAQErrorHandler(iStatus%, "WFM_Check", iIgnoreWarning%)

' CLEANUP - Don't check for errors on purpose.

' Set group back to initial state.

iStatus% = WFM_Group_Control(iDevice%, iGroup%, iOpCLEAR%)

' Set output at 0 volts.

iStatus% = AO_VWrite(iDevice%, iChan%, 0#)

Call PrintText(txtStatusBox, "The waveform generation is done! ")

Else

Call PrintText(txtStatusBox, "The buffer was not made correctly. " + "Check the parameters for NIDAQMakeBuffer. ")

End If


' Re-enable buttons
cmdDoOperation.Enabled = True
cmdExit.Enabled = True

End Sub
0 Kudos
Message 1 of 2
(7,054 Views)
This is just the nature of the while loop in the VB code. The processor is going to execute this loop as fast as possible. The DoEvents in the loop allows the processor to perform other operations if needed. Try opening MAX or some other program and drag windows around at the same time the code is executing. You will notice that the processor load will shift away from the VB6 program to other processes.

If you want to avoid this try adding a function similar to sleep in the while loop. However, this will slow the speed of your program and I would not recommend it, because the program is designed to run as fast as possible, but still allow other programs to execute.

I hope this helps.

Joshua
Message 2 of 2
(7,054 Views)