串口打印
1.设置串口参数
2.重定向
3.勾选use mcrolib
初始化串口
void USART1_Config(void)
{
/* USART configuration structure for USART1 */
USART_InitTypeDef usart1_init_struct;
/* Bit configuration structure for GPIOA PIN9 and PIN10 */
GPIO_InitTypeDef gpioa_init_struct;
/* Enalbe clock for USART1, AFIO and GPIOA */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA PIN9 alternative function Tx */
gpioa_init_struct.GPIO_Pin = GPIO_Pin_9;
gpioa_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
gpioa_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpioa_init_struct);
/* GPIOA PIN9 alternative function Rx */
gpioa_init_struct.GPIO_Pin = GPIO_Pin_10;
gpioa_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
gpioa_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpioa_init_struct);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
/* Baud rate 9600, 8-bit data, One stop bit
* No parity, Do both Rx and Tx, No HW flow control
*/
usart1_init_struct.USART_BaudRate = 115200;
usart1_init_struct.USART_WordLength = USART_WordLength_8b;
usart1_init_struct.USART_StopBits = USART_StopBits_1;
usart1_init_struct.USART_Parity = USART_Parity_No ;
usart1_init_struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart1_init_struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
/* Configure USART1 */
USART_Init(USART1, &usart1_init_struct);
/* Enable RXNE interrupt */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 global interrupt */
}
2.重定向
int fputc(int ch, FILE *f)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
return(ch);
}
int fgetc(FILE *f)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
3.勾选microLib
main.c
#include "stm32f10x.h"
#include "led.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "usart.h"
#include <stdio.h>
void Delay(uint32_t nCount){
uint32_t i;
for(i=0;i<nCount;i++);
}
int main(){
LED_GPIO_Config();
USART1_Config();
while(1){
LED1(ON);
Delay(0xFFFFFF);
LED1(OFF);
Delay(0xFFFFFF);
printf("2");
}
}