收到小uFun已经第二周啦,周末正好有时间盘~~它,要盘它肯定先要有个稳定的IDE环境。
本人用的是KeilV5版本如图,
随便打开一个以前的工程,把芯片改成STM32F103RC,然后咱们开始配置串口,当然写程序
之前先要仔细看看电路图不是,要和电路图对应上,顺便说一句,我没用用HAL库配置工具
都是自己改,如果是用HAL库的同学更要仔细看电路图了,要把配置一一对应,来看下电路图
板子出口用的是USART1,PA9 TX PA10 RX,如图:
好了,心里有数了,开始修改配置代码,步骤说下,
配置IO模式->配置对应串口参数->配置中断相关参数->中断函数
下面贴代码:
IO设,PA9置成推挽复用输出,PA10设置成输入
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = UART1TX_Pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; \\推挽输出模式
- GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = UART1RX_Pin;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; \\输入模式
- GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = 115200; //波特率 115200
- 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; //输入输出模式
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART1, ENABLE); //使能串口
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
- 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)
- {
- USART_SendData(USART1,USART_ReceiveData(USART1));//收到什么发送什么
- }
- }
游客,如果您要查看本帖隐藏内容请回复
最后上一张接线图,板子接STLINK(古老的家伙)接电脑USB:
更多好玩有趣的项目,可关注ZcorePlayer公众号哟~~
第一期链接:https://mbb.eet-china.com/forum/topic/69612_1_1.html