1. 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.
    Dismiss Notice

Serial comm between uC and PC...

Discussion in 'Microcontrollers' started by koolguy, Apr 26, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    thanks................
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, do you know processing software....?
     
  3. Brian Griffin

    Brian Griffin Member

    Joined:
    May 23, 2013
    Messages:
    49
    Likes:
    8
    Location:
    Kuala Lumpur
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia

    Awesome.

    Mike.
     
    • Like Like x 1
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    For storing string array is working correct.
    Code (text):

    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 (text):
     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

    }
            }
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    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.
     
  9. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    yes but how to set it at initial position??
     
  10. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    This always starts from index 0 and loops through 0...4.
    Code (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..
    Code (C):

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

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Yes, i am asking then how to read after storing the string???
     
  12. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    How do you set it to initial position the first time? How do you get the pointer?
     
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Why don't you just use indexing?
     
  14. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    because i want to use pointer and save memory....


    ??
     
  15. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    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...
     
  16. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, just leave but anyway how to read or point to initial the pointer to read string??
     
  17. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    You need to re-define the pointer every time before going into the loop. You need an extra variable for that.

    Code (text):

    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 (text):

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

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Can you explain me bit more its working?
     
  19. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    You need to look at the code snippet I provided. Go through it and think about how it works..
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    char *temp;
    temp = ch;
    for(char t=0;t<5;t++) {
    ch=*temp++;
    HSerout(ch); //sending back the data

    OK you are copying......

    }
     
  21. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I added comments to the code:

    Code (text):

    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
     
    }
     
     

Share This Page