We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi everybody! I work with Keil C251 uVision2. in my programm i want to recieve string into controller. i have an array of characters incom[20], which i use as a buffer for recieving string. i recieve current byte into variable bufIn and then replace it into my array:
1. strcat(incom, bufIn);
2. incom[i] = bufIn; i++;
for(i = 0; i < 20; i++) Print(incom[i]);
>1. strcat(incom, bufIn); >2. incom[i] = bufIn; > i++; How you declare "bufIn" ? Function strcat need "pointer to char" (not a "char") as second parameter. This must be NULL-terminated string with length less than unused area in "incom" !!! So, your code probably have a bug or bug's. I think this is not C251-specific question. >Why I can't create any array of >characters more than 20 symbols? Why not ? No problem.
char Buf[32]; // default memory space // or char idata Buf[64]; // or char xdata Buf[1000]; // or char edata Buf[32]; // C251-native addressing
Thank You for anser. i thik i've understood my problem. Thanks to all.
Sorry. I didn't found my mistake. my code is:
bit Input = 0; int i = 0; unsigned char incom[18] = ""; unsigned char bufIn[2] = ""; .... int PrntTo (unsigned char ch) { TI = 0; SBUF = ch; while(!TI); } void port_io(void) interrupt 4 using 2 { if (RI) { bufIn[0] = SBUF; Input = 1; } } ... void main (void) { ... while(1) { memset (incom, 0, 18); ... while (Input) { PrntTo(bufIn[0]); RI = 0; Input = 0; strcat(incom, bufIn); i++; if(i == 17) { for(i = 0; i < 17; i++) PrntTo(incom[i]); i = 0; } } } ... }
Your code includes several handling errors. I included my remarks in the code section.
bit Input = 0; int i = 0; /* initialized variables blow up the code size. Try to avoid them. */ unsigned char incom[18] = ""; /* The buffer must have a size of 19 if you want to receive 18 characters. Since you handle the buffer as string, a trailing 0x00 is inserted. */ unsigned char bufIn[2] = ""; /* The second byte of bufIn is never set to 0x00. This must be done for the strcat()-function. */ .... int PrntTo (unsigned char ch) { TI = 0; SBUF = ch; while(!TI); /* TI generates in interrupt here. You must handle this IRQ somehow. */ } void port_io(void) interrupt 4 using 2 { if (RI) { bufIn[0] = SBUF; /* RI should be cleared immediately after reading SBUF. Otherwise you may lose a character. */ Input = 1; } /* Deal here to clear the TI-Interrupt-request. Easiest way: TI =0; */ } ... void main (void) { ... while(1) { memset (incom, 0, 18); /* use sizeof instead of 18. This applies to all occurences of the buffer's size. */ ... while (Input) { PrntTo(bufIn[0]); RI = 0; Input = 0; /* Setting this bit to 0 forces the controller to exit the while-Loop. Then the while(1)-loop is executed again, which clears your buffer again. All characters in the buffer are lost.... */ strcat(incom, bufIn); i++; if(i == 17) { for(i = 0; i < 17; i++) PrntTo(incom[i]); i = 0; } } } ... }
Your code not full... I still don't understand it. No hardware initialization sequence. (serial port, interrupts, timers) No sense to comment all of your code, just at first look: - you must clear RI manually
if (RI) { RI = 0; // !!!
void port_io(void) interrupt 4 using 2 { if( TI ) { TI = 0; // etc } if (RI) { RI = 0; // etc } }
void port_io(void) interrupt 4 using 2
>The buffer must have a size of 19 if >you want to receive 18 characters :) This guy "want" to receive 17 (I think :)
i++; if(i == 17) { for(i = 0; i < 17; i++)