First, let's summarize the serial ports 232, 422, and 485.
Serial port 232: bidirectional transmission, full-duplex, maximum rate 20Kbps, negative logic level, -15V~-3V logic "1", 3V~15V logic "0".
Serial port 422: bidirectional transmission, 4-wire full-duplex, 2-wire simplex.
Serial port 485: bidirectional transmission, 4-wire full-duplex, 2-wire simplex, maximum rate 10Mb/s, differential signal, sending end: 2V~6V logic "1", -2V~-6V logic "0", receiving end: 200mV logic "1", -200mV logic "0".
There are two solutions for the implementation of the serial port:
Solution 1, like Atom's "Example of STM32", first receive, then process, without message verification, so that message overwrite will occur, the system will freeze after message error, commands cannot be clearly distinguished, and handshake signals cannot be responded to in time. Solution 2, borrowing from the message queue of uC/OSII, after entering the interrupt service function, turn off the interrupt, receive data, if no data is received, wait for a period of time (time and baud rate related) before turning on the interrupt, and then process the received data. See the code below:
Message queue and its initialization function:
In this way, all the data is stored at once. The rest is to process the messages in the message buffer message_buff[]. This solves the problem of message overwriting and uncorrectable message errors. As for how to process the messages, it depends on different needs. Also note that handshake signals use timer interrupts.
## Basic operations of STM32 IO ports##
1. Initialize the structure
Let's first look at the GPIO_InitTypeDef structure. The source code is as follows
The structure contains GPIO_Pin, GPIO_Speed and GPIO_Mode information.
GPIO_Pin has a macro definition in stm32f10x_gpio.h
It just corresponds to 16 ports.
GPIO_Speed is an enumeration type structure, which has macro definitions in stm32f10x_gpio.h:
When the GPIO port of STM32 is set to output mode, there are three speeds to choose from: 2MHz, 10MHz and 50MHz. This speed refers to the speed of the I/O port driver circuit and is used to select different output driver modules to achieve optimal noise control and reduce power consumption.
High-frequency drive circuits also have high noise. When you do not need a high output frequency, please choose a low-frequency drive circuit, which is very helpful in improving the EMI performance of the system.
Of course, if you want to output a higher frequency signal but choose a lower frequency driver module, you will most likely get a distorted output signal.
The key is to match the GPIO pin speed to the application.
For example, for the serial port, if the maximum baud rate is only 115.2k, then a 2M GPIO pin speed is sufficient, which saves power and reduces noise.
For the I2C interface, if you use a 400k baud rate and want to leave more margin, the 2M GPIO pin speed may not be enough. In this case, you can use a 10M GPIO pin speed.
For the SPI interface, if you use 18M or 9M baud rate, the 10M GPIO pin speed is obviously not enough, and you need to use a 50M GPIO pin speed.
GPIO_Mode is also an enumerated structure:
2. Set the system clock
The peripheral clock must be turned on before using the port. Before turning on the peripheral clock, we must first configure the system clock. The system clock setting is mainly completed in the library function SystemInit(). There is the following code in the startup file startup_stm32f10x_md.s:
Before calling the main function, you must first call the SystemInit() function, which is defined in system_stm32f10x.c. The function prototype is
The main function of this function is to reset all the registers related to the configuration clock to the default values and call
the SetSysClock(void) function.
The macro definition of SYSCLK_FREQ is given at the beginning of the system_stm32f10x.c file. The default system clock is 72M, of course, the prerequisite is an external 8M crystal oscillator.
3. Enable the peripheral clock
Turning on or off the peripheral clock is mainly set by the following functions
Different peripherals call different functions. If the io pin multiplexing function is used, in addition to turning on the io function clock, you also need to turn on the multiplexing function clock. For example, Pin4 of GPIOC can also be used as the input pin of ADC1. When it is used as ADC1, in addition to turning on the GPIOC clock, you also need to turn on the ADC1 clock.
For other operation functions, see the library function manual.
丨The article is organized to spread relevant technologies, the copyright belongs to the original author丨
丨If there is any infringement, please contact us to delete丨
|