① 硬件仿真
(1)寄存器值
1、MPU;
2、外设;
(2)内存类
1、栈内值;
2、全局变量;
(3)控制类
1、断点;
2、单步:
< 1 > 遇到函数进入;
< 2 > 遇到函数跳出;
< 3 > 遇到函数不跳;
② 串口中断
(1)NVIC:
1、寄存器组;
2、设置抢占和响应优先级;
3、使能;
(2)UART1:
1、RXNE;
2、使能;
(3)应用;
③ 用中断方式控制通信
成果:不同长度字符串读取,控制呼吸灯颜色和蜂鸣器开关
代码:
#include "UART1_Use.h"
#define Timeout 0xFFFF
#define MAX 20
static u32 j=MAX, i=0, k=0, l=0, a=3;
uint8_t UART1_Ctrl[6][100]={"led_r","led_g","led_b","beep_on","led_off","beep_off"};
uint8_t Mesage[100];
uint8_t UART1_IT_Rev;
uint32_t Rec_Str_Len = 0;
HAL_StatusTypeDef UART1_SendStr(uint8_t *buf,uint32_t size)
{
return HAL_UART_Transmit(&huart1, buf, size, Timeout);
}
HAL_StatusTypeDef UART1_ResvStr(uint8_t *buf,uint32_t size)
{
return HAL_UART_Receive(&huart1, buf, size, Timeout);
}
/********************** printf 重定向 ***************************/
//
int fputc(int data, FILE *f) //
{ //
UART1_SendStr((uint8_t *)&data,1); //
} //
//
/*******************************************************************************/
void UART1_App_Init()
{
if(Rec_Str_Len == 8)
{
if( ! memcmp(Mesage,UART1_Ctrl[5],8) )
BEEP_OFF();
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
if(Rec_Str_Len == 7)
{
if( ! memcmp(Mesage,UART1_Ctrl[4],7) )
{
a = 3;
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
if( ! memcmp(Mesage,UART1_Ctrl[3],7) )
{
BEEP_ON();
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
}
if(Rec_Str_Len == 5)
{
if( ! memcmp(Mesage,UART1_Ctrl[0],5) )
{
a = 0;
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
if( ! memcmp(Mesage,UART1_Ctrl[1],5) )
{
a = 1;
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
if( ! memcmp(Mesage,UART1_Ctrl[2],5) )
{
a = 2;
Rec_Str_Len = 0;
memset(Mesage,0,100);
}
}
}
void UART1_App_Crtl()
{
while(1)
{
UART1_App_Init();
if(a == 0)
{
LED_G_OFF();
LED_B_OFF();
LED_R_ON();
HAL_Delay(i);
LED_R_OFF();
HAL_Delay(j);
}
if(a == 1)
{
LED_R_OFF();
LED_B_OFF();
LED_G_ON();
HAL_Delay(i);
LED_G_OFF();
HAL_Delay(j);
}
if(a == 2)
{
LED_R_OFF();
LED_G_OFF();
LED_B_ON();
HAL_Delay(i);
LED_B_OFF();
HAL_Delay(j);
}
if(a == 3)
LED_ALL_OFF();
if(l < MAX)
{
l++;
i++;
j--;
}
if(l == MAX)
{
k++;
i--;
j++;
}
if(k == MAX)
{
HAL_Delay(100);
k = 0;
l = 0;
}
}
}
void UART1_IT_EN()
{
HAL_UART_Receive_IT(&huart1, &UART1_IT_Rev, 1);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
Mesage[Rec_Str_Len] = UART1_IT_Rev;
Rec_Str_Len ++;
UART1_IT_EN();
}