I am working on Some embedded project[PIC MCU] where I will receive the data over UART..The data will contain some ascii values which I have to convert into HEX and those converted HEX value will be used further for some operation.I needed help So my question is anyone has the logical part how it can be done? I have done some work though..any stackoverflow thread , hint , or CODE will be highly appreaciated Here is my code till now
#include "mcc_generated_files/mcc.h"
#define _XTAL_FREQ 16000000
#define FRAMESIZE 18
void main(void)
{
uint8_t data ,i,j;
uint8_t value;
uint8_t RX_Buffer [] ,RGB_data[] ,HEX_data[] ;
// initialize the device
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
do
{
data = EUSART_Read(); // Read received character
for (i = 0; i<FRAMESIZE ; i++)
{
RX_Buffer[i] = data;
}
EUSART_Write(data); // Echo back the data received
}while(!RCIF); //check if any data is received
// my UART data command starts with R and ends with '\n'
if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-1] == '\n')
{
LATAbits.LATA2 = 1; //check flag
__delay_ms(2000);
LATAbits.LATA2 = 0;
for (j = 0 ; j = 5; j++ ) // get the RGB value in separate array
{
RGB_data[j] = RX_Buffer[j+3];
HEX_data[value] = RGB_data[j]/16 ;
// so I am somewhat stuck how can I collect the UART data {for eg "RGBFF00A0AA" is my command which will be in ascii
if (HEX_data[value] <=9)
{
HEX_data[value] += '0';
}
else
{
HEX_data[value]=HEX_data[value]-10+'A';
}
}
}
}
If you have stdio, converting an ASCII input to HEX is trivial.
void AsciiToHex(char * src, char * dest)
{
while(*src)
{
sprintf(dest + strlen(dest), " %02X", *src);
src++;
}
}
EDIT:
In small microcontrollers (attiny, PIC 16F, 8051 and others) you will not have stdio available... In that case, you will need a more hands on approach, something like the following code:
void AsciiToHex(char * src, char * dest)
{
char hex[] = "0123456789ABCDEF";
while(*src)
{
char low, high;
high = *src >> 4; // high nibble
low = *src & 0x0f; // low nibble
*dest = hex[high];
dest++;
*dest = hex[low];
dest++;
*dest = ' ';
dest++;
src++;
}
*dest = 0;
}