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; //输出频率最大50MHzGPIO_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; //输出频率最大50MHzGPIO_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; //输出频率最大50MHzGPIO_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; //输出频率最大50MHzGPIO_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); //复位串口2GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2--TXGPIO_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--RXGPIO_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); //延时 }}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
