日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STM32L152RC 在keil4中使用printf()和scanf() 函数

發布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()了。

總結

以上是生活随笔為你收集整理的STM32L152RC 在keil4中使用printf()和scanf() 函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。