?? usart_printf.c
字號:
/****************************************************************************
* Copyright (C), 2010 安富萊電子 www.armfly.com
*
* 文件名: usart_printf.c
* 內容簡述: 本模塊實現printf和scanf函數重定向到串口1
* 實現重定向,只需要添加2個函數
int fputc(int ch, FILE *f);
int fgetc(FILE *f);
*
* 文件歷史:
* 版本號 日期 作者 說明
* v0.1 2009-12-27 armfly 創建該文件
*
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
/*******************************************************************************
函數名:PrintfLogo
輸 入: 例程名稱和例程最后更新日期
輸 出:
功能說明:
*/
void PrintfLogo(char *strName, char *strDate)
{
printf("*************************************************************\n\r");
printf("* Example Name : %s\r\n", strName);
printf("* Update Date : %s\r\n", strDate);
printf("* StdPeriph_Lib Version : V3.1.2\n\r");
printf("* \n\r");
printf("* Copyright www.armfly.com \r\n");
printf("* QQ : 1295744630 \r\n");
printf("* Email : armfly@qq.com \r\n");
printf("*************************************************************\n\r");
}
/*******************************************************************************
函數名:USART_Configuration
輸 入:
輸 出:
功能說明:
初始化串口硬件設備,未啟用中斷。
配置步驟:
(1)打開GPIO和USART的時鐘
(2)設置USART兩個管腳GPIO模式
(3)配置USART數據格式、波特率等參數
(4)最后使能USART功能
*/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 第1步:打開GPIO和USART部件的時鐘 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* 第2步:將USART Tx的GPIO配置為推挽復用模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第3步:將USART Rx的GPIO配置為浮空輸入模式
由于CPU復位后,GPIO缺省都是浮空輸入模式,因此下面這個步驟不是必須的
但是,我還是建議加上便于閱讀,并且防止其它地方修改了這個口線的設置參數
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第3步已經做了,因此這步可以不做
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
*/
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第4步:配置USART參數
- BaudRate = 115200 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 = 115200;
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);
/* 第5步:使能 USART, 配置完畢 */
USART_Cmd(USART1, ENABLE);
/* CPU的小缺陷:串口配置好,如果直接Send,則第1個字節發送不出去
如下語句解決第1個字節無法正確發送出去的問題 */
USART_ClearFlag(USART1, USART_FLAG_TC); /* 清發送外城標志,Transmission Complete flag */
}
/*******************************************************************************
函數名:fputc
輸 入:
輸 出:
功能說明:
重定義putc函數,這樣可以使用printf函數從串口1打印輸出
*/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
/*******************************************************************************
函數名:fputc
輸 入:
輸 出:
功能說明:
重定義getc函數,這樣可以使用scanff函數從串口1輸入數據
*/
int fgetc(FILE *f)
{
/* 等待串口1輸入數據 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
return (int)USART_ReceiveData(USART1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -