![](/media/cache/profiles/84d2c8892fcc6e60580f09d23082aa6f.jpg.50x50_q85.jpg)
Updated functions to deal with USB clocking. (PPL1) See http://www.nxp.com/documents/user_manual/UM10360.pdf Section 4.6 & 4.7.3
Fork of ClockControl by
CoreMark/cvt.c@0:b5d3bd64d2dc, 2010-01-24 (annotated)
- Committer:
- no2chem
- Date:
- Sun Jan 24 15:46:26 2010 +0000
- Revision:
- 0:b5d3bd64d2dc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
no2chem | 0:b5d3bd64d2dc | 1 | #include <math.h> |
no2chem | 0:b5d3bd64d2dc | 2 | #define CVTBUFSIZE 80 |
no2chem | 0:b5d3bd64d2dc | 3 | static char CVTBUF[CVTBUFSIZE]; |
no2chem | 0:b5d3bd64d2dc | 4 | |
no2chem | 0:b5d3bd64d2dc | 5 | static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag) |
no2chem | 0:b5d3bd64d2dc | 6 | { |
no2chem | 0:b5d3bd64d2dc | 7 | int r2; |
no2chem | 0:b5d3bd64d2dc | 8 | double fi, fj; |
no2chem | 0:b5d3bd64d2dc | 9 | char *p, *p1; |
no2chem | 0:b5d3bd64d2dc | 10 | |
no2chem | 0:b5d3bd64d2dc | 11 | if (ndigits < 0) ndigits = 0; |
no2chem | 0:b5d3bd64d2dc | 12 | if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2; |
no2chem | 0:b5d3bd64d2dc | 13 | r2 = 0; |
no2chem | 0:b5d3bd64d2dc | 14 | *sign = 0; |
no2chem | 0:b5d3bd64d2dc | 15 | p = &buf[0]; |
no2chem | 0:b5d3bd64d2dc | 16 | if (arg < 0) |
no2chem | 0:b5d3bd64d2dc | 17 | { |
no2chem | 0:b5d3bd64d2dc | 18 | *sign = 1; |
no2chem | 0:b5d3bd64d2dc | 19 | arg = -arg; |
no2chem | 0:b5d3bd64d2dc | 20 | } |
no2chem | 0:b5d3bd64d2dc | 21 | arg = modf(arg, &fi); |
no2chem | 0:b5d3bd64d2dc | 22 | p1 = &buf[CVTBUFSIZE]; |
no2chem | 0:b5d3bd64d2dc | 23 | |
no2chem | 0:b5d3bd64d2dc | 24 | if (fi != 0) |
no2chem | 0:b5d3bd64d2dc | 25 | { |
no2chem | 0:b5d3bd64d2dc | 26 | p1 = &buf[CVTBUFSIZE]; |
no2chem | 0:b5d3bd64d2dc | 27 | while (fi != 0) |
no2chem | 0:b5d3bd64d2dc | 28 | { |
no2chem | 0:b5d3bd64d2dc | 29 | fj = modf(fi / 10, &fi); |
no2chem | 0:b5d3bd64d2dc | 30 | *--p1 = (int)((fj + .03) * 10) + '0'; |
no2chem | 0:b5d3bd64d2dc | 31 | r2++; |
no2chem | 0:b5d3bd64d2dc | 32 | } |
no2chem | 0:b5d3bd64d2dc | 33 | while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++; |
no2chem | 0:b5d3bd64d2dc | 34 | } |
no2chem | 0:b5d3bd64d2dc | 35 | else if (arg > 0) |
no2chem | 0:b5d3bd64d2dc | 36 | { |
no2chem | 0:b5d3bd64d2dc | 37 | while ((fj = arg * 10) < 1) |
no2chem | 0:b5d3bd64d2dc | 38 | { |
no2chem | 0:b5d3bd64d2dc | 39 | arg = fj; |
no2chem | 0:b5d3bd64d2dc | 40 | r2--; |
no2chem | 0:b5d3bd64d2dc | 41 | } |
no2chem | 0:b5d3bd64d2dc | 42 | } |
no2chem | 0:b5d3bd64d2dc | 43 | p1 = &buf[ndigits]; |
no2chem | 0:b5d3bd64d2dc | 44 | if (eflag == 0) p1 += r2; |
no2chem | 0:b5d3bd64d2dc | 45 | *decpt = r2; |
no2chem | 0:b5d3bd64d2dc | 46 | if (p1 < &buf[0]) |
no2chem | 0:b5d3bd64d2dc | 47 | { |
no2chem | 0:b5d3bd64d2dc | 48 | buf[0] = '\0'; |
no2chem | 0:b5d3bd64d2dc | 49 | return buf; |
no2chem | 0:b5d3bd64d2dc | 50 | } |
no2chem | 0:b5d3bd64d2dc | 51 | while (p <= p1 && p < &buf[CVTBUFSIZE]) |
no2chem | 0:b5d3bd64d2dc | 52 | { |
no2chem | 0:b5d3bd64d2dc | 53 | arg *= 10; |
no2chem | 0:b5d3bd64d2dc | 54 | arg = modf(arg, &fj); |
no2chem | 0:b5d3bd64d2dc | 55 | *p++ = (int) fj + '0'; |
no2chem | 0:b5d3bd64d2dc | 56 | } |
no2chem | 0:b5d3bd64d2dc | 57 | if (p1 >= &buf[CVTBUFSIZE]) |
no2chem | 0:b5d3bd64d2dc | 58 | { |
no2chem | 0:b5d3bd64d2dc | 59 | buf[CVTBUFSIZE - 1] = '\0'; |
no2chem | 0:b5d3bd64d2dc | 60 | return buf; |
no2chem | 0:b5d3bd64d2dc | 61 | } |
no2chem | 0:b5d3bd64d2dc | 62 | p = p1; |
no2chem | 0:b5d3bd64d2dc | 63 | *p1 += 5; |
no2chem | 0:b5d3bd64d2dc | 64 | while (*p1 > '9') |
no2chem | 0:b5d3bd64d2dc | 65 | { |
no2chem | 0:b5d3bd64d2dc | 66 | *p1 = '0'; |
no2chem | 0:b5d3bd64d2dc | 67 | if (p1 > buf) |
no2chem | 0:b5d3bd64d2dc | 68 | ++*--p1; |
no2chem | 0:b5d3bd64d2dc | 69 | else |
no2chem | 0:b5d3bd64d2dc | 70 | { |
no2chem | 0:b5d3bd64d2dc | 71 | *p1 = '1'; |
no2chem | 0:b5d3bd64d2dc | 72 | (*decpt)++; |
no2chem | 0:b5d3bd64d2dc | 73 | if (eflag == 0) |
no2chem | 0:b5d3bd64d2dc | 74 | { |
no2chem | 0:b5d3bd64d2dc | 75 | if (p > buf) *p = '0'; |
no2chem | 0:b5d3bd64d2dc | 76 | p++; |
no2chem | 0:b5d3bd64d2dc | 77 | } |
no2chem | 0:b5d3bd64d2dc | 78 | } |
no2chem | 0:b5d3bd64d2dc | 79 | } |
no2chem | 0:b5d3bd64d2dc | 80 | *p = '\0'; |
no2chem | 0:b5d3bd64d2dc | 81 | return buf; |
no2chem | 0:b5d3bd64d2dc | 82 | } |
no2chem | 0:b5d3bd64d2dc | 83 | |
no2chem | 0:b5d3bd64d2dc | 84 | char *ecvt(double arg, int ndigits, int *decpt, int *sign) |
no2chem | 0:b5d3bd64d2dc | 85 | { |
no2chem | 0:b5d3bd64d2dc | 86 | return cvt(arg, ndigits, decpt, sign, CVTBUF, 1); |
no2chem | 0:b5d3bd64d2dc | 87 | } |
no2chem | 0:b5d3bd64d2dc | 88 | |
no2chem | 0:b5d3bd64d2dc | 89 | char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) |
no2chem | 0:b5d3bd64d2dc | 90 | { |
no2chem | 0:b5d3bd64d2dc | 91 | return cvt(arg, ndigits, decpt, sign, buf, 1); |
no2chem | 0:b5d3bd64d2dc | 92 | } |
no2chem | 0:b5d3bd64d2dc | 93 | |
no2chem | 0:b5d3bd64d2dc | 94 | char *fcvt(double arg, int ndigits, int *decpt, int *sign) |
no2chem | 0:b5d3bd64d2dc | 95 | { |
no2chem | 0:b5d3bd64d2dc | 96 | return cvt(arg, ndigits, decpt, sign, CVTBUF, 0); |
no2chem | 0:b5d3bd64d2dc | 97 | } |
no2chem | 0:b5d3bd64d2dc | 98 | |
no2chem | 0:b5d3bd64d2dc | 99 | char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) |
no2chem | 0:b5d3bd64d2dc | 100 | { |
no2chem | 0:b5d3bd64d2dc | 101 | return cvt(arg, ndigits, decpt, sign, buf, 0); |
no2chem | 0:b5d3bd64d2dc | 102 | } |