Question
USART judges that the input character has no response
#include "stm32f4xx.h"
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void My_Usart2_Printf(char *string){
while(*string){
USART_SendData(USART2, (unsigned short int) *string++);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
}
int Uart_Init(){
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
NVIC_EnableIRQ(USART2_IRQn);
}
int main(){
Uart_Init();
while (1){
}
}
/* Determine if the 2 string arrays (Char Array)
are the same Same return, 1, not the same return 0 */
int charArrayEquals(char a [], char b []){
int as = strlen(a);
int bs = strlen(b);
int i = 0;
if(as != bs){
return 0;
}
for(i = 0; i < as; i++){
if(a [i] != b [i]){
return 0;
}
}
return 1;
}
char buff [] = "";
/*
* USART2 Interrupt function
*/
void USART2_IRQHandler(){
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
char c = USART_ReceiveData(USART2); //Received characters
char check [] = "Hello"; // String array to be judged
if(c != '\r' && c != '\n'){
// Add the received characters to the end of the buff
sprintf (buff, "%s%c", buff,c);
}else{
My_Usart2_Printf(buff); // Return the message entered by the user
if(charArrayEquals(buff, check)){ // Determine if the buff is the same as the check string array
My_Usart2_Printf(" ... OK\n"); // If the input is Hello, it will display OK.
}else{
My_Usart2_Printf("795222\n"); // If the input is not Hello, the line is directly wrapped.
}
memset(buff, 0, strlen(buff)); // Empty the buff string
}
}
}