【发布时间】:2018-03-31 11:15:29
【问题描述】:
工作: 我正在从内存(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,这非常快,我希望处理字节的速度而不是发送它们的时间。)
【问题讨论】:
-
IIRC,执行一条指令需要 PIC 四个时钟周期,因此如果您想以每秒 115200 个字符的速度发送,则每个字符的预算只有 8 条指令。这是相当严格的,特别是如果你正在计算奇偶校验和东西,你似乎这样做了(尽管你还没有发布那段特定的代码)。如果你想实现这一点,那么这可能是时候改用手写汇编了,而不是试图让编译器做正确的事情。
-
为什么需要以这种形式发送?
-
这是我试图与之交流的软件的需求..
-
添加校验码
-
如果我提高 Fosc 速度,它会起作用吗?它可能会减少一些时间,但我认为这不是一个很好的解决方案??
标签: c microcontroller pic data-conversion mplab