STM32F103 端口设置及串口通信

STM32F103   下载调试口PA13,PA14,PA15,PB3,PB4设为普通IO口,   还有其它端口设置作为输出端口使用,如果用其他IO口的以此类推

void GPIO_config(void)
{
		GPIO_InitTypeDef	GPIO_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);	//使能APB2总线外设时钟
		
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//PA13,PA14,PA15作普通IO口,(复用功能重映射为调试下载器引脚)

		GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;	//选择引脚
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//输出频率最大50MHz
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
		GPIO_Init(GPIOA,&GPIO_InitStructure);
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);		//PB3,PB4作普通IO口
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); 	//PB3,PB4,PA13,PA14,PA15作普通IO口,(复用功能重映射为调试下载器引脚)
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;			//选择引脚
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//输出频率最大50MHz
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
		GPIO_Init(GPIOB,&GPIO_InitStructure);
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;			//选择引脚
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		//上拉
		GPIO_Init(GPIOB,&GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;			//选择引脚
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//输出频率最大50MHz
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
		GPIO_Init(GPIOC,&GPIO_InitStructure);
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;	//选择引脚
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 			//上拉
		GPIO_Init(GPIOC,&GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;		
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//输出频率最大50MHz
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	//推挽
		GPIO_Init(GPIOD,&GPIO_InitStructure);
}

UART2 通信设置,

PA2------------TX2

PA3------------RX2

void init_hardware_usart2(u32 bound)
{
	GPIO_InitTypeDef 	GPIO_InitStructure;
	USART_InitTypeDef 	USART_InitStructure;
	NVIC_InitTypeDef 	NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	USART_DeInit(USART2);  							//复位串口2
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;		//PA.2--TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;		//PA.3--RX
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = bound;        //波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_9b;    //带一位校验位
	USART_InitStructure.USART_StopBits = USART_StopBits_1;        //一位停止位
	USART_InitStructure.USART_Parity = USART_Parity_Even;        //偶校验
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;	//无硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART2, &USART_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;        //接收中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART2, ENABLE);
}  

UART2 接收中断

void USART2_IRQHandler(void)
{
		if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)	//USART_IT_RXNE=接收中断标志位,读操作会清零
			rx_data[rx_n] = USART_ReceiveData(USART2);
		rx_n++;
}

MAIN函数

int main(void) //主函数
 {    
		u8	m;
		GPIO_config();					//IO=口初始化
		init_hardware_usart2(9600);		//UART2初始化,波特率9600,偶校验

        m=0x55;
		USART_SendData(USART2, m);
		while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);		//等待发送完成

	 
		while(1)			
		{	
		LED1_NOT;			//LED指示灯
        delay(1000);        //延时 
        }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值