stm32 uart打印

来源:互联网 发布:淘宝千牛客服怎么设置 编辑:程序博客网 时间:2024/06/07 21:53

电子调试借助uart打印,轻松实现实时debug。
STM32模块操作常用的方式,先配置GPIO与相对模块对接,然后对模块配置;stm32串口打印示例如下,以stm32f030x8为例:
1.配置gpio,配置uart:
增加gpio 和 usart lib文件,可以到lib查找相关结构体的使用;
GPIO_InitTypeDef // IO相关结构体
USART_InitTypeDef // uart相关结构体
RCC_AHBPeriphClockCmd // 时钟配置,差点别忘记了
到#include “stm32f0xx_rcc.h”//查看每个模块时钟设置方式,每个芯片基本都有点区别,这个需要留意,确保所选时钟与配置时钟函数对应上;

void Uart_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* Enable GPIO clock */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);/* USART1 Pins configuration ************************************************//* Connect pin to Periph */GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);  /* Configure pins as AF pushpull */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOA, &GPIO_InitStructure);  //USART 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);   USART_Cmd(USART1, ENABLE);                    

}
2. 增加#include “stdio.h”,配置uart打印与系统 io对应,此操作方便用系统printf打印输出;增加相关函数如下:
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch);

/* Loop until transmit data register is empty */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}

return ch;
}
3.在target修改设置如下:
这里写图片描述
4.设置OK后,在main里初始化完成后便可以用printf打印输出。
int main(void)
{
Uart_config();
printf(“uart_init is ok”);
}

1 0
原创粉丝点击