当前位置: 代码迷 >> 综合 >> STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
  详细解决方案

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

热度:32   发布时间:2023-10-16 08:08:22.0

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

1.目的:做回显实验:
2.开发步骤:
只要是用到串口的实验,都必须对串口做下列的配置
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

3.所用到的软件和硬件:
4.串口操作的相关的函数、寄存器:
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
USART_SR状态寄存器:

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
USART_DRSTM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
USART_DR
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

五、逻辑分析:
1.我们是做回显实验,会用到串口,所以我们的先分析串口我们应该怎么设置。
之前任何操作都需要使能。不仅仅是外设,那该使能哪一个串口呢?我们查看原理图和系统结构图可知。我们需要的串口最好是异步通信的,这样代码书写要简单一点。而异步通信串口有好几个,这里我们随便选择一个,选择USAET1。由于开发板未来节约用电,硬件上面,当没有用的硬件,就不会给它供电,所以我们需要先进行使能。
*

而使能函数也有很多个,那我们应该找哪一个使能函数呢?*

看下图可知,USART1挂载在APB2线上面,所以我们去使能函数是APB2的。

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

而使能函数放在那里的呢?

放在下面的这个文件里面。
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

那这些使能函数写在哪儿呢?

你可以写在main函数外面,也可以写在main里面,但是由于这些代码知识单纯的一句,所以你最好是写成一个函数,然后main里面再调用这个函数。而这是对于串口的一个操作,就把它命名为串口函数,把关于串口的初始化操作都可以放在这个函数中。

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

这些函数里面的参数有哪些可能的取值呢?

先按照这个函数找到,这个函数的定义在哪一个STM32提供的库函数的C文件里面的那个位置。然后点击下面这个函数,就能够知道,这个函数的这个参数的允许取值有哪些了。
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
这就是该参数的允许取值。

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

为啥使能GPIOA呢?

通过分析系统结构图和原理图知道,USART1是一个串口,它是进行发送和接收数据的,而接收和发送数据是通过PA9和·PA10的复用功能实现的,所以的使能GPIOA。

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

3.这里是设置GPIOA的PA9和PA10引脚。

为啥先定义一个结构体呢?

在STM32开发中,ST公司提供的STM32固件库,里面为每一个外设的可能涉及到的配置全部都写进了一个结构体里面,这样有利于加快开发工作的进度。

 GPIO_InitTypeDef GPIO_InitStrue;

为啥设置为GPIO_Mode_Out_PP模式?

GPIO_InitTypeDef GPIO_InitStrue;GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;GPIO_InitStrue.GPIO_Mode=GPIO_Mode_Out_PP;

这个需要STM32数据手册分析结构图和STM32参考手册。得知PA9在usart1中作为输出口。PA10作为输入口。

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

怎样判断的是全双工工作模式呢?

由于我的输入和输出是用的两跟不同的引脚,所以使用全双工工作模式。
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

速度怎么选?

速度没啥影响,随便选一个就行。

	 GPIO_InitTypeDef GPIO_InitStrue;GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;GPIO_InitStrue.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;

最后初始化,这是为了把之前做的这些设置全部写到开发板相应的寄存器中去

 GPIO_InitTypeDef GPIO_InitStrue;GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;GPIO_InitStrue.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;GPIO_Init(GPIOA,&GPIO_InitStrue);

波特率随便选一个,只要最后通信双方一样就行。
硬件控制流我也不知道怎么选
USART模式,因为输入和输出都需要。
后面三个,通信双方设置成一样的就行。

	//串口初始化USART_InitStrue.USART_BaudRate=115200;USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;USART_InitStrue.USART_Mode=USART_Mode_Rx |USART_Mode_Tx ;USART_InitStrue.USART_Parity=USART_Parity_No;USART_InitStrue.USART_StopBits=USART_StopBits_1;USART_InitStrue.USART_WordLength=USART_WordLength_8b;USART_Init(USART1,&USART_InitStrue);
	//串口使能USART_Cmd(USART1,ENABLE);
//开启接收中断USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

NVIC_InitStruce.NVIC_IRQChannel=USART1_IRQn;这个是为啥呢?我只不知道

NVIC_InitStruce.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruce.NVIC_IRQChannelSubPriority=1;
因为选择了中断组2
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

		//设置中断NVIC_InitStruce.NVIC_IRQChannel=USART1_IRQn;NVIC_InitStruce.NVIC_IRQChannelCmd=ENABLE;NVIC_InitStruce.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStruce.NVIC_IRQChannelSubPriority=1;NVIC_Init(&NVIC_InitStruce);

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

中断处理函数。名字是系统定义好的,这个的你自己去找。
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

u8 res;为啥定义的是8位,好像这个数据寄存器一次只能存储8位。
USART_GetITStatus(USART1,USART_IT_RXNE这个是为了确定此处中断是不是由USART_IT_RXNE这个位造成的。因为USART1的这个好像是状态寄存器有很多标志位,每一个标志位所造成的中断,都会进入到这个中断服务函数里面来执行。所以的判断是不是由USART_IT_RXNE这个位造成的中断。

res = USART_ReceiveData(USART1);这个函数会自动到USART1的数据寄存器里面去取数据。

USART_SendData(USART1,res);开发板,有吧数据原封不动的发回给USART1。这儿直接发给USART1就从,该函数会自动的把数据存储在USART的数据寄存器里面。

void USART1_IRQHandler(void)
{
    u8 res;if(USART_GetITStatus(USART1,USART_IT_RXNE)){
    res = USART_ReceiveData(USART1);USART_SendData(USART1,res);}	
}

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

这是设置中断优先级分组,这个的在中断配置之前设置。

 while(1);这个的设置,是为了让这个程序一直处于运行状态,虽然这个运行什么也没做。这个是因为,程序现在下载到开发板,主函数一直在进行,而上面的代码,是说明,开启了中断接收的,其实,开发板一直就是在等待这个中断出现。当中断出现了,主函数就被挂起,开发板上面的CPU就去运行,中断服务函数void USART1_IRQHandler(void)。当运行完中断服务函数,就又会回到主函数,继续进行死循环,等待下一次中断的发生。
 int main(void){
    		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	 My_USART1_Init();while(1);}

补充:
首先我们进行的实验,我们要分析这个实验所涉及的硬件和软件有哪些,这个实验我们要用到开发板,调试助手,串口等。
串口是一个外设,所以我们首先要去STM32固件库里面找到该外设的结构体,分析结构体包含了那些成员,以及这些成员应该赋什么值。
串口就应该到usart头文件去找:
STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )

  相关解决方案