STM32 UART2程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
void USART2_IRQHandler(void)
{ if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{ Uห้องสมุดไป่ตู้ART_SendData(USART2,
USART_ReceiveData(USART2)); }
} 该程序功能是返回你发送到串口的数据。
/* Configure USART2 Tx (PD.05) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
如果你的 USART2 接到 PD 端口上面了,要修改程序如下,这里就需要端口映射了, 映射寄存器是 AFIO 控制的,它接在 APB2 时钟区域,所以下面的红色代码必须要 有,蓝色代码对端口进行初始化。 void USART2_Initialise( u32 bound ) {
GPIO_InitTypeDef GPIO_InitStructure;//I/Oú3ê ˉ á11ì NVIC_InitTypeDef NVIC_InitStructure;//D 3ê ˉ á11ì USART_InitTypeDef USART_InitStructure;//′ ú3ê ˉ á11ì
/* Enable the USART2 Pins Software Remapping */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
void USART2_IRQHandler(void)
{ if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET) { USART_SendData(USART2,
USART_ReceiveData(USART2)); }
}
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
USART_ART_BaudRate = bound;//ò»°ãéèÖÃÎa9600; USART_ART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½ USART_ART_StopBits = USART_StopBits_1;//ò»¸öí £Ö1λ USART_ART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ USART_ART_HardwareFlowControl = USART_HardwareFlowControl_None;//¾Yá÷¿ØÖÆ USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
/* Configure USART2 Rx (PD.06) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure);
£Ö1λ
USART_ART_Parity =
USART_Parity_No;
//ÎTÆæżD£Ñéλ
USART_ART_HardwareFlowControl =
USART_HardwareFlowControl_None;//Yá÷¿ØÖÆ
/* Enable the USART2 Pins Software Remapping */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD ,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); }
/* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
USART1 程序很多,看看 USART2 程序。注意红色部分,首先要使能相关端口的时 钟。 因为 USART2 可以映射成不同的端口,所以要端口映射。 结合你自己的开发板,设置正确的映射口.(正点原子的是 PA03PA02.) STM32F103ZE 默认的 USART2 就是 PA03PA02,如果配置映射,USART2 端口是 PD05PD06 了。
USART_ART_BaudRate =
bound;
//ò»°ãéèÖÃÎa9600;
USART_ART_WordLength =
USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_ART_StopBits = USART_StopBits_1; //ò»¸öí
程序在正点原子开发板 SRAM 中调试通过,开发环境 IAR5.30.
void USART2_Initialise( u32 bound ) {
GPIO_InitTypeDef GPIO_InitStructure;//I/Oú3ê ˉ á11ì NVIC_InitTypeDef NVIC_InitStructure;//D 3ê ˉ á11ì USART_InitTypeDef USART_InitStructure;//′ ú3ê ˉ á11ì
USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); }
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
void USART2_IRQHandler(void)
{ if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{ Uห้องสมุดไป่ตู้ART_SendData(USART2,
USART_ReceiveData(USART2)); }
} 该程序功能是返回你发送到串口的数据。
/* Configure USART2 Tx (PD.05) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Enable the USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
如果你的 USART2 接到 PD 端口上面了,要修改程序如下,这里就需要端口映射了, 映射寄存器是 AFIO 控制的,它接在 APB2 时钟区域,所以下面的红色代码必须要 有,蓝色代码对端口进行初始化。 void USART2_Initialise( u32 bound ) {
GPIO_InitTypeDef GPIO_InitStructure;//I/Oú3ê ˉ á11ì NVIC_InitTypeDef NVIC_InitStructure;//D 3ê ˉ á11ì USART_InitTypeDef USART_InitStructure;//′ ú3ê ˉ á11ì
/* Enable the USART2 Pins Software Remapping */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
void USART2_IRQHandler(void)
{ if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET) { USART_SendData(USART2,
USART_ReceiveData(USART2)); }
}
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
USART_ART_BaudRate = bound;//ò»°ãéèÖÃÎa9600; USART_ART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½ USART_ART_StopBits = USART_StopBits_1;//ò»¸öí £Ö1λ USART_ART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ USART_ART_HardwareFlowControl = USART_HardwareFlowControl_None;//¾Yá÷¿ØÖÆ USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
/* Configure USART2 Rx (PD.06) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure);
£Ö1λ
USART_ART_Parity =
USART_Parity_No;
//ÎTÆæżD£Ñéλ
USART_ART_HardwareFlowControl =
USART_HardwareFlowControl_None;//Yá÷¿ØÖÆ
/* Enable the USART2 Pins Software Remapping */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD ,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); }
/* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
USART1 程序很多,看看 USART2 程序。注意红色部分,首先要使能相关端口的时 钟。 因为 USART2 可以映射成不同的端口,所以要端口映射。 结合你自己的开发板,设置正确的映射口.(正点原子的是 PA03PA02.) STM32F103ZE 默认的 USART2 就是 PA03PA02,如果配置映射,USART2 端口是 PD05PD06 了。
USART_ART_BaudRate =
bound;
//ò»°ãéèÖÃÎa9600;
USART_ART_WordLength =
USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_ART_StopBits = USART_StopBits_1; //ò»¸öí
程序在正点原子开发板 SRAM 中调试通过,开发环境 IAR5.30.
void USART2_Initialise( u32 bound ) {
GPIO_InitTypeDef GPIO_InitStructure;//I/Oú3ê ˉ á11ì NVIC_InitTypeDef NVIC_InitStructure;//D 3ê ˉ á11ì USART_InitTypeDef USART_InitStructure;//′ ú3ê ˉ á11ì
USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); }