STM32串口操作相關事項

來源:互聯網
上載者:User

作者:number007cool

轉自:http://blog.chinaunix.net/uid-21658993-id-3025218.html

放了一段時間,對stm32似乎有點陌生,總結一下!(基於3.0韌體庫,晶片stm32f103rbt6) 1、配置串口的管腳和時鐘

由於串口1、2是在GPIOA上:

所以要是能串口GPIOA、AFIO和1或者2的串口時鐘,代碼如下:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | 

            RCC_APB2Periph_AFIO |

            RCC_APB2Periph_USART1 , 

            ENABLE);

2、對串口的具體物理管腳進行相應的配置:

 /* A9 USART1_Tx */

    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);

    /* A10 USART1_Rx  */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入-RX

GPIO_Init(GPIOA, &GPIO_InitStructure);

3、在設定傳輸速率、資料位元、停止位。。。。。。。

完整測試程式如下:

串口的配置:

void USART_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
 USART_ClockInitTypeDef USART_ClockInitStructure;
 
 //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
            RCC_APB2Periph_AFIO |
            RCC_APB2Periph_USART1 ,
            ENABLE);

    /* A9 USART1_Tx */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* A10 USART1_Rx  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
    GPIO_Init(GPIOA, &GPIO_InitStructure);

 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);
    /* Enable the USARTx */
    USART_Cmd(USART1, ENABLE);
 
 
 //
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,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);
 
 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(USART2, &USART_ClockInitStructure);
    USART_Init(USART2, &USART_InitStructure);
   
    USART_Cmd(USART2, ENABLE);
 //
 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
串口發送函數:

void USART1_Putc(unsigned char c)
{
    USART_SendData(USART1, c);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}

void USART1_Puts(char * str)
{
    while(*str)
    {
        USART_SendData(USART1, *str++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    }
}

串口1接收中斷函數:

void USART1_IRQHandler(void)
{
 //接收中斷
 if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
 {
  USART_ClearITPendingBit(USART1,USART_IT_RXNE);
  Uart1_Get_Data=USART_ReceiveData(USART1);

 }
 
 //溢出-如果發生溢出需要先讀SR,再讀DR寄存器 則可清除不斷入中斷的問題
 if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
 {
  USART_ClearFlag(USART1,USART_FLAG_ORE); //讀SR
  USART_ReceiveData(USART1);    //讀DR
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.