Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] USART PIC String comparison

Status
Not open for further replies.

sasidharallnew@gmail.com

Newbie level 2
Joined
Sep 4, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
18
Hi All
i want to compare the data received from RCREG of PIC 16f877a and store it in a array and compare it with the array Which i already declared and take decision accordingly.

Pls find the code below..

The problem is it is only comparing character
eg: Suppose it i type some random data like"gdsgs" i am receiving ss in the output.

Pls help to sort out the issue.



Code:
#include <pic.h>     // Define PIC Registers
#include<htc.h>
#include<string.h>
__CONFIG(0x3f72);

char temp1[]={""};
char temp2[]={"sasi"};
static int count2;
void main()

{
     static int count=3;   // added for count increment
      TRISB=0x01;            // Configure RB0 as I/P
      TRISC=0b11000000;            // Configure PORTC as O/P
      RC0=0;
      PORTB=0;
      INTCON=0x90;      // Enable the Global Interrupt & External Interrupt

	SPBRG = 129; // the hex value selected from the table
	TXSTA = 0b00100110; // determining the settings for the transmitter
	RCSTA = 0b10010000; // determining the settings for the receiver
	TXREG = 0X0; // initializing the binary value of the transmitted information
	TRISA=0x00;
	PORTA=0xFF;
	TRISD=0x00;

do // beginning of the endless loos from “do” to “while(1)”
{
int strlen1=0;
while (!RCIF);
{
temp1[strlen1]=RCREG;
strlen1++;
}

if(*temp1==*temp2)
{
TXREG=*temp1;
}

int a=strlen1;//sizeof(temp1);

PORTD=a;
RCSTA = 0b10010000; // determining the settings for the receiver
for (int i=0; i< 200; i++); // delay in order to identify the change by looking at the LEDs
}      
while(1);


}
 

This is wrong.

Code:
while (!RCIF);

When serial data is received RCIF will be set and not of it is 0. So, while(0) will not execute as it is false.

You have to use

Code:
while(RCIF) {
   temp1[strlen1++] = RCREG;
   RCIF = 0;
}

You have to do this in while(1) loop. A better way is to use ISR to write the serial interrupt code.
 

You should also consider what happens if the matching string isn't found and it just keeps collecting characters - where do they go?
If the match is found it should only transmit one character back.

If your incoming data has a delimiter character, it is best to capture the data until the delimiter is found then make the comparison.

Some compilers will complain about the line "int a=strlen1;//sizeof(temp1);" which makes a new 'int' assignment inside the loop. It's better to declare it outside the loop in case the compiler adds extra memory management code to allocate an address each time.

Using an ISR is a FAR better way of reading the UART!

Brian.
 

i am receiving ss in the output

I don’t use PIC to tell about settings, but from C point of view, it seems reasonable to get just the "ss" back, because 's' is found two times in the word “sasi”. What you do now is clear strlen1 each time the loop starts, and when the byte comes you send it back if it is the same with the first letter of "sasi" (‘s’).

You need to clear strlen1 only when it reaches the sizeof(temp2), in order to send all the letters of the string. And also change the pointer comparison with if(temp1[strlen]==temp2[strlen]), because in this way you are using it, you will only compare the first character of the arrays.
 

hi,
Do you want to compare total array. But as per my understanding you are comparing the temp1[0] with temp2[0].

regards,
NSN
 

Hi,
First of all sorry for the delayed reply...
Thanks PIC programmer and betwixt....
I have made changes according to ur suggestions and the code is working fine as anticipated....

Thanks for Helping...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top