-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiComms2.c
185 lines (121 loc) · 3.19 KB
/
hiComms2.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
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
175
176
177
178
179
180
181
182
183
184
#include "hiComms2.h"
uint8_t BCDLUT1[] = {0,2,4,6,8,0,2,4,6,8}; // ones place for x2
uint8_t BCDLUT2[] = {0,0,0,0,0,1,1,1,1,1}; // carry for x2
void printNumDec32 ( uint32_t bv ) {
uint32_t i;
uint8_t d0, d1, d2, d3, d4, d5, d6, d7, d8, d9; // ssh.
d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 0;
for (i = 0x80000000; i ; i>>=1) {
d9 <<= 1;
d9 |= BCDLUT2[d8];
d8 = BCDLUT1[d8];
d8 |= BCDLUT2[d7];
d7 = BCDLUT1[d7];
d7 |= BCDLUT2[d6];
d6 = BCDLUT1[d6];
d6 |= BCDLUT2[d5];
d5 = BCDLUT1[d5];
d5 |= BCDLUT2[d4];
d4 = BCDLUT1[d4];
d4 |= BCDLUT2[d3];
d3 = BCDLUT1[d3];
d3 |= BCDLUT2[d2];
d2 = BCDLUT1[d2];
d2 |= BCDLUT2[d1];
d1 = BCDLUT1[d1];
d1 |= BCDLUT2[d0];
d0 = BCDLUT1[d0];
if (bv & i) d0 |= 1;
}
USART_Transmit( d9|0x30 );
USART_Transmit( d8|0x30 );
USART_Transmit( d7|0x30 );
USART_Transmit( d6|0x30 );
USART_Transmit( d5|0x30 );
USART_Transmit( d4|0x30 );
USART_Transmit( d3|0x30 );
USART_Transmit( d2|0x30 );
USART_Transmit( d1|0x30 );
USART_Transmit( d0|0x30 );
}
void printSignNumDec16( int16_t bv) {
if (bv < 0) {
USART_Transmit( '-' );
printNumDec16(-bv);
} else {
printNumDec16(bv);
}
}
void printNumDec16( uint16_t bv ) {
uint16_t i;
uint8_t d0, d1, d2, d3, d4; // ssh.
d0 = d1 = d2 = d3 = d4 = 0;
for (i = 0x8000; i ; i>>=1) {
d4 <<= 1;
d4 |= BCDLUT2[d3];
d3 = BCDLUT1[d3];
d3 |= BCDLUT2[d2];
d2 = BCDLUT1[d2];
d2 |= BCDLUT2[d1];
d1 = BCDLUT1[d1];
d1 |= BCDLUT2[d0];
d0 = BCDLUT1[d0];
if (bv & i) d0 |= 1;
}
USART_Transmit( d4|0x30 );
USART_Transmit( d3|0x30 );
USART_Transmit( d2|0x30 );
USART_Transmit( d1|0x30 );
USART_Transmit( d0|0x30 );
}
void printSignNumDec8( int16_t bv) {
if (bv < 0) {
USART_Transmit( '-' );
printNumDec8(-bv);
} else {
printNumDec8(bv);
}
}
void printNumDec8( uint8_t bv ) {
uint8_t i;
uint8_t d0, d1, d2;
d0 = d1 = d2 = 0;
for (i = 0x80; i ; i>>=1) {
d2 <<= 1;
d2 |= BCDLUT2[d1];
d1 = BCDLUT1[d1];
d1 |= BCDLUT2[d0];
d0 = BCDLUT1[d0];
if (bv & i) d0 |= 1;
}
USART_Transmit( d2|0x30 );
USART_Transmit( d1|0x30 );
USART_Transmit( d0|0x30 );
}
void printNumHex8(uint8_t i){
uint8_t hi,lo;
// hi=i&0xF0; // High nibble
hi=i>>4;
hi+='0';
if (hi>'9') hi+=7;
lo=(i&0x0F)+'0'; // Low nibble
if (lo>'9') lo+=7;
USART_Transmit( hi );
USART_Transmit( lo );
}
void printNumHex16(uint16_t v) {
printNumHex8(v >> 8);
printNumHex8(v & 0xFF);
}
void printNumHex32(uint32_t v) {
printNumHex8( v >> 24 );
printNumHex8( ( v >> 16 ) & 0xFF);
printNumHex8( ( v >> 8 ) & 0xFF);
printNumHex8( v & 0xFF);
}
void USART_printstring( char *data){
while(*data) {
USART_Transmit(*data);
data++;
}
}