📄 dio.c
字号:
break;
case DI_MODE_TOGGLE_HIGH_GOING:
if (pdi->DIPrev == 0 && pdi->DIIn == 1) {
pdi->DIVal = pdi->DIVal ? 0 : 1;
}
pdi->DIPrev = pdi->DIIn;
break;
}
}
pdi++; /* Point to next DIO_DO element */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* DISCRETE I/O MANAGER INITIALIZATION
*
* Description : This function initializes the discrete I/O manager module.
* Arguments : None
* Returns : None.
*********************************************************************************************************
*/
void DIOInit (void)
{
INT8U err;
INT8U i;
DIO_DI *pdi;
DIO_DO *pdo;
pdi = &DITbl[0];
for (i = 0; i < DIO_MAX_DI; i++) {
pdi->DIVal = 0;
pdi->DIBypassEn = FALSE;
pdi->DIModeSel = DI_MODE_DIRECT; /* Set the default mode to direct input */
#if DI_EDGE_EN
pdi->DITrigFnct = (void *)0; /* No function to execute when transition detected */
pdi->DITrigFnctArg = (void *)0;
#endif
pdi++;
}
pdo = &DOTbl[0];
for (i = 0; i < DIO_MAX_DO; i++) {
pdo->DOOut = 0;
pdo->DOBypassEn = FALSE;
pdo->DOModeSel = DO_MODE_DIRECT; /* Set the default mode to direct output */
pdo->DOInv = FALSE;
#if DO_BLINK_MODE_EN
pdo->DOBlinkEnSel = DO_BLINK_EN_NORMAL; /* Blinking is enabled by direct user request */
pdo->DOA = 1;
pdo->DOB = 2;
pdo->DOBCtr = 2;
#endif
pdo++;
}
#if DO_BLINK_MODE_EN
DOSyncCtrMax = 100;
#endif
DIOInitIO();
OSTaskCreate(DIOTask, (void *)0, &DIOTaskStk[DIO_TASK_STK_SIZE], DIO_TASK_PRIO);
}
/*$PAGE*/
/*
*********************************************************************************************************
* DISCRETE I/O MANAGER TASK
*
* Description : This task is created by DIOInit() and is responsible for updating the discrete inputs and
* discrete outputs.
* DIOTask() executes every DIO_TASK_DLY_TICKS.
* Arguments : None.
* Returns : None.
*********************************************************************************************************
*/
static void DIOTask (void *data)
{
data = data; /* Avoid compiler warning (uC/OS requirement) */
for (;;) {
OSTimeDly(DIO_TASK_DLY_TICKS); /* Delay between execution of DIO manager */
DIRd(); /* Read physical inputs and map to DI channels */
DIUpdate(); /* Update all DI channels */
DOUpdate(); /* Update all DO channels */
DOWr(); /* Map DO channels to physical outputs */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SET THE STATE OF THE BYPASSED SENSOR
*
* Description : This function is used to set the state of the bypassed sensor. This function is used to
* simulate the presence of the sensor. This function is only valid if the bypass 'switch'
* is open.
* Arguments : n is the discrete input channel (0..DIO_MAX_DI-1).
* val is the state of the bypassed sensor:
* 0 indicates a negated sensor
* 1 indicates an asserted sensor
* > 0 indicates the number of edges detected in edge mode
* Returns : None.
*********************************************************************************************************
*/
void DISetBypass (INT8U n, INT16U val)
{
DIO_DI *pdi;
if (n < DIO_MAX_DI) {
pdi = &DITbl[n];
OS_ENTER_CRITICAL();
if (pdi->DIBypassEn == TRUE) { /* See if sensor is bypassed */
pdi->DIVal = val; /* Yes, then set the new state of the DI channel */
}
OS_EXIT_CRITICAL();
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SET THE STATE OF THE SENSOR BYPASS SWITCH
*
* Description : This function is used to set the state of the sensor bypass switch. The sensor is
* bypassed when the 'switch' is open (i.e. DIBypassEn is set to TRUE).
* Arguments : n is the discrete input channel (0..DIO_MAX_DI-1).
* state is the state of the bypass switch:
* FALSE disables sensor bypass (i.e. the bypass 'switch' is closed)
* TRUE enables sensor bypass (i.e. the bypass 'switch' is open)
* Returns : None.
*********************************************************************************************************
*/
void DISetBypassEn (INT8U n, BOOLEAN state)
{
if (n < DIO_MAX_DI) {
OS_ENTER_CRITICAL();
DITbl[n].DIBypassEn = state;
OS_EXIT_CRITICAL();
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE DISCRETE OUTPUT BLINK MODE
*
* Description : This function is used to configure the blink mode of the discrete output channel.
* Arguments : n is the discrete output channel (0..DIO_MAX_DO-1).
* mode is the desired blink mode:
* DO_BLINK_EN Blink is always enabled
* DO_BLINK_EN_NORMAL Blink depends on user request's state
* DO_BLINK_EN_INV Blink depends on the complemented user request's state
* a is the ON time relative to how often the DIO task executes (1..250)
* b is the period (in DO_MODE_BLINK_ASYNC mode) (1..250)
* Returns : None.
*********************************************************************************************************
*/
#if DO_BLINK_MODE_EN
void DOCfgBlink (INT8U n, INT8U mode, INT8U a, INT8U b)
{
DIO_DO *pdo;
if (n < DIO_MAX_DO) {
pdo = &DOTbl[n];
OS_ENTER_CRITICAL();
pdo->DOBlinkEnSel = mode;
pdo->DOA = a;
pdo->DOB = b;
pdo->DOBCtr = 0;
OS_EXIT_CRITICAL();
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE DISCRETE OUTPUT MODE
*
* Description : This function is used to configure the mode of a discrete output channel.
* Arguments : n is the discrete output channel to configure (0..DIO_MAX_DO-1).
* mode is the desired mode and can be:
* DO_MODE_LOW output is forced LOW
* DO_MODE_HIGH output is forced HIGH
* DO_MODE_DIRECT output is based on state of DOBypass
* DO_MODE_BLINK_SYNC output will be blinking synchronously with DOSyncCtr
* DO_MODE_BLINK_ASYNC output will be blinking based on DOA and DOB
* inv indicates whether the output will be inverted:
* TRUE forces the output to be inverted
* FALSE does not cause any inversion
* Returns : None.
*********************************************************************************************************
*/
void DOCfgMode (INT8U n, INT8U mode, BOOLEAN inv)
{
DIO_DO *pdo;
if (n < DIO_MAX_DO) {
pdo = &DOTbl[n];
OS_ENTER_CRITICAL();
pdo->DOModeSel = mode;
pdo->DOInv = inv;
OS_EXIT_CRITICAL();
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET THE STATE OF THE DISCRETE OUTPUT
*
* Description : This function is used to obtain the state of the discrete output.
* Arguments : n is the discrete output channel (0..DIO_MAX_DO-1).
* Returns : TRUE if the output is asserted.
* FALSE if the output is negated.
*********************************************************************************************************
*/
BOOLEAN DOGet (INT8U n)
{
BOOLEAN out;
if (n < DIO_MAX_DO) {
OS_ENTER_CRITICAL();
out = DOTbl[n].DOOut;
OS_EXIT_CRITICAL();
return (out);
} else {
return (FALSE);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SEE IF BLINK IS ENABLED
*
* Description : See if blink mode is enabled.
* Arguments : pdo is a pointer to the discrete output data structure.
* Returns : TRUE if blinking is enabled
* FALSE otherwise
*********************************************************************************************************
*/
#if DO_BLINK_MODE_EN
static BOOLEAN DOIsBlinkEn (DIO_DO *pdo)
{
BOOLEAN en;
en = FALSE;
switch (pdo->DOBlinkEnSel) {
case DO_BLINK_EN: /* Blink is always enabled */
en = TRUE;
break;
case DO_BLINK_EN_NORMAL: /* Blink depends on user request's state */
en = pdo->DOBypass;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -