
'============================================================
'By: Steve Hageman
'Written: 26Dec98
'Purpose: SigGen Hardware Control Routines
'Notes: All module variables are accessed by Set / Get routines.
'Requires:  1) Read some cal factors and stuff from IniFile.bas
'           2) Known com port connection and address.
'============================================================

'============================================================
'By: Steve Hageman
'Written: 06Jul99
'Purpose: Sets the amplitude cal information
'Inputs: amplitude and frequency linear regression coefficients.
'Outputs: none
'Dependencies: Should set this first! before calling initialize
'Comments:
'============================================================
Public Sub Set_Amplitude_Cal_Constants(ByVal freq_a!, ByVal freq_b!, ByVal amp_a!, ByVal amp_b!)


'============================================================
'By: Steve Hageman
'Written: 05Jun99
'Purpose: Sets the spur avoidance state.
'Inputs: TRUE or FALSE
'Outputs: Sets the local module global
'============================================================
Public Sub Set_Spur_Avoidance(ByVal state As Boolean)


'============================================================
'By: Steve Hageman
'Written: 05Jun99
'Purpose: Sets the cal state
'Inputs: TRUE or FALSE
'Outputs: Sets the module level global
'============================================================
Public Sub Set_Cal_State(ByVal state As Boolean)


'============================================================
'By: Steve Hageman
'Written: 07Jun99
'Purpose: Sets the local error correction value
'Inputs: gain_error! as a percent
'Outputs: Sets the local global FrequencyGainError!
'============================================================
Public Sub Set_Frequency_Correction(gain_error!)


'============================================================
'By: Steve Hageman
'Written: 07Jun99
'Purpose: Gets the local error correction value
'Inputs: none
'Outputs: Gets the local global FrequencyGainError! (in %)
'============================================================
Public Function Get_Frequency_Correction!()


'============================================================
'By: Steve Hageman
'Written: 07Jun99
'Purpose: Sets the local error correction value
'Inputs: offset_error! in dBm
'Outputs: Sets the local global AmplitudeOffsetError!
'============================================================
Public Sub Set_Amplitude_Correction(offset_error!)


'============================================================
'By: Steve Hageman
'Written: 05Jun99
'Purpose: Sets the connected RS232 port.
'Inputs: Port number (1-16)
'Outputs: Sets the module level global.
'============================================================
Public Sub Set_Connected_Port(ByVal port%)


'============================================================
'By: Steve Hageman
'Written: 05Jun99
'Purpose: Returns the currently connected port.
'Inputs: none
'Outputs: Connected port (1-16)
'============================================================
Public Function Get_Connected_Port%()


'============================================================
'By: Steve Hageman
'Written: 10Apr99
'Purpose: Sets the hardware address
'Inputs: address (0-3)(bounds the value)
'Outputs:
'============================================================
Public Sub Set_Hardware_Address(ByVal new_addr%)


'============================================================
'By: Steve Hageman
'Written: 10Apr99
'Purpose:
'Inputs:
'Outputs:
'============================================================
Public Function Get_Hardware_Address%()


'============================================================
'By: Steve Hageman
'Written: 05Jun99
'Purpose: Returns the firmware version
'Inputs: The signal generator needs to be found and initialized
' on a port before this will return proper information
'Outputs: Returns an single of the firmware version
'============================================================
Public Function Get_Firmware_Version!()


