Reputation: 881893
Posted as a new quesion based on this one (OP originally made a massive change to the question that was rolled back), as user stated they could not ask the question themselves.
About PIC 16F877A: I receive from a UART (a COM port) and show to an LCD 16x4. My data received is formed:
Line1#Line2#Line3#Line4
I want to put the data received to LCD as below:
/*LCD16x4
:~~~~~~~~~~~~~~~~:
: Line1 :
: Line2 :
: Line3 :
: Line4 :
:________________:
*/
I put the char
array to row x
, col y
(begin at 1
) to a LCD
by:
lcd_gotoxy(x, y);
printf(lcd_putc, data);
I have a char
array:
char data[];
I tried to create
getLcdContents
function to get the each line one by one:
char* getLcdContents(char arr[], int line)
{
char delimiter = '#';
int8 begin = 0;
int8 end = 0;
int step = 0;
char result[20];
//! printf("* Array length: %u \r\n", strlen(arr));
//! printf("* Array : %s \r\n",arr);
for (int8 i = 0; i < strlen(arr); i++)
{
if (arr[i] == delimiter)
{
end = i;
step++;
if (step == line)
{
for (int k = 0; k < 20; k++)
{
if (k < end - begin)
{
result[k] = arr[k + begin];
//printf("*result[%u]: %s \r\n",k,arr[k+begin]);
}
else
{
result[k] = " ";
}
}
}
begin = i + 1;
}
}
result[20] = '\0';
printf("* Line%u : %s \r\n", line, result);
return result;
}
Then get data from
RDA_isr(void)
to put toLCD
by:
#INT_RDA
Void RDA_isr(Void)
{
gets(rx);
char* row1 = getLcdContents(rx, 1);
lcd_gotoxy(1, 1);
printf(lcd_putc, row1);
char* row2 = getLcdContents(rx, 2);
lcd_gotoxy(1, 2);
printf(lcd_putc, row2);
char* row3 = getLcdContents(rx, 3);
lcd_gotoxy(1, 3);
printf(lcd_putc, row3);
char* row4 = getLcdContents(rx, 4);
lcd_gotoxy(1, 4);
printf(lcd_putc, row4);
}
But is does not work. Can anyone help me? Thank you very much!
Upvotes: 2
Views: 202
Reputation: 825
Thanks for all your help.
I 've done with this code in 2 solutions:
- Split by
#
char:
#include <string.h>
#include <stdio.h>
char rx[80];
char result[17];
//Line1#Line2#X: 11226655#Y: 123#
char *getLcdContents(char *arr,int line)
{
char delimiter = '#';
int begin = 0;
int end = 0;
int step = 0;
//printf("* Array length: %u \r\n", strlen(arr));
//printf("* Array : %s \r\n",arr);
for(int i = 0; i < strlen(arr); i++)
{
if (arr[i] == delimiter)
{
end = i;
step ++;
if(step == line)
{
for (int k = 0; k < 16; k++)
{
if(k<end-begin)
{
result[k] = arr[k+begin];
}
else
{
result[k] = ' ';
}
}
}
begin = i + 1;
}
}
result[16] ='\0';
//printf("* Line%u : %s \r\n",line,result);// in len pc
return result;
}
void main()
{
char *a = "Line1#Line2#X: 11226655#Y: 123#";
char* content = NULL;
content = getLcdContents(a, 1);
printf("%s\r\n",content);
content = getLcdContents(a, 2);
printf("%s\r\n",content);
content = getLcdContents(a, 3);
printf("%s\r\n",content);
content = getLcdContents(a, 4);
printf("%s\r\n",content);
//system("pause");
}
- Split each line by a char in a array:
char delimiter[]={'~','!','@','#','$'}
:
#include <string.h>
#include <stdio.h>
#include<time.h>
char rx[80];
char result[17];
void setTimeout(int milliseconds)
{
// If milliseconds is less or equal to 0
// will be simple return from function without throw error
if (milliseconds <= 0) {
fprintf(stderr, "Count milliseconds for timeout is less or equal to 0\n");
return;
}
// a current time of milliseconds
int milliseconds_since = clock() * 1000 / CLOCKS_PER_SEC;
// needed count milliseconds of return from this timeout
int end = milliseconds_since + milliseconds;
// wait while until needed time comes
do {
milliseconds_since = clock() * 1000 / CLOCKS_PER_SEC;
} while (milliseconds_since <= end);
}
//Data~Line1!Line2@Line3#Line3$
void action(char *arr,int line)
{
char delimiter[]={'~','!','@','#','$'};
int begin = 0;
int end = 0;
int step = 0;
switch(line)
{
case 0: step = 0; break;
case 1: step = 1; break;
case 2: step = 2; break;
case 3: step = 3; break;
case 4: step = 4; break;
default: break;
}
//printf("* Array length: %u\r\n", strlen(arr));
printf("* Array : %s \r\n",arr);
for(int i = 0; i < strlen(arr); i++)
{
if (arr[i] == delimiter[step])
{
for(int k = i; k >= 0; k--)
{
if(arr[k] == delimiter[step-1])
{
begin = k + 1;
}
}
//printf("Begin: %u\r\n",begin);
//printf("End : %u\r\n",i);
for(int l = 0; l < 16; l++)
{
if(l<i-begin)
result[l] = arr[l+begin];
else
result[l] = ' ';
}
break;
}
}
result[16] ='\0';
if (line == 0)
{
printf("Check condition here...\r\n");
}
else
{
printf("* Line%u: %s \r\n",line,result);
}
//lcd_gotoxy(1,line);
//printf(lcd_putc,result);
}
char rx[80];
int main()
{
char *a = "Dat~Line1!Line2@Line3#Line4$";
while(1)
{
for(int i = 0; i < strlen(a);i++)
{
setTimeout(50);
char rcv=a[i];
rx[i]=rcv;
if(rcv=='~')
action(rx, 0);
else if(rcv=='!')
action(rx, 1);
else if(rcv=='@')
action(rx, 2);
else if(rcv=='#')
action(rx, 3);
else if(rcv=='$')
{
action(rx, 4);
memset(rx, 0, sizeof rx);
}
}
}
//system("pause");
}
Upvotes: 1
Reputation: 67713
First of all you return local variable which does not exists any more when function returns.
As it is an PIC uC I would personally reuse the original string to preserve the memory.
int split(char **argv, char *string)
{
int argc = 0;
argv[0] = string;
while(*string)
{
if(*string == '#')
{
*string = 0;
argc++;
argv[argc] = string + 1;
}
string++;
}
if(argv[argc][0]) argc++;
return argc;
}
Upvotes: 2