STM32F40x学习之USART

来源:互联网 发布:软件自制license实现 编辑:程序博客网 时间:2024/06/05 02:39

STM32F40x系列单片机的外设使用基本上要遵循三步曲
1、时钟使能
2、GPIO配置(如果涉及到GPIO的外设)
3、对应的外设配置并使能

对于USART的配置也不例外,下面以USART3为例进行介绍。
第一步:配置USART3的功能,使之可以通过USART3发送数据。(发送数据接收数据容易多了)
/*
本人使用的芯片信息如下:
 名称:STM32F407IGT6
 时钟:168MHz

相关硬件连线,USART3的TX/RX对应GPIO引脚如下:
PD8  -- USART3_TX
PD9  -- USART3_RX
*/

/*
三步曲第一步:时钟配置
*/
/* 
使能:GPIOD的时钟,查阅STM32F40x的手册我们可以知道GPIOD是AHB1设备
*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);


/*使能:USART3的时钟,查阅STM32F40x的手册我们可以知道USART3是APB1设备*/
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);


/*三步曲第二步:GPIO配置*/


    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;   //使用内部上拉电阻
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;   //使用复用功能模式

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;      //Usart3 Tx
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
//配置GPIOD的pin8为Tx
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    /* 开启GPIO引脚的复用功能*/
    /*要特别注意的是GPIO_PinAFConfig函数配置时用GPIO_PinSource8而不是GPIO_Pin8*/
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);

/*三步曲地三步:USART配置*/
    USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 115200;               //波特率为115200
    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_Tx;           //发送模式
       
/*初始化USART3设备*/
    USART_Init(USART3, &USART_InitStructure);          
/*使能USART3设备*/
    USART_Cmd(USART3, ENABLE); 


/*完整的例程如下*/
void usartCfg(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
     
     /* Enable GPIO Clock, gpio的时钟是在AHB1*/
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);
    
     /*Enable USART3 Clock , usart的时钟是APB1*/
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
    
    /* Configure USART Tx as alternate function  */
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Usart3 Tx
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);


    /* Configure USART Rx as alternate function  */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
    
    USART_InitStructure.USART_BaudRate = 115200;//BaudRate default value
    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_Tx;
       
    USART_Init(USART3, &USART_InitStructure);
    USART_Cmd(USART3, ENABLE);                    
}


/*下面我们就可以屌丝般地通过串口向PC机发送大名鼎鼎的"Hello World!\r\n" 了*/


void usartSend(char*str)
{
     int i=0;
    char * ptr = (uint8_t*)pbuf;
while ( *ptr != (char)0 )
{
     USART_SendData(USART3,*ptr++);
      /* Loop until the end of transmission */
      while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
      {

      }
    }
}
/**/
void main(void)
{
/*
这里要先加上你自己的系统时钟初始化函数.

*/

  usartCfg();
  usartSend("Hello World\r\n");
  while(1)
  {
  
  }
}

原创粉丝点击