'============================================================
'By: Steve Hageman
'Written: 06Jul99
'Purpose:  Initilizes the whole driver. Call this before anything
' else to set all the constants to some reasonable default state.
' Then set the parameters you wish, then call hardware_initilize.
'Inputs: none
'Outputs: Sets various local globals
'Dependencies: many
'Comments: The basic rule here is to set a value to everything
'that may not get calculated within the first call to a hardware
'function.
'============================================================
Public Function Initilize_SigGen_Driver()


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Initilizes the Sig Gen Hardware. Call this routine
' after the hardware has been found and the Ini file has been
' read.
'Inputs: none
'Outputs: none
'============================================================
Public Sub Initilize_SigGen_Hardware()


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Returns the status of the PLL's and leveling loop
'Inputs: None
'Outputs: values set are,
'   PLL0 = TRUE if locked, FALSE if unlocked
'   PLL1 = TRUE if locked, FALSE if unlocked
'   LEVEL = TRUE if leveling loop is leveled, FALSE if unleveled
'============================================================
Public Sub SigGen_Status(ByRef pll_0%, ByRef pll_1%, ByRef level%)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Given a possible NCO frequency (long) this function
' will return a legally bounded value within the range specified
' in the constants as defined in this module.
'Inputs: Frequency (long)
'Outputs: Bound Frequency (long) withing the limiting values
' defined.
'============================================================
Public Function Bound_NCO_Frequency&(ByVal freq&)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Returns the current NCO frequency setting.
'Inputs: None
'Outputs: NCO Frequency (Hz) (Long)
'============================================================
Public Function Get_NCO_Frequency&()


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Sets the local variable that contains the NCO frequency
' and sets the NCO at the hardware level.
' NOTE: should call Bound_Frequency before using this routine
'Inputs: frequency (long)
'Outputs: none
'Notes: No error correction is done in this routine. The error
'correction is all done in the Set_Frequency routine.
'============================================================
Public Sub Set_NCO_Frequency(ByVal frequency&)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Given a possible signal generator frequency this
' function will return a legally bounded value as defined
' in the constants defined in this module.
'Inputs: frequency (long)
'Outputs: bound frequency (long)
'============================================================
Public Function Bound_SigGen_Frequency&(ByVal freq&)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Returns the current signal generator frequency
'Inputs: none
'Outputs: sig gen frequency (long)
'============================================================
Public Function Get_SigGen_Frequency&()


'============================================================
'By: Steve Hageman
'Written: 02Jun99
'Purpose: Gets the output level
'Inputs: none
'Outputs: The currently set output level
'============================================================
Public Function Get_Level!()


'============================================================
'By: Steve Hageman
'Written: 07Jun99
'Purpose:
'Inputs:
'Outputs:
'============================================================
Public Function Get_Level_Dac_Value%()


'============================================================
'By: Steve Hageman
'Written: 25Jun99
'Purpose: Bounds the level
'Inputs: The level to set to
'Outputs: Either the level requested or the bound value
'============================================================
Public Function Bound_Level!(ByVal level!)


'============================================================
'By: Steve Hageman
'Written: 02Jun99
'Purpose: Sets the leveling DAC for the proper output level
'Inputs: level! the dBm level to set the output to
'Outputs: none
'============================================================
Public Sub Set_Level(ByVal level!)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Sets the signal generator frequency to the value
' given. Make a call to bound frequency first to make sure that
' the value is legal. This function actually sets the hardware.
'Inputs: frequency (long)
'Outputs: none
'Notes: On the frequency error value. The error value is defined
'as the observed output error which is the combination of both
'PLL errors. But since the observed error is reletive to both
'errors, i.e. the total observed output frequency error, I only
'need to correct the PLL_0 value.
'
' Also calls the Set_Level routine to reset the level when the
' frequency changes.
'============================================================
Public Sub Set_SigGen_Frequency(ByVal freq&)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Sets the pll specified's N register value
'Inputs: N value, PLL number
'Outputs: none
'============================================================
Private Sub Set_PLL_N_Register(ByVal n_reg%, ByVal pll_number%)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Sets the pll specified's A register value
'Inputs: A value, PLL number
'Outputs: none
'============================================================
Private Sub Set_PLL_A_Register(ByVal a_reg%, ByVal pll_number%)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Sets the pll specified's R register value
'Inputs: R value, PLL number
'Outputs: none
'Note: This is set at startup and should not be messed with
'afterwards, unless you know what you are doing!
'============================================================
Private Sub Set_PLL_R_Register(ByVal r_reg%, ByVal pll_number%)


'============================================================
'By: Steve Hageman
'Written: 20Feb99
'Purpose: Updates the specified PLL in hardware. This actually
' updates the hardware.
'Inputs: PLL Number, NOTE: The A and N regs for this PLL must
' have been set first!!!
'Outputs: none
'============================================================
Private Sub Update_PLL(ByVal pll_number%)


