User`s manual

ThinkCore IA241/240 Linux User’s Manual Programmer’s Guide
6-12
Example 2
File Name: tduration.c
Description: The program indicates to connect DO1 to DI1 and program will change digital output
state automatically at the fixed frequency, then detect event change of the digital input state is high
or low in different duration. (OK)
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <moxadevice.h>
#ifdef DEBUG
#define dbg_printf(x...) printf(x)
#else
#define dbg_printf(x...)
#endif
#define DURATION_NUM 7
#define TEST_NUM 10
static int ndin_StateChangeDetected, ndout_StateChangeDetected;
static int nDuration;
static unsigned long duration[2][DURATION_NUM]={ { 50, 40, 35, 30, 25, 20, 15 }, { 160,
140, 120, 100, 80, 60, 40, } };
/********************************************************************
When the din state changed form high to low, this function will be invoked
********************************************************************/
static void low2highevent(int diport)
{
ndin_StateChangeDetected++;
dbg_printf(“din state changed:%d\n”,ndin_StateChangeDetected);
}
/********************************************************************
This function is used to exchange the dout state periodically
********************************************************************/
void dout_control(int signo)
{
int state;
get_dout_state(0, &state);
dbg_printf(“dout state changed:%d\n”,state);
if(state) // exchange the dout state periodically
{
ndout_StateChangeDetected++;
set_dout_state(0, 0);
}
else
{
set_dout_state(0, 1);
}
}
void dio_test_function(void )
{
struct itimerval value;
int j, i, nChoice;
struct timeval tv;
do {
printf(“0.Test for Din duration==0.\n”);