Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/22/09 08:53
Read: times


 
#168455 - Serial port control of a microcontroller
I did manage to trouble shoot my hardware with the help of expert advise from this forum in the following thread.
http://www.8052.com/forum/read/166422

Now I have run into some software issues..

I am trying to close a series of relays sequentialy and then read the volatge(from a source meter) across the closed circuit into the PC in a LABVIEW pgm.

Even though the microcontroller(AT89S52) can send different signals sequentially to close different relays the data read into the PC may not be synchronized.

So I also sent the input data to the microcontroller from PC (from the same labview pgm)thro a serial port using three wire Rs232 cable (9600/8bit/no control)to send input to microcontroller which will then send ouput signals to close particular relays.

NExt i wait for few msecs(60-140) and then read the voltage from the sourcemeter into the PC(thro a GPIB cable).

This keeps continuing sequentially.


THe labview pgm is structured as follows:

Send input signal to microcontroller----->delay time---->read from sourcemeter.

these three actions are looped continously.

Though the whole process works fine the time taken at the present settings is abt 200 msec minimum even if i set a very low delay time.

I am wondering if it is because of 9600 baud rate setting of the microcontroller.
Will changing it to 19200 by using SMOD=1 increase the speed of communication?

The input i send is a 4 digit string.assuming each digit is one byte.the whole string plus start and stop bits together make only 34 bits which should take only 4 msec at 9600 bit/sec rating.

1.So will changing to higher baud rate serve any purpose?

2.Also is only 9600 or 14400 baud rates possible as only these rates have integral value of TH1?

3.Also if I use 14400 baud rate based TH1 and set Smod =1.what baud rate should the PC port have as 28800 is not a std rate.?

Below is the code inside my microcontoller to recieve input data in serial mode 1 using a 8 bit timer 1 in auto reload mode 2 and SMod set to 0.

So if I need to change the pgm to higher baud rate I just need to change SMOD to 1 and change the value of baud rate and can leave the rest of code untouched am i right ?









..#include<reg52.h>
#include<stdio.h>
unsigned char In_read_index_G;      // Data in buffer that has been read 
unsigned char In_waiting_index_G;   // Data in buffer not yet read
static void PC_LINK_IO_Send_Char(const char);
#define RECV_BUFFER_LENGTH 4
#define OSC_FREQ (11059200UL)
#define OSC_PER_INST (12)
static unsigned char Recv_buffer[RECV_BUFFER_LENGTH];

/*------------------------------------------------------------------*-

  PC_LINK_IO_Update()

  Checks for character in the UART (hardware) receive buffer
 
-*------------------------------------------------------------------*/
void PC_LINK_IO_Update(void)  
   {
    // Only dealing with received bytes here
   // -> Just check the RI flag
   if (RI == 1)
      {
      // Flag only set when a valid stop bit is received, 
      // -> data ready to be read into the received buffer

      // Want to read into index 0, if old data has been read
      // (simple ~circular buffer)
      if (In_waiting_index_G == In_read_index_G)
         { 
         In_waiting_index_G = 0;
         In_read_index_G = 0;
         } 
      
      // Read the data from UART buffer   
      Recv_buffer[In_waiting_index_G] = SBUF;

      if (In_waiting_index_G < RECV_BUFFER_LENGTH)
         {
         // Increment without overflowing buffer
         In_waiting_index_G++;
         }
    
      RI = 0;  // Clear RT flag
      }
   }

void PC_LINK_IO_Init_T1(const unsigned int BAUD_RATE)
   {
   PCON &= 0x7F;   // Set SMOD bit to 0 (don't double baud rates)

   //  Receiver enabled.
   //  8-bit data, 1 start bit, 1 stop bit, 
   //  Variable baud rate (asynchronous)
   //  Receive flag will only be set if a valid stop bit is received
   //  Set TI (transmit buffer is empty)
   SCON = 0x72;

   TMOD |= 0x20;   // T1 in mode 2, 8-bit auto reload

   TH1 = (256 - (unsigned char)((((unsigned long)OSC_FREQ / 100) * 3125) 
            / ((unsigned long) BAUD_RATE * OSC_PER_INST * 1000)));

   TL1 = TH1;  
   TR1 = 1;  // Run the timer
   TI = 1;   // Send first character (dummy)

   // Set up the buffers for reading and writing
   In_read_index_G = 0;
   In_waiting_index_G = 0;
   // Interrupt *NOT* enabled
   ES = 0;
   }

main()

{
unsigned char Ch;
unsigned int i,n1,n2,n;
PC_LINK_IO_Init_T1(9600);
i=0;
n1=0;
n2=0;
n=0;
while(1)
{
   PC_LINK_IO_Update();
if(In_waiting_index_G==4)
{
while(i<4)
{
if(i<2)
n1=n1*10+(unsigned int)(Recv_buffer[i]-'0');
if(i>=2)
n2=n2*10+(unsigned int)(Recv_buffer[i]-'0');
i++;
}
n=n1*(2*2*2*2)+n2;

P2=n;

i=0;
/*printf("%d",n);*/
In_waiting_index_G=0;
i=0;
n1=0;
n2=0;
n=0;
}
}//end of outer while
  

 



List of 4 messages in thread
TopicAuthorDate
Serial port control of a microcontroller            01/01/70 00:00      
   GPIB?            01/01/70 00:00      
      GPIB vs serial            01/01/70 00:00      
         Many ways to lose time besides baudrate            01/01/70 00:00      

Back to Subject List