Projects Research & Design Hub

MCU-Based IC Links USB to Legacy PC I/O

Versatile Solution

In PCs, serial interfaces have now been replaced almost completely by USB. But many of those interfaces are still used in control and monitoring embedded systems. In this project article, Hossam describes his ATSTAMP design. ATSTAMP is an MCS-51 (8051)-compatible microcontroller chip that can be connected to the USB port of any PC via any USB-to-serial bridge currently available on the market.

To interface a PC to the external world for control and monitoring applications, the computer requires an external I/O interface. Legacy ports such as parallel ports, serial ports, PS/2 or game ports were found in early PCs. Currently, USB replaces all those legacy ports and you rarely find them in modern PCs.

Compared to USB protocol, dealing with the serial port protocol is straightforward with regard to hardware and software. With that in mind, it’s no wonder that considerable numbers of important communication modules—such as Wi-Fi, Bluetooth, GSM and GPS—have serial UART interfaces. Because it’s such a widely used standard, the RS-232 serial port is still used in many types of industrial, communication and medical equipment.

Several companies manufacture USB-to-serial bridge ICs, which can be connected to USB ports and give the PC what is called a “virtual COM port” (or VCP). These companies include Silicon Labs (CP210x), FTDI Chip (FT232RL), Jiangsu Yuheng (CH34x) and Prolific Technology (PL2303). Each type has characteristic specs and features. Drivers for different operating systems are available from the manufacturers’ websites.

Even though some microcontrollers (MCUs) do have built-in USB peripherals, using such MCUs may complicate the firmware and software. They also may require customized drivers to be developed. In this project, a Silicon Labs CP2102-based module is used as the USB-to-serial bridge. The module provides TX, RX, DTR, VCC and GND pins for connecting ATSTAMP with a PC’s USB port via a micro-USB socket cable. The module I used is available from amazom.com—search “Wingoneer CP2102 Micro USB to TTL.” The module is easy to plug into a breadboard without the need for any flying wires. Alternately, any similar module can be used, provided that it has a DTR pin for downloading firmware into the MCU, as will be discussed later in this article.

ATSTAMP has been used for several years in many educational and industrial projects. It has been used in microprocessor courses in interfacing and understanding the operation of peripheral ICs, such as 8255, 8259, 8253, 8251 and 6821. It was also used in embedded systems courses in interfacing with ICs working with different protocols, such as SPI and I2C. Operation of LCDs, keypads, ADCs and DACs has been also explored. Several types of EPROM and MCU programming kits also used ATSTAMP as the main processor.

ATSTAMP is well suited as a tool for debugging your design and verifying its functionality. By adding transistors, current drivers, relays, triacs and protection circuits, ATSTAMP can be used in a variety of industrial control and monitoring applications. An inexpensive industrial PC connected to ATSTAMP can replace expensive HMI (Human Machine Interface) screens and Programmable Logic Controllers (PLC). Any program written using the ATSTAMP-provided functions can be easily converted into embedded C language for any MCS-51-based MCU, which facilitates developing stand-alone embedded applications without the need of a PC connection.

— ADVERTISMENT—

Advertise Here

HARDWARE DESIGN
The ATSTAMP’s schematic is shown in Figure 1. ATSTAMP provides four ports—P0, P1, P2 and P3—that each can be programmed as input or output. All port pins are individually programmable for input or output operation (except P3.0 and P3.1). In the schematic, an LED and a push button are connected to P1.0 and P2.0, respectively, to test the functions, as will be shown later. A prototype has been wired on a breadboard as shown in Figure 2. The ports are organized and bus oriented, so it’s easy to use 8-way flat cable to connect the breadboard to your interface. It should be noted that the hardware is compatible with the following MCUs from Microchip Technology (formerly Atmel): AT89S51, AT89S52, AT89S53, AT89S8252 and AT89S8253. Because of the versatility of ATSTAMP—especially in the industrial field—a development board has been designed and fabricated as shown in Figure 3. The board also has been used as a general board for 8051-based faculty laboratory experiments. The schematic and design files for this general board are freely available from the author upon request.

FIGURE 1 – Shown here is a schematic of the ATSTAMP circuitry.
FIGURE 2 – ATSTAMP breadboard prototype.
FIGURE 3 – This the ATSTAMP finished development board.

ATSTAMP logic levels are 0 V and 5 V. Low current devices such as LED indicators can be controlled directly by making the output port pin sinks current (active low). Input devices such as push buttons can be connected to the port pin with a pull-up resistor. Figure 1 shows the connection of an LED and a push button to two I/O pins of ATSTAMP.

