Home > other >  Stm32f10x serial port to send and receive
Stm32f10x serial port to send and receive

Time:10-06

# include "stm32f10x. H"




Void My_USART1_Init (void)
{
GPIO_InitTypeDef GPIO_InitStrue;//define I/o interface structure
USART_InitTypeDef USART_InitStrue;//define serial correlation structure
NVIC_InitTypeDef NVIC_InitStrue;//define interrupt structure

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//initialize GPIOA clock
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, ENABLE);//initialize serial port USART clock

GPIO_InitStrue. GPIO_Mode=GPIO_Mode_AF_PP;//push-pull multiplexing output
GPIO_InitStrue. GPIO_Pin=GPIO_Pin_9;//open 9 IO mouth
GPIO_InitStrue. GPIO_Speed=GPIO_Speed_10MHz;//port speed 10 MHZ
GPIO_Init (GPIOA, & amp; GPIO_InitStrue);//GPIOA9 initialization

GPIO_InitStrue. GPIO_Mode=GPIO_Mode_IN_FLOATING;//floated input
GPIO_InitStrue. GPIO_Pin=GPIO_Pin_10;//IO mouth open 10
GPIO_InitStrue. GPIO_Speed=GPIO_Speed_10MHz;//port speed 10 MHZ
GPIO_Init (GPIOA, & amp; GPIO_InitStrue);//GPIO10 initialization

USART_InitStrue. USART_BaudRate=115200;//serial port baud rate setting
USART_InitStrue. USART_HardwareFlowControl=USART_HardwareFlowControl_None;//no hardware flow
USART_InitStrue. USART_Mode=USART_Mode_Tx | USART_Mode_Rx;//sending, receiving enabled
USART_InitStrue. USART_Parity=USART_Parity_No;//and white parity
USART_InitStrue. USART_StopBits=USART_StopBits_1;//stop bit is 1
USART_InitStrue. USART_WordLength=USART_WordLength_8b;//word length 8
USART_Init (USART1, & amp; USART_InitStrue);//USART1 initialization
USART_Cmd (USART1, ENABLE);//can make a serial port 1

USART_ITConfig (USART1, USART_IT_RXNE, ENABLE);//interrupt configuration: open to receive interrupt (accept the cache is not empty enabled, ENABLE)
NVIC_InitStrue. NVIC_IRQChannel=USART1_IRQn;//define interrupt channel
NVIC_InitStrue. NVIC_IRQChannelCmd=ENABLE;//open the interrupt channel
NVIC_InitStrue. NVIC_IRQChannelPreemptionPriority=1;//set the preemption priority 1
NVIC_InitStrue. NVIC_IRQChannelSubPriority=1;//set the son priority 1
NVIC_Init (& amp; NVIC_InitStrue);//the interrupt initialization
}

Void USART1_IRQHandler (void)//write interrupt handlers
{
U8 res;//unsigned characters res

If (USART_GetITStatus (USART1, USART_IT_RXNE))//receiving data into the interrupt, determine a serial port 1 receive buffer is not empty make for 1 or not
{
Res=USART_ReceiveData (USART1);//to 1, data to the variables of a serial port 1
USART_SendData (USART1, res);//continue to see this data from USART1 send out
}
}

Int main (void)
{
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);//set the priority group of 2, 2 a grab two response
My_USART1_Init ();//serial port function called
While (1);//to death cycle, there is a data into the interrupt data sent over again read the main program

}

CodePudding user response:

When you configure a serial port should open the reuse clock function

CodePudding user response:

Receiving, after the completion of a flag bit in the set, and then put the received data in the main loop to send out

CodePudding user response:

Problem phenomenon? A serial port to send and receive does not need to open the reuse the clock because the reusability push-pull output is set
  • Related