Discussion in "8051 Discussion Forum" started by    vishie90    Jan 11, 2010.
Mon Jan 11 2010, 01:42 PM
#1
hi im using a p89v51rd2 ic
i wrote a simple program for serial communication with the pc i am having the following problem.
1) i want the char 'a' & 's' to be shown on the output only once and dont want them to be repeated as shown below.
PLS reply at the earliest
PROGRAM
#include#define sw P1_1
void main(void)
{
unsigned char fname='a';
unsigned char lname='s';
sw=1;
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
if(sw==0)
{
SBUF=fname;
while(TI==0);
TI=0;
}
else
{
SBUF=lname;
while(TI==0);
TI=0;
}}
OUTPUT(as seen in TERMINAL)
ssssssaaasssaaasssaa ...



[ Edited Mon Jan 11 2010, 03:11 PM ]
Mon Jan 11 2010, 03:10 PM
#2
When using a microcontroller (no operating system),
you must remember that it is fetching and executing
instructions, one after another, over and over again,
while there is power applied.

What do you suppose happens when it gets to the end
of the main() function above?

It continues trying to fetch another instruction, but from where?

The very next memory location.

If there is a NOP there, it continues. If an executable
instruction, then it will execute that instruction.
If garbage, then who knows?

This will continue over and over again until it ether hangs, runs out of memory (0xFFFF),
where it will start over again at 0x0000, or resets, but it all depends on
what is in memory at the time.

A good rule of thumb is "never leave your processor unattended",
sort of like a child with sharp objects.

Try adding an infinite loop at the end of your main() function, like this:
 while(1);


See if this doesn't solve your problem...
Mon Jan 11 2010, 03:22 PM
#3
no it is still producing the continuous characters!!
Mon Jan 11 2010, 03:27 PM
#4
i would be happy if u told me how to produce just a single character only once!!
Mon Jan 11 2010, 08:07 PM
#5
But I did...

I told you how, and you said it is still producing continuous characters, so you must have made a mistake!

If you want a simple solution to your problem WITHOUT learning anything, you can just copy the software from this thread:

http://www.8051projects.net/forum-t28908.html

I wrote this thread as an example that can be studied, and hopefully learned from, but it will do what you want very simply...

Here is a complete example for you to just copy and paste, if you like:
//=====================================================================
// simple example software for testing serial interface...            
//=====================================================================
#include "AT89X51.H"        //    
#include <stdio.h>
 //

#define sw P1_1   //

#define BUF_SIZE           16          // buffer size for both Rx  
                                       // and Tx buffers            
#define MICRO_FREQUENCY    11059200.0  // frequency of micro...    

typedef unsigned char BYTE;
typedef unsigned long ULONG;
typedef unsigned int WORD;

#define UART0_VECTOR       4           //0x23 Serial Port 0        

volatile BYTE TxBuffer[ BUF_SIZE ];  
volatile BYTE RxBuffer[ BUF_SIZE ];  
volatile BYTE RxInChar;
volatile BYTE RxOutChar;
volatile BYTE TxInChar;
volatile BYTE TxOutChar;
volatile BYTE TxEnabled;

//=====================================================================
// init routine for ISR handler...                                    
//=====================================================================
void SER_init()
   {
   /* disable interrupts...       */
   EA          = 0;
   
   /* clear com buffer indexes... */
   TxInChar    = 0;
   TxOutChar   = 0;
   TxEnabled   = 0;
   RxInChar    = 0;
   RxOutChar   = 0;
   
   /* Setup serial port registers */
   SM0         = 0;
   SM1         = 1;           // serial port MODE 1
   SM2         = 0;
   REN         = 1;           // enable serial receiver
   TI          = 0;           // clear transmit interrupt
   RI          = 0;           // clear receiver interrupt
   ES          = 1;           // enable serial interrupts
   PS          = 0;           // set to low priority
   EA          = 1;           // Enable Interrupts
   }

//=====================================================================
//=====================================================================
void SER_ISR() interrupt UART0_VECTOR using 2
   {
   /* Received value interrupt    */
   if( RI )
      {
      RI = 0;

      /* get byte if there's room in the buffer */
      if( ((RxInChar + 1)%BUF_SIZE) != RxOutChar )
         {
         RxBuffer[ RxInChar++ ] = SBUF;
         RxInChar %= BUF_SIZE;
         }
      }

   /* Transmitted value interrupt */
   if( TxEnabled )
      {
      /* if previous byte is finished transmitting */
      if( TI )
         {
         TI = 0;

         /* adjust queue and transmit character... */
         if (TxOutChar != TxInChar)
            {
            SBUF = TxBuffer [TxOutChar++];
            TxOutChar %= BUF_SIZE;
            }
         else
            /* nothing else to transmit, so disable function... */
            TxEnabled = 0;
         }
      }
   }

