STM32 串口1打印例程
單片機源程序如下:
- /*-------------------------------------------------------------------------------
- 文件名稱:main.c
- 文件描述:通過串口1,使用printf函數打印信息,編譯時需勾選Use MicroLIB
- 硬件平臺:尼莫M3S開發板
- 備 注:通過簡單修改可以移植到其他開發板,部分資料來源于網絡。
- ---------------------------------------------------------------------------------*/
- #include <stdio.h>
- #include "stm32f10x.h"
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "timer.h"
- #include "beep.h"
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- int main(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA時鐘
- USART_DeInit(USART1); //復位串口1
- //USART1_TX PA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
-
- //USART1_RX PA.10
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
- /* USARTx configured as follow:
- - BaudRate = 9600 baud 波特率
- - Word Length = 8 Bits 數據長度
- - One Stop Bit 停止位
- - No parity 校驗方式
- - Hardware flow control disabled (RTS and CTS signals) 硬件控制流
- - Receive and transmit enabled 使能發送和接收
- */
- 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_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE); //使能串口
- LED_Init();//LED初始化
- KEY_Init();//按鍵初始化
- SysTick_Init();//延時初始化
- BEEP_Init(); //蜂鳴器初始化
- while (1)
- {
- //使用printf函數循環發送固定信息
- printf("\n\rUSART Printf Example: (德飛萊)尼莫M3S開發板串口測試程序\r");
- Delay_ms(500);
- LED2_REV;
- }
- }
- PUTCHAR_PROTOTYPE
- {
- /* Place your implementation of fputc here */
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
0.png (43.5 KB, 下載次數: 55)
下載附件
2017-6-13 16:32 上傳
所有資料51hei提供下載:
Keil代碼.7z
(178.77 KB, 下載次數: 38)
2022-10-17 23:27 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|