前言
我们在学习32单片机的时候,定时器和串口是最基础的也是最重要的部分。相信大家在写代码的时候可能会遇到类似的问题,可以用下面的方法解决一下,现在和大家分享一下我自己的经验。
一、串口
在串口中断进不去的情况下,这是我所经历的最普遍的一个问题,身边的朋友也有好多对这个问题有困惑。有几种解决方法,其一,检查硬件,也就是从PCB上面看和我们的串口相连的走线,电阻检查有没有焊错型号,和芯片相连的话看芯片有没有虚焊,好多我们搞硬件的都拌在了虚焊上面了,毕竟是手焊的,和机器没有办法相比。其二,检查软件,就是我们写的代码在配置串口这里可能会出现一些问题。
1.串口配置
像下面的代码是基础的配置代码
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// USART3 Tx (PB.10)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// USART3 Rx (PB.11)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// USART3 MODE
USART_InitStructure.USART_BaudRate = 9600;
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(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
2.引脚复用
我们在用到复用引脚的时候需要这样写代码:
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
// USART2 Tx (PA.2)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART2 Rx (PA.3)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART2 MODE
USART_InitStructure.USART_BaudRate = 9600;
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
注意:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);尤其是这句代码,一定要加上,否则串口配置不成功,串口中断进不去。
解决了这些问题之后,再配置好需要的中断分组,我们的串口基本上就可以使用了。
二、定时器
定时器的定时时长计算方法为:(1+TIM_Prescaler)/72M× \times×(1+TIM_Period)
其中:TIM_Prescaler是预分频系数,TIM_Period是定时周期,例如TIM_Prescaler=1439,TIM_Period=5000,计算结果为:(1+1439)/72000000× \times×(1+5000)=100ms
三、特殊引脚
普通引脚的定义不再讲述,STM32单片机有的引脚并不是向普通的IO口那样定义,对于复用引脚的定义,例如PA.14引脚是需要这样进行定义:
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_14);
注意:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE); //这两句代码顺序不能颠倒,否则PA.14引脚不能正常使用。
总结
学习STM32的道路上虽然会遇到一些困难,但是我们要坚信自己并不孤单,因为有志同道合的伙伴们!天气转凉,记得多穿点!