c - 需要转换数据(字节值转换为十六进制,十六进制字符转换为ASCII字符)

标签 c microcontroller pic data-conversion mplab

工作: 我正在从内存(EEPROM/FLASH 等)读取一个字节,然后我想将该字节发送到计算机,而不是作为实际值,而是作为其十六进制值的 ascii 字符。 例如,我从内存中读取了 160,即十六进制的 0xA0,现在我想将此数字发送为“A”和“0”(即 0x41 和 0x30),而不是 160, 为此,我在 MPLAB IDE 中使用这种类型的 C 代码,

    //Here is the code for Parity:
    uint8_t unAddParitytoByte(uint8_t unByte)
    { 
           uint8_t unNumberofOnes = 0;
           for(uint8_t unI = 0x80; unI ; unI>>=1)
           {
                 if((unByte & unI) != 0)
                {
                     unNumberofOnes++;
                }
          }
          if((unNumberofOnes%2) == 0)
          {
                  return unByte;
          }
          else
          {
               return (unByte|BIT7);
          }
    }
    void vSendByteToSoftware(uint8_t unDataByte)
    {
        uint8_t unTemp = 0, unHalfByte = 0;
        unTemp = (unDataByte >> 4) & 0x0F;
        unHalfByte = unReturnASCII(unTemp);
    /*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
        vSerialTransmitCharacter(unAddParitytoByte(unHalfByte)); 
        unBCCByte ^= unAddParitytoByte(unHalfByte);
        unTemp = unDataByte & 0x0F;
        unHalfByte = unReturnASCII(unTemp);
        vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
        unBCCByte ^= unAddParitytoByte(unHalfByte);
    }
    uint8_t unReturnASCII(uint8_t unNibble)
    {
         uint8_t unChar = 0;
         switch(unNibble)
         {
            case 0:
                 unChar = '0';
                 break;
            case 1:
                 unChar = '1';
                 break;
            case 2:
                 unChar = '2';
                 break;
            case 3:
                 unChar = '3';
                 break;
            case 4:
                 unChar = '4';
                 break;
            case 5:
                 unChar = '5';
                 break;
            case 6:
                 unChar = '6';
                 break;
            case 7:
                 unChar = '7';
                 break;
            case 8:
                 unChar = '8';
                 break;
            case 9:
                 unChar = '9';
                 break;
            case 10:
                 unChar = 'A';
                 break;
            case 11:
                 unChar = 'B';
                 break;
            case 12:
                unChar = 'C';
                break;
            case 13:
                unChar = 'D';
                break;
            case 14:
                unChar = 'E';
                break;
            case 15:
                unChar = 'F';
                break;
            default:
                break;
        }
        return unAddParitytoByte(unChar);
    }
    vSendByteToSoftware(unReadBytesfromTargetFlash());

我希望这是可以理解的。 问题: 我担心的是,我有一个频率为 3.6864MHz 的 Controller ,我必须在几乎 1M 字节或更多字节上执行此操作,因此非常耗时。

我想知道对于每个字节是否有先进且最快的方法来处理这个过程,这可以使我的操作变得相当快?

注意:(波特率为 115200,相当快,我想要处理字节的速度而不是发送它们的时间。)

最佳答案

每个半字节有十六个可能的值,这些值是连续的并且从零开始。这对于查找表来说是理想的选择。

    uint8_t const ascii_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    void vSendByteToSoftware(uint8_t unDataByte)
    {
        uint8_t unTemp = 0, unHalfByte = 0;
        unTemp = (unDataByte >> 4) & 0x0F;
        unHalfByte = ascii_hex[unTemp];
    /*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
        vSerialTransmitCharacter(unAddParitytoByte(unHalfByte)); 
        unBCCByte ^= unAddParitytoByte(unHalfByte);
        unTemp = unDataByte & 0x0F;
        unHalfByte = ascii_hex[unTemp];
        vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
        unBCCByte ^= unAddParitytoByte(unHalfByte);
    }

更新: 由于您传输的字符仅限于 16 个字符,因此您可以预先计算这 16 个字符的奇偶校验,然后使用查找表来获取奇偶校验值。 (请仔细检查我的奇偶校验计算是否正确。)

uint8_t const ascii_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
uint8_t const ascii_hex_with_parity[16] = { 0x30, 0xB1, 0xB2, 0x33, 0xB4, 0x35, 0x36, 0xB7, 0xB8, 0x39, 0x41, 0x42, 0xC3, 0x44, 0xC5, 0xC6};

void vSendByteToSoftware(uint8_t unDataByte)
{
    uint8_t unTemp = 0, unHalfByte = 0;
    unTemp = (unDataByte >> 4) & 0x0F;
    unHalfByte = ascii_hex[unTemp];
/*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
    vSerialTransmitCharacter(unAddParitytoByte(unHalfByte)); 
    unBCCByte ^= ascii_hex_with_parity[unTemp];
    unTemp = unDataByte & 0x0F;
    unHalfByte = ascii_hex[unTemp];
    vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
    unBCCByte ^= ascii_hex_with_parity[unTemp];
}

关于c - 需要转换数据(字节值转换为十六进制,十六进制字符转换为ASCII字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587097/

相关文章:

arm - STM32F103中的ADC可以采样高达3Msps吗?

c - 在 ansi C 中为嵌入式设备编写可重用的低级图形库

c - MPlab 中的图 C 处程序意外地回到了要点

c - 系统如何识别不同函数中同名的静态变量?

c - 如何确定接口(interface)名称

microcontroller - STM32L151 RTC闹钟中断

c - 当端口从 0 变为 1 时,如何添加 90 分钟的延迟?

c - 在二进制数中找到第一个 "1"

c - "Aborted"程序使用 GNU MP "large numbers"库

c - Atmel Cortex-M0+ SAMC21 在 SysTick_Config 之后挂起