I’m using a PIC18F microcontroller with mikroC PRO for PIC.
The PIC micro reads strings over the UART, which Software implemented because there is only one UART, and I need two.
I’m trying to write a copy of **“void UARTx_Read_Text(char Output, char Delimiter, char Attempts);” function and my code seem reluctant and suboptimal.
Can anyone please help me improve my code?
Thanks to all the answers,
Yishai,
The code block:
/*
* Prototype : void Soft_UART_Read_Text(char *S, char *Delimiter, char Attempts);
* Returns : Nothing
* Description : Reads characters received via UART until the delimiter sequence is detected.
* The read sequence is stored in the parameter output; delimiter sequence is stored in the parameter delimiter.
* This is a blocking call: the delimiter sequence is expected, otherwise the procedure exits (if the delimiter is not found).
* Parameters : S - Received text
* Delimiter - Sequence of characters that identifies the end of a received string.
* Attempts - Defines number of received characters in which Delimiter sequence is expected.
* If Attempts is set to 255, this routine will continuously try to detect the Delimiter sequence.
* Requires : Nothing
* Example : UART1_Read_Text(output, "OK", 10); // reads text until 'OK' is found
*/
void
Soft_UART_Read_Text(char *S, char *Delimiter, char Attempts)
{
int DelimiterLength = strlen(Delimiter);
int StringLength = BUFF_SIZE; // string length
char i = 0;
char DelimiterCount = 0;
char read_byte;
while(DelimiterCount <= DelimiterLength)
{
read_byte = Soft_UART_Read(&error);
if(error) PORTD.B1 = 1;
else PORTD.B1 = 0;
if(read_byte == Delimiter[DelimiterCount])
{
DelimiterCount++;
if(DelimiterCount == DelimiterLength)
break;
}
else
{
*(S+i) = read_byte;
//i++;
if(++i > StringLength) i= 0;
if(Attempts < 255)
{
if(Attempts == i)
break;
}
}
}
}
Write C code to read string over the UART,
Improve my C code.
5