串口
串口重定义
#include "stdio.h"
#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);
return ch;
}
串口接收中断
uint8_t rs232_Buf = 0;
uint8_t rs232_Buf_count = 0;
char rs232_Buf_try[rs232_Buf_Max];
HAL_UART_Receive_IT(&huart4, &rs232_Buf, 1);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart4)
{
if (rs232_Buf_count < rs232_Buf_Max)
{
rs232_Buf_try[rs232_Buf_count++] = rs232_Buf;
}
else
{
rs232_Buf_count = 0;
}
HAL_UART_Receive_IT(&huart4, &rs232_Buf, 1);
}
}
串口调试打印
#define DEBUG
#ifdef DEBUG
#define Debug_Print(format, ...) \
{ \
printf("\r\nFile : %s, Line : %04d, %s," format, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
fflush(stdout); \
}
#define Printf(format, ...) \
{ \
printf(format "\r\n", ##__VA_ARGS__); \
fflush(stdout); \
}
#define Print(format, ...) \
{ \
printf(format, ##__VA_ARGS__); \
fflush(stdout); \
}
#else
#define Debug_Print(format, ...)
#define Printf(format, ...)
#define Print(format, ...)
#endif
串口发送
unsigned char str[100];
memset(str, 0, 100);
sprintf((char *)str, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", ServerIP, ServerPort);
HAL_UART_Transmit_IT(&huart4, (uint8_t *)str, strlen(str));
定时器
开启定时器
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_SR_UIF);
HAL_TIM_Base_Start_IT(&htim3);
__HAL_TIM_SET_COUNTER(&htim3, 0);
HAL_TIM_Base_Stop_IT(&htim2);
定时器中断
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim3)
{
}
if (htim == &htim2)
{
if (time_count++ >= maximum_time)
{
time_count = 0;
}
}