5 直接操作STM32寄存器的UART测试程序(实现printf)和仿真
5.1 串口代码
串口管脚功能配置请参考下表。
uart.h
#ifndef USART_H
#define USART_H
void usart_init(void);
void usart_send(int c);
int usart_recv(void);
#endif
uart.c
#include <stdio.h>
#include "stm32f10x.h" // Device header
void usart_init(void)
{
// 使能APB2的GPIOA的时钟
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
// 使能APB2的USART的时钟
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
// PA9和PA10配置为串口功能
GPIOA->CRH = (GPIOA->CRH & ~(0xF << 4)) | (0xB << 4);
GPIOA->CRH = (GPIOA->CRH & ~(0xF <<