Скачиваний:
93
Добавлен:
16.07.2022
Размер:
3.93 Mб
Скачать

Приложение г Функции для работы с генератором на языке Си

Ниже приведены типы данных, определенные для работы с библиотекой функций.

/*

* Pointer to a device handle

*

*/

typedef FT_HANDLE GHandle;

/*

* Signal forms

*

*/

typedef enum _signalForm {

FORM_SIN = 0,

FORM_TRIANGLE,

FORM_SQUARE

} SignalForm;

/*

* Reserved addreses for calibration data

*

*/

typedef enum _address {

ADRS_SIN_1 = 0,

ADRS_SIN_2,

ADRS_TRIANGLE_1,

ADRS_TRIANGLE_2,

ADRS_SQUARE_1,

ADRS_SQUARE_2,

ADRS_ERASE = 0xF

} Address;

/*

* Operation codes

*

*/

typedef enum _code {

FREQ_CODE = 0x80,

FORM_CODE = 0x90,

AMPL_CODE = 0xA0,

C_SET_CODE = 0xB0,

C_GET_CODE = 0xC0

} Code;

С использованием типов данных, указанных выше были написаны функции для работы с устройством, код которых указан ниже.

#include "dds.h"

/*

* Function: open

* -------------------

* opens the device with particular description

*

* handle: pointer to a device handle

* description: USB name of the device

*

* returns: not zero when there is no errors.

* return zero on error (device closed, io error)

*

*/

boolean open_g(GHandle * handle, char * description)

{

FT_STATUS status =

FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, handle);

if (status == FT_OK)

status = FT_SetTimeouts(*handle, 1000, 1000); // Read and write timeout 1sec

return status == FT_OK;

}

/*

* Function: close

* -------------------

* closes device handle

*

* handle: pointer to a device handle

*

* returns: not zero when there is no errors

* return zero on error (device closed, io error)

*/

boolean close_g(GHandle handle)

{

FT_STATUS status = FT_Close(handle);

return status == FT_OK;

}

/*

* Function: setFreq

* -------------------

* writes frequency to the device

*

* handle: pointer to a device handle

* freqCode: frequency code (0 - 0xfffffff)

* real frequency may be computed by multiplication on 0.02Hz

*

* returns: not null when there is no errors.

* return zero on error (device closed, io error)

*/

boolean setFreq_g(GHandle handle, unsigned int freqCode)

{

FT_STATUS status;

DWORD written;

unsigned int header = FREQ_CODE;

status = FT_Write(handle, &header, 1, &written);

if (status != FT_OK)

return FALSE;

status = FT_Write(handle, &freqCode, 4, &written);

return status == FT_OK && written == 4;

}

/*

* Function: setAmpl

* -------------------

* writes amplitude to the device

*

* handle: pointer to a device handle

* amplCode: amplitude code (0 - 0xffff)

*

* returns: not null when there is no errors.

* return zero on error (device closed, io error)

*/

boolean setAmpl_g(GHandle handle, unsigned int amplCode)

{

FT_STATUS status;

DWORD written;

unsigned int header = AMPL_CODE;

status = FT_Write(handle, &header, 1, &written);

if (status != FT_OK)

return FALSE;

status = FT_Write(handle, &amplCode, 4, &written);

return status == FT_OK && written == 4;

}

/*

* Function: setForm

* -------------------

* writes signal form to the device

*

* handle: pointer to a device handle

* form: one of three possible signal forms (sin, triangle, square)

*

* returns: not null when there is no errors.

* return zero on error (device closed, io error)

*/

boolean setForm_g(GHandle handle, SignalForm form)

{

FT_STATUS status;

DWORD written;

unsigned int message = FORM_CODE | form;

status = FT_Write(handle, &message, 1, &written);

return status == FT_OK && written == 1;

}

/*

* Function: setFlashDataInt

* -------------------

* writes int data to the device flash memory on address adrs

*

* handle: pointer to a device handle

* adrs: address to write data

* data: writting data

*

* returns: not null when there is no errors.

* return zero on error (device closed, io error)

*/

boolean setFlashDataInt_g

(GHandle handle, Address adrs, int data)

{

FT_STATUS status;

DWORD written;

unsigned int message = C_SET_CODE | adrs;

status = FT_Write(handle, &message, 1, &written);

if (status != FT_OK)

return FALSE;

status = FT_Write(handle, &data, 4, &written);

return status == FT_OK && written == 4;

}

/*

* Function: setFlashDataFloat

* -------------------

* writes float data to the device flash memory on address adrs

*

* handle: pointer to a device handle

* adrs: address to write data

* data: writting data

*

* returns: not null when there is no errors.

* return zero on error (device closed, io error)

*/

boolean setFlashDataFloat_g

(GHandle handle, Address adrs, float data)

{

FT_STATUS status;

DWORD written;

unsigned int message = C_SET_CODE | adrs;

status = FT_Write(handle, &message, 1, &written);

if (status != FT_OK)

return FALSE;

status = FT_Write(handle, &data, 4, &written);

return status == FT_OK && written == 4;

}

/*

* Function: setFlashData

* -------------------

* reads int data from device flash memory

*

* handle: pointer to a device handle

* adrs: address to read data

*

* returns: read data from device flash memory

* return -1 on error (device closed, io error)

*/

int getFlashDataInt_g(GHandle handle, Address adrs)

{

FT_STATUS status;

int message;

DWORD header = C_GET_CODE | adrs;

DWORD written;

status = FT_Write(handle, &header, 1, &written);

status = FT_Read(handle, &header, 1, &written);

status = FT_Read(handle, &message, 4, &written);

if (status != FT_OK || written != 4)

return -1;

return message;

}

/*

* Function: setFlashData

* -------------------

* reads float data from device flash memory

*

* handle: pointer to a device handle

* adrs: address to read data

*

* returns: read data from device flash memory

* return -1 on error (device closed, io error)

*/

float getFlashDataFloat_g(GHandle handle, Address adrs)

{

FT_STATUS status;

float message;

DWORD header = C_GET_CODE | adrs;

DWORD written;

status = FT_Write(handle, &header, 1, &written);

status = FT_Read(handle, &header, 1, &written);

status = FT_Read(handle, &message, 4, &written);

if (status != FT_OK || written != 4)

return -1;

return message;

}