STM32串口中断接收

 我来答
百度网友a78899f8c
2012-12-20 · TA获得超过2763个赞
知道大有可为答主
回答量:782
采纳率:50%
帮助的人:781万
展开全部
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_FLAG_ORE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_FLAG_ORE);
UART2_temp_buff = USART_ReceiveData(USART2);
}
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
UART2_temp_buff = USART_ReceiveData(USART2);
}
}
huyudu
2012-05-07 · TA获得超过495个赞
知道小有建树答主
回答量:312
采纳率:100%
帮助的人:200万
展开全部
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/

/** @addtogroup USART_Interrupt
* @{
*/
GPIO_InitTypeDef GPIO_InitStructure;

/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Define the STM32F10x hardware depending on the used evaluation board */

#define USART1_GPIO GPIOA
#define USART1_CLK RCC_APB2Periph_USART1
#define USART1_GPIO_CLK RCC_APB2Periph_GPIOA
#define USART1_RxPin GPIO_Pin_10
#define USART1_TxPin GPIO_Pin_9
#define USART1_IRQn USART1_IRQn
#define USART1_IRQHandler USART1_IRQHandler

#define USART2_GPIO GPIOA
#define USART2_CLK RCC_APB1Periph_USART2
#define USART2_GPIO_CLK RCC_APB2Periph_GPIOA
#define USART2_RxPin GPIO_Pin_3
#define USART2_TxPin GPIO_Pin_2
#define USART2_IRQn USART2_IRQn
#define USART2_IRQHandler USART2_IRQHandler

/*板子相关定义*/自己根据IO口的连接做相应修改
#define RCC_GPIO_LED RCC_APB2Periph_GPIOF /*LED使用的GPIO时钟*/
#define LEDn 4 /*神舟III号LED数量*/
#define GPIO_LED GPIOF /*神舟III号LED灯使用的GPIO组*/

#define DS1_PIN GPIO_Pin_6 /*DS1使用的GPIO管脚*/
#define DS2_PIN GPIO_Pin_7 /*DS2使用的GPIO管脚*/
#define DS3_PIN GPIO_Pin_8 /*DS3使用的GPIO管脚*/
#define DS4_PIN GPIO_Pin_9 /*DS4使用的GPIO管脚*/

/* Private define ------------------------------------------------------------*/
#define TxBufferSize1 (countof(TxBuffer1) - 1)
#define TxBufferSize2 (countof(TxBuffer2) - 1)
#define RxBufferSize1 TxBufferSize2
#define RxBufferSize2 TxBufferSize1

/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))

/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
uint8_t TxBuffer1[] = "串口中断收发示例: 串口1 -> 串口2 (中断收发)";
uint8_t TxBuffer2[] = "串口中断收发示例: 串口2 -> 串口1 (中断收发)";
uint8_t RxBuffer1[RxBufferSize1];
uint8_t RxBuffer2[RxBufferSize2];
__IO uint8_t TxCounter1 = 0x00;
__IO uint8_t TxCounter2 = 0x00;
__IO uint8_t RxCounter1 = 0x00;
__IO uint8_t RxCounter2 = 0x00;
uint8_t NbrOfDataToTransfer1 = TxBufferSize1;
uint8_t NbrOfDataToTransfer2 = TxBufferSize2;
uint8_t NbrOfDataToRead1 = RxBufferSize1;
uint8_t NbrOfDataToRead2 = RxBufferSize2;
__IO TestStatus TransferStatus1 = FAILED;
__IO TestStatus TransferStatus2 = FAILED;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

/* Private functions ---------------------------------------------------------*/

/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/

/* System Clocks Configuration */
RCC_Configuration();

/* NVIC configuration */
NVIC_Configuration();

/* Configure the GPIO ports */
GPIO_Configuration();

GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
/* USART1 and USART2 configuration ------------------------------------------------------*/

/* USART1 and USART2 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为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; /*发送和接收*/

