NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Communicating between 2 MCUs

September 15, 2009
by Schrat
Schrat's Avatar

I have a program written on MCUt (t = transmitting microcontroller) that sets an int b to either 1, 2, or 3 depending on the amount of time a button is pushed. It then sends this to pin 3 by:

printf_P(PSTR("%drn"), b);

Pin 3 is connected to the serial on my computer and pin 2 on MCUr (r = receiving). The first time the button is pressed the computer receives and displays the corresponding b value and MCUr shows nothing. Then next times the button is pressed the computer displays the correct value but the MCUr displays the previous value of b. I also tried moving the serial cable of the computer to MCUr and it then shows the previous value of b. Here is the code of MCUr:

while(1) { while(b == 0){ scanf_P(PSTR("%dn"), &b); }

lcd_home();    
fprintf_P(&lcd_stream, PSTR("Temp: %d"), b);// write message to LCD
lcd_write_string(PSTR("   2temp "));// second method of writing to LCD
lcd_write_int16(b);
printf_P(PSTR("%d\r\n"), b);// sends to serial port

b = 0; lcd_line_two();
lcd_write_int16(b); } Why/where does MCUr hold the value of b until a new value is entered? How do I get it to display instantaneously?

Thanks Dave

September 15, 2009
by rusirius
rusirius's Avatar

I'm an MCU newb, so take it for what it's worth...

First I'll assume the %drn is actually supposed to be:

    %d\r\n

but the forum stripped out the slashes?

The why the %dn on the scanf? Remember that scanf will automatically look for the carriage return...

Try changing the scanf to ONLY have %d and see if you get better results... ;)

September 15, 2009
by rusirius
rusirius's Avatar

Going back and looking over it again, I think maybe you realized scanf would automatically pick up the return but you left the n thinking it would be an extra character?

scanf will SCAN the stream looking for ONLY what you specify... In other words, if you scanf with %d, it doesn't matter if 1000 other characters are in there, it will ONLY look for and input on a decimal number.

September 15, 2009
by Schrat
Schrat's Avatar

You were right the first time, taking the n out did the trick. I am curious where or why it stored the value but am ecstatic that it works.

Thanks a bunch Dave

September 16, 2009
by rusirius
rusirius's Avatar

It didn't actually store the value...

Think of it this way...

First, remember that scanf is a BLOCKING function... In other words, the chip will do absolutely nothing else while it is waiting for input...

You essentially told scanf to look for this pattern "A number followed then a newline"

scanf is smart enough to know to wait for a cr/nl along with the number... (otherwise entering 2 would fire off when you really wanted to enter 2100 for example)

So essentially what you were REALLY telling scanf to look for was "A number, then a carriage return, then a newline, then a newline"...

The first "a number, then a carriage return, then a newline" was assumed by the "%d"... The extra "then a newline" was tacked on by you... SO....

scanf starts monitoring the stream... you type 22 followed by the enter...

scanf sees "22+cr+nl", but it is STILL looking for an extra nl....

the you type "29 followed by enter...

scanf sees "29+cr+nl"... It finally sees the "next" newline... So what scanf "ingested" was "22+cr+nl+29+cr+nl", but it ignored everything in the string except what you told it, the first %d, and the nl on the end... ;)

If you would have continued on, and entered a 3rd number you would have found that instead of displaying 29, it would have instead done nothing again, the fourth number would have printed the third, etc...

Post a Reply

Please log in to post a reply.

Did you know that an electroluminescent backlight for an LCD panel requires hundreds of volts AC to run? Learn more...