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

Sun Oct 13 2019, 02:02 AM
Aline Hutchinson
Hey there,
ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum
Tue Jul 23 2019, 06:13 PM
BUSTER123
hello! newbie here. i am kind of a guy who hates using ready - made existing libraries, so this helped a lot as i was trying to control and write to a character lcd display. however, i can't figure out how to set the cursor position. for example, i want to set it to the 5th column in the first row, so position 5. so according to the table, i am sending 10000101 on the 8 data lines of the lcd (register select pulled low). the cursor does not move there however. and i cant move the cursor left or right too. is it something wrong that i am doing? thank you.
ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html

Downloads

Comments

ilylily07
Tue Oct 15 2019, 04:32 PM
hientran123
Tue Oct 15 2019, 02:27 AM
Sat Oct 05 2019, 02:38 PM
zexalo
Thu Oct 03 2019, 09:53 AM
thanhle12345
Thu Oct 03 2019, 07:37 AM
ramon_vsc
Fri Sep 20 2019, 04:53 PM
youssoufba
Thu Sep 19 2019, 11:57 AM
Taiphan000
Tue Sep 17 2019, 04:21 AM

Online

Guests: 41, Members: 0 ...

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

Members: 38097
Newest member: ilylily07
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Oct 22 2019, 05:18 AM
wimac Avatar
Posted By wimac
Mon Oct 21 2019, 09:18 AM
I’ve been following this thread and looking at the...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:05 PM
sir, 3rd and 4th dot matrix display flickering, ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 18 2019, 09:18 PM
Hopefully you understand some of the program.Which...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Thu Oct 17 2019, 09:38 PM