📄 mb_dkxx.c
字号:
/******************************************************************************
*
* (c) 2004 by BECK IPC GmbH
*
*******************************************************************************
*
* Module: mb_dkxx.c
* Function: DKxx specific modbus device functions,executed at incoming modbus
* client requests
* 1. Initialize DKxx (PCS6, PIO 13 and PIO 3) ,pio3 and 13 not at dk60
* 2. Read and write registers (according to definition below)
3. Read and write coils (according to definition below)
********************************************************************************
* DKxx Modbus holding register definition
* Register address Access Meaning
* 00 R/W DKxx 8Bit IO port at IO addr 600h/C00h
* (on DK40 only Bit 6-0 available)
* 01 R/W 16 Bit variable, for
* demonstration purpose only
* 02 R/W 16 Bit counter variable, for
* demonstration purpose only, counter
* is automatically incremented by a RTOS tiner
* 03 R/W another 16 Bit counter variable, for
* demonstration purpose only, counter
* is automatically changed by a RTOS timer
*
* DKxx Modbus input register definition
* Register address Access Meaning
* 00 R DKxx 8Bit I port at IO addr 600h/C00h
* (on DK40 only Bit 6-0 available)
* 01 R 16 Bit counter variable, for
* demonstration purpose only, counter
* is automatically incremented by a RTOS
* 02 R 16 Bit counter variable, for
* demonstration purpose only, counter
* is automatically changed by a RTOS timer
********************************************************************************
* DKxx Modbus coil definition
* Register address Access Meaning
* 00 R/W DKxx/SC1x PIO 3 (not avail at SC1x3)
* 01 R/W DKxx/SC1x PIO 13 (not avail at SC1x3)
* 02 R/W Bit 0 of mb_dkxx_var (Holding reg var 01)
* 03 R/W Bit 1 of mb_dkxx_var
* 04 R/W Bit 2 of mb_dkxx_var
* 05 R/W Bit 3 of mb_dkxx_var
* 06 R/W Bit 4 of mb_dkxx_var
* 07 R/W Bit 5 of mb_dkxx_var
* 08 R/W Bit 6 of mb_dkxx_var
* 09 R/W Bit 7 of mb_dkxx_var
* 10 R/W Bit 8 of mb_dkxx_var
* 11 R/W Bit 9 of mb_dkxx_var
* 12 R/W Bit 10 of mb_dkxx_var
* 13 R/W Bit 11 of mb_dkxx_var
* 14 R/W Bit 12 of mb_dkxx_var
********************************************************************************
* DKxx Modbus discrete input definition
* Register address Access Meaning
* 00 R Bit 0 of DKxx IO port 0x600/0xC00
* 01 R Bit 1 of DKxx IO port 0x600/0xC00
* 02 R Bit 2 of DKxx IO port 0x600/0xC00
* 03 R Bit 3 of DKxx IO port 0x600/0xC00
* 04 R Bit 4 of DKxx IO port 0x600/0xC00
* 05 R Bit 5 of DKxx IO port 0x600/0xC00
* 06 R Bit 6 of DKxx IO port 0x600/0xC00
********************************************************************************
* Disclaimer: This program is an example and should be used as such.
* If you wish to use this program or parts of it in your application,
* you must validate the code yourself. BECK IPC GmbH can not be held
* responsible for the correct functioning or coding of this example.
*******************************************************************************
*
* $Header: c:\prj\IPC@CHIP\Examples\industrial\Modbus\mb_tcp_s\source\mb_dkxx.c, 4, 02.09.2005 12:18:33, Markus Bartat$
*
******************************************************************************/
/******************************************************************************
* Includes
******************************************************************************/
#include <stdio.h>
#include <dos.h>
#include "clib.h"
#include "modbus.h"
#ifdef SC123
#define IO_ADDR 0xC00
#else
#define IO_ADDR 0x600
#endif
/******************************************************************************
* Defines
******************************************************************************/
//#define MB_DKxx_DEBUG
//******************************************************************************
// Global counter variables for demo modbus holding registers 03 and 04
//******************************************************************************
//Holding register 01 R/W 16 Bit variable, for demonstration purpose only
static unsigned int mb_dkxx_var;
//Holding register 02 R/W 16 Bit counter variable, incremented by a rtos timer
static unsigned int mb_dkxx_timercnt;
//Holding register 03 R/W 16 Bit counter variable, changed by a rtos timer
static unsigned int mb_dkxx_timercnt_2;
//******************************************************************************
// RTOS timer for counting mb_dkxx_timercnt (dkxx modbus register 05)
//******************************************************************************
static void huge DKxx_timer(void); //prototype timer procedure
unsigned int timerID; //timerID storage
TimerProc_Structure TProc ={&timerID, //pointer to storage the unique timerID
DKxx_timer, //pointer to the procedure to be executed
NULL, //dummy pointer, not used
{'N','T','E','X'}, //unique 4 character name
100L //timer execution interval (ms)
};
/******************************************************************************
* DKxx_timer functions
* Increments dkxx modbus register 04 (mb_dkxx_timercnt)
******************************************************************************/
static void huge DKxx_timer(void)
{
//modify timer counters
mb_dkxx_timercnt++;
if(mb_dkxx_timercnt%10 == 0)
{
mb_dkxx_timercnt_2++;
}
}
//******************************************************************************
// Initialize the DKxx hardware
// Return 0: succesful
// -1: Device init error
//******************************************************************************
int mb_dkxx_init(void)
{
//Init PCS6 and set port value to zero
pfe_enable_pcs (6);
//Enable PIO 13 and 3 as outputs, value 0
#ifndef SC123
pfe_enable_pio (13,5);
pfe_enable_pio (3,5);
#endif
//Create RTOS timer for counting mb_dkxx_timercnt
if(RTX_Install_Timer(&TProc)!=0)
{
RTX_Remove_Timer(timerID);
return MB_ERROR;
}
mb_dkxx_timercnt=0;
if(RTX_Start_Timer(timerID)!=0)
{
RTX_Remove_Timer(timerID);
return MB_ERROR;
}
//Init dkxx register 03 variable
mb_dkxx_var=0;
return MB_NO_ERROR;
}
//******************************************************************************
// DeInitialize the DKxx hardware
//******************************************************************************
int mb_dkxx_deinit(void)
{
//Clear port 600h/C00h
outportb(IO_ADDR,0);
pfe_enable_pcs ( 6 );
//PIO 13 and 3 = 0
#ifndef SC123
hal_write_pio (13, 0);
hal_write_pio (3, 0);
#endif
//Stop/remove RTOS timer for counting mb_dkxx_timercnt
RTX_Remove_Timer(timerID);
return MB_NO_ERROR;
}
//******************************************************************************
// Read discrete input functions, read current bits of IO port 0x600/0xC00
// Return 0: Sucessful, value contains coil status or discrete input value
// else errorcode see modbus.h
//******************************************************************************
//get Bit0 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_00(unsigned char * value)
{
*value = inportb(IO_ADDR) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 00: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit1 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_01(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>1) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 01: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit2 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_02(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>2) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 02: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit3 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_03(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>3) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 03: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit4 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_04(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>4) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 04: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit5 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_05(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>5) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 05: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get Bit6 of IO port 0x600/0xC00
int mb_dkxx_read_discr_inp_06(unsigned char * value)
{
*value = (inportb(IO_ADDR)>>6) & 0x01;
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read discr inp 06: %02X",*value);
#endif
return MB_NO_ERROR;
}
//******************************************************************************
// Read coil functions, read current output value of pio 3 and pio 13 (not avail at Sc1x3)
// Return 0: Sucessful, value contains coil status or discrete input value
// else errorcode see modbus.h
//******************************************************************************
//get pio 3
#ifndef SC123
int mb_dkxx_read_coil_00(unsigned char * value)
{
*value = hal_read_pio (3);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 0: %02X",*value);
#endif
return MB_NO_ERROR;
}
//get pio 13
int mb_dkxx_read_coil_01(unsigned char * value)
{
*value = hal_read_pio (13);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 01: %02X",*value);
#endif
return MB_NO_ERROR;
}
#else
int mb_dkxx_read_coil_00(unsigned char * value)
{
*value = 0;
return MB_NO_ERROR;
}
//get pio 13
int mb_dkxx_read_coil_01(unsigned char * value)
{
*value = 0;
return MB_NO_ERROR;
}
#endif
//Get Bit 0 of timer counter var
int mb_dkxx_read_coil_02(unsigned char * value)
{
*value = (unsigned char)(mb_dkxx_var & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 02: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 1 of timer counter var
int mb_dkxx_read_coil_03(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>1) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 03: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 2 of timer counter var
int mb_dkxx_read_coil_04(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>2) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 04: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 3 of timer counter var
int mb_dkxx_read_coil_05(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>3) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 05: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 4 of timer counter var
int mb_dkxx_read_coil_06(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>4) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 06: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 5 of timer counter var
int mb_dkxx_read_coil_07(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>5) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 07: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 6 of timer counter var
int mb_dkxx_read_coil_08(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>6) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 08: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 7 of timer counter var
int mb_dkxx_read_coil_09(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>7) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 09: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 8 of timer counter var
int mb_dkxx_read_coil_10(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>8) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 10: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 9 of timer counter var
int mb_dkxx_read_coil_11(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>9) & 0x01);
#ifdef MB_DKxx_DEBUG
printf("\r\nDKxx read coil 11: %02X",*value);
#endif
return MB_NO_ERROR;
}
//Get Bit 10 of timer counter var
int mb_dkxx_read_coil_12(unsigned char * value)
{
*value = (unsigned char)((mb_dkxx_var>>10) & 0x01);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -