專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

STM32 USART 串口簡(jiǎn)單使用

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月14日   【字體:

STM32 USART 串口簡(jiǎn)單使用(轉(zhuǎn))

STM32 的庫(kù)實(shí)在強(qiáng)大~!函數(shù)長(zhǎng)的像句子......

好了開始了:

使用查詢方式的USART:

設(shè)置時(shí)鐘:

      RCC_APB2Periph_AFIO  功能復(fù)用IO時(shí)鐘
         RCC_APB2Periph_GPIOA  GPIOA時(shí)鐘

         RCC_APB2Periph_USART1 USART1時(shí)鐘

你可以用 
 //使能串口1,PA,AFIO總線  RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);

或直接

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL,ENABLE); //全部APB2外設(shè)時(shí)鐘開啟

注意USART2的你開啟為RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

設(shè)置GPIO:

   GPIO_InitTypeDefGPIO_InitStructure;

        
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;  //推挽輸出-TX
   GPIO_Init(GPIOA, &GPIO_InitStructure);

        
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入-RX
   GPIO_Init(GPIOA, &GPIO_InitStructure);

 

設(shè)置USART:

   這里我用的是3.0的庫(kù)相對(duì)于2.0的庫(kù)來(lái)說(shuō)多了一步,先說(shuō)2.0

   USART_InitTypeDef USART_InitStructure;

   USART_StructInit(&USART_InitStructure);//裝填默認(rèn)值

   USART_Init(USART1, &USART_InitStructure);//根據(jù)USART_InitStruct中指定的參數(shù)初始化外設(shè)USARTx寄存器
   USART_Cmd(USART1, ENABLE); //啟用

就好了~!

   而3.0的庫(kù)需要

   USART_InitTypeDefUSART_InitStructure;

   USART_ClockInitTypeDefUSART_ClockInitStructure;

  USART_StructInit(&USART_InitStructure);
   USART_ClockStructInit(&USART_ClockInitStructure);
   USART_ClockInit(USART1,&USART_ClockInitStructure);
   USART_Init(USART1,&USART_InitStructure);
   USART_Cmd(USART1,ENABLE);

 

//只是多分出了1個(gè)USART_ClockInitStructure 我也不知為啥要這樣??為了同步異步模式?USART_InitStruct中指定的參數(shù)內(nèi)容為:(2.0的)

typedef struct
{
u32 USART_BaudRate; //USART傳輸?shù)牟ㄌ芈?br /> u16 USART_WordLength;//一個(gè)幀中傳輸或者接收到的數(shù)據(jù)位數(shù)通常是8
u16 USART_StopBits; //停止位
u16 USART_Parity; //奇偶校驗(yàn)
u16 USART_HardwareFlowControl; //硬件流控制模式使能還是失能
u16 USART_Mode; //指定了使能或者失能發(fā)送和接收模式
u16 USART_Clock;//提示了USART時(shí)鐘使能還是失能
u16 USART_CPOL;//指定了下SLCK引腳上時(shí)鐘輸出的極性
u16 USART_CPHA;//指定了下SLCK引腳上時(shí)鐘輸出的相位
u16 USART_LastBit;

//來(lái)控制是否在同步模式下,在SCLK引腳上輸出最后發(fā)送的那個(gè)數(shù)據(jù)字通常用USART_LastBit_Disable
} USART_InitTypeDef;

 

我靠~!太細(xì)了~!我只知道(9600,8,n,1)這就夠了 其他的統(tǒng)統(tǒng)默認(rèn)~!

  USART_StructInit(&USART_InitStructure);
   USART_ClockStructInit(&USART_ClockInitStructure); //2.0不用這句,這樣就設(shè)好了好了~!自動(dòng)為您裝填了默認(rèn)參數(shù)。默認(rèn)的參數(shù)如下(3.0的庫(kù)):

voidUSART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
 
  USART_InitStruct->USART_BaudRate= 9600;
 USART_InitStruct->USART_WordLength =USART_WordLength_8b;
  USART_InitStruct->USART_StopBits= USART_StopBits_1;
  USART_InitStruct->USART_Parity =USART_Parity_No ;
  USART_InitStruct->USART_Mode =USART_Mode_Rx | USART_Mode_Tx;
 USART_InitStruct->USART_HardwareFlowControl =USART_HardwareFlowControl_None; 
}


 

void USART_ClockStructInit(USART_ClockInitTypeDef*USART_ClockInitStruct)
{
 
 USART_ClockInitStruct->USART_Clock =USART_Clock_Disable;
  USART_ClockInitStruct->USART_CPOL= USART_CPOL_Low;
  USART_ClockInitStruct->USART_CPHA= USART_CPHA_1Edge;
 USART_ClockInitStruct->USART_LastBit =USART_LastBit_Disable;
}

/************************************************************************************************/

當(dāng)然了你也可以自己設(shè)參數(shù),比如這樣。

 

void USART_Configuration(void)

{

 USART_InitTypeDef USART_InitStructure;
 USART_ClockInitTypeDefUSART_ClockInitStructure;

 

 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_ClockInitStructure.USART_Clock =USART_Clock_Disable;
 USART_ClockInitStructure.USART_CPOL =USART_CPOL_Low;
 USART_ClockInitStructure.USART_CPHA =USART_CPHA_2Edge;
 USART_ClockInitStructure.USART_LastBit =USART_LastBit_Disable;

 USART_ClockInit(USART1,&USART_ClockInitStructure);
 USART_Init(USART1,&USART_InitStructure);

   USART_Init(USART1, &USART_InitStructure);

   USART_ClockInit(USART1,&USART_ClockInitStructure);
   USART_Cmd(USART1, ENABLE);

} ////USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;除了這句以外其他的都和默認(rèn)的參數(shù)一樣,二者有啥區(qū)別我至今也不太清楚但就一般的應(yīng)用來(lái)說(shuō)兩個(gè)都可以正常工作。

 

收發(fā)的方法:

 1.發(fā)送

void USART1_Puts(char *str)
{
   while(*str)
    {
       USART_SendData(USART1, *str++);
       
       while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    }
}

 

 USART1_Puts("hello-java~!\r\n"); //這樣就發(fā)送了hello-java~!

跟C語(yǔ)言的printf不太一樣在于\n并沒(méi)有另起一行要用個(gè)\r這樣在終端上好看。

 2.接收

u8 uart1_get_data; //存放接受的內(nèi)容

while(1)

{

  if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
    {           
      uart1_get_data = USART_ReceiveData(USART1);
      USART1_Puts("\r\n獲取到串口1數(shù)據(jù):");
       USART1_Putc(uart1_get_data);
      USART1_Puts("\r\n");  
    }

}

 

查詢法的可以看出要不斷掃描不是很好,下面介紹中斷法。

首先配置時(shí)鐘:這里我拿USART2說(shuō)事:

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//USART2和USART3都在在APB1上而USART1是在APB2上的

設(shè)置GPIO:

 

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO |ENABLE);

 

    //A2 做T2X
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

    //A3 做R2X
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

 配置SUART2:

 USART_InitTypeDef USART_InitStructure;
 USART_ClockInitTypeDefUSART_ClockInitStructure;

 

 USART_StructInit(&USART_InitStructure);
 USART_ClockStructInit(&USART_ClockInitStructure);
 USART_ClockInit(USART2,&USART_ClockInitStructure);
 USART_Init(USART2,&USART_InitStructure);
 USART_Cmd(USART2, ENABLE);

 

 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//開啟SUART2的接收中斷同理還有【看圖】



然后中斷服務(wù)程序:這個(gè)自己在stm32f10x_it.c添加就可以了。

 

void USART2_IRQHandler(void)
{
 //接收中斷
 if(USART_GetITStatus(USART2,USART_IT_RXNE)==SET)
 {
  USART_ClearITPendingBit(USART2,USART_IT_RXNE);
  Uart2_Get_Data=USART_ReceiveData(USART2);
  Uart2_Get_Flag=1;
 }
 
 //溢出-如果發(fā)生溢出需要先讀SR,再讀DR寄存器則可清除不斷入中斷的問(wèn)題[牛人說(shuō)要這樣]
 if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
 {
  USART_ClearFlag(USART2,USART_FLAG_ORE); //讀SR其實(shí)就是清除標(biāo)志
  USART_ReceiveData(USART2);    //讀DR
 }
}

 

然后在main里檢測(cè)Uart2_Get_Flag

 

if(Uart2_Get_Flag)
  {
   Uart2_Get_Flag=0;
   USART2_Puts("\r\n2獲取到串口2數(shù)據(jù):");
   USART2_Putc(Uart2_Get_Data);
   USART2_Puts("\r\n");
  }

關(guān)閉窗口

相關(guān)文章