For controlling AC-DC loads working on higher voltages (currents), a relay or triac can be used, as shown in Figure 4 and Figure 5. Note that clearing the IO pin activates the relay or the triac. If the input comes from an industrial process that operates on 24 V, an optocoupler may be used to interface with the ATSTAMP input pin (Figure 6).

FIGURE 4 – Controlling DC-AC
FIGURE 5 – Controlling AC
FIGURE 6 – Reading inputs from 24 V

DOWNLOADING THE FIRMWARE
ATSTAMP firmware is provided as a binary hex file (astamp.hex) on the Circuit Cellar article code & files download page. The firmware can be programmed into any 8051-based MCU from any manufacturer, including Microchip, Silicon Labs, STC and Maxim Integrated. Each company provides hardware and software programming tools for its MCUs. Personally, I prefer the Microchip (formerly Atmel) AT89S52 MCU because it is widely available, relatively inexpensive and comes in a DIP package.

The problem with Microchip 8051-based MCUs is that the latest available programmer from the manufacturer is based on the legacy Centronics Parallel Port. It is called AT89ISP. Some variants have a built-in bootloader (such as AT89C51ED2) and can be programmed via a serial port using Microchip’s FLIP software. The developer should search for a USB clone programmer or a universal programmer for downloading the firmware into AT89S5x MCUs.

I solved this problem with an unconventional technique by using the same USB-to-UART module for downloading the firmware into the AT89S52 MCU. While programming the firmware, connect the TX and DTR pins of the module to MOSI (pin 6) and SCK (pin 8) pins of the MCU, respectively, and run the provided program, ATSTAMP_Downloader.EXE. I wrote this program to bit bang the ISP pins of the MCU. The binary hex file is embedded into the .exe file.

Alternately, you may program the controller using a ready programmer, if you have one available. The software will ask the user to press the RST button while programming and release it when downloading is completed. A video named atstamp_download.mp4 is provided on the Circuit Cellar article code & files download page. The video shows how the firmware is downloaded into the AT89S52 MCU. Using the USB-to-TTL module for programming the firmware and for communication with the PC enables you start your first ATSTAMP applications in less than an hour.

LOW LEVEL ASCII COMMANDS
The firmware of ATSTAMP is written in MCS-51 assembly language. ASCII control characters (commands) are sent to ATSTAMP with baud rate 115200, 7 data bits, no parity, one stop bit and no hardware handshake. It should be noted that each time a command is sent to ATSTAMP acting upon a certain I/O pin, the previous setting or value of that pin is overridden. ASCII commands can be sent directly from terminal emulator software, or from any device with a UART interface. Commands are made of one, two or three characters to perform different input and output port operations.

To keep the communication speed between the host PC and ATSTAMP as high as possible, no acknowledgement is returned to the host from ATSTAMP. Regarding the timing, setting or clearing a bit in a port may take around 0.5 ms—depending on the given crystal and baud rate. The ASCII commands with their arguments and return values are listed in Table 1.

— ADVERTISMENT—

Advertise Here

TABLE 1 – ASCII commands for ATSTAMP

Note that P3.0 and P3.1 are used for serial communication with the PC, so don’t use them in any of the library functions. In fact, any command involving these two pins won’t be recognized by the ATSTAMP firmware. If you write values to these two bits, commands will be discarded, and if you read them, ATSTAMP always will return a “1”. Those functions can be converted directly into embedded MCS-51 assembly language or C language.

After connecting ATSTAMP to the PC’s USB port via the CP2102 module, check the virtual COM port number associated with the module (from Control Panel > Device Manager). You will find it under Ports (COM & LPT)—assume it is COM1. Use a Terminal Emulator program such as “SerialPort Terminal,” which is very efficient open-source software. Open the COM port with the settings described earlier. Now, if you send the ping command “T“, ATSTAMP will reply by “HO” meaning it’s alive. To switch on the LED, clear P1.0 by sending “n00”. To switch it off, set the same bit by sending “N00”.

To set P1 to “FF”, send “XFF”, and to clear it to “00”, send “X00”. To check the status of the push button connected to P2.0, send “G00”. You should get “01”, indicating the button is released. Now press the button and issue the command again, and you will receive “00”. Note that to program a pin as input, you should set this bit first. Also, to program a port as all inputs, you should send “FF” to it first. This is a common practice for MCS-51 MCUs. You can practice all the commands in Table 1 to verify the proper operation of ATSTAMP.

