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
    This is not working................

    Code (text):
    for(char t=0;t<6;t++){
     
        *c++= HSerin();
    }

        c = c-5;
    for(char t=0;t<5;t++) {
    ch=*c++;
        HSerout(ch); //sending back the data
     
    }
    not sending string back....
     
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    But, like I said, It is useless to try "outsmart" the compiler. If you write this:
    Code (text):

    for(char t=0;t<5;t++) {
    ch=c[t];
        HSerout(ch); //sending back the data
     
    }
    The compiler will most likely produce this:

    Code (text):

    HSerout(c[0]);
    HSerout(c[1]);
    HSerout(c[2]);
    HSerout(c[3]);
    HSerout(c[4]);
     
     
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    Not working this toooooo....
     
  4. dave

    Dave New Member

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


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    Pay a little attention now! you put the "c = c-5;" in the wrong place. Read my examples again.. and be careful with the details. Programming is all about the details. You can't hurry it.
     
  6. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I would still use the original code that works. It is simple and elegant.

    Code (text):

    for(char t=0;t<5;t++) {
    ch=c[t];
        HSerout(ch); //sending back the data
     
    }
     
    You do not save any memory by trying to be smarter than the compiler. You only make it worse..

    You can change that to:

    Code (text):

    for(char t=0;t<5;t++) {
        HSerout(c[t]); //sending back the data
    }
     
    What code could be more beautiful than that?
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    for(char t=0;t<6;t++){

    *c++= HSerin();
    }

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

    }
    c = c-4;
     
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, I am doing pointer for learning...
    anyway.
    in this code:

    Code (text):
    ch='a';
    HSerout(ch); //for testing UART initially
    for(char t=0;t<5;t++){
     
        *c++= HSerin();
    }
    ch='b';//testing after saving...
    HSerout(ch);

    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++) {
    c=*temp++; // Increment the pointer and get the character from that position
      ch=c;
      HSerout(ch); //sending back the data
     
    }
    you can see comment after reading it is not displaying char b on window only a at starting/resetting, why??
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Because after the first loop, the pointer is at the end of the string.
     
  10. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    If you really want to use pointers, you need to use a temporary pointer for each loop:

    Code (text):

    ch='a';
    char *temp; // Temporary pointer that we will use inside the loop.
    temp = c; // Set the pointer at the beginning
    HSerout(ch); //for testing UART initially
    for(char t=0;t<5;t++){
     
        *temp++= HSerin();
    }
    ch='b';//testing after saving...
    HSerout(ch);
     
    temp = c; // Set the pointer at the beginning
    for(char t=0;t<5;t++) {
    c=*temp++; // Increment the pointer and get the character from that position
      ch=c;
      HSerout(ch); //sending back the data
     
    }
     
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Your original motivation was to save memory.. but can you now see how using pointers only makes things more complicated (in this case) and uses more memory than the "elegant" way:

    Code (text):

    ch='a';
    HSerout(ch); //for testing UART initially
    for(char t=0;t<5;t++){
        c[t]= HSerin();
    }
    ch='b';//testing after saving...
    HSerout(ch);
     
    for(char t=0;t<5;t++) {
      HSerout(c[t]); //sending back the data
    }
     
     
  12. koolguy

    koolguy Active Member

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


    not working here is the result!!
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Now the problem is why its ia not working??
     
  14. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    not working.. I agree with that.
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    why it is not working??
    Code (text):
    ch='a';
    char *temp; // Temporary pointer that we will use inside the loop.
    temp = c; // Set the pointer at the beginning
    HSerout(ch); //for testing UART initially
    for(char t=0;t<5;t++){
     
        *temp++= HSerin();
    }
    ch='b';//testing after saving...
    HSerout(ch);
    the problem is in storing the string characterb is not coming on window??
     
  16. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I have posted many different versions of code examples.. Ian has posted many times more code than I have. And you copy paste every piece of code without thinking.. you even copy paste the codes that are supposed to be "bad example"- code. And you put them in your project without thinking.. so, it is impossible to say "what is wrong" without seeing the actual code you are testing. READ THE CODE YOU ARE WRITING AND THINK ON YOUR OWN. sorry for shouting.
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Here is the code........

    Code (text):
        #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
    unsigned char *c;

    unsigned char  HSerin(void);
     void HSerout(unsigned char ch),
     HSerinit(void);

     void main(void)                        // program entry  
       {    
       TRISB=0x00;
       unsigned char ch ;         // <- LOOK HERE.
       ADCON1 = 0x6;                    // Analogue off    
       HSerinit();      
      __delay_ms(1);

    while(1)                        // endless Loop      
      {  

    ch='a';
    char *temp; // Temporary pointer that we will use inside the loop.
    temp = c; // Set the pointer at the beginning
    HSerout(ch); //for testing UART initially
    for(char t=0;t<5;t++){
     
        *temp++= HSerin();
    }
    ch='b';//testing after saving...
    HSerout(ch);
     

        }
            }






           



           
     


     void HSerinit()    
     {    
       TRISC = 0b10000000;                    // should ideally be set  
       SPBRG = 129;                    // 20Mhz xtal 9600 BAUD    
       TXSTA = 0x24;                    // TXEN and BRGH  
       RCSTA = 0x90;                    // SPEN and CREN    
     }  

     void HSerout(unsigned char ch)     {  
       while(!TXIF);                    // Wait for module to finish  
      TXREG = ch;                        // ready to send  
      }

     unsigned char HSerin()    
     {     while(!RCIF);                    // Wait for a character    
     return RCREG;                    // return character    
      }




     
     
  18. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Is the "HSerin();" function blocking or unblocking function?

    .. that is a thinker for you.
     
  19. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    after reading and doing some experiment with code i think it is blocking function because code execution is not coming out of function, don't know why??
     
  20. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    It does not come out of that function because it is blocking. It waits until it receives a character every time you call that function. If you do not send a character.. it is stuck.
     
  21. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    then what to do?, i also notice it only take two character and it does not depend whether for loop used for 5,10,etc..
     

Share This Page