Digital input and output circuit and implementation method thereof
By designing a digital input/output circuit, a combination of resistors, MOSFETs, capacitors, and controllers is used to achieve multi-functional switching on the same port, solving the problems of high hardware cost and low flexibility in traditional metering products, and improving the product's adaptability and signal transmission stability.
Patent Information
- Application Number
- CN202511391391.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-26
- Publication Date
- 2026-01-09
AI Technical Summary
In traditional electronic metering products, the separate setup of input and output interfaces results in high hardware costs and large space occupation. Furthermore, the fixed interface mode reduces the flexibility of the product and cannot meet diverse input and output needs.
Design a digital input/output circuit that enables input or output functions on the same port through software configuration. Utilize a combination of resistors, MOSFETs, capacitors, and a controller to support push-pull output, open-drain output, and input modes. Combine a serial-to-infrared interface to achieve wireless configuration and fault self-diagnosis.
It significantly reduces hardware costs, saves internal product space, improves product configurability and flexibility, adapts to diverse input/output requirements, and enhances circuit compatibility and signal transmission stability.
Smart Images

Figure CN121308745A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of metering, in particular to a digital input and output circuit and a method for implementing the same. BACKGROUND
[0002] In electronic metering products, an input interface is used to receive pulse signals generated by devices such as flow sensors, and an output interface is used to output product parameters such as cumulative flow and cumulative heat.
[0003] In traditional technology, the input interface and the output interface are usually independently arranged, which not only increases the hardware cost of the product, but also occupies a large amount of space. At the same time, due to the uncertainty of input and output requirements, such as the need to output multiple parameters or receive multiple input signals at the same time, the fixed interface mode reduces the flexibility of the product. SUMMARY
[0004] The present application provides a multiplexed input and output circuit and a method for implementing the same, which can realize input or output functions through software configuration of the same port, significantly improving the configurability of the product and reducing the hardware cost.
[0005] To achieve the above object, the present application provides the following technical scheme:
[0006] A digital input and output circuit, comprising resistors R1 and R2, a MOS transistor Q1, a diode D1, a capacitor C1, and a controller, wherein:
[0007] One end of the resistor R2 is connected to the gate of the MOS transistor Q1 and connected to the control pin of the controller to form an I / O out control end, and the other end is connected to the source of the MOS transistor Q1 and connected to the GND pin of the controller;
[0008] The negative electrode of the diode D1 is connected to the drain of the MOS transistor Q1, and the positive electrode is connected to the resistor R1, and the other end of the resistor R1 is connected to the power supply VCC of the product;
[0009] The common end of the resistor R1 and the diode D1 is connected to the pin of the controller to form an IO in port;
[0010] One end of the capacitor C1 is connected to GND, and the other end is connected to the negative electrode of the diode D1 and the drain of the MOS transistor Q1 to form an input and output port IO of the circuit.
[0011] The circuit structure realizes voltage division control of the gate voltage of the MOS transistor Q1 through the resistor R2, so that the MOS transistor can be reliably turned on or turned off according to the high or low level signal output by the controller.
[0012] The setting direction of the diode D1 ensures that the current can be effectively blocked to the VCC when the IO port inputs a high level, and provides a path for the current when the IO port outputs a low level.
[0013] The capacitor C1 is connected in parallel between the IO port and the GND, and functions as a filter and a voltage stabilizer, which can effectively suppress the influence of external interference signals on the circuit and improve the stability of signal transmission.
[0014] Further, the controller is a single-chip microcomputer, and the single-chip microcomputer is provided with a communication interface, which is a serial-to-infrared interface, for communication with the configuration software.
[0015] The single-chip microcomputer as the controller has the characteristics of small size, low power consumption and high integration, and can meet the requirements of the circuit for control logic.
[0016] The design of the serial-to-infrared interface enables the single-chip microcomputer to communicate wirelessly with the configuration software through infrared signals, thus getting rid of the limitation of physical interfaces and facilitating users to configure and manage the circuit in different environments.
[0017] The communication interface supports bidirectional data transmission, which can not only receive the commands sent by the configuration software, but also feed back the working status and related parameters of the circuit to the configuration software.
[0018] An implementation method of a digital input and output circuit based on the above digital input and output circuit, comprising:
[0019] A push-pull output mode, and an implementation process of the push-pull output mode is as follows: the I / O out pin is set to an output mode, the IO in is set to an input mode, when the I / O out pin outputs a high level, the IO port outputs a low level; when the I / O out outputs a low level, the IO port outputs a high level.
[0020] An open-drain output mode, and an implementation process of the open-drain output mode is as follows: the VCC is disconnected, an external pull-up resistor of a meter reading device is connected to the IO port, and the pull-up power supply depends on the meter reading device; when the I / O out pin outputs a high level, the IO port outputs a low level; when the I / O out outputs a low level, the IO port outputs a high level, and the level of the IO port is consistent with the external pull-up;
[0021] An input mode, and an implementation process of the input mode is as follows: the I / O out pin is set to an input / high resistance mode, and the IO in is set to an input mode; when the IO port inputs a high level, the IO in is a high level; when the IO port inputs a low level, the IO in is a low level; the IO port supports switch signal input, when the IO port is connected with the GND, the IO in is identified as a low level; when the IO port is disconnected with the GND, the IO in is identified as a high level.
[0022] Push-pull output mode: In this mode, the circuit can provide strong driving capability, suitable for directly driving some high-current loads. By controlling the level of the I / O out pin by the controller, the on-off characteristics of the MOS tube are used to realize the reverse output of the IO port level.
[0023] Open drain output mode: In this mode, the IO port level of the circuit is determined by the external pull-up power supply, which makes the circuit flexible to adapt to different voltage levels of external devices, enhancing the compatibility and versatility of the circuit.
[0024] Input mode: The circuit sets the I / O out pin to a high-impedance state to avoid interference with external input signals, while accurately reading the level state of the IO port through the IO in port to detect external switch signals or level signals.
[0025] Further, when the configuration port is in push-pull output mode, the controller performs the pin configuration action in push-pull output mode and updates the output of the required output parameters to the corresponding port in a timely manner.
[0026] After receiving the instruction to configure the push-pull output mode, the controller first initializes the working mode of the I / O out pin and the IO in pin, setting the I / O out to output mode and the IO in to input mode.
[0027] A timing update mechanism is established to read the parameter values to be output from the internal memory at preset time intervals and convert them into corresponding level signals for output through the I / O out pin.
[0028] Before each parameter update, the controller will verify the output parameters to ensure the accuracy and effectiveness of the output data, avoiding abnormal operation of the circuit caused by data errors.
[0029] Further, when the configuration port is in open drain output mode, the controller performs the pin configuration action in open drain output mode and updates the output of the required output parameters to the corresponding port in a timely manner.
[0030] After recognizing the open drain output mode configuration instruction, the controller will disconnect the connection with VCC and initialize the I / O out pin and the IO in pin in open drain mode.
[0031] Similar to the push-pull output mode, a timing update mechanism is used to convert parameter values into level signals for output, but in open drain mode, the output of high level relies on external pull-up resistance and power supply.
[0032] The controller monitors the state of the external pull-up power supply in real time to ensure that the stability is within a normal range. If the pull-up power supply is detected to be abnormal, the controller will take protective measures and send an alarm to the configuration software through the communication interface.
[0033] Further, the input voltage of the IO port depends on the selection of the diode D1 and the MOS tube Q1. The diode D1 is selected to have a preset reverse voltage value, and the MOS tube Q1 is selected to have a preset drain-source breakdown voltage. The selection of the two components enables the IO port to adapt to an input voltage range of 3V-5V.
[0034] The selection of the diode D1 needs to consider the reverse voltage and the forward voltage drop, which is less than 1V. In a specific implementation, the selection of the diode D1 is related to the supply voltage and the threshold of the IO port for identifying high and low levels, by referring to the data manual of the single-chip microcomputer. It is necessary to ensure that the diode can reliably work in the reverse cut-off state when the input voltage of the IO port reaches the upper limit of 5V, to prevent reverse current from damaging the circuit.
[0035] The MOS tube Q1 is selected to be an enhancement type N-channel MOS tube with a drain-source breakdown voltage not less than 10V, which can maintain good voltage resistance performance under high voltage input and avoid breakdown phenomenon.
[0036] By reasonably selecting the parameters of the diode D1 and the MOS tube Q1, and combining with the cooperation of other components in the circuit, the IO port can stably work in a wide voltage range of 3V-5V, meeting the requirements of input voltage in different application scenarios.
[0037] Further, during the switching from the output mode to the input mode, the controller first sets the I / O out pin to a high-impedance state and maintains it for 100ms, while monitoring whether the IO port voltage is stable within the range of 3V-5V through the IO in port. If the voltage is stable, the mode switching is completed. If the voltage is detected to be out of range, the controller starts the internal voltage clamping module to clamp the IO port voltage in the safety interval before completing the switching. During the switching process, the charging current of the capacitor C1 is limited within 5mA to avoid the impact of current surge on the stability of the circuit.
[0038] After receiving the mode switching instruction, the controller first closes the driving circuit of the I / O out pin to make it enter a high-impedance state, which can avoid interference of the output signal to the input signal during the mode switching process.
[0039] During the high-impedance state maintenance period, the controller monitors the IO port voltage in real time through the IO in port at a sampling frequency of 10kHz. After 10 consecutive samplings, the average value and fluctuation range of the voltage are calculated to determine whether it is within the safety range of 3V-5V.
[0040] If the voltage is detected to be out of the safe range, the controller will immediately start the internal voltage clamping module, which will quickly clamp the IO port voltage between 3V-5V by dynamically adjusting the working state of the current-limiting resistor and the voltage stabilizing diode in parallel with the IO port.
[0041] The main function of capacitor C1 is to filter the input signal. During the entire mode switching process, by connecting a 1kΩ current-limiting resistor and a constant current source circuit in series in the charging loop of capacitor C1, the charging current is strictly limited to within 5mA, which can effectively suppress the impact of current surge on the circuit.
[0042] Further, when the configuration port is in input mode, the controller performs the pin configuration action in input mode and counts the input signal and updates the corresponding parameter variable.
[0043] After being configured in input mode, the controller sets the I / O out pin to high impedance state, and enables the input buffer and Schmidt trigger of the IO in pin to improve the anti-interference ability of the input signal.
[0044] The edge-triggered counting mechanism is adopted, and when the rising edge or falling edge of the IO port input signal is detected, the internal counter is triggered to perform the add 1 operation. To avoid false triggering, a 20ms debounce time is set, and only stable edge signals are continuously detected to perform counting.
[0045] The controller stores the counting result in the internal parameter variable, and updates the latest counting value to the corresponding memory address through the timing task, so that other program modules or configuration software can read it at any time.
[0046] To prevent the counting value from overflowing, when the counter reaches the maximum value, the controller will automatically reset the counting value to zero and send a counting overflow alarm message to the configuration software through the communication interface.
[0047] Further, the port mode is configured through the configuration software. The configuration software communicates with the controller through the communication interface of the controller. The user selects the port mode through the configuration interface of the configuration software and executes the save configuration command. The configuration software sends a command containing user configuration data to the controller. The controller analyzes the command and executes the corresponding pin configuration action.
[0048] The configuration software adopts a graphical interface design, providing a drop-down menu or a single selection button for users to select the port mode, including push-pull output mode, open-drain output mode and input mode.
[0049] When the user completes the mode selection and clicks the save configuration button, the configuration software will package the user-selected mode information into a command frame of a specific format and send it to the controller through the serial-to-infrared interface.
[0050] After the controller receives the command frame, it first calculates and compares the checksum of the command frame to ensure the accuracy of data transmission. If the verification is passed, the mode configuration information in the command frame is parsed, and the corresponding pin configuration action is performed according to the parsing result.
[0051] After the configuration is successful, the controller returns confirmation information to the configuration software. After receiving the confirmation information, the configuration software displays a prompt on the interface indicating that the configuration is successful, and saves the current configuration information to a local file for automatic loading next time.
[0052] Further, it also includes a mode intelligent switching and fault self-diagnosis linkage mechanism. The controller monitors the signal characteristic parameters in the current mode in real time. The signal characteristic parameters include the output level jump frequency in the push-pull output mode, the external pull-up voltage stability in the open-drain output mode, and the signal edge steepness in the input mode. When any signal characteristic parameter is monitored for 3 consecutive times beyond the preset normal range, the controller automatically triggers the mode switching sequence, first switches to the high-impedance buffer mode for 50ms to isolate abnormal signals, and then performs parameter adaptability detection on the push-pull output mode, the open-drain output mode, and the input mode in turn, selects the mode with the highest matching degree with the current external signal and automatically switches, and uploads the fault information and mode switching record to the configuration software through the communication interface. The parameter adaptability detection includes output drive capability matching degree calculation and input signal recognition accuracy verification.
[0053] The monitoring module inside the controller monitors the signal characteristic parameters of the IO port in real time at a frequency of 100Hz. Special monitoring algorithms are set for different modes.
[0054] In the push-pull output mode, the level signal feedback from the IO in port is analyzed by spectrum analysis, the output level jump frequency is calculated, and compared with the preset normal frequency range.
[0055] In the open-drain output mode, the voltage value of the IO in port is sampled, the fluctuation amplitude within a 100ms time window is calculated, and the stability of the external pull-up voltage is judged. The normal fluctuation range is set to ±0.5V.
[0056] In the input mode, the rising time and falling time of the IO in port signal are measured, the signal edge steepness is calculated, and the normal edge steepness range is set to 0.5V / μs-5V / μs.
[0057] When any signal characteristic parameter is monitored for 3 consecutive times beyond the preset range, the controller immediately triggers the mode switching sequence, first switches the circuit to the high-impedance buffer mode, during which the IO port is electrically isolated from the internal circuit, effectively preventing further damage to the internal circuit by abnormal signals.
[0058] After maintaining the high resistance buffer mode for 50 ms, the controller activates the push-pull output mode, the open drain output mode and the input mode in turn, and performs parameter adaptability detection for each mode:
[0059] Output drive capability matching degree calculation: in the push-pull and open drain output modes, a standard test signal is output to the IO port, the feedback signal strength of the IO in port is monitored, and the matching degree of the output drive capability and the external load is calculated.
[0060] Input signal recognition accuracy verification: in the input mode, a test signal with a known frequency and amplitude is input to the IO port, the ratio of the number of times that the controller correctly recognizes the signal to the total test times is calculated, and the input signal recognition accuracy is obtained.
[0061] The controller selects the mode with the highest matching degree as the new working mode according to the parameter adaptability detection results, and automatically completes the mode switching. At the same time, the time, type and detailed process of the mode switching are uploaded to the configuration software through the communication interface, which facilitates the user to analyze the fault and maintain the system.
[0062] In summary, compared with the prior art, the present application provides a digital input and output circuit and an implementation method thereof, which has the following beneficial effects:
[0063] 1、The present application realizes the switching of three modes of push-pull output, open drain output and input through the circuit design of a single IO port combined with resistors R1 and R2, MOS tube Q1, diode D1 and capacitor C1, and software configuration, reduces the number of interfaces, significantly reduces the hardware cost, saves the internal space of the product, and is especially suitable for meters and other volume-sensitive metering equipment;
[0064] 2、The present application realizes flexible adjustment of port functions through a mode switching mechanism: the push-pull output provides stable drive to output metering parameters, the open drain output supports external connection of different voltage pull-up power supply to adapt to multi-voltage equipment, the input mode can receive a wide range of signals to adapt to various input equipment, and without re-designing, it can meet diversified scenes, improve product versatility and flexibility; at the same time, the input mode is compatible with switch signals that can be connected to point sensors, the open drain output can communicate with bus equipment, and the push-pull output can drive external devices, which expands the signal interaction capability without increasing hardware, and lays a foundation for function upgrade. BRIEF DESCRIPTION OF DRAWINGS
[0065] Figure 1 is the circuit diagram of the present application. DETAILED DESCRIPTION
[0066] Please refer to Figure 1The application provides the following technical scheme: a digital input and output circuit and an implementation method thereof, comprising:
[0067] I. Circuit design and component selection
[0068] 1.1 Circuit structure composition
[0069] The digital input and output circuit of the embodiment mainly comprises a resistor R1, a resistor R2, a MOS tube Q1, a diode D1, a capacitor C1 and a controller. The following is the specific connection relationship and function of each component:
[0070] Resistor R1:
[0071] Resistance selection: a precision resistor in the range of 10kΩ-100kΩ is usually selected, and 47kΩ is preferred in the embodiment.
[0072] Connection mode: one end is connected to the power supply VCC (usually 5V or 3.3V) of the product, and the other end is connected to the anode of the diode D1.
[0073] Function: as a pull-up resistor in the open-drain output mode, it provides high-level driving capability for the IO port; in the input mode, it cooperates with the diode D1 to form a voltage divider circuit, and converts the external input signal into a level signal recognizable by the controller.
[0074] Resistor R2:
[0075] Resistance selection: according to the characteristics of the MOS tube Q1 and the driving capability of the output pin of the controller, a resistor in the range of 1kΩ-10kΩ is selected, and 4.7kΩ is preferred in the embodiment.
[0076] Connection mode: one end is connected to the gate of the MOS tube Q1 and connected to the control pin of the controller to form an I / O out control end; the other end is connected to the source of the MOS tube Q1 and connected to the GND pin of the controller.
[0077] Function: the gate voltage of the MOS tube Q1 is controlled by voltage division, ensuring that the MOS tube can be reliably turned on or turned off when the controller outputs high or low level, and limiting the gate current to protect the output pin of the controller.
[0078] MOS tube Q1:
[0079] Type selection: an N-channel enhancement mode MOS tube is selected, with a drain-source breakdown voltage (VDS) not less than 10V and a conduction resistance (RDS(on)) less than 10Ω.
[0080] Connection mode: the gate is connected to the I / O out control end of the controller through the resistor R2, the source is grounded, the drain is connected to the negative electrode of the diode D1 and one end of the capacitor C1 to form the input and output port IO of the circuit.
[0081] Function: As a switching element, the high and low level signals output by the controller control its on or off state, thereby realizing the level inversion function of the IO port.
[0082] Diode D1:
[0083] Type selection: Choose a fast recovery diode, the reverse voltage value should not be less than 10V, and the forward voltage drop should be less than 1V.
[0084] Connection mode: The negative electrode is connected with the drain electrode of MOS tube Q1, and the positive electrode is connected with resistor R1.
[0085] Function: In the input mode, when the IO port inputs high level, diode D1 is in reverse off state, blocking the current flowing to VCC, protecting the circuit from high voltage input impact; in the output mode, when MOS tube Q1 is turned on, diode D1 provides a path for current, ensuring that the IO port can output low level.
[0086] Capacitor C1:
[0087] Capacitance selection: According to the working frequency and anti-interference requirements of the circuit, select a ceramic capacitor or electrolytic capacitor with a capacitance in the range of 0.1uF-10uF. In this embodiment, a 1nF / 50V ceramic capacitor is preferred.
[0088] Connection mode: One end is connected with GND, and the other end is connected with the negative electrode of diode D1 and the drain electrode of MOS tube Q1, forming the input and output port IO of the circuit.
[0089] Function: Filter and stabilize the voltage signal of the IO port, suppress external electromagnetic interference and voltage fluctuation, and improve the stability of signal transmission.
[0090] Controller:
[0091] Model selection: Choose a single-chip microcomputer with sufficient I / O pins and processing capacity. In this embodiment, an STM32F103 series single-chip microcomputer is preferred, which has abundant GPIO pins and strong processing capacity, and can meet the control requirements of the circuit.
[0092] Communication interface: The controller has a serial-to-infrared interface for communication with the configuration software. In this embodiment, an HC-05 Bluetooth module is used to realize the serial-to-infrared function, making it convenient for users to remotely configure the working mode of the circuit through mobile phones or computers.
[0093] 1.2 Circuit working principle
[0094] The circuit of the present application can realize three working modes: push-pull output mode, open-drain output mode and input mode through software configuration. The specific working principles of each mode are as follows:
[0095] Push-pull output mode:
[0096] Configuration mode: Set I / O out pin as output mode, and IO in as input mode.
[0097] Working process: When the I / O out pin of the controller outputs high level, the gate voltage of MOS tube Q1 is raised through resistance R2, MOS tube Q1 is turned on, IO port is pulled low to GND level, and low level is output; when the I / O out pin outputs low level, the gate voltage of MOS tube Q1 is 0V, MOS tube Q1 is cut off, IO port is pulled up through resistance R1 and VCC, and high level is output. In this way, the level inversion function of the output signal is realized.
[0098] Open drain output mode:
[0099] Configuration mode: disconnect VCC, and the metering device needs to be externally connected with a pull-up resistor to the IO port (the pull-up power supply is determined by the metering device); set I / O out pin as output mode, and IO in as input mode.
[0100] Working process: similar to the push-pull output mode, when the I / O out pin outputs high level, MOS tube Q1 is turned on, IO port is pulled low to GND level, and low level is output; when the I / O out pin outputs low level, MOS tube Q1 is cut off, and the level of IO port is determined by the external pull-up power supply, so that voltage compatibility with external devices is realized.
[0101] Input mode:
[0102] Configuration mode: set I / O out pin as input / high resistance mode (to avoid interference with the input signal), and set IO in as input mode.
[0103] Working process: when the IO port inputs high level, current is conducted to IO in port through diode D1 and resistance R1, so that high level is recognized; when the IO port inputs low level (or is connected with GND), IO in port is pulled low, and low level is recognized. In this way, the detection function of external switch signal or voltage signal is realized.
[0104] II. Specific steps of the implementation method
[0105] 2.1 Mode configuration process
[0106] The working mode of the circuit of the application is set through configuration software, and the specific process is as follows:
[0107] Step 1: start the configuration software
[0108] User starts the configuration software through computer or mobile phone, and the configuration software establishes communication connection with the controller through the serial-to-infrared interface.
[0109] Step 2: Select port mode
[0110] The configuration software provides a graphical interface, and the user selects the port and its working mode (push-pull output mode, open-drain output mode or input mode) on the interface.
[0111] Step 3: Send configuration command
[0112] After the user confirms the configuration, the configuration software encapsulates the mode information selected by the user into a command frame of a specific format and sends it to the controller through the serial-to-infrared interface.
[0113] Step 4: Analyze command and configure pin
[0114] After the controller receives the command frame, it first calculates and compares the checksum of the command frame to ensure the accuracy of data transmission. If the verification is passed, the mode configuration information in the command frame is analyzed, and the corresponding pin configuration action is performed according to the analysis result:
[0115] If it is configured as push-pull output mode, set I / O out pin to output mode and IO in to input mode.
[0116] If it is configured as open-drain output mode, disconnect VCC, set I / O out pin to output mode and IO in to input mode.
[0117] If it is configured as input mode, set I / O out pin to input / high impedance mode and IO in to input mode.
[0118] Step 5: Feedback configuration result
[0119] After the controller completes the pin configuration, it returns a confirmation information of successful configuration to the configuration software. After receiving the confirmation information, the configuration software displays a prompt of successful configuration on the interface and saves the current configuration information to a local file for automatic loading next time.
[0120] 2.2 Specific implementation process of each mode
[0121] Implementation of push-pull output mode:
[0122] Pin configuration: the controller configures I / O out pin as push-pull output mode and IO in pin as input mode.
[0123] Parameter update: The controller establishes a timing update mechanism, reads the parameter values (such as cumulative flow, cumulative heat, etc.) that need to be output from the internal memory according to the preset time interval (such as 1ms), and converts them into corresponding level signals to output through the I / O out pin. For example, if a high-level signal needs to be output, the I / O out pin is set to high level, and the IO port outputs low level through the conduction of MOS tube Q1; if a low-level signal needs to be output, the I / O out pin is set to low level, and the IO port outputs high level.
[0124] Signal monitoring: During output, the controller monitors the actual output level of the IO port through the IO in pin in real time, compares it with the expected output level, and adjusts it in time if it finds inconsistency, to ensure the accuracy of the output signal.
[0125] Implementation of open drain output mode:
[0126] Pin configuration: The controller configures the I / O out pin as push-pull output mode, the IO in pin as input mode, and disconnects the VCC connection.
[0127] Parameter update: Similar to the push-pull output mode, the controller reads the parameter values that need to be output from the internal memory in a timely manner, and converts them into corresponding level signals to output through the I / O out pin. But in the open drain output mode, when the I / O out pin outputs low level, the high level of the IO port is determined by the external pull-up power supply, so the controller needs to adjust the driving ability of the output signal according to the voltage value of the external pull-up power supply, to ensure that the signal can be correctly recognized by the external device.
[0128] Pull-up power supply monitoring: The controller monitors the state of the external pull-up power supply in real time, detects the voltage value of the IO port through the IO in pin, and judges whether the pull-up power supply is working normally. If the pull-up power supply is detected to be abnormal (such as too low or too high voltage), the controller will promptly alarm to the configuration software through the communication interface, prompting the user to check the external pull-up circuit.
[0129] Implementation of input mode:
[0130] Pin configuration: The controller configures the I / O out pin as input / high impedance mode, and the IO in pin as input mode.
[0131] Signal acquisition: The controller acquires the level signal of the IO port in real time through the IO in pin, and judges the state of the external input signal. When the IO port input is high level, the IO in pin recognizes it as high level; when the IO port input is low level (or connected to GND), the IO in pin recognizes it as low level.
[0132] Signal counting and processing: For the switch signal input, the controller uses an edge-triggered counting mechanism. When the rising or falling edge of the IO port input signal is detected, the internal counter is triggered to perform the +1 operation. To avoid false triggering, a 20ms debounce time is set. Only when the stable edge signal is continuously detected will the counting be performed. The controller stores the counting result in the internal parameter variable and updates the latest count value to the corresponding memory address through the timing task (such as every 100ms) so that other program modules or configuration software can read it at any time.
[0133] III. Mode intelligent switching and fault self-diagnosis linkage mechanism
[0134] 3.1 Signal feature monitoring
[0135] The controller monitors the signal feature parameters of the IO port in the current mode in real time. The specific monitoring contents are as follows:
[0136] Push-pull output mode:
[0137] Monitoring parameter: output level jump frequency.
[0138] Monitoring method: The controller monitors the level change of the IO port through the IO in pin, records the level jump frequency in unit time, and calculates the jump frequency.
[0139] Normal range: The preset jump frequency range is 10Hz-10kHz. If the jump frequency exceeds this range, the signal is considered abnormal.
[0140] Open-drain output mode:
[0141] Monitoring parameter: external pull-up voltage stability.
[0142] Monitoring method: The controller periodically samples the voltage value of the IO port through the IO in pin and calculates the fluctuation amplitude within a 100ms time window.
[0143] Normal range: The preset voltage fluctuation range is ±0.5V. If the fluctuation amplitude exceeds this range, the pull-up voltage is considered unstable.
[0144] Input mode:
[0145] Monitoring parameter: signal edge steepness.
[0146] Monitoring method: The controller measures the rise time and fall time of the IO in pin signal through a high-precision timer and calculates the signal edge steepness (voltage change rate).
[0147] Normal range: The preset edge steepness range is 0.5V / μs-5V / μs. If it exceeds this range, the signal quality is considered poor.
[0148] 3.2 Abnormality processing flow
[0149] When the signal characteristic parameters in any mode are monitored to exceed the preset normal range for 3 times in succession, the controller automatically triggers the mode switching sequence, and the specific flow is as follows:
[0150] Step 1: Switch to high-impedance buffer mode
[0151] The controller sets the I / O out pin to a high-impedance state and simultaneously disconnects the connection with VCC, so that the IO port is electrically isolated from the internal circuit, and is maintained for 50 ms to isolate abnormal signals and prevent further damage to the internal circuit caused by abnormal signals.
[0152] Step 2: Parameter adaptability detection
[0153] After maintaining the high-impedance buffer mode for 50 ms, the controller activates the push-pull output mode, open-drain output mode and input mode in turn, and performs parameter adaptability detection for each mode:
[0154] Output drive capability matching degree calculation:
[0155] In the push-pull output mode, the controller outputs a standard test signal (such as a square wave signal with a frequency of 1 kHz and a duty cycle of 50%) to the IO port, monitors the strength and waveform quality of the feedback signal through the IO in port, and calculates the matching degree of the output drive capability and the external load.
[0156] In the open-drain output mode, a standard test signal is also output, the response of the external pull-up power supply to the signal is monitored, and the matching degree of the pull-up resistor and the power supply with the circuit is evaluated.
[0157] Input signal recognition accuracy verification:
[0158] In the input mode, the controller inputs a test signal with a known frequency and amplitude (such as a square wave signal with a frequency of 100 Hz and an amplitude of 5V) to the IO port, calculates the ratio of the number of times of correctly identifying the signal to the total number of tests, and obtains the input signal recognition accuracy.
[0159] Step 3: Mode selection and switching
[0160] The controller selects the mode with the highest matching degree as the new working mode according to the parameter adaptability detection results. For example, if the input signal recognition accuracy is the highest in the input mode, the input mode is switched to; if the output drive capability matching degree is the best in the push-pull output mode, the push-pull output mode is switched to.
[0161] Step 4: Fault information uploading
[0162] The controller uploads the time of fault occurrence, type (such as jump frequency anomaly, unstable pull-up voltage, etc.) and detailed process of mode switching to the configuration software through the communication interface. After receiving the fault information, the configuration software generates a detailed fault report and displays the alarm information on the interface to prompt the user to take corresponding measures.
[0163] Four, voltage adaptation and mode switching safety mechanism
[0164] 4.1 Wide voltage adaptation implementation
[0165] The IO port of the application can adapt to the input voltage range of 3V-5V, and the specific implementation is as follows:
[0166] Selection of diode D1:
[0167] A diode with a reverse voltage value not less than 10V is selected to ensure that the diode can still work reliably in the reverse blocking state when the IO port input voltage reaches the upper limit of 5V, preventing reverse current from damaging the circuit, and the forward voltage drop is less than 1V.
[0168] Selection of MOS tube Q1:
[0169] Select a MOS tube with a drain-source breakdown voltage not less than 10V to maintain good voltage resistance performance under high voltage input conditions and avoid breakdown. At the same time, the on-resistance of the MOS tube should be as small as possible to reduce power loss when turned on.
[0170] Voltage division and current limiting design:
[0171] When the IO port inputs high voltage, the input voltage is reduced to a range that the controller can withstand through the voltage division of resistor R1 and diode D1. At the same time, resistor R1 also plays a current limiting role, limiting the current flowing into the controller and protecting the controller from large current impact.
[0172] 4.2 Mode switching safety mechanism
[0173] In order to ensure the safe and stable operation of the circuit during the switching from output mode to input mode, the application adopts the following safety mechanism:
[0174] Step 1: Set high resistance state and delay
[0175] After receiving the mode switching instruction, the controller first closes the drive circuit of the I / O out pin, making it enter a high resistance state to avoid interference of the output signal with the input signal during mode switching. At the same time, the high resistance state is maintained for 100ms to provide a stable transition time for the circuit.
[0176] Step 2: Voltage monitoring and clamping
[0177] During the high impedance state retention period, the controller monitors the IO port voltage in real time through the IO in port at a sampling frequency of 10 kHz. After 10 consecutive samples, the average value and fluctuation range of the voltage are calculated to determine whether it is within the safe range of 3V-5V.
[0178] If the monitored voltage exceeds the safe range, the controller immediately activates the internal voltage clamping module. This module dynamically adjusts the working state of the current-limiting resistor and the voltage stabilizing diode connected in parallel with the IO port to quickly clamp the IO port voltage between 3V-5V, preventing excessive or low voltage from damaging the circuit.
[0179] Step 3: Limiting the charging current
[0180] During the mode switching process, to avoid excessive charging current of capacitor C1 impacting the circuit, a 1kΩ current-limiting resistor and a constant current source circuit are connected in series in the charging circuit of capacitor C1, strictly limiting the charging current to within 5mA. In this way, the impact of current surge on the circuit is effectively suppressed, improving the stability of mode switching.
[0181] Five, application scenarios and examples
[0182] 5.1 Smart water meter application
[0183] In a smart water meter, the digital input and output circuit of the present application can achieve the following functions:
[0184] Flow pulse signal acquisition:
[0185] Configure the circuit as input mode, connect the IO port to the output end of the flow sensor. When water flows through the water meter, the flow sensor generates pulse signals, which are received by the circuit through the IO port and transmitted to the controller through the IO in port for counting and processing, thereby realizing the measurement of water flow.
[0186] Data output and communication:
[0187] Configure the circuit as open-drain output mode, connect the IO port to the communication line of the meter reading device through an external pull-up resistor. The controller converts the accumulated water flow data into digital signals and outputs them to the meter reading device through the IO port, realizing remote data transmission. Due to the flexibility of open-drain output mode, it can adapt to meter reading devices of different voltage standards.
[0188] State indication:
[0189] The circuit is configured as a push-pull output mode, and the IO port is connected to the LED indicator. The controller outputs corresponding level signals through the IO port according to the working state of the water meter (such as normal operation, low battery, fault, etc.), and drives the LED indicator to display different colors or flashing modes, providing intuitive state indication for users.
[0190] 5.2 Industrial automation control application
[0191] In the industrial automation control system, the circuit of the application can realize the following functions:
[0192] Sensor signal acquisition:
[0193] The circuit is configured as an input mode, and the IO port is connected to the output end of various sensors (such as temperature sensors, pressure sensors, position sensors, etc.). The controller collects the signals output by the sensors through the IO in port, analyzes and processes them, and realizes the monitoring and control of the industrial production process.
[0194] Actuator control:
[0195] The circuit is configured as a push-pull output mode, and the IO port is connected to the relay, solenoid valve, etc. The controller outputs corresponding control signals through the IO port according to the control algorithm and preset parameters, drives the actuator to act, and realizes the automation control of industrial equipment.
[0196] Inter-device communication:
[0197] The circuit is configured as an open drain output mode, and the IO port is connected to the industrial bus (such as Modbus, CAN, etc.) through an external pull-up resistor. The controller exchanges data with other devices on the bus through the IO port, realizes the communication and cooperative work between industrial devices.
[0198] Six, test and verification
[0199] 6.1 Function test
[0200] Test environment:
[0201] Power supply: Provide DC power supply with multiple voltage levels such as 3.3V, 5V, 12V, 24V, etc.
[0202] Signal source: Can generate various signals (such as square wave, sine wave, pulse wave, etc.) with frequency range of 0.1Hz-100kHz and amplitude range of 0-30V.
[0203] Load: Including resistance load with different resistance values, LED indicator, relay, etc.
[0204] Test equipment: Oscilloscope, logic analyzer, multimeter, etc.
[0205] Test content and results:
[0206] Push-pull output mode test:
[0207] Input control signals with different frequencies and duty cycles, observe the output waveform of the IO port through the oscilloscope, verify the level inversion function and stability of the output signal.
[0208] Connect different loads (such as 1kΩ resistor, LED indicator), test the driving ability of the circuit, ensure that the output signal quality meets the requirements under different load conditions.
[0209] Test results: The output signal waveform is complete, the level inversion is accurate, and the driving ability meets the design requirements.
[0210] Open-drain output mode test:
[0211] Connect 3.3V, 5V, 12V external pull-up power supply respectively, test the output performance of the circuit under different pull-up voltages.
[0212] Monitor the communication between the output signal and external equipment through the logic analyzer, verify the accuracy and reliability of signal transmission.
[0213] Test results: The circuit can adapt to different voltage external pull-up power supply, signal transmission is accurate and reliable.
[0214] Input mode test:
[0215] Input voltage signals with different amplitudes and frequencies, test the adaptability of the circuit to a wide range of input voltages.
[0216] Input switch signals (such as dry contact signals), verify the recognition ability and counting accuracy of the circuit to switch signals.
[0217] Test results: The circuit can accurately identify input voltage signals and switch signals within the range of 3V-5V, with a counting error less than 0.1%.
[0218] 6.2 Stability and reliability test
[0219] Test content and results:
[0220] Anti-interference test:
[0221] In a strong electromagnetic interference environment (such as near frequency converters, motors, etc.), test the anti-interference ability of the circuit.
[0222] Observe the signal waveform of the IO port through the oscilloscope, record the number of false actions caused by interference.
[0223] Test results: In a strong electromagnetic interference environment, the circuit can work normally, with a false action frequency less than 1 time / hour.
[0224] Long-term operation test:
[0225] Let the circuit run continuously for 72 hours, during which the working mode is constantly switched, and the working state and performance changes of the circuit are monitored.
[0226] Record the temperature, power consumption and other parameters of the circuit, and check whether there are any abnormal conditions.
[0227] Test results: The circuit works stably during long-term operation, the temperature and power consumption changes are within the normal range, and no faults occur.
[0228] Mode switching test:
[0229] Frequently switch the working mode of the circuit (such as switching every 5 seconds), test the stability and reliability of mode switching.
[0230] Record the signal changes during mode switching through a logic analyzer, check whether there are any signal distortion or abnormal conditions.
[0231] Test results: The mode switching process is smooth, the signal is not distorted or abnormal, and the switching success rate reaches 100%.
[0232] In summary, the present application realizes the multifunctional reuse of digital input and output ports through unique circuit design and software configuration methods, has flexible voltage adaptation ability, intelligent mode switching mechanism and reliable fault self-diagnosis function, can meet the needs of different application scenarios, has wide application prospect and significant practical value.
Claims
1. A digital input / output circuit, characterized in that, The circuit includes resistors R1 and R2, a MOSFET Q1, a diode D1, a capacitor C1, and a controller. One end of resistor R2 is connected to the gate of MOSFET Q1 and to the control pin of the controller to form an I / O out control terminal. The other end of resistor R2 is connected to the source of MOSFET Q1 and to the GND pin of the controller. The cathode of diode D1 is connected to the drain of MOSFET Q1, and the anode is connected to resistor R1. The other end of resistor R1 is connected to the power supply VCC of the product. The common terminal of resistor R1 and diode D1 is connected to a pin of the controller to form an IO in port. One end of capacitor C1 is connected to GND, and the other end is connected to the cathode of diode D1 and the drain of MOSFET Q1 to form the circuit's input / output port I / O.
2. The digital input / output circuit according to claim 1, characterized in that, The controller is a microcontroller, which has a communication interface, which is a serial-to-infrared interface, used to communicate with the configuration software.
3. A method for implementing a digital input / output circuit, based on the digital input / output circuit of claim 1, characterized in that, include: The push-pull output mode is implemented as follows: the I / O out pin is set to output mode and the IOin pin is set to input mode. When the I / O out pin outputs a high level, the IO port outputs a low level; when the I / O out pin outputs a low level, the IO port outputs a high level. The open-drain output mode is implemented as follows: VCC is disconnected, and an external pull-up resistor is connected to the I / O port of the meter reading device. The pull-up power supply depends on the meter reading device. When the I / O out pin outputs a high level, the I / O port outputs a low level. When the I / O out outputs a low level, the I / O port outputs a high level, and the level of the I / O port is consistent with the external pull-up. The input mode is implemented as follows: the I / O out pin is set to input / high impedance mode, and the IO in pin is set to input mode; when the IO port input is high, IO in is high; when the IO port input is low, IO in is low; the IO port supports switch signal input; when the IO port is connected to GND, IO in is identified as low; when the IO port is disconnected from GND, IO in is identified as high.
4. The method for implementing the digital input / output circuit according to claim 3, characterized in that, When the port is configured as push-pull output mode, the controller performs pin configuration actions in push-pull output mode and periodically updates the parameters that need to be output to the corresponding port.
5. The method for implementing the digital input / output circuit according to claim 3, characterized in that, When the port is configured as open-drain output mode, the controller performs pin configuration actions in open-drain output mode and periodically updates the parameters that need to be output to the corresponding port.
6. The method for implementing the digital input / output circuit according to claim 3, characterized in that, The input voltage of the I / O port depends on the selection of diode D1 and MOSFET Q1. The diode D1 is selected with a preset reverse withstand voltage value, and the MOSFET Q1 is selected with a preset drain-source breakdown voltage. The combination of these two selections enables the I / O port to adapt to an input voltage range of 3V-5V.
7. The method for implementing the digital input / output circuit according to claim 6, characterized in that, During the switch from output mode to input mode, the controller first sets the I / O out pin to a high-impedance state and holds it for 100ms. At the same time, it monitors whether the I / O port voltage is stable within the range of 3V-5V through the I / O in port. If the voltage is stable, the mode switch is completed. If the voltage is detected to be out of range, the controller activates the internal voltage clamping module to clamp the I / O port voltage within a safe range before completing the switch. During the switch, the charging current of capacitor C1 is limited to less than 5mA to avoid the current surge at the moment of switching from affecting the stability of the circuit.
8. The method for implementing the digital input / output circuit according to claim 3, characterized in that, When the configuration port is in input mode, the controller performs pin configuration actions in input mode, counts the input signals, and updates the corresponding parameter variables.
9. The method for implementing the digital input / output circuit according to claim 3, characterized in that, The port mode is configured through configuration software. The configuration software communicates with the controller through the controller's communication interface. The user selects the port mode through the configuration interface of the configuration software and executes the save configuration command. The configuration software sends a command containing the user's configuration data to the controller. After parsing the command, the controller executes the corresponding pin configuration action.
10. The method for implementing the digital input / output circuit according to claim 3, characterized in that, It also includes a linkage mechanism for intelligent mode switching and fault self-diagnosis. The controller monitors the signal characteristic parameters of the I / O port in the current mode in real time. The signal characteristic parameters include the output level transition frequency in push-pull output mode, the external pull-up voltage stability in open-drain output mode, and the signal edge steepness in input mode. When the signal characteristic parameters of any mode exceed the preset normal range three times in a row, the controller automatically triggers the mode switching sequence. First, it switches to a high-impedance buffer mode and holds it for 50ms to isolate abnormal signals. Then, it performs parameter compatibility testing on push-pull output mode, open-drain output mode, and input mode in sequence, selects the mode with the highest matching degree with the current external signal, and automatically switches to it. At the same time, the fault information and mode switching record are uploaded to the configuration software through the communication interface. The parameter compatibility testing includes output drive capability matching degree calculation and input signal recognition accuracy verification.