Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING
Free library / Schemes of radio-electronic and electrical devices

PIC controller interface with a computer. Encyclopedia of radio electronics and electrical engineering

Free technical library

Encyclopedia of radio electronics and electrical engineering / Microcontrollers

Comments on the article Comments on the article

When developing a device on a microcontroller (MC), the problem often arises of its connection with a computer for information exchange. In most cases, bidirectional mode is required at a relatively low exchange rate. In the best case, the MK can have a serial interface, but most often it has to be chosen from among cheap ones that are not equipped with such an interface. For example, Microchip's PIC16F84A MK, which is very popular lately, does not have such an interface. The article considers a variant of the software implementation of the serial interface both from the side of the MC and from the side of the computer.

To communicate with the device on the MK, you can use the parallel (LPT) or serial (COM) port of the computer. The first one is easier to work with - it can use a relatively larger number of input and output signals, the levels of which are compatible with TTL. The disadvantage of this port is that if under DOS or Linux simple I / O operations are enough to use it, then for correct operation under Windows, strict adherence to the data transfer protocol is required, which is not effective when working with MK. It is also possible to directly control individual lines of the LPT port, but this requires the installation of a special driver. The "disadvantage" of the LPT port can be considered that in most computers it is only one and, as a rule, is occupied by the printer.

The main advantages of the COM port are that the standard Windows programming interface (API) allows you to directly control some output lines and control the input ones, and also has the function of waiting for some event associated with the COM port. Its advantage is that the RS-232 standard, according to which COM ports are made, allows connecting and disconnecting cables during device operation (hot plug). In addition, almost always a computer has a free COM port. The disadvantage of the port is that the signal level is different from TTL, in which the low logic level corresponds to a voltage of -12, and the high logic level corresponds to +12 V.

The implementation of the standard RS-232 interface would require the MC to strictly observe the time intervals between the output signals. In a real situation, the microcontroller's quartz resonator may not correspond to the data transmission frequency, and the MC itself is usually busy with something more important than the formation of accurate time intervals. As a result, it turns out to be easier to programmatically implement a serial synchronous exchange option, when each data bit is confirmed by a synchronization pulse.

The schematic diagram of the proposed interface is shown in fig. one.

PIC controller interface with computer

Resistive dividers R232R1 and R4R2 are used to convert RS-5 levels to TTL. Diodes VD1 and VD2 are necessary in order not to pass a negative voltage corresponding to a logical zero. The output TTL signal of the MK does not need to be converted and can be fed directly to the input lines of the COM port. Resistor R3 limits the output current of the MK in case of a possible accidental short circuit.

As you can see from the diagram, four wires are required to communicate with the computer. The computer initiates the data exchange by issuing clock pulses on the DTR line, setting the transmitted data on the RTS line and receiving received data on the CTS line. The computer and MK can change the data only when the logical level of the synchronization signal is low. This implementation of the interface allows you to implement a duplex mode of data transmission.

The XS1 pin numbers in the diagram are for a DB-25F socket using a standard modem cable. See Table 1 for pin numbers for other connectors and when using a null modem cable. XNUMX.

PIC controller interface with computer

The frequency of the synchronizing pulses must be chosen so that the MC is guaranteed to have time to process data from the computer, responding to each synchronizing pulse. Information bits are transmitted sequentially. At the end of the transmission of bits of one byte, the transmission of bits of the next byte follows, with the most significant information bit being transmitted first. To bring the interface to its original state (setting the number of the transmitted byte to 0), the computer must log. 1 on the clock line to change the state of the data line. The MCU outputs a new bit of data on the CTS line on the fall of the positive polarity pulses at the DTR clock input, and reads data from the RTS line on the rising edge of the positive polarity pulses. The exchange can be interrupted at any time by stopping the supply of synchronization pulses. The timing diagram of data exchange is shown in fig. 2.

PIC controller interface with computer

When implementing the interface, it is recommended to pass control values ​​in some bytes to check the correctness of the transmitted data.

The source code of the procedure for MK PIC16F84A [1] in C language, which implements the proposed interface, is given in Table. 2. The link() procedure call is located in the main program loop and is constantly called during the operation of the MK in order to control the state of the interface. All variables used by the procedure are declared as global. On each call, it reads the states of the input lines of the interface (RB6 and RB7) and compares them with their states on the previous call. Under certain conditions (synchronization fall, synchronization edge, interface reset), actions are performed according to the logic of the interface.

PIC controller interface with computer

The source code of the procedure for a computer in Pascal (Delphi) is given in Table. 3. Here, the link procedure is called once to carry out the act of information exchange with the MK. Before calling it, it is necessary to fill the passed buffer obuf. At the end of the procedure, the received data will be in the ibuf array. The procedure opens the specified COM port of the computer and, using the Windows API functions [2], controls the state of the output lines and polls the input lines. After the exchange of information is completed, the port is closed.

PIC controller interface with computer

In the link procedure, time delays are implemented using the sleep() function. Their values ​​are calculated or selected experimentally by the absence of bit loss during data exchange between the MC and the computer. The example shows the delays for the exchange with a PIC controller with a quartz resonator at a frequency of 4 MHz, which, in addition, performs other useful work. If the exchange procedure takes too long, it can be moved to a separate thread of the operating system execution so that it runs in parallel with the main program [2].

