ponnus
Full Member level 2
- Joined
- Mar 17, 2011
- Messages
- 142
- Helped
- 8
- Reputation
- 16
- Reaction score
- 8
- Trophy points
- 1,298
- Location
- Cochin, INDIA
- Activity points
- 2,226
For a binary to decimal conversion that doesn't involve division/modulo function, you may want to review the double dabble algorithm. http://en.wikipedia.org/wiki/Double_dabble
In C-programming, itoa() or sprintf() are mostly the reasonable method.
Yes, but it's the closest answer to the question in the original post ("using shifts").This is a great method but kind of tricky to apply in C at least when trying to get the most efficient code
Hai,
I want to enter digits into a character array. For example, I want to enter decimal 123 as '1','2','3'.
But it should take only few instruction cycles.
.......I also tried to convert the decimal number to hex, separate each nibble of the hex ,add 0x30 to convert to ascii. But, decimal to hex conversion used division in that, which takes more time than actually needed.
So, I need to convert values upto 4095. i'm trying to edit the double dabber algorithm code which I got from the link provided by alexan_e.
void adc2ascii(uint16_t value, char *str)
{
char *p = str;
if (value > 3999) { *p++ = '4'; value -= 4000; }
else if (value > 2999) { *p++ = '3'; value -= 3000; }
else if (value > 1999) { *p++ = '2'; value -= 2000; }
else if (value > 999) { *p++ = '1'; value -= 1000; }
else { *p++ = '0'; }
if (value > 899) { *p++ = '9'; value -= 900; }
else if (value > 799) { *p++ = '8'; value -= 800; }
else if (value > 699) { *p++ = '7'; value -= 700; }
else if (value > 599) { *p++ = '6'; value -= 600; }
else if (value > 499) { *p++ = '5'; value -= 500; }
else if (value > 399) { *p++ = '4'; value -= 400; }
else if (value > 299) { *p++ = '3'; value -= 300; }
else if (value > 199) { *p++ = '2'; value -= 200; }
else if (value > 99) { *p++ = '1'; value -= 100; }
else { *p++ = '0'; }
if (value > 89) { *p++ = '9'; value -= 90; }
else if (value > 79) { *p++ = '8'; value -= 80; }
else if (value > 69) { *p++ = '7'; value -= 70; }
else if (value > 59) { *p++ = '6'; value -= 60; }
else if (value > 49) { *p++ = '5'; value -= 50; }
else if (value > 39) { *p++ = '4'; value -= 40; }
else if (value > 29) { *p++ = '3'; value -= 30; }
else if (value > 19) { *p++ = '2'; value -= 20; }
else if (value > 9) { *p++ = '1'; value -= 10; }
else if (p != str) { *p++ = '0'; }
*p++ = value + '0';
*p = 0;
}
void adc2ascii(uint16_t value, char *str)
{
char *p = str;
if (value > 2999)
{
if (value > 3999) { *p++ = '4'; value -= 4000; }
else { *p++ = '3'; value -= 3000; }
}
else
{
if (value > 1999) { *p++ = '2'; value -= 2000; }
else if (value > 999) { *p++ = '1'; value -= 1000; }
else { *p++ = '0'; }
}
if (value > 499)
{
if (value > 699)
{
if (value > 899) { *p++ = '9'; value -= 900; }
else if (value > 799) { *p++ = '8'; value -= 800; }
else { *p++ = '7'; value -= 700; }
}
else
{
if (value > 599) { *p++ = '6'; value -= 600; }
else { *p++ = '5'; value -= 500; }
}
}
else
{
if (value > 299)
{
if (value > 399) { *p++ = '4'; value -= 400; }
else { *p++ = '3'; value -= 300; }
}
else
{
if (value > 199) { *p++ = '2'; value -= 200; }
else if (value > 99) { *p++ = '1'; value -= 100; }
else { *p++ = '0'; }
}
}
if (value > 49)
{
if (value > 69)
{
if (value > 89) { *p++ = '9'; value -= 90; }
else if (value > 79) { *p++ = '8'; value -= 80; }
else { *p++ = '7'; value -= 70; }
}
else
{
if (value > 59) { *p++ = '6'; value -= 60; }
else { *p++ = '5'; value -= 50; }
}
}
else
{
if (value > 19)
{
if (value > 39) { *p++ = '4'; value -= 40; }
else if (value > 29) { *p++ = '3'; value -= 30; }
else { *p++ = '2'; value -= 20; }
}
else
{
if (value > 9) { *p++ = '1'; value -= 10; }
else if (p != str) { *p++ = '0'; }
}
}
*p++ = value + '0';
*p = 0;
}
void adc2ascii(unsigned short value)
{
unsigned char v, a, b;
if (value > 499)
{
if (value > 799)
{
if (value > 999)
{
// Treated as a special case as there
// are only 24 values above 999
str[0] = '1';
str[1] = '0';
v = value - 1000;
if (v > 19) { str[2] = '2'; v -= 20; }
else if (v > 9) { str[2] = '1'; v -= 10; }
else { str[2] = '0'; }
str[3] = v + '0';
str[4] = 0;
return;
}
if (value > 899) { a = '9'; v = value - 900; }
else { a = '8'; v = value - 800; }
}
else if (value > 699) { a = '7'; v = value - 700; }
else if (value > 599) { a = '6'; v = value - 600; }
else { a = '5'; v = value - 500; }
}
else if (value > 299)
{
if (value > 399) { a = '4'; v = value - 400; }
else { a = '3'; v = value - 300; }
}
else if (value > 199) { a = '2'; v = value - 200; }
else if (value > 99) { a = '1'; v = value - 100; }
else { a = '0'; v = value; }
if (v > 49)
{
if (v > 69)
{
if (v > 89) { b = '9'; v -= 90; }
else if (v > 79) { b = '8'; v -= 80; }
else { b = '7'; v -= 70; }
}
else if (v > 59) { b = '6'; v -= 60; }
else { b = '5'; v -= 50; }
}
else if (v > 19)
{
if (v > 39) { b = '4'; v -= 40; }
else if (v > 29) { b = '3'; v -= 30; }
else { b = '2'; v -= 20; }
}
else if (v > 9) { b = '1'; v -= 10; }
else { b = '0'; }
if (a == '0')
{
if (b == '0')
{
str[0] = v + '0';
str[1] = 0;
}
else
{
str[0] = b;
str[1] = v + '0';
str[2] = 0;
}
}
else
{
str[0] = a;
str[1] = b;
str[2] = v + '0';
str[3] = 0;
}
}
But the code using half interval time is suitable for my application..Thanks
Code C - [expand] 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 #include "MSP430F5529.h" #include "Types.h" void adc2ascii(uint16 value, char *str) ; void IToA(uint16 value, char * str); char str1[6]; char str2[6]; void main(void) { uint16 i; uint16 div10, trick10; MPY = 6554; for(i = 100 ; i < 200 ; ++i) { IToA(i, str1); // Runs in 100 clocks for range [0..9999] (*) adc2ascii(i, str2); // Runs in 82 to 91 clocks for range [0..4999] } } // (*) The above figures are given for MSP430F5529, IAR compiler, with low optimization // void IToA(uint16 value, char * str) { // Get d0 MPY = 6554; // Divide by 10 OP2 = value; // Example val = 1234 MPY=10; // Multiply by 10 OP2 = RES1; // Recycle last result as an operand to multiply it back by 10 str[3] = '0' + value - RES0; value = OP2; // Use last operand (that was divided by 10 as a new value, and do the same stuff again. MPY = 6554; OP2 = value; MPY=10; OP2=RES1; str[2] = '0' + value - RES0; value = OP2; MPY = 6554; OP2 = value; str[0] = '0' + RES1; MPY=10; OP2=RES1; str[1] = '0' + value - RES0; } void adc2ascii(uint16 value, char *str) { char *p = str; if (value > 2999) { if (value > 3999) { *p++ = '4'; value -= 4000; } else { *p++ = '3'; value -= 3000; } } else { if(value > 1999) { *p++ = '2'; value -= 2000; } else if(value > 999) { *p++ = '1'; value -= 1000; } else { *p++ = '0'; } } if (value > 499) { if (value > 699) { if (value > 899) { *p++ = '9'; value -= 900; } else if (value > 799) { *p++ = '8'; value -= 800; } else { *p++ = '7'; value -= 700; } } else { if (value > 599) { *p++ = '6'; value -= 600; } else { *p++ = '5'; value -= 500; } } } else { if (value > 299) { if (value > 399) { *p++ = '4'; value -= 400; } else { *p++ = '3'; value -= 300; } } else { if (value > 199) { *p++ = '2'; value -= 200; } else if (value > 99) { *p++ = '1'; value -= 100; } else { *p++ = '0'; } } } if (value > 49) { if (value > 69) { if (value > 89) { *p++ = '9'; value -= 90; } else if (value > 79) { *p++ = '8'; value -= 80; } else { *p++ = '7'; value -= 70; } } else { if (value > 59) { *p++ = '6'; value -= 60; } else { *p++ = '5'; value -= 50; } } } else { if (value > 19) { if (value > 39) { *p++ = '4'; value -= 40; } else if (value > 29) { *p++ = '3'; value -= 30; } else { *p++ = '2'; value -= 20; } } else { if (value > 9) { *p++ = '1'; value -= 10; } else if (p != str) { *p++ = '0'; } } } *p++ = value + '0'; *p = 0; }
Code C - [expand] 1 2 3 4 for (i=0; i < n; ++i) { for (j=0; j < 16; ++j) { for (k=smin; k < nscratch; ++k) for (k=smin; k < nscratch-1; ++k) {
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?