//=====================================================================
//=====================================================================
void SER_setbaud( ULONG BaudRate )
   {
   /* disable interrupts */
   ES           = 0;
   
   /* Clear interrupt flag and buffer */
   TI           = 0;
   TxInChar     = 0;
   TxOutChar    = 0;

   /* transmitter is disabled */
   TxEnabled    = 0;
   
   /* Set timer 1 up as a baud rate generator.  */
   TR1         = 0;           // stop timer 1
   ET1         = 0;           // disable timer 1 interrupt
   PCON       |= 0x80;        // 0x40=SMOD1: set serial baudrate doubler
   TMOD       &= 0x0F;        // clear timer 1 mode bits
   TMOD       |= 0x20;        // put timer 1 into MODE 2 : 8-bit, auto-reload
   TH1         = (BYTE) (256.0 - (MICRO_FREQUENCY / (192.0 * (float)(BaudRate))));
   TR1         = 1;           // start timer 1
   
   /* enable interrupts */
   ES           = 1;
   }

//=====================================================================
//=====================================================================
char putchar( char ThisChar )
   {
   /* wait if buffer is full... */
   while( ( (TxInChar+1) % BUF_SIZE) == TxOutChar );
   
   /* add this character to the transmit buffer */
   ES = 0;
   TxBuffer[ TxInChar++ ] = ThisChar;
   TxInChar %= BUF_SIZE;

   /* if transmitter is disabled enable it */
   if( !TxEnabled )          
      {
      TxEnabled = 1;
      TI = 1;
      }
   
   ES = 1;

   /* return character as default value */
   return( ThisChar );
   }

//=====================================================================
//=====================================================================
void main()
   {
   char fname='a';
   char lname='s';

   // set port pin to 1              
   sw = 1;

   // initialize serial interface...
   SER_init();
   SER_setbaud( 19200 );

   // send 'a' or 's', depending on state of sw...
   if( sw == 0 )
      {
      putchar( fname );
      }
   else
      {
      putchar( lname );
      }
   
   // loop forever...
   while(1);
   }


But, even in this example, you will notice I put the "loop forever" instruction at the end of main(), which is exactly what I suggested you do...

You will have to adjust the value of MICRO_FREQUENCY to fit your target hardware system, and you will have to adjust the value sent to SER_setbaud(...) to match your serial terminal of course.

I hope this will make you happy...
Tags serial programming using 80518051 interrupt driven UART8051 uart library8051 serial port uart
Tue Jan 12 2010, 04:53 AM
#6
thanks for the reply. i just realised it would have been much simpler if i had used an external interrupt and a delay which makes it print only the char i want only once!!
and now.. again another question.. how do i make the serial output come to the new line; ie like printf("\n")???
Tue Jan 12 2010, 01:31 PM
#7
jus put 0x0d and 0x0a to sbuf register to print the serial output in next line
0x0d-->carriage return
0x0a-->line feed
Tue Jan 12 2010, 05:14 PM
#8
in other words as hplaneti said
'\r' <- carriage return
'\n' <- line feed

[Topic moved to 8051 Discussion forum]
Wed Jan 13 2010, 03:26 AM
#9
ok new problem. im not able to use more than 1 printf in the program it says multiple reference warning
Wed Jan 13 2010, 08:22 PM
#10
well are you trying to use printf in interrupt?

Get Social

Information

Powered by e107 Forum System

Conversation

Wed May 12 2021, 08:20 AM
Edgardo
If some one wants to be updated with most recent technologies afterward he must be visit this web page and be up to date every day. annual credit report https://creditscoresetf.com
Wed May 12 2021, 06:11 AM
Laurinda
Great website. A lot of useful information here. I'm sending it to some pals ans also sharing in delicious. And of course, thanks on your effort! annual credit report https://creditscorecheckn.com
Wed May 12 2021, 05:37 AM
Joann
Does your site have a contact page? I'm having problems locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time. get credit report https://freecreditreportww.com
Wed May 12 2021, 04:47 AM
Jenna
This paragraph will help the internet people for creating new weblog or even a weblog from start to end. Credit Report Online https://creditscoresws.com
Wed May 12 2021, 03:37 AM
Tanja
This article is actually a nice one it assists new the web people, who are wishing in favor of blogging. credit report https://creditreportsww.com

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 101, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123