Working: I am reading a byte from memory (EEPROM/FLASH etc) and then i want to send this byte to computer not as actual value but as a ascii characters of its hex value. eg i read 160 from memory, which is 0xA0 in hex, now i want to send this number not as 160 but as 'A' and '0' (which are 0x41 and 0x30), for this i am using this type of c code in MPLAB IDE,
//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());
I hope this is understandable. Problem: My concern is that i have a controller with frequency 3.6864MHz and i have to perform this operation on almost 1M bytes or more, so its a lot time consuming.
I was wondering if there is advance and fastest approach to this process for every single byte which can make my operation pretty fast?
Note:(Baud rate is 115200 which is quite fast and i want speed in processing bytes rather than time to send them.)
Each half-byte has sixteen possible values which are sequential and start with zero. This is ideal for a lookup table.
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);
}
Update : Since the characters you are transmitting are limited to the set of sixteen, you can precalculate the parity for these sixteen characters and then use a lookup table to get the parity value. (Please double check whether I've done the parity calculations correctly.)
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];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.