If the exchange of information requires separate reading and writing, it is possible to spread the arrays of transmitted and received data to different addresses, as shown in Fig. 2.

In MK, it is convenient to build the formation of transmitted data and the use of received data in the form of upload() and download() procedures, called before transmission and when receiving the next byte, respectively. The first of them must return the value of the transmitted byte by its number in the transmitted information packet, the second one receives the value of the received byte and its number in the packet and must use these values ​​to change the MK registers, write to EEPROM, etc. The implementation of these procedures for processing an information packet of size 4 bytes (Table 4) is shown in Table. 5.

PIC controller interface with computer

An example of a program for MK is given for the C2C compiler [3]. The procedure for a computer can be used in a program written in Borland Delphi 3 and above.

Literature

  1. .
  2. Microsoft Developer Network, Technical Articles. - .
  3. .

Author: S.Kuleshov, Kurgan

See other articles Section Microcontrollers.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

Machine for thinning flowers in gardens 02.05.2024

In modern agriculture, technological progress is developing aimed at increasing the efficiency of plant care processes. The innovative Florix flower thinning machine was presented in Italy, designed to optimize the harvesting stage. This tool is equipped with mobile arms, allowing it to be easily adapted to the needs of the garden. The operator can adjust the speed of the thin wires by controlling them from the tractor cab using a joystick. This approach significantly increases the efficiency of the flower thinning process, providing the possibility of individual adjustment to the specific conditions of the garden, as well as the variety and type of fruit grown in it. After testing the Florix machine for two years on various types of fruit, the results were very encouraging. Farmers such as Filiberto Montanari, who has used a Florix machine for several years, have reported a significant reduction in the time and labor required to thin flowers. ... >>

Advanced Infrared Microscope 02.05.2024

Microscopes play an important role in scientific research, allowing scientists to delve into structures and processes invisible to the eye. However, various microscopy methods have their limitations, and among them was the limitation of resolution when using the infrared range. But the latest achievements of Japanese researchers from the University of Tokyo open up new prospects for studying the microworld. Scientists from the University of Tokyo have unveiled a new microscope that will revolutionize the capabilities of infrared microscopy. This advanced instrument allows you to see the internal structures of living bacteria with amazing clarity on the nanometer scale. Typically, mid-infrared microscopes are limited by low resolution, but the latest development from Japanese researchers overcomes these limitations. According to scientists, the developed microscope allows creating images with a resolution of up to 120 nanometers, which is 30 times higher than the resolution of traditional microscopes. ... >>

Air trap for insects 01.05.2024

Agriculture is one of the key sectors of the economy, and pest control is an integral part of this process. A team of scientists from the Indian Council of Agricultural Research-Central Potato Research Institute (ICAR-CPRI), Shimla, has come up with an innovative solution to this problem - a wind-powered insect air trap. This device addresses the shortcomings of traditional pest control methods by providing real-time insect population data. The trap is powered entirely by wind energy, making it an environmentally friendly solution that requires no power. Its unique design allows monitoring of both harmful and beneficial insects, providing a complete overview of the population in any agricultural area. “By assessing target pests at the right time, we can take necessary measures to control both pests and diseases,” says Kapil ... >>

Random news from the Archive

stem cell management 12.02.2020

A team of microbiologists from the University of Edinburgh has done a rather interesting experiment to turn off one important mechanism in stem cells - namely, to deactivate a regulatory molecule that controls the cell's immune defense impulse when it is threatened. The results of the study, being very positive, promise to soon become the basis on which even more effective and interesting experiments will be carried out.

Unlike any other cells in the body, stem cells do not have their own defense system and cannot use an immune impulse for this protection - on the other hand, when some viruses interact, they still send this impulse, which somewhat complicates the work with stem cells, because in this way it becomes difficult to modify them in the laboratory.

That is why Scottish specialists conducted research, according to which they discovered a special protein called MAVS, which in turn triggers an activation impulse of the miR-673 regulatory molecule. And this molecule, in turn, activates the immune defense of the stem cell, preventing it from being manipulated as scientists need. After conducting several sessions of experiments and observations on embryonic stem cells belonging to mice, scientists found that the removal of this regulatory molecule leads to a significant simplification of work with the cell itself.

In other words, by deactivating this molecule, specialists can more easily manipulate cells, thus changing their structure and chemical composition much faster and easier. And this, in turn, leads to much more efficient results when remaking a stem cell into something else. However, it remains to wait for new results.

Other interesting news:

▪ Budget Chinese electric cars

▪ happiness Formula

▪ The longest living creatures on Earth have been found

▪ Purification of water from uranium using magnetic bacteria

▪ iiyama ProLite XU2490HS-B1 and XU2590HS-B1 monitors

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the site Stories from the life of radio amateurs. Selection of articles

▪ article Everywhere is life. Popular expression

▪ article Why is the cloth on most billiard tables green? Detailed answer

▪ shark knot article. Tourist tips

▪ article Semi-automatic octane corrector. Encyclopedia of radio electronics and electrical engineering

▪ article Transmitter on MC2833. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024