STM32Cube can save a lot of time for the development of new projects, freeing you from the tedious chip initialization
1. STM32 UART initialization part, configure the project, use STM32cube to generate code, and the initialization is completed.
2. The serial port interrupt mode is triggered: HAL_UART_Transmit_IT
Without any processing, we use printf in the program to print information to the display screen. At this time, we need to redirect printf to print the printf message to the serial port. Add the following function in the main function:
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit_IT(&huart1, (uint8_t *)&ch, 1);
osDelay(1);
return 0;
}
//The redirection is completed, and executing printf can send characters of any length to the serial port.
3. Receive action of serial port interrupt: HAL_UART_Receive_IT
HAL_UART_Receive_IT(&huart1,(uint8_t *)&value,1);//The program will not stop at this statement, and will directly store the received data in value according to the interrupt method. However, this statement can only enable the serial port interrupt once. Therefore, it is necessary to re-enable it in the interrupt service function or callback function.
Code:
// Initialize the receive buffer
#define MAX_RECV_LEN 1024
uint8_t msg_buff[MAX_RECV_LEN] = {0};
uint8_t * msg = msg_buff;
static int flag = false;
//Receive interrupt callback function
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
uint8_t right = HAL_OK;
msg++;
if( msg == msg_buff + MAX_RECV_LEN)
{
msg = msg_buff;
}
do
{
ret = HAL_UART_Receive_IT(UartHandle,(uint8_t *)msg,1);
}while(ret != HAL_OK);
if(*(msg-1) == '\n') //Receive a string ending with \n, which means the reception is complete
{
flag = true;
}
}
void uart_main(void)
{
extern UART_HandleTypeDef huart1;
HAL_UART_Receive_IT(&huart1, (uint8_t *)msg, 1); // Enable the first interrupt
while(1)
{
if (flag == true)
{
printf("msg_buff = %s\r\n",msg_buff);
memset(msg_buff, 0, sizeof(msg_buff)); //Clear the buffer
// Point to the head of the receive buffer
msg = msg_buff;
(&huart1)->pRxBuffPtr = msg;
flag = false;
}
osDelay(10);
}
}
Previous article:STM32 Interrupts and Events --- An example of using GPIO as an external interrupt
Next article:HAL library-STM32F4 external interrupt-delay
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Design of automatic fingerprint recognition system using TMS320C5416 processor and P89C52 single chip microcomputer
- Weak DC voltage signal amplification
- A large number of books have been released after graduation, including C/C++, Linux, algorithms, and other books. They are on sale now. Only 3 days left
- Develop a moon lamp using Gizwits SOC solution
- About connector model 503175-0604
- [AB32VG1 Development Board Review] Non-discriminative color conversion processing
- IWR1642Boost people counting DEMO program operation process
- I would like to ask the experts to introduce the generation of 2HZ in detail. Now the buzzer circuit 2HZ does not work! ! ! ! ! !
- Why doesn't the CPU top cover use silver, which has better heat dissipation?
- Summary of low power consumption STM32L151+RTC wake-up application