common.c
4.26 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
*********************************************************************************************************
*
* 模块名称 :
* 文件名称 :
* 版 本 : V1.0
* 说 明 :
* 修改记录 :
* 版本号 日期 作者 说明
*
* Copyright (C), 2016-2017, 湖南驰众机器人 www.cizon.com.cn
*
*********************************************************************************************************
*/
#include "common.h"
void BitToByte(unsigned char byte_data,unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
unsigned int byteOffset,bitOffset;
unsigned char mask[8]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
byteOffset = *byte_offset;
bitOffset = *bit_offset;
if((bit_len == 0) || (bit_len > 8) || (bitOffset == 0) || (bitOffset > 8))
return;
byte_data &= mask[bit_len-1];
if(bit_len <= bitOffset)
{
byte_buf[byteOffset] |= (byte_data << (bitOffset - bit_len));
bitOffset = bitOffset - bit_len;
}
else
{
byte_buf[byteOffset] |= (byte_data >> (bit_len - bitOffset));
byteOffset++;
byte_buf[byteOffset] = (byte_data << (8 - (bit_len - bitOffset)));
bitOffset = (8 - (bit_len - bitOffset));
}
if(bitOffset == 0)
{
byteOffset++;
bitOffset = 8;
}
*byte_offset = byteOffset;
*bit_offset = bitOffset;
}
void SetBitToByte(unsigned long data,unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
unsigned short byteOffset,bitOffset;
unsigned byte_data;
byteOffset = *byte_offset;
bitOffset = *bit_offset;
if((bit_len == 0) || (bit_len > 32) || (bitOffset == 0) || (bitOffset > 8))
return;
while(bit_len > 0)
{
if(bit_len > bitOffset)
{
byte_data = (data >> (bit_len - bitOffset));
BitToByte(byte_data,(unsigned char)bitOffset,byte_buf,byte_offset,bit_offset);
bit_len -= bitOffset;
byteOffset = *byte_offset;
bitOffset = *bit_offset;
}
else
{
byte_data = data;
BitToByte(byte_data,bit_len,byte_buf,byte_offset,bit_offset);
bit_len = 0;
byteOffset = *byte_offset;
bitOffset = *bit_offset;
}
}
}
unsigned long GetBitFromByte(unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
unsigned short byteOffset,bitOffset;
unsigned long data,tempdata;
byteOffset = *byte_offset;
bitOffset = *bit_offset;
data = 0;
if((bit_len > 32) || (bitOffset == 0) || (bitOffset > 8))
return 0;
while(bit_len > 0)
{
if(bit_len <= bitOffset)
{
tempdata = (byte_buf[byteOffset] >> (bitOffset - bit_len));
tempdata &= ((0x01 << bit_len) - 1);
bitOffset -= bit_len;
data += tempdata;
bit_len = 0;
}
else
{
tempdata = byte_buf[byteOffset] & ((0x01 << bitOffset) - 1);
tempdata <<= (bit_len - bitOffset);
bit_len -= bitOffset;
byteOffset++;
bitOffset = 8;
data += tempdata;
}
}
if(bitOffset == 0)
{
byteOffset++;
bitOffset = 8;
}
*byte_offset = byteOffset;
*bit_offset = bitOffset;
return data;
}
unsigned char HexAscii2Decimal(unsigned char asciiH,unsigned char asciiL)
{
unsigned char decimal = 0;
unsigned char hexH,hexL;
if((asciiH >= '0') && (asciiH <= '9'))
{
hexH = asciiH - '0';
}
else if((asciiH >= 'a') && (asciiH <= 'f'))
{
hexH = asciiH - 'a' + 10;
}
else if((asciiH >= 'A') && (asciiH <= 'F'))
{
hexH = asciiH - 'A' + 10;
}
else
{
hexH = 0;
}
if((asciiL >= '0') && (asciiL <= '9'))
{
hexL = asciiL - '0';
}
else if((asciiL >= 'a') && (asciiL <= 'f'))
{
hexL = asciiL - 'a' + 10;
}
else if((asciiL >= 'A') && (asciiL <= 'F'))
{
hexL = asciiL - 'A' + 10;
}
else
{
hexL = 0;
}
decimal = hexH * 16 + hexL;
return decimal;
}
////////////////////////////////////////////////////////////////////