Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial comm between uC and PC...

Status
Not open for further replies.
Hello,

Here's the Getting Started for Processing: **broken link removed**

Very straightforward, I'm also reading and trying this now. :)
 
For storing string array is working correct.
Code:
while(1)                        // endless Loop       
  {  
ch='a';
HSerout(ch);//for testing uart is working
 
   for(char t=0;t<6;t++){

    c[t]= HSerin();

    
            }
for(char t=0;t<5;t++) {
ch=c[t];
    HSerout(ch); //sending back the data

}
        }


But using pointer are not displaying back the data ....

Code:
 while(1)                        // endless Loop       
  {  
ch='a';
HSerout(ch);//for testing uart is working
 
   for(char t=0;t<6;t++){

    *c++= HSerin();

    
            }
for(char t=0;t<5;t++) {
ch=*c++;
    HSerout(ch); //sending back the data

}
        }
 
The pointer keeps incrementing and never returns to the beginning of the array. When you use indexing, the loop always starts from the beginning of the array. The code where you use pointer starts always from the point where it left the last time.
 
This always starts from index 0 and loops through 0...4.
C:
for(char t=0;t<5;t++) {
ch=c[t];
    HSerout(ch); //sending back the data
 
}

In this code, the pointer c keeps on incrementing every time the loop is executed..
C:
for(char t=0;t<5;t++) {
ch=*c++;
    HSerout(ch); //sending back the data
 
}
 
Why don't you just use indexing?
 
Leave optimization to the compiler. Usually when a programmer starts "optimizing" his code, he actually makes it worse. Writing good readable code produces much better results most of the time. Modern compilers are very good at optimizing code...
 
Leave optimization to the compiler. Usually when a programmer starts "optimizing" his code, he actually makes it worse. Writing good readable code produces much better results most of the time. Modern compilers are very good at optimizing code...

OK, just leave but anyway how to read or point to initial the pointer to read string??
 
You need to re-define the pointer every time before going into the loop. You need an extra variable for that.

Code:
char *temp;
temp = c;
for(char t=0;t<5;t++) {
ch=*temp++;
    HSerout(ch); //sending back the data
 
}

another way is to "rewind" the pointer to the original position after the loop:

Code:
for(char t=0;t<5;t++) {
ch=*c++;
    HSerout(ch); //sending back the data
 
}
c = c-4;
 
char *temp;
temp = ch;
for(char t=0;t<5;t++) {
ch=*temp++;
HSerout(ch); //sending back the data

OK you are copying......

}
 
I added comments to the code:

Code:
char *temp; // Temporary pointer that we will use inside the loop.
temp = c; // Set the pointer at the beginning
for(char t=0;t<5;t++) {
ch=*temp++; // Increment the pointer and get the character from that position
    HSerout(ch); //sending back the data
 
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top