ADC values over serial in C

I have had some success with your gyro module and Wixels and Maestro.
But getting stuck on a C programming approach.
The problem is that adcRead returns a uint16 how do I sent this as a byte array with usbComTxSend.
Simply how do I convert uint16 to uint8 XDATA * ?
The code block below throws away most of the resolution and chops off the upper 128 values.
I should be able to use something like itoa and send that with usbComTxSend.

void txGyroState()
    uint8 XDATA * txBuf;
    static uint8 lastTx = 0;
    if ((uint8)(getMs() - lastTx) > TX_INTERVAL )
		uint16 fx, fz;
		uint8 com;
                fx = adcRead(2 | ADC_BITS_12) / 8;  // read 0 to 2047 divide by 8 to range
                fz = adcRead(1 | ADC_BITS_12) / 8;
		//txBuf[0] = (int8)fx;
		//txBuf[1] = (int8)fz;
		com = usbComTxAvailable();  // todo test later
        lastTx = getMs();

I am reading the virtual com port with terminal.
Thank you,

Typical terminal programs interpret bytes from the virtual COM port as ASCII characters, so what you need to do is convert the raw data from the ADC in to an ASCII string.

The easiest way to do this is to use sprintf:

if (usbComTxAvailable() >= 64)
    uint8 XDATA report[64];
    uint8 reportLength;
    uint16 fx, fz;
    fx = adcRead(2);
    fz = adcRead(1);
    reportLength = sprintf(report, "%4d, %4d\r\n", fx, fz);
    usbComTxSend(report, reportLength);

I haven’t tested the code above but I think it will work.

Don’t forget to put this at the top of your source file:

#include <stdio.h>

If you continue to have trouble, I recommend that you look at the test_adc app in the Wixel SDK. It reads the values on all of the ADC inputs and sends them to the computer. If you open the virtual COM port in a terminal program, you’ll see a bargraph showing the ADC values. Even if you don’t have trouble, running the test_adc app can be useful to give you a visual sense of what your gyro is outputting.

SDCC also has an _itoa function, declared in stdlib.h, but I have not used it.


Thank you,
I will check out _itoa and use sprintf for now.