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.

[SOLVED] How can I use output of a function ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Friend,

I have a function below, how can I use the output of that function in the main() ?

Code:
unsigned char ReceiveSerial() {
unsigned char c;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1 = 253;                 /* TH1:  reload value for 9600 baud @ 11.0592MHz +/- 3.5% error  */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

	while( (SCON & 0x01) == 0 ) /* wait for receive data */;
	[B]	c = SBUF;
	return c;[/B]
	
}
// would it be like this :
main()
{
          ReceiveSerial(); // how can I use the output of this function ?
              [B]if (c="A") 
		       {
			  display_spectrum();
			}[/B]
}

can it be like this :

Code:
if (ReceiveSerial()="A")
// Do something

I tried to use putty but nothing happened yet ?

Thanks
 
Last edited:

Friend,

I have a function below, how can I use the output of that function in the main() ?

Code:
unsigned char ReceiveSerial() {
unsigned char c;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1 = 253;                 /* TH1:  reload value for 1200 baud @ 11.0592MHz +/- 3.5% error  */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

	while( (SCON & 0x01) == 0 ) /* wait for receive data */;
	[B]	c = SBUF;
	return c;[/B]
	
}
// would it be like this :
main()
{
          c=ReceiveSerial(); // how can I use the output of this function ?
              [B]if (c=="A") 
		       {
			  display_spectrum();
			}[/B]
}

can it be like this :

Code:
if (ReceiveSerial()="A")
// Do something


Thanks

now it work.
 

It will be like your second code segment.
 

Yes you can , the function returns a char so you can check its value but make sure that you use == and not =

Code:
if (ReceiveSerial()[B][COLOR="#FF0000"]==[/COLOR][/B]"A")
// Do something

Alex
 

can it be like this :

Code:
if (ReceiveSerial()="A")
// Do something

The single "=" is the assignment operator, not the equality relational operator which is "==".

Also when comparing a single character to a variable of type char, you should enclose the single character in single quotes, 'A', not double quotes which represents a string literal which are terminated by a NULL character.

Example:
Code:
if ('A' == ReceiveSerial())


BigDog
 
Yes you can , the function returns a char so you can check its value but make sure that you use == and not =

Code:
if (ReceiveSerial()[B][COLOR="#FF0000"]==[/COLOR][/B]"A")
// Do something

Alex

I sent "A" into puTTy

but not working yet ? why is that ?
 

Amazing, it took us three answers to finally give him a complete and correct reply for this simple question.
Pretest missed something, I missed something else... :roll:
 

I sent "A" into puTTy

but not working yet ? why is that ?

Try single quotes around a character as I explained in my previous post.

Code:
if ([COLOR="#FF0000"]'[/COLOR]A[COLOR="#FF0000"]'[/COLOR] == ReceiveSerial())

BigDog
 

Try single quotes around a character as I explained in my previous post.

Code:
if ([COLOR="#FF0000"]'[/COLOR]A[COLOR="#FF0000"]'[/COLOR] == ReceiveSerial())

BigDog

How can I know that I have sent A correctly via Putty ?

---------- Post added at 01:36 ---------- Previous post was at 01:34 ----------

Will it be like this :
Code:
       ReceiveSerial();
	   	if ('A'==ReceiveSerial())
	         { 
		   display_spectrum();
		 }
 

How can I know that I have sent A correctly via Putty ?

Will it be like this :
Code:
       ReceiveSerial();
	   	if ('A'==ReceiveSerial())
	         { 
		   display_spectrum();
		 }

You should only call the ReceiveSerial() routine from within the If statement expression, not before it.

Example:

Code:
  [COLOR="#FF0000"]//     ReceiveSerial(); Remove This Statement[/COLOR]
	   	if ('A'==ReceiveSerial())
	         { 
		   display_spectrum();
		 }

Or if you want to checkout your UART routines, like ReceiveSerial(), you could also simply echo back the character received from the PC, back to the PC or Putty.

BigDog
 
You should only call the ReceiveSerial() routine from within the If statement expression, not before it.

Example:

Code:
  [COLOR="#FF0000"]//     ReceiveSerial(); Remove This Statement[/COLOR]
	   	if ('A'==ReceiveSerial())
	         { 
		   display_spectrum();
		 }

Or if you want to checkout your UART routines, like ReceiveSerial(), you could also simply echo back the character received from the PC, back to the PC or Putty.

BigDog

Is it right for 9600 baud on 11.0592Mhz ?
Code:
unsigned char ReceiveSerial() {
unsigned char c;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
	TH1 = 253;                 /* TH1:  reload value for 9600 baud @ 11.0592MHz +/- 3.5% error  */
	TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

	while( (SCON & 0x01) == 0 ) /* wait for receive data */;
		c = SBUF;
	return c;
	 
}

How can I echo it back the character I sent ?
 

Is it right for 9600 baud on 11.0592Mhz ?
Code:
unsigned char ReceiveSerial() {
unsigned char c;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
	TH1 = 253;                 /* TH1:  reload value for 9600 baud @ 11.0592MHz +/- 3.5% error  */
	TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

	while( (SCON & 0x01) == 0 ) /* wait for receive data */;
		c = SBUF;
	return c;
	 
}

Depends on the make and model of your microcontroller.

What microcontroller are you using exactly, an 8051 variant?

How can I echo it back the character I sent ?

Simple.


Example:
Code:
while(1)
{
     TransmitSerial(ReceiveSerial());
}

BigDog
 

Depends on the make and model of your microcontroller.

What microcontroller are you using exactly, an 8051 variant?



Simple.


Example:
Code:
while(1)
{
     TransmitSerial(ReceiveSerial());
}

BigDog

Yes it's 8051 variant, AT89LV55,
I don't have
TransmitSerial(); function,

Do you have TransmitSerial function ?

thanks

---------- Post added at 03:18 ---------- Previous post was at 03:13 ----------

Like this :
Code:
	if ('A'==ReceiveSerial())
			     {
				   while(RI==0)
					    {
						  printf(ReceiveSerial());
						  RI=0;
						}
				   display_spectrum();

				 }
 

Possible like this :

Hello Friends,

How can I send serial data in MCS51 and verify it ?
Can I do like this ? :

Code:
unsigned char Serial_read() {
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
	TH1 = 253;                 /* TH1:  reload value for 9600 baud @ 11.0592MHz +/- 3.5% error  */
	TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

        while(!RI);
        RI = 0;
        return SBUF;

}

void serial_send(unsigned char dat){
        while(!TI);
        TI = 0;
        SBUF = dat;
}

main()
{
 serial_char=Serial_read();
	   	   	if ('A'==Serial_read())
			     {
				   serial_send(serial_char);
				   display_spectrum();

				 }
}
 

It's working on simulation but I don't understand, there's no response on hardware ??
serial.jpg
 

how you connect your controller to max232 your connection should like thismax232.png

and from max232 to PC u need use straight null modem cable. and your hyper terminal setting should be same as you set in your code it should work
 

how you connect your controller to max232 your connection should like thisView attachment 72289

and from max232 to PC u need use straight null modem cable. and your hyper terminal setting should be same as you set in your code it should work
I'm using that circuit already, and using serial cable DB9 with USB to Serial converter, does it matter ?
 

no if your converter is working fine it doesn't matter. then you just try with simple sending char code to debug the problem. if other code works on that hardware then there might be other issue.
 

no if your converter is working fine it doesn't matter. then you just try with simple sending char code to debug the problem. if other code works on that hardware then there might be other issue.

Ok, I will check with a simple sending char to this circuit and see what happen
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top