历史上的今天
今天是:2025年02月14日(星期五)
2020年02月14日 | STM32的串口中断配置
2020-02-14 来源:elecfans
STM32的串口中断配置,也是很简单的.
首先是配置UART的GPIO口
/**********************************************
* Name : UART1_GPIO_Configuration
* Deion : Configures the uart1 GPIO ports.
* Input : None
* Output : None
* Return : None
**********************************************************/
void UART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure USART1_Tx as alternate push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
然后是配置串口参数
/*******************************************************
* Name : UART1_Configuration
* Deion : Configures the uart1
* Input : None
* Output : None
* Return : None
*********************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
Uart1_GPIO_Configuration();
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
}
然后是在中断设置,需要修改stm32f10x_it.c 中的串口中断函数 并且需要修改void NVIC_Configuration(void)函数
修改NVIC_Configuration函数
/***********************************************************
* Name : NVIC_Configuration
* Deion : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
***************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//串口中断
void USART1_IRQHandler(void)
{
//处理接收到的数据
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
//发送中断
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_SendData(USART1, Send_Data[Send_Length++]);
if (Send_Length==SEND_LENGTH)
{
//发送字节结束
USART_ClearITPendingBit(USART1,USART_IT_TXE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
}
}
//发送完成
if (USART_GetITStatus(USART1, USART_IT_TC) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_TC);
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
}
}
在需要发送的程序里Send_Data[SEND_LENGTH]和发送长度设置好,
void Send_to_PC(void)
{
//设置好Send_Data[SEND_LENGTH]数组
//打开发送中断
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
至此 串口就可以工作起来了!~
史海拾趣
|
诸位,咱当电子工程师也是十余年了,不算有出息,环顾四周,也 没有看见几个有出息的!回顾工程师生涯,感慨万千,愿意讲几句掏 心窝子的话,也算给咱们师弟师妹们提个醒,希望他们比咱们强! [1]好好规划自己的路,不要跟着感觉走!根据个人的 ...… 查看全部问答> |
|
作为局域网的主要连接设备,以太网交换机成为应用普及最快的网络设备之一,同时,也是随着这种快速的发展,交换机的功能不断增强,随之而来则是交换机端口的更新换代以及各种特殊设备连接端口不断的添加到交换机上,这也使得交换机的接口类型变得非 ...… 查看全部问答> |
|
本帖最后由 jameswangsynnex 于 2015-3-3 19:57 编辑 Job description: · Responsible for the creation, implementation and debugging of board level tests for Xbox accessories. · The test engineer must also be able to create d ...… 查看全部问答> |
|
大家好,68013的GPIO口一般是用8个端口(如P0-P7)接收8位数据,或16个端口(如P0-P15)接收16位数据,也就是第一个端口接收一位数据,现在我有另一个GPIO接口,只有一条线,输出的数据是以b0,b1~b7,这样的顺序,输完一个字节,又接着输第二个字节. 请问怎样才 ...… 查看全部问答> |
|
嵌入式领域较新,目前发展非常快,因此踏进这个行业的难度比较大,嵌入式人才稀缺,身价自然也水涨船高,那么究竟嵌入式软件工程师待遇如何呢?下面就由福州卓跃教育就这个问题为大家做具体介绍。   ...… 查看全部问答> |




