Home > other >  I am a STM32 small white, recently had a question about serial port receiving interrupt.
I am a STM32 small white, recently had a question about serial port receiving interrupt.

Time:09-16

[/code] the younger brother with reference to the accurate atomic tutorial USART serial port receiving interrupt function, but why can't realize the upper machine to send, receive interrupt, sent back to the upper machine, dear brother a great god help the younger brother look at the code,

Void my_usart1_Init ()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//can make GPIO clock, Usart clock
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);


//initialize PA10 TXD
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;//multiplexing output
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_10;//10
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_10MHz;//speed 10 MHZ

GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

//initialize PA9 RXD
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//floated input
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_9;//9
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_10MHz;//speed 10 MHZ
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

//reset a serial port configuration
USART_DeInit (USART1);
//serial port initialization
USART_InitStructure. USART_BaudRate=115200;//baud rate
USART_InitStructure. USART_HardwareFlowControl=USART_HardwareFlowControl_None;//no hardware control flow
USART_InitStructure. USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//receive delivery mode
USART_InitStructure. USART_Parity=USART_Parity_No;//no base parity bit
USART_InitStructure. USART_StopBits=USART_StopBits_1;//1 stop bit
USART_InitStructure. USART_WordLength=USART_WordLength_8b;//data eight word length

USART_Init (USART1, & amp; USART_InitStructure);

//can make a serial port
USART_Cmd (USART1, ENABLE);

/* * * * * * * * * * * * * * * * * * * to a serial port as an interrupt source initialization serial port interrupt * * * * * * * * * * * * * * * * * * * * * * * * * */
//set the serial port receiving interrupt
USART_ITConfig (USART1, USART_IT_RXNE, ENABLE);
//the interrupt priority group
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//set the interrupt priority initialization
NVIC_InitStructure. NVIC_IRQChannel=USART1_IRQn;//a serial port interrupt channel
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;//enabled interrupt
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=0 x00;//set the preemption priority
NVIC_InitStructure. NVIC_IRQChannelSubPriority=0 x00;//set the corresponding priority

NVIC_Init (& amp; NVIC_InitStructure);
}
/* */write interrupt service function
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* @ 1, when the microcontroller receives the data, then the interrupt
* @ 2, manage interrupt vector interrupt controller allocation priority and preemption priority response
* @ 3, into the interrupt service function,
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void USART1_IRQHandler (void)
{
Uint8_t shuju;
If (USART_GetITStatus (USART1, USART_IT_RXNE)==SET)//reading register is not empty, RXNE position 1, MCU can read data,
{
Shuju=USART_ReceiveData (USART1);//the data assigned to the variable date
}
USART_SendData (USART1, shuju);
}

//* * * * * * * * * * * * * * * * * * the main function * * * * * * * * * * * * * *//
Int main (void)
{
Led_Init ();
Delay_init ();
My_usart1_Init ();
While (1)
{
Led0 (off);
Led1 (on);
Delay_ms (100);
Led0 (on);
Led1 (off);
Delay_ms (100);
}
}
PC interface





Younger brother great god waiting for you answer, thank die,

CodePudding user response:

See blog https://blog.csdn.net/u014694105/article/details/105215091, direct copy and paste the code inside a serial port to send and receive can be realized

CodePudding user response:

RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO, ENABLE); May be less this way, your assistant serial port to send only one bytes at a time should be no problem, you can send a string will be out of the question, why do you want to send data in receiving interrupt, interrupt inside some time-consuming long operation is not permitted, see more atoms is how to deal with accept function, come on,

CodePudding user response:

Originally I pin initialization was wrong, he this is not to need to make to AFIO clock

CodePudding user response:

Originally I pin initialization was wrong, he this is not to need to make to AFIO clock

CodePudding user response:

Watched the rough, probably because in receiving interrupt when there is no clear the status bit 0
If (USART_GetITStatus (USART1, USART_IT_RXNE)==SET)//reading register is not empty, RXNE position 1, MCU can read data,
{
USART_ClearFlag (USART1, USART_IT_RXNE);
Shuju=USART_ReceiveData (USART1);//the data assigned to the variable date

USART_SendData (USART1, shuju);
}
So give it a try

CodePudding user response:

I originally is to initialize pin went wrong,

CodePudding user response:

reference 5 floor childe chu reply:
watched the rough, probably because in receiving interrupt when there is no clear the status bit 0
If (USART_GetITStatus (USART1, USART_IT_RXNE)==SET)//reading register is not empty, RXNE position 1, MCU can read data,
{
USART_ClearFlag (USART1, USART_IT_RXNE);
Shuju=USART_ReceiveData (USART1);//the data assigned to the variable date

USART_SendData (USART1, shuju);
}
So try

I originally is to initialize pin went wrong,

CodePudding user response:

reference 1st floor koukou_1538725213 response:
https://blog.csdn.net/u014694105/article/details/105215091 blog, direct copy and paste code can be realized in a serial port to send and receive

Learning can't reproduce it

CodePudding user response:

9 is the tx, 10 is rx watch a while to find other should have no problem

CodePudding user response:

reference link, 9/f, red moon response:
9 is the tx, 10 is rx should have no problem a while to find other

nullnullnullnullnullnullnull
  • Related