back to embedded

C Int to Serial/UART/ASCII

 

void sendascii(unsigned int val, BOOL lcd, unsigned int decimal){
    BYTE i, outval;
    unsigned int div=10000;
    BOOL sent=FALSE;
    for(i=0; i<5; i++){
        outval = (BYTE)(val / div);
        if(outval > 0 || sent || i == 4){
            outval += 0x30;
            //the following function is for sending to serial port
            UART_1_SendData(outval);
            if(decimal == div){
                UART_1_SendData('.');
            }
            sent = TRUE;
        }
        val = val % div;
        div = div / 10;
    }
}

 

Sends an integer out to a serial port in ascii format. Also has support for inserting a decimal point in the output in case a shifted integer was used in place of a floating point variable (ie 1475 == 14.75). Especially useful for outputting formatted values from an AD converter.