C LIBRARY FUNCTIONS
On one hand, terminal emulator programs provide a nice way to check the low-level ASCII commands accepted by ATSTAMP. It’s a good idea to get familiar with them so you can develop your own functions and customized applications. On the other hand, providing high-level software library functions can be used more efficiently and make you focus on the application. Currently, C++ Builder Starter Edition from Embarcadero Technologies is used for developing ATSTAMP PC-based programs. It is a fully featured free C++ IDE for creating native cross-platform applications.

Using C++ Builder software looks weird, and you may ask: Why aren’t I using the fancier Visual Studio C# environment? Truth is I’ve been working with Borland products for three decades. Although Borland was acquired by Inprise, CodeGear then finally by Embarcadero, C++ Builder is the best environment for me because of its vast speed and optimization. For years many embedded systems experts have known Borland C and C++ Builder software as the optimum choice. The starter edition of C++ Builder can be downloaded for free from the Embarcadero website. On the Circuit Cellar article code & files webpage, a video named embar_starter.mp4 is provided for demonstrating how to download and install C++ Builder Free Starter Edition. The current version is 10.2 Tokyo. The video also shows how to start your first C++ Builder Console application.

The ATSTAMP.LIB library provided contains the definition of the main functions used for communication between the PC and ATSTAMP controller. There are seven high-level functions, which are each described here:

1. OpenCom
bool OpenCom(unsigned int ComPortNumber, int BAUDRATE);

The OpenCom function accepts a COM port number (physical or virtual) and a baud rate (which is fixed at 115200). You may get the COM port number from the Windows Device Manager, as discussed previously. You may also get it automatically in your code by reading it from the registry. This function is responsible for opening the PC COM port and establishing reliable communication. The function returns true (1) if the communication is established, and returns false (0) if there is an error in communication.

When there are no problems in communication, your program should proceed normally. If you enter a non-existing COM port number in the OpenCom function, you will get the following message: Fatal Error: Cannot open COM port. Also, this function pings ATSTAMP to check if it is connected or not. If ATSTAMP is not connected or there is any problem with its hardware, you will get the message: ATSTAMP is not detected, Check the hardware and try again. If you are sure that hardware is OK, press the RST button and run the program again.

Hardware problems may result from any of the following:

• Either VCC or GND is not connected to the ATSTAMP circuit.
• The serial cable (or USB cable) is not connected to the PC or the ATSTAMP circuit.
• The value of the quartz crystal is different than 22.11884 MHz
• Pin 31 of ATSTAMP is not connected to VCC.
• The crystal or the capacitors are not connected properly or have different values.
• TXD and RXD pins of ATSTAMP are swapped when connecting the circuit.
In any of the above cases, try to solve the problem and run your program again.

2. CloseCom
void CloseCom();

This function is responsible for closing the previously opened COM port, so that it can be released and used in any other application, such as the serialTerminal. This function should be invoked at the end of your C program. If you forget to include it in your program, the COM port will close automatically when you close the application.

For the following five input/output control functions, the portnum argument can be P0, P1, P2 or P3, and the bit argument can be 0 to 7. Remember that P3.0 and P3.1 cannot be used.

3. setb
void setb(unsigned int portnum, unsigned char bit);

— ADVERTISMENT—

Advertise Here

This function sets any bit in any of the four ports. For example, if you want to set bit 5 of port 2, then you call the function setb(P2, 5). setb is also used when you want to configure a bit in port as input. For example, if you want to read the logic level on pin P0.4, then you should first call setb(P0, 4) to make this pin input, then you can read its value using GetBit function. The equivalent with MCS-51 would be setb P0.3 (in assembly language) or P0^3 = 1 (in C language).

4. clr
void clr(unsigned int portnum, unsigned char bit);

This function clears any bit in any of the four ports. For example, if you want to clear bit 2 of port 1, then you call the function clr(P1, 2). The equivalent with MCS-51 would be clr P1.2 (in assembly language) or P1^2 = 0 (in C language).

5. GetBit
bool GetBit(unsigned int portnum, unsigned char bit);

This function reads any bit in any of the four ports. For example, if you want to read the logic level on bit 3 of port 0, then you call the function x = GetBit(P0, 3). The equivalent with MCS-51 would be move c, P0.3 (in assembly language), where c is the carry flag or x = P0^3 (in C language).

6. SerialOutput
void SerialOutput(unsigned int portnum, unsigned char data);

This function outputs the value given by the data argument as logic levels to any of the four ports. For example, if you want to output the hexadecimal value F3 to port 2, then you call the function SerialOutput(P2, 0xF3). The equivalent with MCS-51 would be move A, #0F3H (in assembly language) or P2 = 0xF3 (in C language).

