专注电子技术学习与研究
当前位置:单片机教程网 >> STM32 >> 浏览文章

STM32串口USART2程序

作者:佚名   来源:本站原创   点击数:  更新时间:2014年08月17日   【字体:

原理图:


 头文件及完整例程下载:http://www.51hei.com/f/stm32标准例程库函数.rar
程序分析:

int main(void)
{
 
   uint8_t a=0;
  /* System Clocks Configuration */
  RCC_Configuration();  //系统时钟设置
       
  /*嵌套向量中断控制器 
      说明了USARTx抢占优先级级别0(最多1位) ,和子优先级级别0(最多7位) */ 
  NVIC_Configuration();  //中断源配置
 
  /*对控制LED指示灯的IO口进行了初始化,将端口配置为推挽上拉输出,口线速度为50Mhz。PA2,PA2端口复用为串口2的TX,RX。
  在配置某个口线时,首先应对它所在的端口的时钟进行使能。否则无法配置成功,由于用到了端口B, 因此要对这个端口的时钟
  进行使能,同时由于用到复用IO口功能用于配置串口。因此还要使能AFIO(复用功能IO)时钟。*/
  GPIO_Configuration();  //端口初始化
 
  USART_Config(USART2);  //串口1初始化
  
  USART_OUT(USART2,"****(C) COPYRIGHT 2013 奋斗嵌入式开发工作室 *******\r\n");      //向串口1发送开机字符。
  USART_OUT(USART2,"*                                                 *\r\n");    
  USART_OUT(USART2,"*         奋斗版STM32开发板 USART2 实验           *\r\n");  
  USART_OUT(USART2,"*                                                 *\r\n");      
  USART_OUT(USART2,"*  以HEX模式输入一串数据,以16进制0d 0a作为结束   *\r\n");    
  USART_OUT(USART2,"*                                                 *\r\n");    
  USART_OUT(USART2,"*  奋斗STM32论坛:www.ourstm.net                  *\r\n");   
  USART_OUT(USART2,"*                                                 *\r\n");    
  USART_OUT(USART2,"***************************************************\r\n"); 
  USART_OUT(USART2,"\r\n"); 
  USART_OUT(USART2,"\r\n");    
   while (1)
  {
if(rec_f==1){  //判断是否收到一帧有效数据
rec_f=0;
USART_OUT(USART2,"\r\n您发送的信息为: \r\n");    
USART_OUT(USART2,&TxBuffer1[0]);
if(a==0) {GPIO_SetBits(GPIOB, GPIO_Pin_5); a=1;}          //LED1  V6(V3板) V2(MINI板) 明暗闪烁                
else {GPIO_ResetBits(GPIOB, GPIO_Pin_5);a=0;  }
}
  }

} 
时钟初始化
RCC_APB2Periph_GPIOA  ()

void RCC_Configuration(void)
{
   SystemInit(); 
   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA  | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD |
                           RCC_APB2Periph_AFIO  , ENABLE); 
   RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);  
}

中断向量初始化,分组NVIC_PriorityGroup_0 ,初始化USART2_IRQn 
void NVIC_Configuration(void)
{
  /*  结构声明*/
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Configure the NVIC Preemption Priority Bits */  
  /* Configure one bit for preemption priority */
  /* 优先级组 说明了抢占优先级所用的位数,和子优先级所用的位数   在这里是1, 7 */    
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  
  
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;     //设置串口2中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     //抢占优先级 0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
  NVIC_Init(&NVIC_InitStructure);
} 

GPIO初始化
void GPIO_Configuration(void)
{
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;     //LED1控制--PB5
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //RS485输入输出控制
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  GPIO_SetBits(GPIOD, GPIO_Pin_12);                     //RS485输出模式 禁止485输入
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;         //USART2 TX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //复用推挽输出
  GPIO_Init(GPIOA, &GPIO_InitStructure);     //A端口 
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         //USART2 RX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //复用开漏输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);         //A端口 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;      //LCD背光控制
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  GPIO_ResetBits(GPIOD, GPIO_Pin_13);                  //LCD背光关闭
} 

void USART_Config(USART_TypeDef* USARTx){
  USART_InitStructure.USART_BaudRate = 115200; //速率115200bps
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
  USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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; //收发模式
 
  /* Configure USARTx */
  USART_Init(USARTx, &USART_InitStructure); //配置串口参数函数
 
  
  /* Enable USARTx Receive and Transmit interrupts */
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);                    //使能接收中断
  USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //使能发送缓冲空中断   
 
  /* Enable the USARTx */
  USART_Cmd(USART2, ENABLE);
} 
 

 

关闭窗口

相关文章