访问电脑版页面

导航:老古开发网手机版STM32单片机STM32F103单片机

基于STM32F103ZE的USART2 端口时钟

导读:
关键字:
STM32F103,端口,

USART1程序很多,看看USART2程序。注意红色部分,首先要使能相关端口的时钟。

因为USART2可以映射成不同的端口,所以要端口映射。

结合你自己的开发板,设置正确的映射口.(正点原子的是PA03PA02.)

STM32F103ZE默认的USART2就是PA03PA02,如果配置映射,USART2端口是PD05PD06了。

基于STM32F103ZE的USART2 端口时钟

程序在正点原子开发板SRAM中调试通过,开发环境IAR5.30.

void USART2_IniTIalise( u32 bound )

{

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); 

/* 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);

/* 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);

/* 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_InitStructure.USART_BaudRate = bound;                //ò»°ãéèÖÃÎa9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½

USART_InitStructure.USART_StopBits = USART_StopBits_1;     //ò»¸öí£Ö1λ

USART_InitStructure.USART_Parity = USART_Parity_No;        //ÎTÆæżD£Ñéλ

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//Yá÷¿ØÖÆ

USART_InitStructure.USART_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);

}

void USART2_IRQHandler(void)  

{  

if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)  

{       

USART_SendData(USART2, USART_ReceiveData(USART2));             

}

}

该程序功能是返回你发送到串口的数据。

如果你的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_GPIOD ,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);    

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);    

/* 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);

/* 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);    

USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½

USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ

USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//¾Yá÷¿ØÖÆ

USART_InitStructure.USART_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);

}

void USART2_IRQHandler(void)  

{  

if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)  

{       

USART_SendData(USART2, USART_ReceiveData(USART2));             

}

}

该程序功能是返回你发送到串口的数据。

如果你的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_GPIOD ,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);    

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);   

/* 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);

/* 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);    

USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½

USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ

USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//¾Yá÷¿ØÖÆ

USART_InitStructure.USART_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);

}

void USART2_IRQHandler(void)  

{  

if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)  

{       

USART_SendData(USART2, USART_ReceiveData(USART2));             

}

}

来源:网络整理   作者:工程师3  2018/6/4 7:26:00
栏目: [ STM32F103单片机]

相关阅读

STM32F103 开发板试用体验:超声波测距功能的实现

嵌入式微处理器STM32F103有什么特点和应用?怎样去开发?

利用stm32f103的TIM2实现精确延时

STM32F103学习笔记 (十) TFTLCD 显示

STM32F103试用体验(四):硬件原理与机壳组装

STM32F103试用体验:LCD显示与DHT11测量实验

STM32F103ZET6 之 通用定时器单脉冲模式实验

STM32F103系列单片机中的定时器工作原理解析

STM32F103C8T6 ULN2003步进电机驱动代码

STM32F103外部晶振由8M变为12M

基于STM32F103ZE的USART2 端口时钟

stm32F103单片机无源晶振不起振排除问题

STM32F103利用模拟I2C驱动ADS1115

基于单片机Stm32f103 DAC电流输出解决方案

STM32F103程序调用printf打印串口数据

stm32f103zet6与stm32f103rct6的区别

STM32F103的复位及时钟控制模块头文件

STM32F103试用体验:水共震准备材料中的过程

基于STM32F103单片机的汽车远程防盗系统方案

100脚的STM32F103VE单片机通过FSMC接口读写DS12C887时钟芯片中的寄存器