7. SerialInput
unsigned int SerialInput(unsigned int portnum);

This function reads the logic levels on any of the four ports. For example, if you want to read the levels on port 1, then you call the function x = SerialInput(P1). The equivalent with MCS-51 would be mov A, P1 (in assembly language) or x = P1 (in C language). As discussed earlier, to read a port, you should set all its bits first, so use SerialOutput (P1, 0xFF) before invoking the SerialInput function to read P1.

SAMPLE ATSTAMP APPLICATION
To start your first ATSTAMP interface application, first prepare a project folder (for example, name it atstamp_io). Copy the provided files, atsamp.h and atstamp.lib, to the folder project. Start C++ Builder and select Project > Add New Project > Console Application. Select File > Save Project As, save the project in the folder under same name (atstamp_io.cbproj). Select Project > Add to Project, point to atstamp.lib file. Copy and paste the program listed in Listing 1 to the source file window. You can also get it as the Listing 1.txt file from the Circuit Cellar article code & files download page. Select Project >Build atstamp_io then Run.

// Checks status of push button connected to P2.0 (active low)
// When pushed, LED connected to P1.0 flashes five times
#include “ATSTAMP.h”
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main()
{
if(!OpenCom(1, 115200))
{ exit(0); }
while(1)
{
if(!GetBit(P2,0))
for(int i =0;i<5;i++)
{
clr(P1,0);
Sleep(300);
setb(P1,0);
Sleep(300);
}
}
return 0;
}

LISTING 1 – Shown here is sample C application program for the ATSTAMP.

The program infinitely checks the status of the push button connected to pin P2.0, and when it goes low, it flashes the LED connected to pin P1.0 five times. Note the operation is fully controlled by the commands sent to the ATSTAMP by the running program. The atstamp_io project is provided as a template that can be tuned to your application.

A video named atstamp_app.mp4 is provided to illustrate creating the sample software application, and shows how it runs on the prototype circuit. Download it from the Circuit Cellar article code & files download page. Although the given sample application is console-based, with little a practice you can build your own GUI Windows-based applications.

CONCLUSION
ATSTAMP is a great tool for embedded system development. It may save you lot of programming and firmware uploading time. Actually, a lot of applications and products can be based on ATSTAMP. It has been used for more than a decade in various applications and proved to be useful and reliable. As an example, consider the development of a full-featured stand-alone digital IC tester for TTL 74xxx and CMOS 40xxx families. Without ATSTAMP you may need thousands of upload and erase cycles of your target MCU until you get it work satisfactorily. In contrast, using ATSTAMP requires no MCU programming in the development phase. Just write your test patterns on Embarcadero C++ program, and run it on the fly to test applying the patterns to the target IC and getting its response. Once you settle on the logic, you may convert your Embarcadero C++ code to the C code of your target MCU embedded compiler and upload it once. You’re limited only by your imagination.

Finally, it should be noted that during reading inputs of ATSTAMP, the PC control program constantly polls the ATSTAMP to check level changes. This technique wastes CPU time and generates a lot of traffic over the serial link. It would be more efficient if the ATSTAMP firmware detected input hardware changes and automatically sent a notification to the PC software, which would trigger a call-back function. If this were the case, however, ATSTAMP firmware and the PC software library implementation would be more complex.

Using an MCU with enhanced architecture—such as AVR or Arm—would open the possibility of including functions other than digital I/O. For instance, reading analog values and generating analog voltages would be advantageous.

For detailed article references and additional resources go to:
www.circuitcellar.com/article-materials

RESOURCES
Embarcadero Technologies | www.embarcadero.com
FTDI Chip | www.ftdichip.com
Jiangsu Yuheng | www.wch.cn
Microchip Technology | www.microchip.com
Prolific Technology | www.prolific.com.tw
Silicon Labs | www.silabs.com
STC | www.stcmcu.com

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • MARCH 2019 #344 – Get a PDF of the issue


Don't miss out on upcoming issues of Circuit Cellar. Subscribe today!

 
 
Note: We’ve made the October 2017 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.


Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Become a Sponsor

Dr. Hossam Abdelbaki is a faculty member at Department of Electrical
Engineering, Faculty of Engineering, , Alexandria, Egypt. He has taught embedded and reconfigurable systems since 1995. He served as head of research and development for the Subsea Petroleum Services company, where he supervised building and delivery of three FPGA based Dynamic Positioning (DP) offshore marine vessels. Dr. Hossam also is the CTO of Technoenergy Company, which specializes in designing LED Lighting and energy-saving products. He can be reached by email at: [email protected] or [email protected]