<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          STM32 UART的使用過程

          作者: 時(shí)間:2016-11-17 來源:網(wǎng)絡(luò) 收藏
          1、使用UART前必須啟動(dòng)相應(yīng)的外設(shè)時(shí)鐘,其主要用到固件庫的RCC_APBnPeriphClockCmd函數(shù)。
          使能UART1:使用RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE)
          使能UART2:使用RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE)
          2、使用中斷進(jìn)行UART操作的需要配置NVIC,設(shè)置中斷優(yōu)先級。如:
          /* Configure the NVIC Preemption Priority Bits */
          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

          /* Enable the USART1 Interrupt */
          NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
          NVIC_Init(&NVIC_InitStructure);

          3、配置相應(yīng)的GPIO口。
          如果系統(tǒng)的UART需要進(jìn)行重映射,需要使用GPIO_PinRemapConfig函數(shù)進(jìn)行重映射,如:GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//注意:Rx為浮空,Tx為第二功能上拉。
          將Rx配置為:浮空輸入模式,Tx配置為帶上拉的第二功模式。并用GPIO_Init() 函數(shù)初始化。如:
          /* Configure USART2 Rx PA3 input floating */
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
          GPIO_Init(GPIOA, &GPIO_InitStructure);

          /* Configure USART1 Tx (PA.09) as alternate push-pull */
          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);
          4、配置UART
          當(dāng)在conf文件種配置正確的外晶振后,在USART_InitTypeDef定義的結(jié)構(gòu)體種直接寫入U(xiǎn)ART的波特率、通訊長頓、模式、硬件通訊控制,收發(fā)模式。再用USART_Init()進(jìn)行初始化。如:
          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;

          /* Configure USART1 */
          USART_Init(USART1, &USART_InitStructure);

          而后使能收發(fā)中斷。如:
          /* Enable USART1 Receive and Transmit interrupts */
          USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
          USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
          // USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
          注意:一般不將TXE中斷使能。因?yàn)橐坏⒋酥袛嗍鼓?,如果UART發(fā)送緩沖區(qū)空,則會(huì)立即進(jìn)入U(xiǎn)ART中斷中,因此可在程序中需要發(fā)送數(shù)據(jù)處,使能TXE中斷。在UART中斷種用USART_SendData()來發(fā)送數(shù)據(jù)。
          完成中斷使能后,還需要使能UART口:
          如:
          /* Enable the USART1 */
          USART_Cmd(USART1, ENABLE);
          /* Enable the USART2 */
          USART_Cmd(USART2, ENABLE);

          中斷程序(stm32f10x_it.c)可以如下完成發(fā)送:注意所有的的串口中斷需要在中斷服務(wù)程序中判斷中斷源以分別處理。
          void USART1_IRQHandler(void)
          {
          if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
          {
          /* Read one byte from the receive data register */
          RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);
          if(RxCounter1 == NbrOfDataToRead1)
          {
          USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);//發(fā)送完成后,將RXNE禁止。
          }
          }

          if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
          {
          USART_SendData(USART1, TxBuffer1[TxCounter1++]);
          if(TxCounter1 == NbrOfDataToTransfer1)
          {
          USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
          }
          }
          }


          關(guān)鍵詞: STM32UART使用過

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();