標(biāo)題: STM32 串口1打印例程 [打印本頁]

作者: 楊子123456    時間: 2017-6-13 10:56
標(biāo)題: STM32 串口1打印例程
STM32 串口1打印例程

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



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

  21. int main(void)
  22. {
  23.         GPIO_InitTypeDef GPIO_InitStructure;
  24.         USART_InitTypeDef USART_InitStructure;

  25.          
  26.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);        //使能USART1,GPIOA時鐘
  27.            USART_DeInit(USART1);  //復(fù)位串口1
  28.           //USART1_TX   PA.9
  29.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  30.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //復(fù)用推挽輸出
  32.     GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
  33.    
  34.     //USART1_RX          PA.10
  35.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  36.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
  37.     GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA10     
  38.   /* USARTx configured as follow:
  39.         - BaudRate = 9600 baud  波特率
  40.         - Word Length = 8 Bits  數(shù)據(jù)長度
  41.         - One Stop Bit          停止位
  42.         - No parity             校驗方式
  43.         - Hardware flow control disabled (RTS and CTS signals) 硬件控制流
  44.         - Receive and transmit enabled                         使能發(fā)送和接收
  45.   */
  46.   USART_InitStructure.USART_BaudRate = 9600;
  47.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  48.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  49.   USART_InitStructure.USART_Parity = USART_Parity_No;
  50.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  51.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  52.   USART_Init(USART1, &USART_InitStructure);
  53.   USART_Cmd(USART1, ENABLE);                    //使能串口

  54.         LED_Init();//LED初始化
  55.   KEY_Init();//按鍵初始化
  56.   SysTick_Init();//延時初始化
  57.         BEEP_Init();   //蜂鳴器初始化
  58.   while (1)
  59.   {
  60.         //使用printf函數(shù)循環(huán)發(fā)送固定信息
  61.   printf("\n\rUSART Printf Example: (德飛萊)尼莫M3S開發(fā)板串口測試程序\r");
  62.         Delay_ms(500);                  
  63.   LED2_REV;        
  64.   }
  65. }


  66. PUTCHAR_PROTOTYPE
  67. {
  68.   /* Place your implementation of fputc here */
  69. ……………………

  70. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
Keil代碼.7z (178.77 KB, 下載次數(shù): 38)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1