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 realizedCodePudding 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 clockCodePudding user response:
Originally I pin initialization was wrong, he this is not to need to make to AFIO clockCodePudding user response:
Watched the rough, probably because in receiving interrupt when there is no clear the status bit 0If (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: