Method for realizing single-chip microcomputer arbitrary IO port line operation by using C program pointer and function parameter
Patent Information
- Application Number
- CN202210941460.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-08
- Publication Date
- 2026-08-21
- Estimated Expiration
- 2042-08-08
AI Technical Summary
然而,这两种方法仍然存在较多问题,如:直接操作IO的方式中,根据需操作的IO口数量增加,会增加大量重复代码,占用大量存储空间,且非常不利于同类程序的调试和维护;直接操作IO的方式中,如果在IO口线通信中由于线缆材料或型式的不当选取、通信距离的延长等因素,造成串行数据波形发生畸变,导至通信数据错误,此时如果需调整通信时序(如延时参数等),则需要分别对不同IO口的多个子程序(或函数)进行调整,调试工作量大大增加;利用C程序switch case语句实现用函数操作IO的方法中,虽然实现了使用函数形参方式操作不同IO口线,不需要重复同类型代码,但因为函数调用及分支选择次数增多,程序执行代码量增加,系统运行速度减慢,执行时间增长,导致整个系统的反应不及时等
Smart Images

Figure CN115374027B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of C language programming for microcontrollers, and more specifically, to a method for implementing arbitrary I / O port operations of a microcontroller using C program pointers and function parameters. Background Technology
[0002] Because C is a compiled language, unlike interpreted languages, it cannot use macro substitution instructions to change keywords during runtime. MCU I / O line operations must be clearly defined during the programming phase. In existing technologies, it's inconvenient to use function parameters to point to different I / O ports when performing similar operations on different MCU I / O (input / output) lines, resulting in code duplication. This leads to large memory footprints, low execution efficiency, and increased debugging workload; if parameters in one program are adjusted, one or more very similar programs also need to be adjusted.
[0003] Common methods generally involve direct I / O manipulation or, more innovatively, using C program switch-case statements to implement function-based I / O operations. However, both methods still have several problems. For example, in direct I / O manipulation, as the number of I / O ports to be operated increases, a large amount of repetitive code is added, consuming a significant amount of storage space and making it very difficult to debug and maintain similar programs. In direct I / O manipulation, if improper selection of cable materials or types, or extended communication distances, cause distortion of the serial data waveform and lead to communication data errors, adjusting the communication timing (such as delay parameters) requires adjusting multiple subroutines (or functions) for different I / O ports, greatly increasing the debugging workload. While using C program switch-case statements to implement function-based I / O operations allows for the use of function parameters to operate different I / O lines without repeating the same type of code, the increased number of function calls and branch selections increases the amount of code executed, slows down system operation, increases execution time, and leads to untimely system response.
[0004] In view of this, we propose a method to implement arbitrary I / O line operations of a microcontroller using C program pointers and function parameters. Summary of the Invention
[0005] The purpose of this invention is to provide a method for implementing arbitrary I / O line operations of a microcontroller using C program pointers and function parameters, in order to solve the problems mentioned in the background art.
[0006] To address the aforementioned technical problems, one objective of this invention is to provide a method for implementing arbitrary I / O port line operations in a microcontroller using C program pointers and function parameters. This method utilizes pointers in the C programming language and parameterized functions to access, control, and select the required I / O port lines. A pointer variable array is created, containing pointers to the I / O port function register addresses, based on the number of functions required for each I / O port operation. For microcontrollers with only ports P1 to Px, element 0 of the pointer array has a value of 0, element 1 corresponds to the function address of port P1, and the element value requires a forced pointer type conversion. Element x corresponds to the forced pointer type conversion value of the function address of port x. Before using the I / O port line operations, the port number and port line bits are set. In subsequent I / O port operations, the I / O port line operation statements are converted into assigning or retrieving values to the pointer variable elements corresponding to the port numbers in the selected function selection pointer variable array.
[0007] Specifically, the steps include the following:
[0008] Step 1: Create an array of pointer variables that point to the address pointers of the I / O port function registers;
[0009] Step 2: Set up a two-dimensional constant array that corresponds to the port number and port line position;
[0010] Step 3: Before operating on the microcontroller's I / O lines, use a parameterized I / O port setting function to retrieve the port number value and the I / O line bit value to be operated and save them in two 8-bit global variables.
[0011] Step 4: Write the I / O line operation statements to assign or retrieve values to the corresponding pointer variable elements in the pointer variable array from Step 1.
[0012] As a further improvement to this technical solution, the specific method for creating the array of pointer variables pointing to the I / O port address pointer in step 1 is as follows:
[0013] After forcibly converting the I / O port function address definition word defined in the selected microcontroller C program header file to a pointer, a one-dimensional pointer variable array is created. The order of the I / O port function pointer elements stored in the pointer array corresponds to the port number, that is, the subscript of the array element is equal to the microcontroller port number, so as to realize that the operation on different ports is converted into the assignment of the pointer variable array elements.
[0014] As a further improvement to this technical solution, the method for setting the two-dimensional constant array corresponding to the port number and port line bits in step 2 is as follows:
[0015] Use an unsigned character variable from 0 to m to represent port lines 0 to m respectively; the constant array subscripts [0][0] and [0][1] elements store the port number and port line bit of port line 0 respectively, and the elements [m][0] and [m][1] elements store the port number and port line bit of port line m respectively;
[0016] Among them, port numbers P0 to P7 are represented by 0 to 7 respectively, and port line bit n is written as the number of single bytes in which the port line is in the position of "1" and the others are "0", or written as "1 left shifted n bits";
[0017] As a further improvement to this technical solution, in step 2, a two-dimensional constant array is set to correspond to the port number and port line bit. This step is not necessary. If this step and the constant array are omitted, it will be inconvenient to set the IO port lines and move the program, and it will be impossible to traverse the IO port lines. Furthermore, in step 3, the port number and port line bit to be operated on need to be specified one by one through selection or judgment statements.
[0018] As a further improvement to this technical solution, in step 3, before operating on the microcontroller's I / O lines, an I / O setting function with parameters is used. The function takes out the port number value and the I / O line bit value from the two-dimensional constant array set in step 2 and writes them into two 8-bit global variables for storage, thereby specifying that subsequent I / O line operations correspond to a certain I / O line; the parameters of the I / O line setting function are selected from 0 to m in step 2.
[0019] As a further improvement to this technical solution, in step 4, the I / O line operation statement is written as assigning or retrieving values to the corresponding pointer variable elements in the function selection pointer variable array. The specific method is as follows:
[0020] When assigning a value to the pointer element corresponding to the port, assigning a value of 1 only performs an "OR 1" operation on the port line bit, and assigning a value of 0 only performs an "AND 0" operation on the port line bit.
[0021] When reading the port value, the pointer element corresponding to the port is ANDed with 1 and then incremented by "()".
[0022] As a further improvement to this technical solution, the specific method for creating the array of pointer variables pointing to the I / O port address pointer in step 1 further includes:
[0023] If this MCU does not have a P6 port, then delete the P6 definition;
[0024] If a P7 port exists, the definition of P7 can be added;
[0025] If the MCU has a P0 port, the value of the 0th element in the array can be changed to "(char*)P0" or similar statements;
[0026] For different types of MCUs, you can look up the header file of the MCU to obtain the name of the definition of the I / O port function register address (pointer);
[0027] If PxSEL or similar functions are needed, simply add the definition of the pointer variable array "*PxSEL".
[0028] As a further improvement to this technical solution, in step 3, the port number value and port line bit value to be operated are extracted and written into two 8-bit global variables for storage. These two 8-bit global variables can be omitted. However, the parameterized IO port setting function used before operating the microcontroller IO port line in step 3 needs to be changed to a jump to the constant array pointer in step 2.
[0029] The second objective of this invention is to provide a method running platform device, including a processor, a memory, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it implements the steps of the above-described method for operating arbitrary I / O lines of a microcontroller using C program pointers and function parameters.
[0030] A third objective of this invention is to provide a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the method described above for operating arbitrary I / O lines of a microcontroller using C program pointers and function parameters.
[0031] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0032] 1. This method of implementing arbitrary I / O port operations of a microcontroller using C program pointers and function parameters solves the problem of increased repetitive code and large storage space consumption caused by direct I / O operation as the number of I / O ports to be operated increases. The operation programs (including control, access, gating, etc.) of the same type of peripheral devices no longer need to be rewritten as subroutines or functions. When the upper-level program calls the function parameters, the operation of peripheral devices distributed on different I / O ports can be completed respectively. The program is concise, easy to maintain, easy to modify and portable.
[0033] 2. This method of using C program pointers and function parameters to implement arbitrary I / O port operations of a microcontroller is superior to the direct I / O operation method. In the case of I / O port communication, if the serial data waveform is distorted due to factors such as improper selection of cable material or type or extension of communication distance, resulting in communication data errors, it is necessary to adjust the communication timing (such as delay parameters) separately for multiple subroutines (or functions) of different I / O ports, which greatly increases the debugging workload.
[0034] 3. This method of implementing arbitrary I / O port operations on a microcontroller using C program pointers and function parameters, compared to the method of using C program switch case statements to implement I / O operations using functions, solves the problem of increased function calls and branch selections. It directly operates on the I / O port using pointers to I / O port addresses, resulting in faster execution speed and higher efficiency. Although it involves redefining the address pointer array (only 3 × 7 = 21 bytes in this example), it significantly reduces the amount of code executed by the branch selection function, further improving system speed. For applications with strict timing requirements, it ensures reliable program execution. Furthermore, its effectiveness is even more pronounced in high-speed serial communication.
[0035] 4. This method of using C program pointers and function parameters to implement arbitrary I / O line operations of a microcontroller is particularly effective for 32-bit microcontrollers that widely use the ARM (Advanced Reduced Instruction Set Machine) architecture. Since most microcontrollers have many I / O lines, I / O line function selections, and operation instructions, this method is more effective in removing duplicate code, facilitating programming, improving operating efficiency, improving reliability, and reducing power consumption when a single microcontroller needs to control multiple or a large number of similar peripheral devices.
[0036] 5. This method of using C program pointers and function parameters to implement arbitrary I / O port operations of a microcontroller eliminates the need to use the same set of I / O ports (such as not necessarily P21, P22, P23, P24, etc. arranged sequentially) in PCB (printed circuit board) design, simplifying PCB design. For the finished PCB, different I / O ports can be freely selected based on layout or convenience to achieve communication or control of peripheral devices. For operations on all MCU I / O ports, simply resetting the I / O port constant array in step 2 allows any I / O port to be operated or accessed using a single function parameter. The program is simple to modify and configure, universal, and runs efficiently.
[0037] 6. This method of using C program pointers and function parameters to implement arbitrary I / O port operations of a microcontroller is convenient for quickly traversing all I / O ports using loop statements for similar operations with irregular sorting and a large number of I / O ports. Attached Figure Description
[0038] Figure 1 This is a schematic diagram of the overall method flow as exemplified in this invention;
[0039] Figure 2 This is a schematic diagram illustrating the call relationship of exemplary C program code fragments in this invention;
[0040] Figure 3 This is a detailed flowchart illustrating the exemplary overall method of the present invention;
[0041] Figure 4This is a structural diagram of an exemplary electronic computer platform device in this invention. Detailed Implementation
[0042] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0043] Example 1
[0044] like Figures 1-4 As shown, this embodiment provides a method for implementing arbitrary I / O line operations of a microcontroller using C program pointers and function parameters.
[0045] like Figures 1-2 As shown, based on the pointers in the C programming language and the I / O lines required for accessing, controlling, and strobing functions with parameters, an array of pointer variables is added to the corresponding number of pointers to the I / O port function register addresses according to the number of I / O port operation functions required. For a microcontroller with only ports P1 to Px, the value of element 0 in the pointer array is 0, the value of element 1 corresponds to the function address of port P1, and the element value needs to be forcibly type-casted to the address. Element x corresponds to the forcibly type-casted value of the function address of port x. Before using the I / O lines, the port number and port line bits are set. In subsequent I / O line operations, the I / O line operation statements are converted into assigning or retrieving values to the pointer variable elements corresponding to the port numbers in the function selection pointer variable array.
[0046] Specifically, the steps include the following:
[0047] Step 1: Create an array of pointer variables that point to the address pointers of the I / O port function registers;
[0048] Step 2: Set up a two-dimensional constant array with corresponding port numbers and port lines;
[0049] Step 3: Before operating on the microcontroller's I / O lines, use a parameterized I / O port setting function to retrieve the port number value and the I / O line bit value to be operated and save them in two 8-bit global variables.
[0050] Step 4: Write the I / O line operation statements to assign or retrieve values to the corresponding pointer variable elements in the pointer variable array from Step 1.
[0051] In this embodiment, the specific method for creating the array of pointer variables pointing to the I / O port address pointers in step 1 is as follows:
[0052] After forcibly converting the I / O port function address definition word defined in the selected microcontroller C program header file to a pointer, a one-dimensional pointer variable array is created. The order of the I / O function pointer elements stored in the pointer array corresponds to the port number, that is, the subscript of the array element is equal to the microcontroller port number, so as to realize that the operation on different ports is converted into the assignment of the pointer variable array.
[0053] Furthermore, in step 1, the specific method for creating an array of pointer variables pointing to I / O port addresses also includes:
[0054] If this MCU does not have a P6 port, then delete the P6 definition;
[0055] If a P7 port exists, the definition of P7 can be added;
[0056] If the MCU has a P0 port, the value of the 0th element in the array can be changed to "(char*)P0" or similar statements;
[0057] For different types of MCUs, you can look up the MCU's header file to obtain the definition name of the I / O port function address (pointer);
[0058] If PxSEL or similar functions are needed, simply add the definition of the pointer variable array "*PxSEL".
[0059] In this embodiment, the method for setting the two-dimensional constant array corresponding to the port number and port line bit in step 2 is as follows:
[0060] Use an unsigned character variable from 0 to m to represent port lines 0 to m respectively; the constant array subscripts [0][0] and [0][1] elements store the port number and port line bit of port line 0 respectively, and the elements [m][0] and [m][1] elements store the port number and port line bit of port line m respectively;
[0061] Among them, port numbers P0 to P7 are represented by 0 to 7 respectively, and port line bit n is written as the number of single bytes in which the port line is in the position of "1" and the others are "0", or written as "1 left shifted n bits"; for example, port line P15 is port 1, and port line bit 5 is written as "0x20" or "1<<5".
[0062] This step is not mandatory. If this step and the constant array are omitted, it will be inconvenient to set up and move the I / O lines, and it will be impossible to traverse the I / O lines. Furthermore, in step 3, the port number and I / O line bit to be operated on must be specified one by one through selection or judgment statements.
[0063] In this embodiment, in step 3, before operating on the microcontroller's I / O lines, an I / O line setting function with parameters is used. The function takes out the port number value and the line bit value from the two-dimensional constant array set in step 2 and writes them into two 8-bit global variables for storage, thereby specifying that subsequent line operations correspond to a certain line. The parameters of the I / O line setting function are selected from 0 to m in step 2.
[0064] Once the I / O line setting function is defined and executed, it retrieves the port number and line bit from the constant array and temporarily stores them in the global variable of the line, thus specifying that subsequent line operations correspond to a specific line.
[0065] Furthermore, in step 3, the port number value and port line bit value to be operated are retrieved and written into two 8-bit global variables for storage. These two 8-bit global variables can be omitted. However, the parameterized IO port setting function used before operating the microcontroller IO port line in step 3 needs to be changed to a jump to the constant array pointer in step 2.
[0066] In this embodiment, in step 4, the I / O line operation statement is written to assign or retrieve values to the corresponding pointer variable elements in the function selection pointer variable array. The specific method is as follows:
[0067] When assigning a value to the pointer element corresponding to the port, assigning a value of 1 only performs an "OR 1" operation on the port line bit, and assigning a value of 0 only performs an "AND 0" operation on the port line bit.
[0068] When reading the port value, the pointer element corresponding to the port is ANDed with 1 and then incremented by "()".
[0069] Example 2
[0070] like Figure 3 As shown, the above method can be further refined into the following steps:
[0071] S1, Initial Configuration of C Program:
[0072] S1.1 Define the instruction word for I / O port operations: Use the preprocessor substitution command define to create a function-like I / O port definition. It should be noted that this step is not mandatory, but it helps to simplify and port the program, so it is recommended to keep it.
[0073] S1.2 Define two 8-bit global variables for the port and I / O line bits (if not defined, a pointer to a two-dimensional array can be defined after S1.4 to locate the I / O line to be operated on);
[0074] S1.3, Create an array of pointer variables that point to the addresses of the I / O ports;
[0075] S1.4 Define a two-dimensional array table of the multiple I / O lines to be used;
[0076] S1.5 Define the I / O port setting function;
[0077] S1.6 Define the sub-functions required to operate the peripheral devices (the specific instruction words in this step are the actual form of step S1.1);
[0078] S1.7 Define the parameterized functions required to operate peripheral devices;
[0079] S2. Function call relationship of multiple I / O ports (this step only reflects the logical relationship, not the program execution flow):
[0080] S2.1 Call step S1.7 according to the parameters specified by the IO port line number;
[0081] S2.2 Execute step S1.5. In this step, the port table defined in step S1.4 is used to convert the specified I / O line number into the port line address and port line bit value.
[0082] S2.3 Call any number of sub-functions defined in step S1.6;
[0083] S2.4 When programming, choose whether to execute the next IO operation; if you need to execute the next IO operation or traverse all IO lines in the S1.4 array, return to step S2.1 and execute steps S2.1 to S2.3 in sequence; otherwise, omit this step.
[0084] S2.5, Call the I / O port operation function to end or return a value.
[0085] Furthermore, in step S1.3, the specific method for creating the array of pointer variables pointing to the I / O port address pointers is as follows: use the I / O port address pointers defined in the MCU header file to create a one-dimensional array of pointer variables.
[0086] Furthermore, in step S1.5, the specific method for defining the IO line setting function is as follows: the function parameter Tn is used to change and save the global variable of the corresponding IO line, thereby specifying that subsequent IO line operations all correspond to a certain IO line; wherein, the global variable is the two 8-bit port and IO line bit global variables defined in step S1.2.
[0087] Among them, two 8-bit global variables are predefined: Port_x and Bit_n.
[0088] Furthermore, the sub-functions defined in step S1.6 for operating peripheral devices are mainly, but not limited to, several basic sub-functions used by each peripheral device.
[0089] Specifically, in step S1.6, during the process of defining each sub-function, the instruction words in each sub-function are replaced with the instruction words for I / O port operations defined in step S1.1 during pre-compilation.
[0090] Furthermore, after defining the parameterized functions required to operate the peripheral devices in step S1.7, other parameterized functions for operating the peripheral devices can be defined again. The functions defined in this step can be the same as the functions in step S1.7.
[0091] In this embodiment, regarding the method of this solution, it is worth noting that:
[0092] 1. Step S1.1 defines the instruction word for I / O port operations. This section of code is not mandatory. If this section is omitted, the subsequent operations of DQ1 and DQ0 to output high and low levels on the I / O ports will need to be replaced with direct pointer operations, such as: *PxOUT_addr[Port_x]|=Bit_n, *PxOUT_addr[Port_x]&=~Bit_n; other operations are similar. However, this method reduces the readability and flexibility of the program, making it less concise, so omitting this definition statement is not recommended.
[0093] 2. The operations to be performed on the I / O ports in step S1.1 include reading I / O port values, setting the I / O port status to input and output, setting the I / O port output to a level, and outputting a low level. If the MCU being used does not have status setting operations (such as the MCS-51 microcontroller), or if subsequent program functions only require outputting high and low levels, the unnecessary operation definitions can be deleted. Correspondingly, the pointer variables for the I / O port function definitions in step S1.3 can be deleted, further saving RAM (random access memory) space. If the MCU being used has sufficient storage space, it is recommended to retain the pointer variables for the I / O port function definitions to facilitate programming different models of the same type of microcontroller.
[0094] 3. Step S1.3 provides a relatively universal program that writes all I / O port segments (such as P1 to P6) of the selected MCU into the program, which can facilitate programming most MCUs. If the MCU's code storage space is small, and to save code space, the I / O ports used are concentrated in P1 to P3, and P5 and P6 are not used, the definitions of P5 and P6 can be deleted without affecting the program's operation.
[0095] To verify the feasibility and effectiveness of the above scheme, this embodiment also provides an experimental comparison of the common direct I / O operation method, the method of using C program switch case statements to implement I / O operation using functions, and the method of using C program pointers and function parameters to implement arbitrary I / O line operation of the microcontroller. The specific experiments are as follows:
[0096] First, it is worth noting that all three methods take the C language program of controlling a digital temperature sensor with an MSP430F413 microcontroller as an example. When operating on different I / O ports, first define the functions or subroutines for each I / O port operation, and each subroutine directly calls different I / O ports; specifically, take the operation of three temperature sensors using three I / O ports, namely P12, P31, and P50, as an example.
[0097] Method A: Commonly used direct I / O operation method
[0098] A1.1 Connect the first sensor to port P12, and define the I / O port pointer and function:
[0099] Define DQ1_H as the P12 port line to be pulled high;
[0100] Define DQ1_L as the line to pull P12 low;
[0101] Define DQ1_out as the output of port P12;
[0102] Define DQ1_in as the input of port P12;
[0103] Define DQ1_VL as the value of the P12 port line level;
[0104] A1.2. Connect the second sensor to P31, and define the I / O port pointer and function:
[0105] Define DQ2_H as the P31 port line to be pulled high;
[0106] Define DQ2_L as the P31 port line pulled low;
[0107] Define DQ2_out as the output of port P31;
[0108] Define DQ2_in as the input of port P31;
[0109] Define DQ2_VL as the value of the line level of port P31;
[0110] A1.3. Connect the third sensor to port P50, and define the I / O port pointer and function:
[0111] Define DQ3_H as the P50 port line to be pulled high;
[0112] Define DQ3_L as the line to pull P50 low;
[0113] Define DQ3_out as the output of port P50;
[0114] Define DQ3_in as the input of port P50;
[0115] Define DQ3_VL as the value of the line level of port P50;
[0116] A1.4 Partial program flow for the first sensor:
[0117] A1.4.1 Reset 1# port line sensor subroutine: Start——DQ1_out——DQ1_L——Delay 600μs——DQ1_H——Delay 30μs——DQ1_in——Null command——DQ1_out——DQ1_H——Delay 400μs——Return;
[0118] A1.4.2, Function flow for writing one byte to sensor #1: Start — Loop 8 times [DQ0 — Delay 6μs — If the last bit of the written number is 1, DQ1_H / Otherwise, DQ1_L — Shift the written number right by 1 bit — Delay 30μs — DQ1_H — Delay 10μs] — Return;
[0119] A1.4.3, Function flow for reading one byte from sensor #1: Start — Loop 8 times [Value right shift 1 bit — DQ1_L — DQ1_H — DQ1_in — If DQ1_val is 1, write 1 to the high bit of the value — DQ1_out — DQ1_H — Delay 2μs] — Return value;
[0120] A1.4.4 The function flow for reading the value of sensor #1 (two bytes) is as follows: Start - Define temperature value variable - Call Read_1Byte() to read the lower 8 bits - Call Read_1Byte() to read the higher 8 bits - Merge values - Convert values, etc. - Return value;
[0121] A1.4.5 The startup process of the temperature conversion function for the #1 port sensor is as follows: Start — Call A1.4.1 to reset the IO port — Call A1.4.2 to write 1 byte — A1.4.2 writes 1 byte — Return;
[0122] A1.4.6 The function flow for reading the temperature value of the sensor at port 1 is as follows: Start — A1.4.1 Reset IO port — A1.4.2 Write 1 byte — A1.4.2 Write 1 byte — A1.4.4 Read value (two bytes) — Return value;
[0123] A1.4.7 Other #1 port subroutines: Other code—DQ1 and other statements—Other code;
[0124] A1.5, Partial program flow for the second sensor:
[0125] A1.5.1, Reset the #2 port sensor subroutine: Replace DQ1 with DQ2 in A1.4.1, and the rest is the same;
[0126] A1.5.2, Function flow for writing one byte to the #2 port sensor: Replace DQ1 with DQ2 in A1.4.2, and the rest is the same;
[0127] A1.5.3, Function flow for reading one byte from sensor #2: Replace DQ1 with DQ2 in A1.4.3, the rest is the same;
[0128] A1.5.4 The function flow for reading the sensor value (two bytes) of port 2 is as follows: In A1.4.4, DQ1 is replaced with DQ2, and the rest is the same;
[0129] The A1.5.5 startup function for the #2 port sensor temperature conversion is as follows: Start — Call A1.5.1 to reset the IO port — Call A1.5.2 to write 1 byte — A1.5.2 writes 1 byte — Return;
[0130] The function flow for reading the temperature value of the sensor at port #2 in A1.5.6 is as follows: Start — A1.5.1 Reset I / O port — A1.5.2 Write 1 byte — A1.5.2 Write 1 byte — A1.5.4 Read value (two bytes) — Return value;
[0131] A1.5.7, Other #2 I / O sensor subroutines: Other code—DQ2 and other statements—Other code;
[0132] A1.6, Partial program flow for the third sensor:
[0133] A1.6.1, Reset the #3 port sensor subroutine: Replace DQ1 with DQ3 in A1.4.1, and the rest is the same;
[0134] A1.6.2, Function flow for writing one byte to the #3 port sensor: Replace DQ1 with DQ3 in A1.4.2, and the rest is the same;
[0135] A1.5.3, Function flow for reading one byte from sensor #3: Replace DQ1 with DQ3 in A1.4.3, the rest is the same;
[0136] A1.6.4 The function flow for reading the sensor value (two bytes) of port 3 is as follows: In A1.4.4, DQ1 is replaced with DQ3, and the rest is the same;
[0137] The startup process of the temperature conversion function for the #3 port sensor is as follows: Start — Call A1.6.1 to reset the IO port — Call A1.6.2 to write 1 byte — A1.6.2 writes 1 byte — Return;
[0138] The function flow for reading the temperature value of the sensor at port #3 in A1.6.6 is as follows: Start — A1.6.1 Reset I / O port — A1.6.2 Write 1 byte — A1.6.2 Write 1 byte — A1.6.4 Read value (two bytes) — Return value;
[0139] A1.6.7 Other 3# port line sensor subroutines: Other code - DQ3 and other statements - Other code.
[0140] The above method is a common method. The code from A1.2 to A1.3 is the same as that from A1.1. The code from A1.5 to A1.6 is the same as that from A1.4 except for the I / O lines. The operation process and functions are completely similar, and the code is repeated.
[0141] Since method A would result in a lot of code duplication and bring many drawbacks, we first tested and used a C program switch case branch selection statement to call different I / O ports respectively, i.e., method B as follows.
[0142] B. Implementing I / O operations using functions via a C program's switch-case method.
[0143] B2.1. Use the preprocessor substitution command `define` to create I / O port definitions similar to functions:
[0144] #define P1(dioii)P1##dioii
[0145] ...(similar to above)
[0146] #define P6(dioii)P6##dioii
[0147] B2.2 Define two 8-bit global variables for port and I / O line bits:
[0148] This variable is used to temporarily store the I / O port number that will be used to operate on the temperature sensor. The port number is Port_x, and the bit number is Bit_n. For example, for P32, the Port_x value is 3, and the Bit_n value is 0x04. The program is as follows:
[0149] unsigned char Port_x, Bit_n;
[0150] B2.3 Define five parameterless functions for I / O line operations: a. Set an I / O line as an output function DQ_out(); b. Set an I / O line as an input function DQ_in(); c. Set the output to a high level (pull-high) function DQ1(); d. Set the output to a low level (pull-low) function DQ0(); e. Read the I / O line value function DQ_val(); "()" indicates a parameterless function in C program, the same applies below;
[0151] The function that sets the port to output is as follows:
[0152]
[0153] The other four functions must be defined because they are used for I / O line operations. Among them, the functions DQ_in(), DQ1(), DQ0(), and DQ_val() are replaced by Px(DIR)&=~Bit_n, Px(OUT)|=Bit_n, Px(OUT)&=~Bit_n, and return(Px(IN)&Bit_n) respectively. In these functions, x can be 1 to 6, and so on. If there are also operations such as function selection Px(SEL), they must be defined as well.
[0154] B2.4 Define a two-dimensional array table of different port constants for the three sensors, as follows:
[0155]
[0156]
[0157] B2.5 Define a port configuration function, using the function parameter Tn to change and save the global variable of the corresponding port (see B2.2), thereby specifying that subsequent port operations correspond to a specific port. The program is as follows:
[0158]
[0159] B2.6 Define three sensor operation functions: (1) Reset(); (2) Write_1Byte(unsigned char wdata); (3) Read_1Byte().
[0160] The reset function procedure is as follows:
[0161] Start — DQ_out() — DQ0() — Delay 600μs — DQ1() — Delay 30μs — DQ_in() — Null instruction — DQ_out() — DQ1() — Delay 400μs — Return;
[0162] The procedure for writing 1 byte to the sensor port line is as follows:
[0163] Start — Loop 8 times [DQ0() — Delay 6μs — If the last bit of the written number is 1, DQ1() / Otherwise, DQ0() — Shift the written number right by 1 bit — Delay 30μs — DQ1() — Delay 10μs] — Return;
[0164] The function flow for reading one byte from the sensor is as follows:
[0165] Start — Loop 8 times [Value right shift 1 bit — DQ0() — DQ1() — DQ_in() — If DQ_val() is 1, write 1 to the high bit of the value — DQ_out() — DQ1() — Delay 2μs] — Return value;
[0166] In the three function flows above, "()" represents a function, meaning that a function is called again in the flow; it can be seen that many functions are called, and the calling hierarchy increases;
[0167] B2.7 Define the function `signed int ReadTempe()` to read the sensor temperature value. The function flow is as follows:
[0168] Start—Define temperature value variable—Call Read_1Byte() to read the lower 8 bits—Call Read_1Byte() to read the higher 8 bits—Merge values—Convert to Celsius—Return;
[0169] B2.8 Define a function to start the temperature conversion of one of the sensors, where the parameter Tn represents the various sensors (0, 1, 2, etc.) in the sensor's port table. The program is as follows:
[0170]
[0171] B2.9 Define a function to read the temperature value of one of the sensors, where the parameter Tn represents the different sensors (0, 1, 2, etc.) in the sensor's port table. The program is as follows:
[0172]
[0173] Method C: Implement arbitrary I / O line operations of a microcontroller using C program pointers and function parameters.
[0174] C3.1 Define the instruction word for I / O port operations, as shown in the following program:
[0175]
[0176]
[0177] It should be noted that the above program assumes that the operation to be performed on any I / O port is one of five types: DQ_val retrieves the I / O port value, DQ_out sets it to output, DQ_in sets it to input, DQ1 outputs a high level (pull-high), and DQ0 outputs a low level (pull-low). This covers most I / O port operations. If there is a need for function selection (strobe) PxSEL, the corresponding definition can be added. In addition, there is no "=" sign in the first line of statements, and the parentheses "()" are required.
[0178] C3.2 Define two 8-bit port and I / O line global variables:
[0179] This variable is used to temporarily store the I / O port number to be operated on, with the port number as Port_x and the port bit number as Bit_n; for example, P31 corresponds to Port_x value of 3 and Bit_n value of 0x02; the program is as follows:
[0180] unsigned char Port_x, Bit_n;
[0181] C3.3. Using the I / O port address pointers defined in the MCU header file, create a one-dimensional array of pointer variables. Assuming all I / O ports of the MCU are P1 to P6, the program is as follows:
[0182]
[0183]
[0184] It should be noted that in the above program, if the MCU does not have a P6 port, the definition of P6 should be deleted; if it has a P7 port, the definition of P7 can be added; for different types of MCUs, the definition name of the I / O port function register address (pointer) can be obtained by consulting the MCU's header file; if PxSEL and other pointers are also needed, the corresponding pointer variable array definition can be added; if the MCU has a P0 port, the value of 0 of the 0th element in the array can be changed to (char*)P0 or a similar statement.
[0185] C3.4 Define a two-dimensional array table of the multiple I / O lines to be used. For example, if you need to use three I / O lines: P12, P31, and P50, the program would be as follows:
[0186]
[0187]
[0188] C3.5 Define an I / O line setting function, using the function parameter Tn to change and save the global variable corresponding to the I / O line (defined in C3.2), thus specifying that subsequent I / O line operations should all correspond to a specific I / O line. The program is as follows:
[0189]
[0190] C3.6 Define the sub-functions required to operate peripheral devices:
[0191] Taking a single-bus sensor as an example, assume there are 3 sensors connected to the IO ports defined in C3.4; each sensor will use 3 basic sub-functions, namely: (1) Reset operation function Reset(); (2) Write_1Byte(unsigned char wdata) function to write a byte; (3) Read_1Byte() function to read a byte from the sensor;
[0192] C3.6.1, the reset function flow:
[0193] Start — DQ_out — DQ0 — Delay 600μs — DQ1 — Delay 30μs — DQ_in — Null instruction — DQ_out — DQ1 — Delay 400μs — Return;
[0194] It should be noted that, compared to method B, the operation of the I / O port no longer involves calling a parameterless function and then selecting a branch before operation; instead, it directly uses the I / O port address pointer, resulting in extremely fast program execution speed; the same applies below.
[0195] C3.6.2, the function flow for writing 1 byte to the sensor port:
[0196] Start — Loop 8 times [DQ0 — Delay 6μs — If the last bit of the written number is 1, DQ1 / Otherwise, DQ0 — Shift the written number right by 1 bit — Delay 30μs — DQ1 — Delay 10μs] — Return;
[0197] C3.6.3, the function flow for reading one byte from the sensor:
[0198] Start — Loop 8 times [Value right shift 1 bit — DQ0 — DQ1 — DQ_in — If DQ_val is 1, write 1 to the high bit of the value — DQ_out — DQ1 — Delay 2μs] — Return value;
[0199] C3.7 Define a function `signed int ReadTempe()` to read values (two bytes) from a peripheral device. The function flow is as follows:
[0200] Start—Define temperature value variable—Call Read_1Byte() to read the lower 8 bits—Call Read_1Byte() to read the higher 8 bits—Merge values—Convert values, etc.—Return;
[0201] C3.8 Define a parameterized function to start the temperature conversion of one of the sensors. The parameter Tn represents the various sensors (0, 1, 2, etc.) in the sensor's port table. The program is as follows:
[0202]
[0203] C3.9 Define a parameterized function to read the temperature value of a specific sensor, where the parameter Tn represents the various sensors (0, 1, 2, etc.) in the sensor's port table. The program is as follows:
[0204]
[0205] At this point, the program for various operations on the three I / O ports has been implemented.
[0206] Through experimental comparison, it can be concluded that the shortcomings and deficiencies of methods A and B include:
[0207] 1. In the commonly used method A, increasing the number of I / O ports to be operated will increase a lot of repetitive code, occupy a lot of storage space, and is very detrimental to the debugging and maintenance of similar programs.
[0208] 2. In common method A, if the serial data waveform is distorted due to improper selection of cable material or type, or extension of communication distance in IO port communication, resulting in communication data errors, then if it is necessary to adjust the communication timing (such as delay parameters), it is necessary to adjust multiple subroutines (or functions) of different IO ports, which greatly increases the debugging workload.
[0209] 3. In innovative method B, the switch-case branch statement is used to operate on different I / O ports. Although this allows for the operation of different I / O lines using function parameters and avoids repeating the same type of code, the increased number of function calls and branch selections leads to an increase in the amount of code executed, a slower system speed, and a longer execution time, resulting in a slower overall system response. In applications with strict timing requirements, this method often leads to timing logic errors in peripheral devices or serial communication data errors. In a practical application using a digital temperature sensor for temperature measurement, it was found that this method misses a 15μs reading interval, i.e., a communication timing error, which ultimately leads to data errors.
[0210] Furthermore, based on the method described in this scheme and the comparative experiments described above, it is worth noting that:
[0211] 1. The core of this solution is the method of operating (including accessing, controlling, and strobing) I / O lines using C program pointer definition tables and parameterized functions;
[0212] 2. When operating one or more I / O ports, if the entire bus is operated using bus values (0x00 to 0xFF) when using the same bus I / O port, it means that the method of this solution is not being used; if the combination of pointer definition table and parameterized function is still used, it means that the method of this solution is being used.
[0213] 3. Using pointers to pointers, specifically, is a method of redefining the I / O port operation function using a single pointer or a pointer table that points to the I / O port address;
[0214] 4. The example programs and flowcharts in this method are all for illustrating the program logic and demonstrating the innovation of this method. Those skilled in the art should know that the variable names, function names, subroutine names, etc. of the program can be arbitrarily named according to personal habits and preferences. Changing these names does not mean that the method has not been used.
[0215] like Figure 4 As shown, this embodiment also provides a method running platform apparatus, which includes a processor, a memory, and a computer program stored in the memory and running on the processor.
[0216] The processor includes one or more processing cores. The processor is connected to the memory via a bus. The memory is used to store program instructions. When the processor executes the program instructions in the memory, it implements the steps of the above-mentioned method of using C program pointers and function parameters to implement arbitrary I / O port line operations of a microcontroller.
[0217] Optionally, the memory can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory (FLASH ROM), ferroelectric memory (FRAM), etc.
[0218] In addition, the present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the above-described method for operating arbitrary I / O lines of a microcontroller using a C program pointer and function parameters.
[0219] Optionally, the invention also provides a computer program product containing instructions that, when run on a computer, causes the computer to execute the steps of the method described above for implementing arbitrary I / O port line operations of a microcontroller using the C program pointer and function parameters.
[0220] Those skilled in the art will understand that the process of implementing all or part of the steps of the above embodiments can be carried out by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory or an erasable memory.
[0221] The foregoing has shown and described the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The embodiments and descriptions in the specification are merely preferred examples and are not intended to limit the invention. Various changes and modifications can be made to the invention without departing from its spirit and scope, and all such changes and modifications fall within the scope of the present invention as claimed. The scope of protection of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for implementing arbitrary I / O port line operations of a microcontroller using C program pointers and function parameters, based on pointers to pointers in the C programming language and parameterized functions to access, control, and select the required I / O port lines, characterized by: The number of pointer variables in the array corresponds to the number of I / O port function register addresses to be increased according to the number of I / O port operation functions required. For microcontrollers with only ports P1 to Px, the 0th element of the pointer array has a value of 0, the 1st element corresponds to the function register address of port P1 and the element value is forcibly converted to a pointer type, and the xth element corresponds to the pointer type conversion value of the function address of port x. Before using I / O port line operations, the port number and port line bits are set first. In subsequent I / O port operations, I / O port line operation statements are converted into assigning or retrieving values to the pointer variable elements corresponding to the port numbers in the pointer variable array. Specifically, the steps include the following: Step 1: Create an array of pointer variables that point to the address pointers of the I / O port function registers; Step 2: Set up a two-dimensional constant array that corresponds to the port number and port line position; Step 3: Before operating on the microcontroller's I / O lines, use a parameterized I / O setting function to retrieve the port number value and the line bit value to be operated and save them in two 8-bit global variables. Step 4: Write the I / O line operation statements to assign or retrieve values to the corresponding pointer variable elements in the pointer variable array from Step 1; the specific method is as follows: When assigning a value to the pointer element corresponding to the port, assigning a value of 1 only applies to the port line bit "OR 1", and assigning a value of 0 only applies to the port line bit "AND 0". When reading the port value, the pointer element corresponding to the port is transformed by ANDing the port bit with 1 and then incremented by "()".
2. The method for implementing arbitrary I / O line operations of a microcontroller using C program pointers and function parameters according to claim 1, characterized in that, In step 1, the specific method for creating the array of pointer variables pointing to the I / O port address is as follows: After forcibly converting the I / O port function address definition word defined in the selected microcontroller C program header file to a pointer, a one-dimensional pointer variable array is created. The order of the I / O port function pointer elements stored in the pointer array corresponds to the port number, that is, the subscript of the array element is equal to the microcontroller port number, so as to realize that the operation on different ports is converted into the assignment of the pointer variable array elements.
3. The method for implementing arbitrary I / O port line operations of a microcontroller using C program pointers and function parameters according to claim 1, characterized in that, In step 2, the method for setting the two-dimensional constant array corresponding to the port number and port line positions is as follows: Use an unsigned character variable from 0 to m to represent port lines 0 to m respectively; the constant array subscripts [0][0] and [0][1] elements store the port number and port line bit of port line 0 respectively, and the elements [m][0] and [m][1] elements store the port number and port line bit of port line m respectively; Among them, port numbers P0 to P7 are represented by 0 to 7 respectively, and port line bit n is written as the number of single bytes in which the port line bit is "1" and the others are "0", or as "1 left shifted n bits".
4. The method for implementing arbitrary I / O port line operations of a microcontroller using C program pointers and function parameters according to claim 3, characterized in that: In step 2, a two-dimensional constant array is set up to correspond to the port number and the I / O line bit. This step is not mandatory. If this step and the constant array are omitted, it will be inconvenient to set up and move the I / O lines, it will be impossible to traverse the I / O lines, and it will be impossible to use loop statements to operate on the I / O lines in batches. Furthermore, in step 3, the port number and I / O line bit to be operated on must be specified one by one through selection or judgment statements.
5. The method for implementing arbitrary I / O port operations of a microcontroller using C program pointers and function parameters according to claim 3, characterized in that: In step 3, before operating on the microcontroller's I / O lines, an I / O line setting function with parameters is used. The function takes the port number value and the line bit value from the two-dimensional constant array set in step 2 and writes them into two 8-bit global variables for storage, thereby specifying that subsequent line operations correspond to a certain line. The parameters of the I / O line setting function are selected from 0 to m in step 2.
6. The method for implementing arbitrary I / O port line operations of a microcontroller using C program pointers and function parameters according to claim 2, characterized in that, In step 1, the specific method for creating an array of pointer variables pointing to I / O port addresses further includes: If this MCU does not have a P6 port, then delete the P6 definition; If port P7 exists, then define port P7 accordingly. If the MCU has a P0 port, the value of 0 in the 0th element of the array should be changed to the forced pointer type conversion value of the corresponding P0 port function register address; The definition names of the I / O port function register addresses for different types of MCUs are obtained from the header file corresponding to the MCU. If the same operation needs to be performed on other port function registers, an array of pointer variables pointing to the address of that function register should be added accordingly.
7. The method for implementing arbitrary I / O port line operations of a microcontroller using C program pointers and function parameters according to claim 4, characterized in that, In step 3, the port number value and port line bit value to be operated are retrieved and written into two 8-bit global variables for storage. These two 8-bit global variables are replaced by pointers to constant arrays. Furthermore, the parameterized IO port setting function used before operating the microcontroller IO port lines in step 3 needs to be changed to a jump to the constant array pointer in step 2.
Citation Information
Patent Citations
Static test method and device for misquotation of inner variables by outer pointers
CN102855183A
Single-chip microcomputer simulation method, system, device and equipment and storage medium
CN113064833A