free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Rickey's World :: Discussion Forums :: Discuss and Learn :: 8051 Discussion Forum
 
<< Previous thread | Next thread >>
Help regarding printf and scanf
Go to page       >>  
Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage
Author Post
Amit Kumar Das
Mon Nov 30 2009, 01:48AM
 User Offline
Registered Member #7509
Joined: Mon Apr 28 2008, 06:55PM

Posts: 16
Thanked 0 times in 0 posts
I am using AT89s52 uC, 11.0592 MHz crystal, 9600 baud rate. I am not getting the output in hyper-terminal whereas Tx and Rx LEDs are glowing during receive and transmit of the data or while pressing the keys in keyboard. I am getting the output in debug terminal. I am enclosing the program below. Could anybody please find out where lies the fault. Thx in advance.

#include <reg51.h>
#include <stdio.h>

void serial_init()
{
SCON=0x50;
TMOD=0x20;
TH1=0xF3;
TR1=1;
TI=1;
}

void main()
{
unsigned char a[8];
serial_init();
printf("\nWELCOME TO OUR WORLD\n");
for(;;)
{
printf("\nENTER TEXT\n");
scanf("%s",&a);
printf("\nYOU HAVE ENTERED:%s\n",a);
}
}

Back to top

DavesGarage
Mon Nov 30 2009, 09:52AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 621
Thanked 116 times in 112 posts
Please show all of your code - you haven't shown the interrupt handler or the putchar() function...

Thanks,


-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Ajay Bhargav
Mon Nov 30 2009, 12:15PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1329 times in 1253 posts
i dont trust printf and scanf moreover they are not even recommended to use for embedded applications. why don't you write your own simple functions? its really easy to do. check serial tutorial.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top

gopi
Tue Dec 01 2009, 10:01AM
 User Offline
Registered Member #9289
Joined: Fri Jul 25 2008, 05:04AM

Posts: 192
Thanked 12 times in 12 posts
Amit Kumar Das wrote ...

void serial_init()
{
SCON=0x50;
TMOD=0x20;
TH1=0xF3;
TR1=1;
TI=1;
}


In the code you are setting the baud rate for 2215bps but not for 9600bps.
So change " TH1=-3 or 0xfd or 253"

Never cry in front of others...........
Don't give them the pleasure of knowing how much they hurt you.
Back to top

DavesGarage
Tue Dec 01 2009, 03:10PM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 621
Thanked 116 times in 112 posts
Here are some helpful links regarding using the printf(...) function:

http://www.keil.com/support/man/docs/c51/c51_printf.htm

http://www.keil.com/support/man/docs/c51/c51_xf_usingprintf.htm

http://www.keil.com/support/docs/867.htm

http://www.keil.com/support/docs/788.htm

The important part here is as follows:

This function is implementation-specific and is based on the operation of the _getkey and putchar functions. These functions, as provided in the standard library, read and write characters using the microcontroller's serial port. Custom functions may use other I/O devices.


and here is your most basic version of putchar()
CODE:
char putchar (char c)  
   {
   while (!TI);
   TI = 0;
   return (SBUF = c);
   }
 


-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Amit Kumar Das
Tue Dec 01 2009, 09:52PM
 User Offline
Registered Member #7509
Joined: Mon Apr 28 2008, 06:55PM

Posts: 16
Thanked 0 times in 0 posts
Ajay Sir,

Could u plz give an example of how to use your uploaded library which is available at http://www.8051projects.net/downloads7.html, bcoz it is showing error "requires ANSI style prototype" Please give an example of receiving a string also, as it is not included in the library. Thx..
Back to top

Ajay Bhargav
Wed Dec 02 2009, 12:27PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1329 times in 1253 posts
you need to include uart.h file in your code

CODE:
#include "uart.h"
 


do not include the lib file just the c file is sufficient.

for receiving string, you can use uart_receive() function and keep reading till you get a \r from serial port. uart_receive() will read 1 byte from serial port.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top

Amit Kumar Das
Wed Dec 02 2009, 02:34PM
 User Offline
Registered Member #7509
Joined: Mon Apr 28 2008, 06:55PM

Posts: 16
Thanked 0 times in 0 posts
Can u plz correct my program..

#include <uart.h>
unsigned char mybyte;
char str;
void main()
{
uart_init();
P1=0xff;
P2=0x00;
while(1)
{
mybyte=P1;
uart_send("135");
uart_send(mybyte);
mybyte=bit_8 uart_receive();
P2=mybyte;
uart_string("HELLO");
uart_echo();
do{
str=uart_receive();
}while (str=='\r');
}
}

Thx.
Back to top

DavesGarage
Thu Dec 03 2009, 06:28PM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 621
Thanked 116 times in 112 posts
Have a look at my thread:

http://www.8051projects.net/forum-t28867-last.html

Download the software, and see if it works on your system. Then, feel free to comment in that thread about your findings...

You sound like you have the right system for my "outsourcing" project

-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Amit Kumar Das
Fri Dec 04 2009, 02:27PM
 User Offline
Registered Member #7509
Joined: Mon Apr 28 2008, 06:55PM

Posts: 16
Thanked 0 times in 0 posts
Dear Daves,

I posted here because the thread link you have posted is showing 'thread not found' at the time of posting. I analysed and tested your program on the hardware with the settings mentioned in the program. It just prints "Keyboard Echo active". There is no echo of the character typed in the hyperterminal. Even in debug the same thing happens. Let me clear what my requirement is, Some character has to be stored in a pointer e.g. mystr="WELCOME TO 8051" which has to be transferred to hyperterminal. Secondly, it has to send value of mydata say, mydata=0x08. Thirdly 8051 has to receive the numbers(typed in the hyperterminal) say value of mybyte as well as mystr, store it in ram using pointers and display it in LCD.
Back to top

Go to page       >>   

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems