低优先级放在高优先级内势必是程序卡死
把高优先级放到低优先级内,会使程序卡死
可修改
Debuger调试方法
Pwm
rcc
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
void LED_Disp(uint8_t dsLED)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
void setup()
{HAL_TIM_Base_Start_IT(&htim6);LED_Disp(0x00);//关掉所有LEDLCD_Init();//LCD初始化LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}void loop()
{/* LCD演示 */sprintf(text," number:%x ",led_sta);LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{/* 按键操作演示 */if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET){led_sta=(led_sta&0xfe)|0x01;led_sta=(led_sta&0xfd)|0x00;LED_Disp(led_sta); }}if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET){led_sta=(led_sta&0xfd)|0x02;led_sta=(led_sta&0xfe)|0x00;LED_Disp(led_sta); }} }
key_tim2
#include "key.h"#ifndef _KEY_H_
#define _KEY_H_#include "my_main.h"
struct Bkeys
{uint16_t age;uint8_t short_flag;uint8_t long_flag;uint8_t press;uint8_t double_ageEN;uint8_t double_age;uint8_t double_flag;
};
void key_serv(void);
void key_serv_long(void);
void key_serv_double(void);
#endifstruct Bkeys bkey[5]={0,0,0,0,0,0,0};uint8_t key_read()
{if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0) return 1;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0) return 2;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0) return 3;else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0) return 4;else return 0;
}void key_serv()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age==2)bkey[key_sta].short_flag=1;}else{for(int i=0;i<5;i++) bkey[i].age=0;}
}
/*长按快速增加*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1)bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
//
// bkey[i].press=0;
// bkey[i].age=0;
// bkey[i].long_flag=0;
// }
// }
// if(bkey[key_sta].age>69)bkey[key_sta].long_flag=1;
//}
/*长按后抬起执行*/
void key_serv_long()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].age>69)bkey[i].long_flag=1;if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].short_flag=1;bkey[i].press=0;bkey[i].age=0;}}
}
/*融合按键双击*/
void key_serv_double()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].double_ageEN==1&&bkey[i].press==1){bkey[i].double_flag=1;bkey[i].press=0;bkey[i].double_ageEN=0; }if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].double_ageEN=1;if(bkey[i].double_ageEN==1)bkey[i].double_age++;if(bkey[i].double_ageEN==1&&bkey[i].double_age>20){bkey[i].short_flag=1;bkey[i].double_age=0;bkey[i].double_ageEN=0;}bkey[i].press=0;bkey[i].age=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69)bkey[key_sta].long_flag=1;
}
//PWM模版
/-----------------------------------------key------------------------------------------------------/
#include "key.h"struct Bkeys bkey[5]={0,0,0,0};uint8_t key_read()
{if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0) return 1;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0) return 2;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0) return 3;else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0) return 4;else return 0;
}void key_serv()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;}else{for(int i=0;i<5;i++) bkey[i].age=0;}
}
/*长按快速增加功能*/
void key_serv_long()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].short_flag=1;bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}void key_serv_double()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].double_ageEN==1&&bkey[i].press==1){bkey[i].double_flag=1;bkey[i].double_ageEN=0;bkey[i].press=0;}if(bkey[i].press&&bkey[i].long_flag==0){bkey[i].double_ageEN=1;}if(bkey[i].double_ageEN==1)bkey[i].double_age++;if(bkey[i].double_ageEN==1&&bkey[i].double_age>20){bkey[i].short_flag=1;bkey[i].double_ageEN=0;bkey[i].double_age=0;}bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
extern struct Bkeys bkey[];void LED_Disp(uint8_t dsLED)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
void setup()
{HAL_TIM_Base_Start_IT(&htim6);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);LED_Disp(0x00);//关掉所有LEDLCD_Init();//LCD初始化LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}void loop()
{static uint16_t temp=0;if(bkey[1].short_flag==1){/* LCD演示 */sprintf(text," KEY_1 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 __HAL_TIM_SET_AUTORELOAD(&htim2,250-1);__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);bkey[1].short_flag=0;} if(bkey[2].short_flag==1){/* LCD演示 */sprintf(text," KEY_2 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[2].short_flag=0;} /*长按快速增加功能*/if(bkey[1].long_flag==1){/* LCD演示 */sprintf(text," KEY_1 long!,%d ",temp++);LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 } if(bkey[1].double_flag==1){/* LCD演示 */sprintf(text," KEY_1 double! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[1].double_flag=0;} /*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM6)key_serv_double();
}
TIM_CAP+systick
/---------------------------key----------------------------------/
#include "key.h"struct Bkeys bkey[5]={0,0,0,0};uint8_t key_read()
{if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0) return 1;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0) return 2;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0) return 3;else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0) return 4;else return 0;
}void key_serv()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;}else{for(int i=0;i<5;i++) bkey[i].age=0;}
}
/*长按快速增加功能*/
void key_serv_long()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].short_flag=1;bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}void key_serv_double()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].double_ageEN==1&&bkey[i].press==1){bkey[i].double_flag=1;bkey[i].double_ageEN=0;bkey[i].press=0;}if(bkey[i].press&&bkey[i].long_flag==0){bkey[i].double_ageEN=1;}if(bkey[i].double_ageEN==1)bkey[i].double_age++;if(bkey[i].double_ageEN==1&&bkey[i].double_age>20){bkey[i].short_flag=1;bkey[i].double_ageEN=0;bkey[i].double_age=0;}bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
extern struct Bkeys bkey[];
uint32_t time500ms;void LED_Disp(uint8_t dsLED)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为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)
{uint8_t pos=0x01<<(num-1);led_sta=(led_sta&(~pos))|(pos*sta);LED_Disp(led_sta);
}
void setup()
{HAL_TIM_Base_Start_IT(&htim6);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//TIM15_CH1输入演示HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);//TIM3_CH2输入演示HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);LED_Disp(0x00);//关掉所有LEDLCD_Init();//LCD初始化LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);time500ms=uwTick;
}void loop()
{static uint16_t temp=0;float frq1,frq2=0;float duty1,duty2=0;frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));sprintf(text,"FRQ:%.2fHz ",frq1);LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY:%.2f%% ",duty1);LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新 frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));sprintf(text,"FRQ2:%.2fHz ",frq2);LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY2:%.2f%% ",duty2);LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新 if(uwTick-time500ms>500){static uint8_t LED_sta=0;LED_Chg(2,LED_sta);LED_sta=!LED_sta;time500ms=uwTick;}if(bkey[1].short_flag==1){/* LCD演示 */sprintf(text," KEY_1 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 __HAL_TIM_SET_AUTORELOAD(&htim2,250-1);__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);bkey[1].short_flag=0;} if(bkey[2].short_flag==1){/* LCD演示 */sprintf(text," KEY_2 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[2].short_flag=0;} /*长按快速增加功能*/if(bkey[1].long_flag==1){/* LCD演示 */sprintf(text," KEY_1 long!,%d ",temp++);LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 } if(bkey[1].double_flag==1){/* LCD演示 */sprintf(text," KEY_1 double! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[1].double_flag=0;} /*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM6)key_serv_double();
}
RTC实验
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
extern struct Bkeys bkey[];void LED_Disp(uint8_t dsLED)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
void setup()
{HAL_TIM_Base_Start_IT(&htim6);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//TIM15_CH1输入演示HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);//TIM3_CH2输入演示HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);LED_Disp(0x00);//关掉所有LEDLCD_Init();//LCD初始化LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}void loop()
{static uint16_t temp=0;float frq1,frq2=0;float duty1,duty2=0;RTC_TimeTypeDef time;RTC_DateTypeDef date;HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);sprintf(text,"time:%d:%d:%d ",time.Hours,time.Minutes,time.Seconds);LCD_DisplayStringLine(Line9,(uint8_t *)text);//局部刷新 frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));sprintf(text,"FRQ:%.2fHz ",frq1);LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY:%.2f%% ",duty1);LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新 frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));sprintf(text,"FRQ2:%.2fHz ",frq2);LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY2:%.2f%% ",duty2);LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新 if(bkey[1].short_flag==1){/* LCD演示 */sprintf(text," KEY_1 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 __HAL_TIM_SET_AUTORELOAD(&htim2,250-1);__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);bkey[1].short_flag=0;} if(bkey[2].short_flag==1){/* LCD演示 */sprintf(text," KEY_2 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[2].short_flag=0;} /*长按快速增加功能*/if(bkey[1].long_flag==1){/* LCD演示 */sprintf(text," KEY_1 long!,%d ",temp++);LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 } if(bkey[1].double_flag==1){/* LCD演示 */sprintf(text," KEY_1 double! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[1].double_flag=0;} /*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM6)key_serv_double();
}
key
#include "key.h"struct Bkeys bkey[5]={0,0,0,0};uint8_t key_read()
{if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0) return 1;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0) return 2;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0) return 3;else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0) return 4;else return 0;
}void key_serv()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;}else{for(int i=0;i<5;i++) bkey[i].age=0;}
}
/*长按快速增加功能*/
void key_serv_long()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].short_flag=1;bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}void key_serv_double()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].double_ageEN==1&&bkey[i].press==1){bkey[i].double_flag=1;bkey[i].double_ageEN=0;bkey[i].press=0;}if(bkey[i].press&&bkey[i].long_flag==0){bkey[i].double_ageEN=1;}if(bkey[i].double_ageEN==1)bkey[i].double_age++;if(bkey[i].double_ageEN==1&&bkey[i].double_age>20){bkey[i].short_flag=1;bkey[i].double_ageEN=0;bkey[i].double_age=0;}bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
adc相关配置及代码
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
extern struct Bkeys bkey[];void LED_Disp(uint8_t dsLED)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
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;
}
float adc_read_os(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/65536.0f;return adc_f;
}
void setup()
{HAL_TIM_Base_Start_IT(&htim6);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//TIM15_CH1输入演示HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);//TIM3_CH2输入演示HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);LED_Disp(0x00);//关掉所有LEDLCD_Init();//LCD初始化LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}void loop()
{static uint16_t temp=0;float frq1,frq2=0;float duty1,duty2=0;float adc_data_PB15;RTC_TimeTypeDef time;RTC_DateTypeDef date;HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);sprintf(text,"time:%d:%d:%d ",time.Hours,time.Minutes,time.Seconds);LCD_DisplayStringLine(Line9,(uint8_t *)text);//局部刷新 frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));sprintf(text,"FRQ:%.2fHz ",frq1);LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY:%.2f%% ",duty1);LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新 frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));sprintf(text,"FRQ2:%.2fHz ",frq2);LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新 sprintf(text,"DUTY2:%.2f%% ",duty2);LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新 adc_data_PB15=adc_read_os(&hadc2);sprintf(text,"PB15:%.4fV ",adc_data_PB15);LCD_DisplayStringLine(Line5,(uint8_t *)text);//局部刷新 if(bkey[1].short_flag==1){/* LCD演示 */sprintf(text," KEY_1 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 __HAL_TIM_SET_AUTORELOAD(&htim2,250-1);__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);bkey[1].short_flag=0;} if(bkey[2].short_flag==1){/* LCD演示 */sprintf(text," KEY_2 down! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[2].short_flag=0;} /*长按快速增加功能*/if(bkey[1].long_flag==1){/* LCD演示 */sprintf(text," KEY_1 long!,%d ",temp++);LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 } if(bkey[1].double_flag==1){/* LCD演示 */sprintf(text," KEY_1 double! ");LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新 bkey[1].double_flag=0;} /*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM6)key_serv_double();
}
key
#include "key.h"#ifndef _KEY_H_
#define _KEY_H_#include "my_main.h"
struct Bkeys
{uint16_t age;uint8_t short_flag;uint8_t press;uint8_t long_flag;uint8_t double_ageEN;uint8_t double_age;uint8_t double_flag;
};
void key_serv(void);
void key_serv_long(void);
void key_serv_double(void);
#endifstruct Bkeys bkey[5]={0,0,0,0};uint8_t key_read()
{if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0) return 1;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0) return 2;else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0) return 3;else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0) return 4;else return 0;
}void key_serv()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;}else{for(int i=0;i<5;i++) bkey[i].age=0;}
}
/*长按快速增加功能*/
void key_serv_long()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].press==1&&bkey[i].long_flag==0)bkey[i].short_flag=1;bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}void key_serv_double()
{uint8_t key_sta=key_read();if(key_sta!=0){bkey[key_sta].age++;if(bkey[key_sta].age>1) bkey[key_sta].press=1;}else{for(int i=0;i<5;i++) {if(bkey[i].double_ageEN==1&&bkey[i].press==1){bkey[i].double_flag=1;bkey[i].double_ageEN=0;bkey[i].press=0;}if(bkey[i].press&&bkey[i].long_flag==0){bkey[i].double_ageEN=1;}if(bkey[i].double_ageEN==1)bkey[i].double_age++;if(bkey[i].double_ageEN==1&&bkey[i].double_age>20){bkey[i].short_flag=1;bkey[i].double_ageEN=0;bkey[i].double_age=0;}bkey[i].age=0;bkey[i].press=0;bkey[i].long_flag=0;}}if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}