📄 dm6430-dac.c
字号:
/* FILE NAME: dm6430-dac.c FILE DESCRIPTION: Digital to analog conversion sample program Sample program that demonstrates how to use the analog output. This program uses the digital to analog converters on the A/D board. If you wire the D/A channel to this A/D channel you can monitor the output of the DAC directly on your computer. Alternatively, you can monitor the output of the DAC with an oscilloscope. PROJECT NAME: Linux DM6430 Driver, Library, and Example Programs PROJECT VERSION: (Defined in README.TXT) Copyright 2004 RTD Embedded Technologies, Inc. All Rights Reserved.*/#include <errno.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/poll.h>#include <unistd.h>#include <dm6430lib.h>char *program_name_p;static voidusage(void) { fprintf( stderr, "\n" "Usage: %s MINOR_NUMBER" "\n", program_name_p ); exit(EXIT_FAILURE);}static voidverify_success(int status, const char *message_p) { if (status == -1) { perror(message_p); exit(EXIT_FAILURE); }}static intisstdindata(int timeout) { struct pollfd in = {0, POLLIN | POLLERR | POLLPRI, 0}; int num_fds; while (((num_fds = poll(&in, 1, timeout)) == -1) && (errno == EINTR)) ; return ((num_fds == 1) && (in.revents & (POLLIN | POLLPRI)));}#define AD_CHAN4 DM6430HR_AIN1 // Channel to read D/A data back#define GAIN4 DM6430HR_GAINx1 // Gain (for A/D)#define SE_DIFF4 DM6430HR_SE_SE // Single-ended#define AD_SLOPE4 (65536.0/20.0) // Number Bits divided by AD Range#define DAC_SLOPE4 (65536.0/20.0) // Number Bits divided by DAC Range#define DAC_OFFSET4 2048 // Offset of the output line signalintmain(int argument_count, char **arguments_p_p) { int descriptor; int status; short DAData; short dac_base; unsigned int minor_number; program_name_p = arguments_p_p[0]; if (argument_count != 2) { fprintf(stderr, "Invalid number of options given.\n"); usage(); } if (sscanf(arguments_p_p[1], "%u", &minor_number) == 0) { fprintf(stderr, "Invalid device minor number.\n"); usage(); } descriptor = OpenBoard6430(minor_number); verify_success(descriptor, "OpenBoard6430() FAILED"); status = InitBoard6430(descriptor); verify_success(status, "InitBoard6430() FAILED"); status = SetConversionSelect6430(descriptor, DM6430HR_CONV_SOFT_TRIGGER); verify_success(status, "SetConversionSelect6430() FAILED"); status = SetChannelGain6430(descriptor, AD_CHAN4, GAIN4, SE_DIFF4); verify_success(status, "SetChannelGain6430() FAILED"); status = ClearADFIFO6430(descriptor); verify_success(status, "ClearADFIFO6430() FAILED"); /* * Base value for data written to D/A converter. This value is modified * as the loop below is executed to allow one to see changes in the A/D * data */ dac_base = 0; while (!isstdindata(200)) { int empty_flag; int16_t data; DAData = (dac_base * DAC_SLOPE4) + DAC_OFFSET4; status = LoadDAC6430(descriptor, DAData); verify_success(status, "LoadDAC6430() FAILED"); status = StartConversion6430(descriptor); verify_success(status, "StartConversion6430() FAILED"); do { status = IsADFIFOEmpty6430(descriptor, &empty_flag); verify_success(status, "IsADFIFOEmpty6430() FAILED"); } while (empty_flag); status = ReadADData6430(descriptor, &data); verify_success(status, "ReadADData6430() FAILED"); fprintf(stdout, "IN = %2.2lf mV\n", data / AD_SLOPE4); /* * Increment base value for D/A data. Only allow it to assume values * in the range [0, 9] because, due to the way the D/A value is * calculated in this program, these base values cause changes in A/D * value. */ dac_base++; if (dac_base == 10) { dac_base = 0; } } status = LoadDAC6430(descriptor, 0); verify_success(status, "LoadDAC6430() FAILED"); exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -