利用STM32 的串口来发送和接收数据实验

0目标

STM32 串口简介

硬件设计

软件设计

下载验证

0.目标
利用串口 


1.STM32 串口简介

串口设置的一般步骤可以总结为如下几个步骤:
1) 串口时钟使能, GPIO 时钟使能
2) 串口复位
3) GPIO 端口模式设置
4) 串口参数初始化
5) 开启中断并且初始化 NVIC(如果需要开启中断才需要这个步骤)
6) 使能串口
7) 编写中断处理函数 
注:对于复用功能的 IO,我们首先要使能 GPIO 时钟,然后使能复用功能时钟,同时要把 GPIO 模式设置为复用功能对应的模式。
查看手册《STM32 中文参考手册 V10》P110 的表格“8.1.11 外设的 GPIO 配置:

硬件设计

(1)LED0接PA0
(2)串口1

3.软件设计
新建工程:

其中SYSTEM下放置原子哥提供的三个文件夹delay、sys、uart(及其文件),HARDWARE下建LED文件夹,及其内建LED.C与LED.H文件。
uart中串口函数:
[html]  view plain  copy
  1. <span style="font-size:18px;"></span>  
[csharp]  view plain  copy
  1. void uart_init(u32 bound){  
  2.     //GPIO端口设置  
  3.     GPIO_InitTypeDef GPIO_InitStructure;  
  4.     USART_InitTypeDef USART_InitStructure;  
  5.     NVIC_InitTypeDef NVIC_InitStructure;  
  6.        
  7.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟  
  8.      //USART1_TX   PA.9  
  9.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9  
  10.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  11.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出  
  12.     GPIO_Init(GPIOA, &GPIO_InitStructure);  
  13.      
  14.     //USART1_RX   PA.10  
  15.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
  16.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入  
  17.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  18.   
  19.    //Usart1 NVIC 配置  
  20.   
  21.     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;  
  22.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3  
  23.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;      //子优先级3  
  24.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;         //IRQ通道使能  
  25.     NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器  
  26.     
  27.    //USART 初始化设置  
  28.   
  29.     USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;  
  30.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式  
  31.     USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位  
  32.     USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位  
  33.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制  
  34.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式  
  35.   
  36.     USART_Init(USART1, &USART_InitStructure); //初始化串口  
  37.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断  
  38.     USART_Cmd(USART1, ENABLE);                    //使能串口   
  39.   
  40. }  

 
    LED.c内容: 
   
[csharp]  view plain  copy
  1. <span style="font-size:18px;">#include "led.h"  
  2.         
  3. //初始化PA0为输出口.并使能这个口的时钟            
  4. //LED IO初始化  
  5. void LED_Init(void)  
  6. {  
  7.    
  8.  GPIO_InitTypeDef  GPIO_InitStructure;  
  9.       
  10.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);   //使能PA端口时钟  
  11.   
  12.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;               //LED0-->PA0 端口配置  
  13.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;        //推挽输出  
  14.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       //IO口速度为50MHz  
  15.  GPIO_Init(GPIOA, &GPIO_InitStructure);                  //根据设定参数初始化GPIOA0  
  16.  GPIO_SetBits(GPIOA,GPIO_Pin_0);                         //PA0 输出高  
  17. }  
  18.  </span>  
led.h:

[csharp]  view plain  copy
  1. <span style="font-size:18px;">#ifndef __LED_H  
  2. #define __LED_H    
  3. #include "sys.h"  
  4.  
  5. #define LED0 PAout(0)// PA0  
  6.   
  7. void LED_Init(void);//初始化  
  8.                              
  9. #endif</span>  

主函数:

[csharp]  view plain  copy
  1. #include "led.h"  
  2. #include "delay.h"  
  3. #include "sys.h"  
  4. #include "usart.h"  
  5.  int main(void)  
  6.  {        
  7.     u8 t;  
  8.     u8 len;   
  9.     u16 times=0;  
  10.     delay_init();            //延时函数初始化      
  11.     NVIC_Configuration();    //设置NVIC中断分组2:2位抢占优先级,2位响应优先级  
  12.     uart_init(9600);     //串口初始化为9600  
  13.     LED_Init();              //LED端口初始化  
  14.     while(1)  
  15.     {  
  16.         if(USART_RX_STA&0x8000)  
  17.         {                        
  18.             len=USART_RX_STA&0x3f;//得到此次接收到的数据长度  
  19.             printf("\r\n您发送的消息为:\r\n\r\n");  
  20.             for(t=0;t<len;t++)  
  21.             {  
  22.                 USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据  
  23.                 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束  
  24.             }  
  25.             printf("\r\n\r\n");//插入换行  
  26.             USART_RX_STA=0;  
  27.         }else  
  28.         {  
  29.             times++;  
  30.             if(times%5000==0)  
  31.             {  
  32.                 printf("\r\n口袋里的超超 串口实验\r\n");  
  33.                 printf("真JB帅\r\n\r\n");  
  34.             }  
  35.             if(times%200==0)printf("请输入数据,以回车键结束\n");    
  36.             if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.  
  37.             delay_ms(10);     
  38.         }  
  39.     }      
  40.  }  

下载验证



猜你喜欢

转载自blog.csdn.net/weibo1230123/article/details/80243443