实现功能 但似乎可以不用中断实现

Signed-off-by: kevin <kevin@lmve.net>
This commit is contained in:
2022-05-02 19:14:55 +08:00
parent 8a3d91d0c2
commit e15506fa7e
21 changed files with 3176 additions and 2258 deletions
+62 -3
View File
@@ -1,11 +1,70 @@
#include "buzzer.h"
struct notes *notes;
notes_buff buzzer_play_buff;
void play_ones(uint16_t freq,uint8_t dutya)
void BUZZER_PLAY(uint16_t freq,float duty)
{
TIM2->ARR = (uint32_t)((72000000/freq)-1);
TIM2->CCR3 = (uint32_t)(((72000000/freq)-1)*((float)duty/100));
}
TIM2->ARR = (uint32_t)((72000000/freq)-1);TIM2->CCR3 = (uint32_t)(((72000000/freq)-1)*((float)dutya/100));
void BUZZER_PLAY_INIT()
{
buzzer_play_buff.head=NULL;
buzzer_play_buff.end=NULL;
buzzer_play_buff.play_busy=0;
BUZZER_PLAY(0,0);
}
void BUZZER_PLAY_NOTES(uint16_t freq,uint16_t delay,uint8_t duty)
{
note *buff,*buff2;
buff =(note*)malloc(sizeof(note));
if(buff!=NULL)
{
buff->delay=delay;
buff->duty=duty;
buff->freq=freq;
buff->next=NULL;
}else{return ;}
if(buzzer_play_buff.head==NULL)
{
buzzer_play_buff.head=buff;
buzzer_play_buff.end=buff;
}else
{
buff2=buzzer_play_buff.end;
buff2->next=buff;
buzzer_play_buff.end=buff;
}
}
void BUZZER_PLAY_SERVER()
{
note *buff;
char data;
if(buzzer_play_buff.play_busy)
{
if(HAL_GetTick()>buzzer_play_buff.play_time)
{
buzzer_play_buff.play_busy=0;
BUZZER_PLAY(0,0);
}
}else
{
if(buzzer_play_buff.head!=NULL)
{
buzzer_play_buff.play_busy=1;
buzzer_play_buff.play_time=HAL_GetTick()+buzzer_play_buff.head->delay;
buff=buzzer_play_buff.head->next;
BUZZER_PLAY(buzzer_play_buff.head->freq,buzzer_play_buff.head->duty);
free(buzzer_play_buff.head);
buzzer_play_buff.head=buff;
}
}
}
+25 -1
View File
@@ -5,7 +5,31 @@
void play_ones(uint16_t freq,uint8_t dutya);
typedef struct note
{
struct note *next;
uint16_t freq; //??
uint8_t duty; //???
uint16_t delay; //????
}note;
typedef struct notes_buff
{
note *head;
note *end;
uint32_t play_time;
uint8_t play_busy:1;
}notes_buff;
void BUZZER_PLAY_INIT();
void BUZZER_PLAY_NOTES(uint16_t freq,uint16_t delay,uint8_t duty);
void BUZZER_PLAY_SERVER();
void BUZZER_PLAY(uint16_t freq,float duty);
#endif /* BUZZER_H_ */
+210
View File
@@ -1,15 +1,225 @@
#include "morsr.h"
#define morsecodenum 38
const struct morsecode morsecodes[morsecodenum]=
{
{
2,0x40,'A'
},
{
4,0x80,'B'
},
{
4,0xa0,'C'
},
{
3,0x80,'D'
},
{
1,0x00,'E'
},
{
4,0x20,'F'
},
{
3,0xc0,'G'
},
{
4,0x00,'H'
},
{
2,0x00,'I'
},
{
4,0x70,'J'
},
{
3,0xa0,'K'
},
{
4,0x40,'L'
},
{
2,0xc0,'M'
},
{
2,0x80,'N'
},
{
3,0xe0,'O'
},
{
4,0x60,'P'
},
{
4,0xd0,'Q'
},
{
3,0x40,'R'
},
{
3,0x00,'S'
},
{
1,0x80,'T'
},
{
3,0x20,'U'
},
{
4,0x10,'V'
},
{
3,0x60,'W'
},
{
4,0x90,'X'
},
{
4,0xb0,'Y'
},
{
4,0xc0,'Z'
},
{
5,0x78,'1'
},
{
5,0x38,'2'
},
{
5,0x18,'3'
},
{
5,0x08,'4'
},
{
5,0x00,'5'
},
{
5,0x80,'6'
},
{
5,0xc0,'7'
},
{
5,0xe0,'8'
},
{
5,0xf0,'9'
},
{
5,0xf8,'0'
},
{
6,0x30,'?'
},
{
5,0x90,'/'
},
};
char get_morse_code(uint8_t len,uint8_t code)
{
for(int a=0;a<morsecodenum;a++)
{
if((morsecodes[a].len==len)&&(morsecodes[a].code==code))
{
return morsecodes[a].letter;
}
}
return ' ';
}
char moser_buff[64+8]={0};
uint8_t moser_buff_int=0;
uint8_t moser_code_len=0;
uint8_t moser_code=0;
uint32_t moser_tick_time=0;
uint8_t moser_input_flag=0;
void mo_server()
{
if(moser_input_flag==1&&HAL_GetTick()>moser_tick_time)
{
moser_input_flag=0;
for(int a=0;a<8;a++)
{
moser_buff[moser_buff_int+a]='\0';
}
moser_buff[moser_buff_int]=get_morse_code(moser_code_len,moser_code);
moser_buff_int+=1;
if(moser_buff_int==64)
{
for(int a=0;a<64;a++)
{
moser_buff[a]='\0';
}
moser_buff_int=0;
}
moser_code_len=0;
moser_code=0;
}
}
void mo_di()
{
moser_buff[moser_buff_int+moser_code_len]='.';
moser_code_len+=1;
if(moser_code_len==8){moser_code_len=0;}
moser_tick_time=HAL_GetTick()+400;
moser_input_flag=1;
}
void mo_da()
{
moser_buff[moser_buff_int+moser_code_len]='-';
moser_code|=(0x80>>moser_code_len);
moser_code_len+=1;
if(moser_code_len==8){moser_code_len=0;}
moser_tick_time=HAL_GetTick()+400;
moser_input_flag=1;
}
void mo_one_tick(uint8_t i)
{
static uint8_t last_input=1;
static uint32_t tick_time;
uint16_t tick_time_cat;
if(i!=last_input)
{
last_input=i;
switch(last_input)
{
case 0:
tick_time=HAL_GetTick();
break;
case 1:
tick_time_cat=HAL_GetTick()-tick_time;
if(tick_time_cat<100)
{
}else if(tick_time_cat<200)
{
mo_di();
}else if(tick_time_cat<400)
{
mo_da();
}else
{
}
break;
}
}
}
+12
View File
@@ -3,6 +3,18 @@
#include "main.h"
extern char moser_buff[64+8];
struct morsecode
{
uint8_t len;
uint8_t code;
char letter;
};
void mo_server();
void mo_di();
void mo_da();
void mo_one_tick(uint8_t i);
#endif /* MYMAIN_H_ */
+48 -156
View File
@@ -13,159 +13,13 @@
#define morse_play_t1 50
#define morse_play_t2 (morse_play_t1*3)
#define morse_play_hz 100
#define morse_play_hz 1000
#define morsecodenum 38
const struct morsecode morsecodes[morsecodenum]=
{
{
2,0x40,'A'
},
{
4,0x80,'B'
},
{
4,0xa0,'C'
},
{
3,0x80,'D'
},
{
1,0x00,'E'
},
{
4,0x20,'F'
},
{
3,0xc0,'G'
},
{
4,0x00,'H'
},
{
2,0x00,'I'
},
{
4,0x70,'J'
},
{
3,0xa0,'K'
},
{
4,0x40,'L'
},
{
2,0xc0,'M'
},
{
2,0x80,'N'
},
{
3,0xe0,'O'
},
{
4,0x60,'P'
},
{
4,0xd0,'Q'
},
{
3,0x40,'R'
},
{
3,0x00,'S'
},
{
1,0x80,'T'
},
{
3,0x20,'U'
},
{
4,0x10,'V'
},
{
3,0x60,'W'
},
{
4,0x90,'X'
},
{
4,0xb0,'Y'
},
{
4,0xc0,'Z'
},
{
5,0x78,'1'
},
{
5,0x38,'2'
},
{
5,0x18,'3'
},
{
5,0x08,'4'
},
{
5,0x00,'5'
},
{
5,0x80,'6'
},
{
5,0xc0,'7'
},
{
5,0xe0,'8'
},
{
5,0xf0,'9'
},
{
5,0xf8,'0'
},
{
6,0x30,'?'
},
{
5,0x90,'/'
},
};
const char testbmp[]=
{
0x00,0x00,0x02,0x00,0x0F,0x00,0x00,0x0F,0x00,0x0F,0x00,0x00,0x09,0x00,0x0F,0x0F,
0x00,0x0D,0x80,0x0F,0x05,0xC0,0x18,0x80,0x0F,0x0C,0x70,0xF0,0xC0,0x0F,0x08,0x3F,
0x81,0x70,0x0F,0x04,0x14,0x7F,0xF0,0x0F,0x0C,0x01,0xAC,0x18,0x0F,0x04,0x0F,0xC0,
0x38,0x0F,0x06,0x78,0x07,0xEC,0x0F,0x0D,0xC0,0x7E,0x86,0x0F,0x07,0x03,0xC0,0x06,
0x0F,0x06,0x1E,0x00,0x0E,0x0F,0x0C,0x70,0x00,0x0F,0x0F,0x0D,0xC0,0x10,0x07,0x8F,
0x0F,0x00,0x18,0x01,0x8F,0x1C,0x00,0x18,0x1F,0xCF,0x18,0x00,0x18,0x75,0xCF,0x38,
0x00,0x00,0x3F,0x4F,0x38,0x00,0x00,0x04,0x6F,0x38,0x00,0x00,0x06,0x3F,0x38,0x00,
0x00,0x02,0x7F,0x68,0x00,0x00,0x0F,0xFF,0x58,0x00,0x00,0xFA,0x2F,0xC8,0x00,0x03,
0xC0,0xEF,0xD8,0x00,0x02,0x0F,0x8F,0x98,0x00,0x00,0x1A,0x0F,0x98,0x00,0x00,0x38,
0x0F,0x38,0x00,0x00,0x10,0x0F
};
char getmorsecode(uint8_t len,uint8_t code)
{
for(int a=0;a<morsecodenum;a++)
{
if((morsecodes[a].len==len)&&(morsecodes[a].code==code))
{
return morsecodes[a].letter;
}
}
return 0;
}
button B1;//创建一个按钮
encoder E1;//创建一个编码器
@@ -187,6 +41,20 @@ menu menu_main=
0,0
};
void di()
{
BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t1,50);
BUZZER_PLAY_NOTES(0,morse_play_t1,0);
mo_di();
}
void da()
{
BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t2,50);
BUZZER_PLAY_NOTES(0,morse_play_t1,0);
mo_da();
}
void mymain()
{
PWR_EN(1);
@@ -199,9 +67,10 @@ void mymain()
OLED_Init();//屏幕初始化
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//启动n通道的pwm
HAL_TIM_Base_Start_IT(&htim3);
BUZZER_PLAY_INIT();
MUTE(0);
//play_ones(1000,50);
BUZZER_PLAY_NOTES(1000,1000,50);
while(1)
{
GEI_BUTTON_CODE(&B1);//循环更新按钮
@@ -217,8 +86,15 @@ void mymain()
break;
case 1:
//主界面
if(encode_c>0)
{
di();
}
if(encode_c<0)
{
da();
}
OLED_Str(0,0,8,moser_buff,1);
fps_++;
@@ -250,8 +126,8 @@ void mymain()
}
mo_server();
BUZZER_PLAY_SERVER();
OLED_Cache_to_hardware();//刷新屏幕
if(HAL_GetTick()>run_tick)
@@ -277,20 +153,36 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
case 1://通过监测B脚的状态识别正反转
E1.code+=1;
E1.move_flag=1;//发生旋转的标记
break;
case 0:
E1.code-=1;
E1.move_flag=1;
break;
}
break;
//外部电键输入
case tack_a_Pin:
test_code++;
switch(tack_a())
{
case 0:
//di();
break;
case 1:
break;
}
break;
case tack_b_Pin:
test_code--;
switch(tack_b())
{
case 0:
//da();
break;
case 1:
break;
}
break;
@@ -299,11 +191,11 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
case 0:
mo_one_tick(0);
//mo_one_tick(0);
break;
case 1:
mo_one_tick(1);
//mo_one_tick(1);
break;
}
break;
+1 -6
View File
@@ -14,12 +14,7 @@
#include "encode.h"
#include "hread_interface.h"
#include "morsr.h"
struct morsecode
{
uint8_t len;
uint8_t code;
char letter;
};
extern TIM_HandleTypeDef htim3;