在STM32提供的官方库里只有串口一的初始化代码,但当你需要用到两个以上的串口,就得自己写相应的代码,下面是串口2的,其他的串口也差不多。
usart2.c
#include "sys.h"
#include "usart2.h"
//////////////////////////////////////////////////////////////////////////////////
//如果使用ucos,则包括下面的头文件即可.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos 使用
#endif
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout2;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit2(int x)
{
x = x;
}
//重定义fputc函数
int fputc2(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
USART2->DR = (u8) ch;
return ch;
}
#endif
/*使用microLib的方法*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/


1万+

被折叠的 条评论
为什么被折叠?



