关于STM32和keil5串口打印

本文介绍了如何使用STM32(以STM32F103C8T6为例)通过USB转TTL模块实现串口打印。主要步骤包括硬件连接(TXD-PA10, RXD-PA9)和代码配置,简化后的代码仅包含发送部分,不涉及接收。在main.c文件中调用uart_init函数初始化串口,并使用printf函数进行串口打印,实现了每50ms打印一次'hello '。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

因为前一段时间要玩玩stm32的串口打印,上网搜了些资料,感觉也不难。当然大多数都是借鉴正点原子的代码,我也是。。。这次我就直接简化了代码,仅仅有MCU发送到电脑的部分(因为串口打印的话,接收也不那么必要吧…)

一、工具

1、stm32(我用的是c8t6最小系统板)
2、usb转ttl模块
3、PC电脑
4、下载器(我用的st-link)
因为我用的面包板,所以要加上几根杜邦线

二、使用步骤

1.连线方式

因为stm32f103c8t6的的串口外设—TXD->PA9 RXD->PA10
所以TTL的TXD—PA10,RXD—PA9
VCC、GND接上就行(不过我看哔站上VCC可以不用接,只要共地就行)

2.代码

代码如下:

USART.C
#include "Device/Include/stm32f10x.h"   // Device header
#include "Usart.h"
#include "sys.h"
#include "usart.h"	  
#include "stdio.h"
#include "Delay.h"
//
//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  
//借鉴正点原子的串口打印重映射
#if 1
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 
FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
//int fputc(int ch, FILE *f)
//{      
//	while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
//    USART1->DR = (u8) ch;      
//	return ch;
//}
int fputc(int ch, FILE *f)
{
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
	USART_SendData(USART1, (uint8_t) ch);
	return ch;
}
#endif 
void uart_init(u32 bound){
  //GPIO端口设置
  GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//使能USART1,GPIOA时钟
	USART_DeInit(USART1);
  
	//USART1_TX   GPIOA.9
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
   
  //USART1_RX	  GPIOA.10初始化
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10  

  //Usart1 NVIC 配置
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
  
   //USART 初始化设置

	USART_InitStructure.USART_BaudRate = bound;//串口波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	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); //初始化串口1
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  USART_Cmd(USART1, ENABLE);                    //使能串口1 

}

USART.H
#ifndef  __USART_H_
#define  __USART_H_
#include "stdio.h"
#include "Device/Include/stm32f10x.h" 
#include "sys.h" 
void uart_init(u32 bound);

#endif
main.c
#include "Device/Include/stm32f10x.h" // Device header
#include "Usart.h"
#include "Delay.h"
#include "stdio.h"
int main()
{
	uart_init(115200);
	while(1)
	{
		Delay_ms(50);
		printf("hello\r\n");
  }
}


运行的结果:
在这里插入图片描述

总结

我只是把正点原子的代码整理下,要玩的道友就瞥一眼吧。在这里插嘿嘿入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值