找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2677|回復(fù): 1
收起左側(cè)

基于STM32的串口5協(xié)議測(cè)試源碼

[復(fù)制鏈接]
ID:225288 發(fā)表于 2018-3-13 19:29 | 顯示全部樓層 |閱讀模式
這個(gè)是STM32F103ZET6的串口5測(cè)試代碼,內(nèi)部附帶一個(gè)簡(jiǎn)單的串口協(xié)議測(cè)試。

單片機(jī)源程序如下:
  1. /*-------------------------------------------------------------------------------
  2. 文件名稱(chēng):main.c
  3. 文件描述:通過(guò)串口5,使用printf函數(shù)打印信息,編譯時(shí)需勾選Use MicroLIB
  4. 硬件平臺(tái):尼莫M3S開(kāi)發(fā)板
  5. 編寫(xiě)整理:shifang
  6. 固件庫(kù)  :V3.5
  7. 備    注:通過(guò)簡(jiǎn)單修改可以移植到其他開(kāi)發(fā)板,部分資料來(lái)源于網(wǎng)絡(luò)。
  8. ---------------------------------------------------------------------------------*/
  9. #include <stdio.h>
  10. #include "stm32f10x.h"
  11. #include "led.h"
  12. #include "delay.h"
  13. #include "key.h"
  14. #include "timer.h"
  15. #include "beep.h"



  16. #ifdef __GNUC__
  17.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  18.      set to 'Yes') calls __io_putchar() */
  19.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  20. #else
  21.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  22. #endif /* __GNUC__ */

  23. int main(void)
  24. {
  25.           GPIO_InitTypeDef GPIO_InitStructure;
  26.           USART_InitTypeDef USART_InitStructure;
  27.     NVIC_InitTypeDef NVIC_InitStructure;
  28.          
  29.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);        //使能PORTC,PORTD時(shí)鐘
  30.           RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //使能UART5
  31.           USART_DeInit(UART5);  //復(fù)位串口5
  32.           //UART5_TX   PC.12
  33.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC.12
  34.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  35.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //復(fù)用推挽輸出
  36.     GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC12
  37.    
  38.     //UART5_RX          PD.2
  39.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  40.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
  41.     GPIO_Init(GPIOD, &GPIO_InitStructure);  //初始化PD2   
  42.        
  43.           //UART5 NVIC 配置

  44.     NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
  45.           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//搶占優(yōu)先級(jí)3
  46.           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //子優(yōu)先級(jí)3
  47.           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
  48.           NVIC_Init(&NVIC_InitStructure);        //根據(jù)指定的參數(shù)初始化VIC寄存器
  49.   /* USARTx configured as follow:
  50.         - BaudRate = 9600 baud  波特率
  51.         - Word Length = 8 Bits  數(shù)據(jù)長(zhǎng)度
  52.         - One Stop Bit          停止位
  53.         - No parity             校驗(yàn)方式
  54.         - Hardware flow control disabled (RTS and CTS signals) 硬件控制流
  55.         - Receive and transmit enabled                         使能發(fā)送和接收
  56.   */
  57.   USART_InitStructure.USART_BaudRate = 9600;
  58.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  59.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  60.   USART_InitStructure.USART_Parity = USART_Parity_No;
  61.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  62.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  63.   USART_Init(UART5, &USART_InitStructure);
  64.         USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//開(kāi)啟中斷
  65.   USART_Cmd(UART5, ENABLE);                    //使能串口

  66.         LED_Init();//LED初始化
  67.   KEY_Init();//按鍵初始化
  68.   SysTick_Init();//延時(shí)初始化
  69.         BEEP_Init();   //蜂鳴器初始化
  70.         printf("\n\rUSART Printf Example: (德飛萊)尼莫M3S開(kāi)發(fā)板串口測(cè)試程序\r輸入任何信息發(fā)送,接收到同樣信息");
  71.   while (1)
  72.   {
  73.         //使用printf函數(shù)循環(huán)發(fā)送固定信息
  74.         Delay_ms(500);                  
  75.   LED2_REV;       
  76.   }
  77. }


  78. PUTCHAR_PROTOTYPE
  79. {
  80.   /* Place your implementation of fputc here */
  81.   /* e.g. write a character to the USART */
  82.   USART_SendData(UART5, (uint8_t) ch);

  83.   /* 循環(huán)等待直到發(fā)送結(jié)束*/
  84.   while (USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET)
  85.   {}

  86.   return ch;
  87. }
復(fù)制代碼

所有資料51hei提供下載:
串口5測(cè)試協(xié)議.zip (310.8 KB, 下載次數(shù): 19)


評(píng)分

參與人數(shù) 1黑幣 +5 收起 理由
linuxcso + 5 贊一個(gè)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:104982 發(fā)表于 2018-5-4 19:51 | 顯示全部樓層
請(qǐng)問(wèn)程序包括 接收中斷處理嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表