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

Code:
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....
 
But, like I said, It is useless to try "outsmart" the compiler. If you write this:
Code:
for(char t=0;t<5;t++) {
ch=c[t];
    HSerout(ch); //sending back the data
 
}

The compiler will most likely produce this:

Code:
HSerout(c[0]);
HSerout(c[1]);
HSerout(c[2]);
HSerout(c[3]);
HSerout(c[4]);
 
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
 
}


Not working this toooooo....
 
This is not working................

Code:
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....

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.
 
I would still use the original code that works. It is simple and elegant.

Code:
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:
for(char t=0;t<5;t++) {
    HSerout(c[t]); //sending back the data
}

What code could be more beautiful than that?
 
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;
 
OK, I am doing pointer for learning...
anyway.
in this code:

Code:
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??
 
Because after the first loop, the pointer is at the end of the string.
 
If you really want to use pointers, you need to use a temporary pointer for each loop:

Code:
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
 
}
 
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:
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
}
 
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:

Now the problem is why its ia not working??
 
not working.. I agree with that.
 
not working.. I agree with that.

why it is not working??
Code:
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??
 
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.
 
Here is the code........

Code:
    #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     
  }
 
Is the "HSerin();" function blocking or unblocking function?

.. that is a thinker for you.
 
Is the "HSerin();" function blocking or unblocking function?

.. that is a thinker for you.

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??
 
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??

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.
 
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..
 
Status
Not open for further replies.

Latest threads

Back
Top