📄 dscdiofunctions.c
字号:
//=============================================================================
// (c) Copyright 2005 Diamond Systems Corporation. Use of this source code
// is subject to the terms of Diamond Systems' Software License Agreement.
// Diamond Systems provides no warranty of proper performance if this
// source code is modified.
//
// File: DSCDIOFunctions.c v5.9
// Desc: Sample program that demonstrates how to use the digital I/O functions
// Created by KL
//=============================================================================
#include <stdio.h>
#ifdef _WIN32
#ifndef _WIN32_WCE
#include <conio.h>
#endif
#include <windows.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "dscud.h"
#endif
// DOS
#ifdef __BORLANDC__
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "../../../../current/dev/source/dscud.h"
#endif
// Linux and QNX
#if defined(linux) || defined(__QNXNTO__) || defined(_WRS_VXWORKS_5_X)
#include <stdlib.h>
#include <math.h>
#include <time.h>
// diamond driver includes
#include "dscud.h"
#ifdef _WRS_VXWORKS_5_X
#include <selectLib.h>
#define main EMM8DSCDIOFunctions
#else
#include <sys/time.h>
#endif
static int kbhit()
{
struct timeval timeout;
fd_set rfds;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
if ( select(0+1, &rfds, NULL, NULL, &timeout) > 0 )
return getchar();
return 0;
}
#endif
// var declarations
BYTE result; // returned error code
DSCB dscb; // handle used to refer to the board
DSCCB dsccb; // structure containing board settings
int portOut; // port used for digital I/O
int portIn; // port used for digital I/O
int output_b; // BYTE sent to digital output
int input_b; // BYTE receiving digital input
int config_bytes; // digital I/O configuration byte
ERRPARAMS errorParams; // structure for returning error code and error string
//=============================================================================
// Name: main()
// Desc: Starting function that calls the driver functions used
//
// NOTE: By convention, you should capture the BYTE return value for each
// driver API call, and check the error code.
//
// I. Driver Initialization
// II. Board Initialization
// III. DIO Input and Output
// IV. Cleanup
//
//=============================================================================
int main( void )
{
//=========================================================================
// I. DRIVER INITIALIZATION
//
// Initializes the DSCUD library.
//
//=========================================================================
if( dscInit( DSC_VERSION ) != DE_NONE )
{
dscGetLastError(&errorParams);
fprintf( stderr, "dscInit error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
return 0;
}
//=========================================================================
// II. BOARD INITIALIZATION
//
// Initialize the EMM8 board. This function passes the various
// hardware parameters to the driver and resets the hardware.
//
//=========================================================================
printf( "\nEMM8 BOARD INITIALIZATION:\n" );
printf("Enter the base address (default 0x300) : ");
scanf("%hx", &dsccb.base_address );
printf("Enter interrupt level for port 0 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[0]);
printf("Enter interrupt level for port 1 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[1]);
printf("Enter interrupt level for port 2 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[2]);
printf("Enter interrupt level for port 3 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[3]);
printf("Enter interrupt level for port 4 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[4]);
printf("Enter interrupt level for port 5 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[5] );
printf("Enter interrupt level for port 6 (2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[6] );
printf("Enter interrupt level for port 7(2-15 or 0) : ");
scanf("%hu", &dsccb.EMM_Interrupt[7]);
printf("Enter IO address for channel 0 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[0]);
printf("Enter IO address for channel 1 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[1]);
printf("Enter IO address for channel 2 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[2]);
printf("Enter IO address for channel 3 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[3]);
printf("Enter IO address for channel 4 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[4]);
printf("Enter IO address for channel 5 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[5]);
printf("Enter IO address for channel 6 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[6]);
printf("Enter IO address for channel 7 (0x100 - 0x300 or 0) : ");
scanf("%hu", &dsccb.EMM_IOAddr[7]);
if(dscInitBoard(DSC_EMM8, &dsccb, &dscb)!= DE_NONE)
{
dscGetLastError(&errorParams);
fprintf( stderr, "dscInitBoard error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
return 0;
}
//=========================================================================
// III. DIO INPUT AND OUTPUT
//
// Perform a set of byte-level inputs and outputs. We start by sending
// an output byte value of 0 to the selected digital I/O port and then
// read from that same port again to verify that the correct value was
// exchanged. For word and bit-level operations, the steps are nearly
// identical with a few minor changes to function names and the size
// of the data to be transferred.
//
// NOTE: Remember that the bytes received from port are supposed to be
// equal to the inversion of the bytes sent. That is, the input
// bytes are equal to 255 minus the output bytes.
//
//=========================================================================
printf( "\nDIO INPUT AND OUTPUT:\n" );
printf("Enter the port configuration in hex: ");
scanf("%x", &config_bytes);
dscDIOSetConfig(dscb, (BYTE*)(&config_bytes));
do
{
printf("Enter the output port : ");
scanf("%d", &portOut ) ;
printf("Enter the input port : ");
scanf("%d", &portIn ) ;
printf("Enter the value to port : ");
scanf("%d", &output_b );
getchar();
if( ( result = dscDIOOutputByte( dscb, (BYTE)portOut, (BYTE)(output_b) ) ) != DE_NONE )
{
dscGetLastError(&errorParams);
fprintf( stderr, "dscDIOOutputByte error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
return 0;
}
if( ( result = dscDIOInputByte( dscb, (BYTE)portIn, (BYTE*)(&input_b) ) ) != DE_NONE )
{
dscGetLastError(&errorParams);
fprintf( stderr, "dscDIOInputByte error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
return 0;
}
printf( "Byte value sent to port: %d, Byte value received from port: %d\n", (int)output_b, (int)input_b );
printf("\nPress 'q' to exit or ENTER to continue \n");
}while( getchar() != 'q' );
//=========================================================================
// IV. CLEANUP
//
// Cleanup any remnants left by the program and free the resources used
// by the driver.
//
// STEPS TO FOLLOW:
//
// 1. free the driver resources
//=========================================================================
dscFree();
printf( "\nDSCDIOFunctions completed.\n" );
return 0;
} // end main()
#ifdef _WIN32_WCE
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
main();
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -