工作: 我正从内存中读取一个字节(EEPROM / FLASH等)然后我想将此字节发送到计算机而不是实际值,而是作为其十六进制值的ascii字符。 例如我从内存中读取160,这是十六进制的0xA0,现在我想发送这个数字不是160,而是'A'和'0'(它们是0x41和0x30), 为此我在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的控制器,我必须在大约1M字节或更多字节上执行此操作,因此耗费了大量时间。
我想知道是否每个字节都有先进且最快的方法来处理这个过程,这可以使我的操作非常快?
注意:(波特率是115200,非常快,我希望处理字节的速度,而不是发送它们的时间。)
答案 0 :(得分:1)
每个半字节有16个可能的值,这些值是顺序的,从零开始。这是查找表的理想选择。
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];
}