ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING PIC controller interface with a computer. Encyclopedia of radio electronics and electrical engineering Encyclopedia of radio electronics and electrical engineering / Microcontrollers 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. 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. 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. 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. 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. 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. 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
Author: S.Kuleshov, Kurgan See other articles Section Microcontrollers. Read and write useful comments on this article. Latest news of science and technology, new electronics: Machine for thinning flowers in gardens
02.05.2024 Advanced Infrared Microscope
02.05.2024 Air trap for insects
01.05.2024
Other interesting news: ▪ Budget Chinese electric cars ▪ 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 Transmitter on MC2833. Encyclopedia of radio electronics and electrical engineering
Leave your comment on this article: All languages of this page Home page | Library | Articles | Website map | Site Reviews www.diagram.com.ua |