F103C8T6 Template
附上f103c8t6模板(正点原子风格):点击下载 附上一些常用的代码块
HAL
溢出中断
PeriodElapsedCallback
**一般用来点亮运行状态指示灯**
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim7)
{
static uint16_t cnt;
cnt++;
if(cnt>999)
{
cnt = 0;
HAL_GPIO_TogglePin(D2_GPIO_Port,D2_Pin);
}
}
}
按键
按键–支持一次、多次
**自行布置位置**
#define KEY0_PRES 0
#define KEY1_PRES 1
#define K0 HAL_GPIO_ReadPin(K0_GPIO_Port,K0_Pin)
#define K1 HAL_GPIO_ReadPin(K1_GPIO_Port,K1_Pin)
uint8_t KEY_Scan(uint8_t mode)
{
static uint8_t key_up=1;
if(mode==1)key_up=1;
if(key_up&&(K0==0||K1==0))
{
HAL_Delay(10);
key_up=0;
if(K0==0) return KEY0_PRES;
else if(K1==0) return KEY1_PRES;
}else if(K0==1&&K1==1)key_up=1;
return 99;
}
void Key_Proc()
{
static uint8_t val;
val = KEY_Scan(0);
if(val == 0){
printf("K0\r\n");
}else if(val == 1){
printf("K1\r\n");
}
}
HAL串口
串口
main:
需要:include "string.h"
if(Fu)
{
printf("%s",(char*)Rb);
memset(Rb,0,sizeof(Rb));
Fu=0;
Rc=0;
}
IRQ:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart==&huart1)
{
Rb[Rc] = Tp[0];
Rc++;
if(0x0a == Tp[0])
{
Fu = 1;
}
HAL_UART_Receive_IT(&huart1,(uint8_t *)Tp, Tl);
}
}
.h
#define Rl 1024
#define Tl 1
extern uint8_t Rb[Rl],Rc;
extern _Bool Fu;
extern uint8_t Tp[Tl];
.c
uint8_t Rb[Rl] = {0},Rc=0;
_Bool Fu=0;
uint8_t Tp[Tl] = {0};
下面这句放在.c中串口的初始化函数void MX_USARTx_UART_Init(void)的用户编写区域里面:
HAL_UART_Receive_IT(&huart1, (uint8_t *)Tp, Tl);
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)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
u8 Res;
Res = USART_ReceiveData(USART1);
Rb[Rc] = USART_ReceiveData(USART1);
if(Res==0x0a)
{
fu = 1;
}
Rc++;
}
}
Common
字符串判断
字符串判断
if(strstr(Rb,"\"Flag\":1"))
else if(strstr(Rb,"\"Flag\":0"))
|