Keil Logo

µVISION DEBUGGER: Automated Serial Input Script


Information in this article applies to:

  • C51 Version 6.21 and later
  • µVision Version 2.20a and later

QUESTION

I have an 8051 application that processes a lot of serial data. I'm simulating the program using the µVision Debugger, but I have to type in a lot of data in the serial window. Is there a way to automate this?

ANSWER

Yes. You can create a simple script that injects your data into the serial input buffer. To demonstrate this, first you will need the following simple test program:

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#include <reg52.h>
#include <stdio.h>

/*------------------------------------------------
The following string is the stuff we're gonna
send into the serial port.
------------------------------------------------*/
unsigned char xdata serial_input_buffer [] =
  "This is a test to see if this data gets "
  "injected into the serial port.\r\n"
  "Have fun.\r\n"
  "\r\n\r\n";

void main (void)
{
/*------------------------------------------------
Setup the serial port for 2400 baud at 12MHz.
------------------------------------------------*/
SCON  = 0x50;  /* SCON: mode 1, 8-bit UART, enable rcvr      */
TMOD |= 0x20;  /* TMOD: timer 1, mode 2, 8-bit reload        */
TH1   = 0xF3;  /* TH1:  reload value for 2400 baud @ 12MHz   */
TR1   = 1;     /* TR1:  timer 1 run                          */
TI    = 1;     /* TI:   set TI to send first char of UART    */

/*------------------------------------------------
Get a key from the serial port and send it
right back out the serial port.
------------------------------------------------*/
while (1)
  {
  putchar (_getkey ());
  }
}

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

Note that this test program simply sets up the serial port for 2400 baud and echos characters it receives.

The data that you will send into the simulated serial port is stored in the serial_input_buffer string declared at the top of this file. This is a convenient way to get this data into memory. However, you may want to create Intel HEX files (at fixed addresses) that contain your test data. You may load the HEX files and then dump their contents into the serial window using a method similar to this one.

Second, you must create a simple debugger script that reads the input data and inputs it into the simulated serial port. The following script does that:

define int seri_index;

signal void seri_start (float baudrate) {
  seri_index = 0;

  for (seri_index = 0; serial_input_buffer[seri_index] != '\0'; seri_index++) {
    SIN = serial_input_buffer[seri_index];
// Wait for a little longer than 1 character time between each character
    swatch (1.0 / ((baudrate/10.0) + 20.0));
  }
}

define button "Send Data" , "seri_start(1200)"

Additionally, a button is created in the toolbox to start sending in the serial data.

There are several things to note about this script.

  1. It can actually send data faster than the 8051's serial port can receive it. So, if the test program runs at 1200 baud and the script sends 200 characters per second, your program will not receive all the serial data.
  2. This script assumes an ANSIIZ (null-terminated) string. You may of course modify it to send a fixed number of bytes.
  3. The serial_input_buffer is a variable in your program. If you want to use a fixed memory address (so you can load in Intel HEX data) you may use the _RBYTE function to read bytes from a specified address.

SEE ALSO

Last Reviewed: Friday, December 11, 2020


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.