在all.c定义缓存变量
c
char uart_tx[50];//uart缓冲区
char uart_rx[50];//uart缓冲区
USRT发送(使用strlen时需要在all.h添加string.h)
c
sprintf(uart_tx ," PB15:%.4f V \r\n ",adc_data_PB15);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);//串口名字 数据 长度 发送间隔
UART接收(接收时需要在setup()中打开)
c
HAL_UARTEx_ReceiveToIdle_IT(&huart1 ,(uint8_t *)uart_rx,50);//用一次关一次,需要重新打开
当收到串口收到hello,屏幕打印"World!"
c
//UART接受
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)//串口接收到数据后就会产生一个回调
{
char recieve[5];//定义接收数据大小
char key[5]="hello";
sscanf(uart_rx,"%s",recieve);//扫描接收数据
if(strcmp(recieve,key) == 0)//对比数据是否为hello
{
sprintf(text," World! ");
LCD_DisplayStringLine(Line6,(uint8_t *)text);
}
HAL_UARTEx_ReceiveToIdle_IT(&huart1 ,(uint8_t *)uart_rx ,50);//用一次关一次,需要重新打开
}
全部代码
c
#include "all.h"
uint8_t led_sta=0x20;//LED初始化参数
extern struct Bkeys bkey[];//按键存储
char text[30]; //存放显示文本
char uart_tx[50];//uart发送缓冲区
char uart_rx[50];//uart接受缓冲区
void LED_Disp(uint8_t dsLED)//LED控制
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位。控制C8~C15引脚,值为1的点亮
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
void LED_Chg(uint8_t num,uint8_t sta)//单独控制一个LED
{
uint8_t pos = 0x01<<(num-1);
led_sta =(led_sta&(~pos))|(pos*sta);
LED_Disp(led_sta);
}
float adc_read(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start (hadc);
adc_val = HAL_ADC_GetValue(hadc);
adc_f = adc_val*3.3f/4096.0f;
return adc_f;
}
void setup()
{
HAL_TIM_Base_Start_IT(&htim6);//开启TIM
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//产生PWM
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);//打开pwm捕获
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);//打开pwm捕获
HAL_ADCEx_Calibration_Start(&hadc2 ,ADC_SINGLE_ENDED);//ADC自动校准
HAL_UARTEx_ReceiveToIdle_IT(&huart1 ,(uint8_t *)uart_rx,50);//用一次关一次,需要重新打开
//LED初始状态
LED_Disp(0x00);
//LCD初始化
LCD_Init();
LCD_Clear(Black );
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
}
void loop()
{
//PWM捕获
float frq1 = 0;
float duty1 = 0;
frq1 = 1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);//获取频率
duty1 = ((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1));//获取占空比
sprintf(text," FQR:%.2f Hz ",frq1);
LCD_DisplayStringLine(Line2,(uint8_t *)text);
sprintf(text," DUTY:%.2f %% ",duty1);
LCD_DisplayStringLine(Line3,(uint8_t *)text);
//ADC
float adc_data_PB15;
adc_data_PB15 = adc_read(&hadc2);
sprintf(text," PB15:%.4f V ",adc_data_PB15);
LCD_DisplayStringLine(Line4,(uint8_t *)text);
// //UART发送
// sprintf(uart_tx ," PB15:%.4f V \r\n ",adc_data_PB15);
// HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),500);//串口名字 数据 长度 发送间隔
if(bkey[1].short_flag==1)
{
LED_Chg(1,1);
sprintf(text," down! ");
LCD_DisplayStringLine(Line1,(uint8_t *)text);
bkey[1].short_flag = 0;
}
if(bkey[1].long_flag==1)
{
LED_Chg(2,1);
sprintf(text," long_down! ");
LCD_DisplayStringLine(Line1,(uint8_t *)text);
}
}
//UART接受
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)//串口接收到数据后就会产生一个回调
{
char recieve[5];//定义接收数据大小
char key[5]="hello";
sscanf(uart_rx,"%s",recieve);//扫描接收数据
if(strcmp(recieve,key) == 0)//对比数据是否为hello
{
sprintf(text," World! ");
LCD_DisplayStringLine(Line6,(uint8_t *)text);
}
HAL_UARTEx_ReceiveToIdle_IT(&huart1 ,(uint8_t *)uart_rx ,50);//用一次关一次,需要重新打开
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//按键服务
{
if(htim->Instance == TIM6)
key_serv();
}