STM32 serial interrupt instance two

int main(void )

{
uint8_t a
=0;//LED high and low voltage control

/* System Clocks Configuration */
RCC_Configuration();
//System clock settings
/*Nested vector interrupt controller
Explains that USART1 preempts priority level 0 (up to 1 digit), and sub-priority level 0 (up to 7 digits)
*/
NVIC_Configuration();
//Interrupt source configuration
/* initializes the IO port that controls the LED indicator, and configures the port as Push-pull pull-up output, the port line speed is 50Mhz. PA9 and PA10 ports are multiplexed as TX and RX of serial port 1.
When configuring a port line, first enable the clock of the port where it is located. Otherwise, the configuration cannot be successful. Because port B is used, the clock of this port
Enable, and at the same time, because the multiplexed IO port function is used, it is used to configure the serial port. Therefore, the AFIO (multiplexed function IO) clock must also be enabled.
*/
GPIO_Configuration();
//Port initialization
USART_Config(USART1); //Serial port 1 initialization

while (1)
{
if(rec_f == 1)
{
// Determine whether a frame of valid data has been received
rec_f = 0;

for(i=0; i<sizeof(TxBuffer1); i++) //Send string
{
USART_SendChar(USART1,TxBuffer1[i]);
Delay(
0x0000ff0);
}
/*for(i=0;i {
USART_SendChar(USART1,RxBuffer1[i]);
}
*/
if(a==0)
{
GPIO_SetBits(GPIOD, GPIO_Pin_2);
//LED1 flashes bright and dark span>
a=1;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
a
=0;
}
}
}
}

The main function is as above.

Related variables

uint8_t TxBuffer1[] = "USART Interrupt Example: This is USART1 DEMO";

uint8_t RxBuffer1[], rec_f, tx_flag, i;

__IO uint8_t TxCounter1
= 0x00;
__IO uint8_t RxCounter1
= 0x00;

uint32_t Rec_Len;

The serial port interrupt function configuration is as follows:

//------------------------- -------------------------------------------------- -----------

void USART_SendChar(USART_TypeDef* USARTx,uint8_t data)
{
USART_SendData(USARTx,data);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);
}
//------------ -------------------------------------------------- ------------------------
void USART_Config(USART_TypeDef* USARTx)
{
USART_InitStructure.USART_BaudRate
= 19200; //The rate is 19200bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8 data bits
USART_InitStructure.USART_StopBits = USART_StopBits_1; //Stop bit 1 bit
USART_InitStructure.USART_Parity = USART_Parity_No; //No parity
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //transceiver mode

/* Configure USARTx */
USART_Init(USARTx,
&USART_InitStructure); //Configure serial port parameter function< /span>


/* Enable USARTx Receive and Transmit interrupts */
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
//Enable reception Interrupted
USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); //Enable transmit buffer empty interrupt< /span>

/* Enable the USARTx */
USART_Cmd(USARTx, ENABLE);
}
//------------ -------------------------------------------------- ------------------------
void Delay(__IO uint32_t nCount)
{
for(; nCount!=0; nCount- -);
}
/*------------ -------------------------------------------------- ------------------------
The system clock is configured as 72MHZ+ peripheral clock configuration
*/
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1
| RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOA, ENABLE);
}
//------------ -------------------------------------------------- ------------------------
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_2; //LED1 control--PD2< /span>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOD,
&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_9; //USART1 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //multiplexed push-pull output
GPIO_Init(GPIOA, &GPIO_InitStructure); //A port

GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_10; //USART1 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //multiplexed open drain input
GPIO_Init(GPIOA, &GPIO_InitStructure); //A port
}
//------------ -------------------------------------------------- ------------------------
void NVIC_Configuration(void)
{
/* Structure declaration*/
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure the NVIC Preemption Priority Bits */
/* Configure one bit for preemption priority */
/* The priority group describes the number of bits used to preempt the priority, and the sub-priority The number of digits used here is 1, 7 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel
= USART1_IRQn; //Set serial port 1 interrupt= USART1_IRQn; span>
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //The child priority is 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable
NVIC_Init(&NVIC_InitStructure);
}

Write the usart function in the interrupt service function.

//Serial port 1 interrupt service routine 

void USART1_IRQHandler(void)
{
unsigned
int i;

// Determine whether the read register is not empty
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
//Cache the read register data in the receive buffer< /span>
RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);

// Determine whether the end flag is 0x0d 0x0a
if(RxBuffer1[RxCounter1-2] == 0x0d && RxBuffer1[RxCounter1-1] == 0x0a)
{
for(i=0; i< RxCounter1; i++)
TxBuffer1[i]
= RxBuffer1[i]; //will receive The data in the buffer is transferred to the sending buffer, ready to be forwarded

//receive success sign
rec_f = 1;

//Send buffer terminator
TxBuffer1[RxCounter1] = 0;
TxCounter1
= RxCounter1;
RxCounter1
= 0;
}
}

//This paragraph is to avoid the first byte of STM32 USART BUG that can’t be posted
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
//It is forbidden to send buffer empty interrupts,
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}

}

The operation result is as follows, do not fill in any characters when sending, send it directly, and display RT Interrupt Example: This is USART1 DEMO, indicating that the first three characters have been occupied and replaced.

Share pictures

Experimental platform alieneck mini stm32V1.9 stm32f103rbt6

int main( void)

{
uint8_t a
=0;//LED high and low voltage control

/* System Clocks Configuration */
RCC_Configuration();
//System clock settings
/*Nested vector interrupt controller
Explains that USART1 preempts priority level 0 (up to 1 digit), and sub-priority level 0 (up to 7 digits)
*/
NVIC_Configuration();
//Interrupt source configuration
/* initializes the IO port that controls the LED indicator, and configures the port as Push-pull pull-up output, the port line speed is 50Mhz. PA9 and PA10 ports are multiplexed as TX and RX of serial port 1.
When configuring a port line, first enable the clock of the port where it is located. Otherwise, the configuration cannot be successful. Because port B is used, the clock of this port
Enable, and at the same time, because the multiplexed IO port function is used, it is used to configure the serial port. Therefore, the AFIO (multiplexed function IO) clock must also be enabled.
*/
GPIO_Configuration();
//Port initialization
USART_Config(USART1); //Serial port 1 initialization

while (1)
{
if(rec_f == 1)
{
// Determine whether a frame of valid data has been received
rec_f = 0;

for(i=0; i<sizeof(TxBuffer1); i++) //Send string
{
USART_SendChar(USART1,TxBuffer1[i]);
Delay(
0x0000ff0);
}
/*for(i=0;i {
USART_SendChar(USART1,RxBuffer1[i]);
}
*/
if(a==0)
{
GPIO_SetBits(GPIOD, GPIO_Pin_2);
//LED1 flashes bright and dark span>
a=1;
}
else
{
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
a
=0;
}
}
}
}

uint8_t TxBuffer1[] = "USART Interrupt Example: This is USART1 DEMO";

uint8_t RxBuffer1[], rec_f, tx_flag, i;

__IO uint8_t TxCounter1
= 0x00;
__IO uint8_t RxCounter1
= 0x00;

uint32_t Rec_Len;

//--------------------------------------------- -----------------------------------------

void USART_SendChar(USART_TypeDef* USARTx,uint8_t data)
{
USART_SendData(USARTx,data);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);
}
//------------ -------------------------------------------------- ------------------------
void USART_Config(USART_TypeDef* USARTx)
{
USART_InitStructure.USART_BaudRate
= 19200; //The rate is 19200bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8 data bits
USART_InitStructure.USART_StopBits = USART_StopBits_1; //Stop bit 1 bit
USART_InitStructure.USART_Parity = USART_Parity_No; //No parity
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //transceiver mode

/* Configure USARTx */
USART_Init(USARTx,
&USART_InitStructure); //Configure serial port parameter function< /span>


/* Enable USARTx Receive and Transmit interrupts */
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
//Enable reception Interrupted
USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); //Enable transmit buffer empty interrupt< /span>

/* Enable the USARTx */
USART_Cmd(USARTx, ENABLE);
}
//------------ -------------------------------------------------- ------------------------
void Delay(__IO uint32_t nCount)
{
for(; nCount!=0; nCount- -);
}
/*------------ -------------------------------------------------- ------------------------
The system clock is configured as 72MHZ+ peripheral clock configuration
*/
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1
| RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOA, ENABLE);
}
//------------ -------------------------------------------------- ------------------------
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_2; //LED1 control--PD2< /span>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOD,
&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_9; //USART1 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //multiplexed push-pull output
GPIO_Init(GPIOA, &GPIO_InitStructure); //A port

GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_10; //USART1 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //multiplexed open drain input
GPIO_Init(GPIOA, &GPIO_InitStructure); //A port
}
//------------ -------------------------------------------------- ------------------------
void NVIC_Configuration(void)
{
/* Structure declaration*/
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure the NVIC Preemption Priority Bits */
/* Configure one bit for preemption priority */
/* The priority group describes the number of bits used to preempt the priority, and the sub-priority The number of digits used here is 1, 7 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel
= USART1_IRQn; //Set serial port 1 interrupt= USART1_IRQn; span>
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //The child priority is 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable
NVIC_Init(&NVIC_InitStructure);
}

//Serial port 1 interrupt service routine

void USART1_IRQHandler(void)
{
unsigned
int i;

// Determine whether the read register is not empty
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
//Cache the read register data in the receive buffer< /span>
RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);

// Determine whether the end flag is 0x0d 0x0a
if(RxBuffer1[RxCounter1-2] == 0x0d && RxBuffer1[RxCounter1-1] == 0x0a)
{
for(i=0; i< RxCounter1; i++)
TxBuffer1[i]
= RxBuffer1[i]; //will receive The data in the buffer is transferred to the sending buffer, ready to be forwarded

//receive success sign
rec_f = 1;

//Send buffer terminator
TxBuffer1[RxCounter1] = 0;
TxCounter1
= RxCounter1;
RxCounter1
= 0;
}
}

//This paragraph is to avoid the first byte of STM32 USART发不出去的BUG
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
//禁止发缓冲器空中断,
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}

}

Leave a Comment

Your email address will not be published.