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

          新聞中心

          STM32 USART 串口簡單使用

          作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
          STM32 的庫實(shí)在強(qiáng)大~!函數(shù)長的像句子......

          好了開始了:

          本文引用地址:http://www.ex-cimer.com/article/201612/324560.htm

          使用查詢方式的USART:

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

          RCC_APB2Periph_AFIO功能復(fù)用IO時(shí)鐘
          RCC_APB2Periph_GPIOAGPIOA時(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的庫相對于2.0的庫來說多了一步,先說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的庫需要

          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;

          //來控制是否在同步模式下,在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的庫):

          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)用來說兩個(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~!"); //這樣就發(fā)送了hello-java~!

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

          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("獲取到串口1數(shù)據(jù):");
          USART1_Putc(uart1_get_data);
          USART1_Puts("");
          }

          }


          上一頁 1 2 下一頁

          關(guān)鍵詞: STM32USART串

          評論


          技術(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); })();