/*配置串口1 */
USART_Init(USART1, &USART_InitStructure);
/*配置串口2*/
USART_Init(USART2, &USART_InitStructure);

/*使能串口1的发送和接收中断*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

/*使能串口2的发送和接收中断*/
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

/* 使能串口1 */
USART_Cmd(USART1, ENABLE);
/* 使能串口2 */
USART_Cmd(USART2, ENABLE);

/* Wait until end of transmission from USART1 to USART2 */
while(RxCounter2 < RxBufferSize2)
{
}

/* Wait until end of transmission from USART2 to USART1 */
while(RxCounter1 < RxBufferSize1)
{
}

/* Check the received data with the send ones */
TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);
/* TransferStatus1 = PASSED, if the data transmitted from USART2 and
received by USART1 are the same */
/* TransferStatus1 = FAILED, if the data transmitted from USART2 and
received by USART1 are different */
TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
/* TransferStatus2 = PASSED, if the data transmitted from USART1 and
received by USART2 are the same */
/* TransferStatus2 = FAILED, if the data transmitted from USART1 and
received by USART2 are different */

while (1)
{
if(TransferStatus1 == PASSED)
{
GPIO_ResetBits(GPIO_LED,DS1_PIN);/*点亮DS1,串口1接收的数据与串口2发送的数据相同*/
}
else if(TransferStatus1 == FAILED)
{
GPIO_ResetBits(GPIO_LED,DS2_PIN);/*点亮DS2,串口1接收的数据与串口2发送的数据不相同*/
}

if(TransferStatus2 == PASSED)
{
GPIO_ResetBits(GPIO_LED,DS3_PIN);/*点亮DS3,串口2接收的数据与串口1发送的数据相同*/
}
else if(TransferStatus2 == FAILED)
{
GPIO_ResetBits(GPIO_LED,DS3_PIN);/*点亮DS3,串口2接收的数据与串口1发送的数据不相同*/
}

}
}

/**
* @brief Configures the different system clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
/*使能串口1和串口2使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK, ENABLE);
/* Enable USART1 Clock */
/*使能串口1时钟*/
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);
/*使能串口2时钟*/
RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE);
/*使能LED灯使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE);
}

/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/*串口1 RX管脚配置*/
/* Configure USART1 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);

/*串口2 RX管脚配置*/
/* Configure USART2 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART2_RxPin;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);

/*串口1 TX管脚配置*/
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);

/*串口2 TX管脚配置*/
/* Configure USART2 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART2_TxPin;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);

/* 配置神舟III号LED灯使用的GPIO管脚模式*/
GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED, &GPIO_InitStructure);
}

/**
* @brief Configures the nested vectored interrupt controller.
* @param None
* @retval None
*/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/**
* @brief Compares two buffers.
* @param pBuffer1, pBuffer2: buffers to be compared.
* @param BufferLength: buffer's length
* @retval PASSED: pBuffer1 identical to pBuffer2
* FAILED: pBuffer1 differs from pBuffer2
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}

pBuffer1++;
pBuffer2++;
}

return PASSED;
}
然后在stm32f10x_it.c文件中添加如下代码:
/**
* @brief This function handles USART1 global interrupt request.
* @param None
* @retval None
*/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);

if(RxCounter1 == NbrOfDataToRead1)
{
/* Disable the USART1 Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
}

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, TxBuffer1[TxCounter1++]);

if(TxCounter1 == NbrOfDataToTransfer1)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}

/**
* @brief This function handles USART2 global interrupt request.
* @param None
* @retval None
*/
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer2[RxCounter2++] = USART_ReceiveData(USART2);

if(RxCounter2 == NbrOfDataToRead1)
{
/* Disable the USART2 Receive interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
}

if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART2, TxBuffer2[TxCounter2++]);

if(TxCounter2 == NbrOfDataToTransfer2)
{
/* Disable the USART2 Transmit interrupt */
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式