forked from liyanboy74/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb-table.c
63 lines (53 loc) · 2.19 KB
/
mb-table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Table for MODBUS
By Liyanboy74
https://github.com/liyanboy74/modbus
*/
#include "mb-table.h"
#if(MB_MODE==MB_MODE_SLAVE)
/**
* MODBUS Data model
* ┌──────────────────┬─────────────┬─────────┐
* │ Rrimary tables │ Object type │ Type of │
* ╞══════════════════╪═════════════╪═════════╡
* │ Discretes Input │ Single bit │ R │
* ├──────────────────┼─────────────┼─────────┤
* │ Coils │ Single bit │ R/W │
* ├──────────────────┼─────────────┼─────────┤
* │ Input Registers │ 16-bit word │ R │
* ├──────────────────┼─────────────┼─────────┤
* │ Holding Registers│ 16-bit word │ R/W │
* └──────────────────┴─────────────┴─────────┘
*/
uint16_t TBALE_Discretes_Input [TBALE_Discretes_Input_Size];
uint16_t TBALE_Coils [TBALE_Coils_Size];
uint16_t TBALE_Input_Registers [TBALE_Input_Registers_Size];
uint16_t TABLE_Holding_Registers[TABLE_Holding_Registers_Size];
void mb_table_write(uint16_t *Table,uint16_t Index,uint16_t Value)
{
Table[Index]=Value;
}
uint16_t mb_table_read(uint16_t *Table,uint16_t Index)
{
return Table[Index];
}
void mb_table_write_bit(uint16_t *TABLE,uint16_t Bit_Index,uint8_t Bit_Value)
{
uint16_t temp;
uint16_t TableIndex=Bit_Index/TABLE_Sel_BitSize;
uint8_t BitOffset=Bit_Index%TABLE_Sel_BitSize;
Bit_Value&=1;
temp=TABLE[TableIndex];
temp&=(~(1<<BitOffset));
temp|=(Bit_Value<<BitOffset);
TABLE[TableIndex]=temp;
}
uint8_t mb_table_read_bit(uint16_t *TABLE,uint16_t Bit_Index)
{
uint16_t ret;
ret=TABLE[Bit_Index/TABLE_Sel_BitSize];
ret=ret>>(Bit_Index%TABLE_Sel_BitSize);
ret&=1;
return (uint8_t)ret;
}
#endif