STM32L152RC 在keil4中使用printf()和scanf() 函数
2016-05-28 21:06
567 查看
STM32L152RC 在keil4中使用printf()和scanf() 函数
1、在keil中配置使用Micro LIB,如下图
2、配置USART模块
void SYS_USARTInit(void) { static USART_InitTypeDef USART_InitStructure; static GPIO_InitTypeDef GPIO_InitStructure; 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; /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Enable UART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Connect PA9 to USARTx_Tx */ GPIO_PinAFConfig(USE_USART_PORT, USE_USART_TX_GPIO_PinSource, GPIO_AF_USART1); /* Connect PA10 to USARTx_Rx */ GPIO_PinAFConfig(USE_USART_PORT, USE_USART_RX_GPIO_PinSource, GPIO_AF_USART1); /* Configure USART Tx PA9 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = USE_USART_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USE_USART_PORT, &GPIO_InitStructure); /* Configure USART Rx PA10 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = USE_USART_RX_PIN; GPIO_Init(USE_USART_PORT, &GPIO_InitStructure); /* USART configuration */ USART_Init(USE_USART, &USART_InitStructure); /* Enable USART */ USART_Cmd(USE_USART, ENABLE); }
3、重定向printf()
在工程中加入下面代码#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) #define GETCHAR_PROTOTYPE int fgetc(FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(USE_USART, (uint8_t)ch); /* Loop until transmit data register is empty */ while (USART_GetFlagStatus(USE_USART, USART_FLAG_TXE) == RESET) {} return ch; } GETCHAR_PROTOTYPE { while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); return (USART_ReceiveData(USART1)); }
上面三个步骤做完即可在工程中调用printf()和scanf()了。
相关文章推荐
- 详解C语言中printf输出的相关函数
- PHP echo,print,printf,sprintf函数之间的区别与用法详解
- php printf输出格式使用说明
- PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
- php输出echo、print、print_r、printf、sprintf、var_dump的区别比较
- C语言printf详细解析
- 在输入输出字符串时scanf(),printf()和gets(),puts()的区别浅谈
- php中sprintf与printf函数用法区别解析
- 深入剖析PHP中printf()函数格式化使用
- 深入浅析php中sprintf与printf函数的用法及区别
- 深入理解php printf() 输出格式化的字符串
- 浅析Java中print、printf、println的区别
- STM32 IO口双向问题
- 使用CMake来构建STM32工程
- printf的返回值问题(转)
- linux内核中printf函数实现原理分析
- Keil STM32工程环境搭建
- stm32之RTC
- STM32串口操作相关事项
- 【eBox生态圈】连载一:简化STM32的编程之eBox生态圈