HAL重定向
不使用micro-Lib
需要:#include "stdio.h"
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
#endif
复制代码
HAL 串口1重定向
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//注意把&huart1改为自己的stm32使用的串口号
return ch;
}
复制代码
标准库串口
串口
main:
需要:include "string.h"
if(fu)
{
fu = 0;
//处理过程
Rc= 0;
memset(Rb,0,sizeof(Rb));
}
.c
char Rb[256] = {0};
u8 Rc = 0;
_Bool fu;
.h
extern char Rb[256];
extern u8 Rc;
extern _Bool fu;
IRQ:
void USART1_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
u8 Res;
Res = USART_ReceiveData(USART1); //读取接收到的数据
Rb[Rc] = USART_ReceiveData(USART1);
if(Res==0x0a)//帧尾
{
fu = 1;
}
Rc++;
}
}
复制代码
Common
字符串判断
字符串判断
if(strstr(Rb,""Flag":1"))//判断是否存在子字符串“Flag“:1
else if(strstr(Rb,""Flag":0"))//判断是否存在子字符串“Flag”:0
//内部双引号需要转义
复制代码
标准库重定向
#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)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{}
return ch;
}
复制代码
|