模拟电磁曲射炮设计

本文最后更新于:2022年5月29日 上午

一、前言

本系统以STM32F103C8T6单片机为控制器,采用物理公式计算模拟电磁炮弹射距离与系统各方面的关系,采用自动充电放电实现全过程一键自动化控制,超声波模块读取定标点与引导标识的距离,摄像头模块读取定标点与引导标识的位置关系。通过大量实验计算充电时间、线圈匝数与弹射距离之间的关系,使系统足够达到设计要求。

二、系统构成

采用两个容值为470uF、额定电压为450V电容并联作为容性储能元件。电容充电采用升压转换模块将12V直流电压转换为175V直流电压;电容放电端连接可控硅,控制电容放电时机;超声波模块HC-SR04测量定标点与引导标识直接的距离;摄像头模块OpenMV4确定定标点与引导标识的位置。

Mb4HoD.png

升压模块优先选择高压升压模块(可达390V左右),因为使用了小容值的电容,如选择升压模块的目标电压过低,很有可能难以保证题目所要求的弹丸射出功率。

三、电路系统图

MbIlHf.png

四、程序设计图

MbI3E8.png

五、程序模块化设计

1,舵机云台

(1)mg996r.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef __MG996R_H
#define __MG996R_H

#include "stm32f10x.h"


// Pin definition
/************************************************************/
#define MG996R_OCPWM_GPIO_CLK RCC_APB2Periph_GPIOB
#define MG996R_OCPWM_GPIO_AF RCC_APB2Periph_AFIO

#define MG996R_OC1PWM_GPIO_PORT GPIOB
#define MG996R_OC1PWM_PIN GPIO_Pin_6
#define MG996R_OC1PWM_PINSOURCE GPIO_PinSource6

#define MG996R_OC2PWM_GPIO_PORT GPIOB
#define MG996R_OC2PWM_PIN GPIO_Pin_7
#define MG996R_OC2PWM_PINSOURCE GPIO_PinSource7


#define MG996R_TIM TIM4
#define MG996R_TIM_CLK RCC_APB1Periph_TIM4

/************************************************************/

void MG996R_Init(void);
void MG996R_TIM_SetPWM_Num(uint8_t chx,uint16_t value);
void MG996R_Initial_Position(void);

#endif /* __MG996R_H */

(2)mg996r.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
******************************************************************************
* @file mg996r.c
* @author Soso
* @date 2019-08-23
* @brief General purpose timer multi-channel PWM output configuration.
* Steering gear pin GPIO mode configuration.
******************************************************************************
*/

#include "mg996r.h"
#include "systick.h"


/**
* @brief Configure the I/O used when the TIM multiplexes the output PWM.
* @param None
* @retval None
*/
static void TIM4_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Enable the relevant GPIO peripheral clock */
RCC_APB2PeriphClockCmd(MG996R_OCPWM_GPIO_CLK|MG996R_OCPWM_GPIO_AF, ENABLE);

/* Timer channel pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

/* Channel 1 initialization */
GPIO_InitStructure.GPIO_Pin = MG996R_OC1PWM_PIN;
GPIO_Init(MG996R_OC1PWM_GPIO_PORT, &GPIO_InitStructure);
/* Channel 2 initialization */
GPIO_InitStructure.GPIO_Pin = MG996R_OC2PWM_PIN;
GPIO_Init(MG996R_OC2PWM_GPIO_PORT, &GPIO_InitStructure);
}


/**
* @brief Timer PWM output mode configuartion.
* @param None
* @retval None
*/
static void TIM4_PWM_Config_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

/* Enable TIMx_CLK */
RCC_APB1PeriphClockCmd(MG996R_TIM_CLK, ENABLE);

/* 1, PSC(prescaler) = 720-1,
timer frequency = 72M/(PSC + 1) = 100KHZ,
calculate the time of one pulse = 1 / 100,000 s
2, ARR (automatic reload register) = 2000-1, counted from 0 to 1999, then counted 2000 times.
3, Time=2000/100,000=0.02s = 20ms */
TIM_TimeBaseStructure.TIM_Period = 20000-1; // 20ms
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// Initialization TIMx
TIM_TimeBaseInit(MG996R_TIM, &TIM_TimeBaseStructure);

/* PWM mode configuration */
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // Configured as PWM mode 1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1-1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // High level when the timer count value is less than CCR1_Val

/* Enable channel 1 ~ channel 2 */
TIM_OC1Init(MG996R_TIM, &TIM_OCInitStructure);
TIM_OC2Init(MG996R_TIM, &TIM_OCInitStructure);

TIM_CtrlPWMOutputs(MG996R_TIM,ENABLE);

/* Enable channel 1 ~ channel 2 overload */
TIM_OC1PreloadConfig(MG996R_TIM, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(MG996R_TIM, TIM_OCPreload_Enable);

/* Enable timer */
TIM_Cmd(MG996R_TIM, ENABLE);
}


/**
* @brief Change the timer channel duty cycle.
* @param ch: where ch can be 1 to 2
* @param value: new value of the TIMx channelx
* @retval None
*/
void MG996R_TIM_SetPWM_Num(uint8_t ch,uint16_t value)
{
if (ch==1)
{
TIM_SetCompare1(MG996R_TIM,value);
}
if (ch==2)
{
TIM_SetCompare2(MG996R_TIM,value);
}
}

void MG996R_Init(void)
{
TIM4_GPIO_Config();
TIM4_PWM_Config_Init();
}

extern uint16_t Pwm1,Pwm2;
// 舵机初始位置
void MG996R_Initial_Position(void)
{
/*
2m 1340
3m 1240
*/
MG996R_TIM_SetPWM_Num(1,1400); // 上面的舵机(仰角)小上大下
MG996R_TIM_SetPWM_Num(2,1500); // 90° 下面的舵机
}
/*********************************************END OF FILE*********************************************/

2,矩阵键盘

(1)keyboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __KEYBOARD_H
#define __KEYBOARD_H

#include "stm32f10x.h"

//Pin definition
/*******************************************************/
#define COLUMN_KEY_GPIO_PORT GPIOA
#define COLUMN_KEY_GPIO_CLK RCC_APB2Periph_GPIOA
#define C1_PIN GPIO_Pin_8
#define C2_PIN GPIO_Pin_9
#define C3_PIN GPIO_Pin_10
#define C4_PIN GPIO_Pin_11


#define ROW_KEY_GPIO_PORT GPIOB
#define ROW_KEY_GPIO_CLK RCC_APB2Periph_GPIOB
#define R1_PIN GPIO_Pin_12
#define R2_PIN GPIO_Pin_13
#define R3_PIN GPIO_Pin_14
#define R4_PIN GPIO_Pin_15
/************************************************************/

void KeyBoard_Init(void);
uint16_t Key_scan(void);

#endif /* __KEYBOARD_H */

(1)keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
******************************************************************************
* @file keyboard.c
* @author Soso
* @date 2019-08-23
* @brief Matrix keyboard configuration.
******************************************************************************
*/

#include "keyboard.h"
#include "systick.h"

extern uint8_t key_flag,Model_flag;

void KeyBoard_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;

/* Enable KeyGPIO port clock */
RCC_APB2PeriphClockCmd(COLUMN_KEY_GPIO_CLK|ROW_KEY_GPIO_CLK,ENABLE);

/* Column Key GPIO Configuration */
GPIO_InitStruct.GPIO_Pin = C1_PIN|C2_PIN|C3_PIN|C4_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(COLUMN_KEY_GPIO_PORT,&GPIO_InitStruct);

/* Row Key GPIO Configuration */
GPIO_InitStruct.GPIO_Pin = R1_PIN|R2_PIN|R3_PIN|R4_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(ROW_KEY_GPIO_PORT,&GPIO_InitStruct);

GPIO_SetBits(COLUMN_KEY_GPIO_PORT,C1_PIN|C2_PIN|C3_PIN|C4_PIN);
GPIO_ResetBits(ROW_KEY_GPIO_PORT,R1_PIN|R2_PIN|R3_PIN|R4_PIN);
}


uint16_t Key_scan(void)
{
uint16_t temp = 0,Key_val = 0;

/*************************** Column 1 ***************************/
GPIO_SetBits(COLUMN_KEY_GPIO_PORT,C1_PIN);
GPIO_ResetBits(COLUMN_KEY_GPIO_PORT,C2_PIN|C3_PIN|C4_PIN);

temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;

if(temp != 0x0000)
{
delay_ms(5);
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
if(temp != 0x0000)
{
key_flag = 1;
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
switch(temp)
{
case 0x8000: Key_val = 1;
Model_flag = 1;
break;
case 0x4000: Key_val = 2;
Model_flag = 2;
break;
case 0x2000: Key_val = 3;
Model_flag = 3;
break;
case 0x1000: Key_val = 4;
Model_flag = 4;
break;
}
}
while(temp != 0x0000)
{
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
}
}

/*************************** Column 2 ***************************/
GPIO_SetBits(COLUMN_KEY_GPIO_PORT,C2_PIN);
GPIO_ResetBits(COLUMN_KEY_GPIO_PORT,C1_PIN|C3_PIN|C4_PIN);

temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;

if(temp != 0x0000)
{
delay_ms(5);
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
if(temp != 0x0000)
{
key_flag = 1;
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
switch(temp)
{
case 0x8000: Key_val = 5;break;
case 0x4000: Key_val = 6;break;
case 0x2000: Key_val = 7;break;
case 0x1000: Key_val = 8;break;
}
}
while(temp != 0x0000)
{
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
}
}

/*************************** Column 3 ***************************/
GPIO_SetBits(COLUMN_KEY_GPIO_PORT,C3_PIN);
GPIO_ResetBits(COLUMN_KEY_GPIO_PORT,C1_PIN|C2_PIN|C4_PIN);

temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;

if(temp != 0x0000)
{
delay_ms(5);
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
if(temp != 0x0000)
{
key_flag = 1;
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
switch(temp)
{
case 0x8000: Key_val = 9;break;
case 0x4000: Key_val = 10;break;
case 0x2000: Key_val = 11;break;
case 0x1000: Key_val = 12;break;
}
}
while(temp != 0x0000)
{
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
}
}

/*************************** Column 4 ***************************/
GPIO_SetBits(COLUMN_KEY_GPIO_PORT,C4_PIN);
GPIO_ResetBits(COLUMN_KEY_GPIO_PORT,C1_PIN|C2_PIN|C3_PIN);

temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;

if(temp != 0x0000)
{
delay_ms(5);
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
if(temp != 0x0000)
{
key_flag = 1;
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
switch(temp)
{
case 0x8000: Key_val = 13;break;
case 0x4000: Key_val = 14;break;
case 0x2000: Key_val = 15;break;
case 0x1000: Key_val = 16;break;
}
}
while(temp != 0x0000)
{
temp = GPIO_ReadInputData(ROW_KEY_GPIO_PORT) & 0xF000;
}
}

return Key_val;
}

/*********************************************END OF FILE*********************************************/

3,LCD12864液晶显示屏

(1)lcd12864.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef __LCD12864_H
#define __LCD12864_H

#include "stm32f10x.h"
#include "systick.h"


#define WRITE_CMD 0xF8 // 写命令
#define WRITE_DAT 0xFA // 写数据


//Pin definition
/*******************************************************/
#define LCD_GPIO_PORT GPIOA
#define LCD_GPIO_CLK RCC_APB2Periph_GPIOA

#define CS_PIN GPIO_Pin_0
#define SID_PIN GPIO_Pin_1
#define SCLK_PIN GPIO_Pin_2

#define CS_H GPIO_SetBits(LCD_GPIO_PORT,CS_PIN)
#define CS_L GPIO_ResetBits(LCD_GPIO_PORT,CS_PIN)

#define SID_H GPIO_SetBits(LCD_GPIO_PORT,SID_PIN)
#define SID_L GPIO_ResetBits(LCD_GPIO_PORT,SID_PIN)

#define SCLK_H GPIO_SetBits(LCD_GPIO_PORT,SCLK_PIN)
#define SCLK_L GPIO_ResetBits(LCD_GPIO_PORT,SCLK_PIN)
/************************************************************/

void LCD_Init(void);
void LCD12864_GPIO_Init(void);

void Send_Byte(u8 zdata);
u8 Receive_Byte(void);
void Check_Busy(void);
u8 Read_Data(void);
void Write_LCD_Command(u8 cmdcode);
void Write_LCD_Data(u8 Dispdata);
void LCD_Clear_Scr( void );
void Set_Display_Pos(u8 x,u8 y);
void Display_LCD_String(u8 *s);
void LCD_Display_Words(uint8_t x,uint8_t y,char *str);

/*************************************** 界面显示函数 ***************************************/

void Display_Main_dire(void);
void Display_Running(void);
void Key_Number(uint8_t value_flag);
void Display_Model1_dire(void);
void Display_Model1_Contr(void);
void Display_Model2_dire1(void);
void Display_Model2_dire2(void);
void Display_Model2_Contr(void);

#endif /* __LCD12864_H */

(2)lcd12864.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
******************************************************************************
* @file lcd12864.c
* @author Soso
* @date 2019-08-23
* @brief LCD 12864 configuration,with font, serial mode.
******************************************************************************
*/

#include "lcd12864.h"


void LCD12864_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Enable GPIO port clock */
RCC_APB2PeriphClockCmd(LCD_GPIO_CLK, ENABLE);

GPIO_InitStructure.GPIO_Pin = CS_PIN|SID_PIN|SCLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(LCD_GPIO_PORT, &GPIO_InitStructure);
}


void LCD_Init(void)
{
LCD12864_GPIO_Init();

delay_ms(50); // 等待液晶自检(延时>40ms)
Write_LCD_Command(0x30); // 功能设定:选择基本指令集,8bit数据流
Write_LCD_Command(0x0c); // 显示器开,光标关闭
Write_LCD_Command(0x01); // 清屏,并且设定地址指针为00H
Write_LCD_Command(0x06); // 进入设定点,初始化完成
}


/*********************************************************
函数名:Send_Byte()
返回值:无
功 能: 写数据到LCD
*********************************************************/
void Send_Byte(u8 zdata)
{
u8 i;
for(i=0; i<8; i++)
{
if((zdata << i) & 0x80)
SID_H;
else
SID_L;
SCLK_H;
SCLK_L;
}
}


/*********************************************************
函数名:Receive_Byte()
返回值:temp1+temp2
功 能: 读LCD数据
*********************************************************/
u8 Receive_Byte(void)
{
u8 i,temp1,temp2,value;
temp1=0;
temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
SCLK_L;
SCLK_H;
SCLK_L;
if(GPIO_ReadOutputDataBit(LCD_GPIO_PORT,SID_PIN)==1)
{
temp1++;
}
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
SCLK_L;
SCLK_H;
SCLK_L;
if(GPIO_ReadOutputDataBit(LCD_GPIO_PORT,SID_PIN)==1)
{
temp2++;
}
}
temp1=0xf0&temp1;
temp2=0x0f&temp2;
value=temp1+temp2;
return value;
}


/*********************************************************
函数名:Check_Busy()
返回值:无
功 能: LCD忙检查
*********************************************************/
void Check_Busy(void)
{
do
Send_Byte(0xfc); //11111,RW(1),RS(0),0
while(0x80&Receive_Byte());
}


/*********************************************************
函数名: Read_LCD_Data()
返回值:LCD收到的数据
功 能: 读取LCD显示内容
*********************************************************/
u8 Read_Data(void)
{
Check_Busy();
Send_Byte(0xfe); //11111,RW(1),RS(1),0 LCD->MCU
return Receive_Byte();
}


/*********************************************************
函数名: Write_LCD_Command()
返回值:无
功 能: 写命令到LCD
*********************************************************/
void Write_LCD_Command(u8 cmdcode)
{
CS_H;
Check_Busy();
Send_Byte(WRITE_CMD);
Send_Byte(cmdcode & 0xf0);
Send_Byte((cmdcode << 4) & 0xf0);
delay_ms(2);
CS_L;
}
/*********************************************************
函数名: Write_LCD_Data()
返回值:无
功 能: 写显示内容到LCD
*********************************************************/
void Write_LCD_Data(u8 Dispdata)
{
CS_H;
Check_Busy();
Send_Byte(WRITE_DAT); //11111,RW(0),RS(1),0
Send_Byte(Dispdata & 0xf0);
Send_Byte((Dispdata << 4) & 0xf0);
delay_ms(2);
CS_L;
}


/*********************************************************
函数名: LCD_Clear_Scr
返回值:无
功 能: LCD清屏
*********************************************************/
void LCD_Clear_Scr( void )
{
Write_LCD_Command(0x01);
delay_ms(2);
}

/*********************************************************
函数名: Set_Display_Pos
参数:X为行,Y为列
返回值:无
功 能:设置显示位置
*********************************************************/
void Set_Display_Pos(u8 x,u8 y)
{
u8 pos;
switch(x)
{
case 0: x=0x80;break;
case 1: x=0x90;break;
case 2: x=0x88;break;
case 3: x=0x98;break;
}
pos=x+y;
Write_LCD_Command(pos);
}

/*********************************************************
函数名:Display_LCD_String()
返回值:无
功 能:显示字符串
*********************************************************/
void Display_LCD_String(u8 *s)
{
while(*s != '\0')
{
Write_LCD_Data(*s);
s++;
delay_ms(2); //控制每一个字符之间显示的时间 间隔
}
}


/*!
* @brief 显示字符或汉字
* @param x: row(0~3)
* @param y: line(0~7)
* @param str: 要显示的字符
*/
void LCD_Display_Words(uint8_t x,uint8_t y,char *str)
{
Set_Display_Pos(x,y); //写初始光标位置
while(*str>0)
{
Write_LCD_Data(*str); //写数据
str++;
}
}

/*********************************************END OF FILE*********************************************/

4,超声波模块

(1)hcsr04.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef __HCSR04_H
#define __HCSR04_H

#include "stm32f10x.h"


//Pin definition
/*******************************************************/
#define HCSR04_PORT GPIOB
#define HCSR04_CLK RCC_APB2Periph_GPIOB
#define HCSR04_TRIG GPIO_Pin_8
#define HCSR04_ECHO GPIO_Pin_9

#define HCSR04_EXTI_PORTSOURCE GPIO_PortSourceGPIOB
#define HCSR04_EXTI_PINSOURCE GPIO_PinSource9
#define HCSR04_EXTI_LINE EXTI_Line9
#define HCSR04_EXTI_IRQ EXTI9_5_IRQn
#define HCSR04_EXTI_IRQHandler EXTI9_5_IRQHandler

#define HCSR04_TIM TIM3
#define HCSR04_TIM_CLK RCC_APB1Periph_TIM3
#define HCSR04_TIM_IRQn TIM3_IRQn
#define HCSR04_TIM_IRQHandler TIM3_IRQHandler


#define HCSR04_TRIG_TIM TIM2
#define HCSR04_TRIG_TIM_CLK RCC_APB1Periph_TIM2
#define HCSR04_TRIG_TIM_IRQn TIM2_IRQn
#define HCSR04_TRIG_TIM_IRQHandler TIM2_IRQHandler

/************************************************************/

void HCSR04_TRIG_TIM_Config(void);
void HCSR04_TIM_Config(void);
void HCSR04_EXTI_Config(void);
void Hcsr04_Trig(void);
void HCSR04_Init(void);
float bubble_sort(float a[],uint8_t m);

#endif /* __HCSR04_H */

(2)hcsr04.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
******************************************************************************
* @file hcsr04.c
* @author Soso
* @date 2019-08-23
* @brief Ultrasonic module ranging configuration.
******************************************************************************
*/

#include "hcsr04.h"


// Configuring nested vectored interrupt controller(NVIC)
static void HCSR04_TRIG_Tim_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure NVIC as a priority group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Configure interrupt source*/
NVIC_InitStructure.NVIC_IRQChannel = HCSR04_TRIG_TIM_IRQn;
/* Configuration preemption priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* Configure sub-priority: 0 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
/* Enable interrupt channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


// Configuring nested vectored interrupt controller(NVIC)
static void HCSR04_TIM_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure NVIC as a priority group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Configure interrupt source*/
NVIC_InitStructure.NVIC_IRQChannel = HCSR04_TIM_IRQn;
/* Configuration preemption priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* Configure sub-priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* Enable interrupt channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


/* Configuring nested vectored interrupt controller(NVIC) */
static void HCSR04_EXTI_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure NVIC as a priority group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Configuration preemption priority: 0 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
/* Configure sub-priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* Enable interrupt channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

/* Configure interrupt source: Echo pin */
NVIC_InitStructure.NVIC_IRQChannel = HCSR04_EXTI_IRQ;
NVIC_Init(&NVIC_InitStructure);
}


void HCSR04_TRIG_TIM_Config(void)
{
SystemInit();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// Enable TIM4_CLK
RCC_APB1PeriphClockCmd(HCSR04_TRIG_TIM_CLK,ENABLE);

/* 1, PSC(prescaler) = 720-1
timer frequency = 72M/(PSC + 1) = 100KHZ
calculate the time of one pulse = 1/1000,000 s
2, ARR (automatic reload register) = 100-1, counted from 0 to 99, then counted 100 times.
3, Time = 100/100,000 = 0.001s = 1ms */
TIM_TimeBaseStructure.TIM_Period = 100-1;
TIM_TimeBaseStructure.TIM_Prescaler = 720-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// Initialization TIM3
TIM_TimeBaseInit(HCSR04_TRIG_TIM,&TIM_TimeBaseStructure);

// Clear timer update interrupt flag
TIM_ClearITPendingBit(HCSR04_TRIG_TIM,TIM_IT_Update);
// Enable timer update interrupt
TIM_ITConfig(HCSR04_TRIG_TIM,TIM_IT_Update,ENABLE);
// Initialization disable timer
TIM_Cmd(HCSR04_TRIG_TIM,DISABLE);

HCSR04_TRIG_Tim_NVIC_Config();
}


void HCSR04_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// Enable TIM4_CLK
RCC_APB1PeriphClockCmd(HCSR04_TIM_CLK,ENABLE);

/* 1, PSC(prescaler) = 1-1
timer frequency = 72M/(PSC + 1) = 72,000KHZ
calculate the time of one pulse = 1/72,000,000 s
2, ARR (automatic reload register) = 100-1, counted from 0 to 99, then counted 100 times.
3, Time = 72/72,000,000 = 0.000001‬s = 1us */
TIM_TimeBaseStructure.TIM_Period = 72-1;
TIM_TimeBaseStructure.TIM_Prescaler = 1-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// Initialization TIM3
TIM_TimeBaseInit(HCSR04_TIM,&TIM_TimeBaseStructure);

// Clear timer update interrupt flag
TIM_ClearITPendingBit(HCSR04_TIM,TIM_IT_Update);
// Enable timer update interrupt
TIM_ITConfig(HCSR04_TIM,TIM_IT_Update,ENABLE);
// Initialization disable timer
TIM_Cmd(HCSR04_TIM,DISABLE);

HCSR04_TIM_NVIC_Config();
}


void HCSR04_EXTI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;

/* Enable KeyGPIO port clock */
RCC_APB2PeriphClockCmd(HCSR04_CLK,ENABLE);

/* Trig pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = HCSR04_TRIG;
GPIO_Init(HCSR04_PORT,&GPIO_InitStructure);

/* Echo pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = HCSR04_ECHO;
GPIO_Init(HCSR04_PORT,&GPIO_InitStructure);

/* Connect the exti interrupt source to the Echo pin */
GPIO_EXTILineConfig(HCSR04_EXTI_PORTSOURCE,HCSR04_EXTI_PINSOURCE);

/* EXTI mode configuration */
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;

/* Select the exti interrupt source */
EXTI_InitStructure.EXTI_Line = HCSR04_EXTI_LINE;
EXTI_Init(&EXTI_InitStructure);

HCSR04_EXTI_NVIC_Config();
}


void Hcsr04_Trig(void)
{
uint16_t i;

GPIO_SetBits(HCSR04_PORT,HCSR04_TRIG);
for(i=0;i<0xf0;i++);
GPIO_ResetBits(HCSR04_PORT,HCSR04_TRIG);
}


void HCSR04_Init(void)
{
HCSR04_TRIG_TIM_Config();
HCSR04_TIM_Config();
HCSR04_EXTI_Config();
}


float bubble_sort(float a[],uint8_t m)
{
uint8_t i = 0;
uint8_t j = 0;
uint8_t tmp = 0;
uint32_t sum = 0;

for (i = 0; i<m - 1; i++)
{
for (j = 0; j < m - i - 1; j++)
{
if (a[j]>a[j + 1])
{
tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
}
}
}

for(i = 2;i<m-2;i++) //取中间6个数值求和 取其平均数
sum += a[i];

return sum/6.0;
}
/*********************************************END OF FILE*********************************************/

5,电容充电控制模块

(1)switch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef __SWITCH_H
#define __SWITCH_H

#include "stm32f10x.h"


//Pin definition
/*******************************************************************************************************/
#define SWITCH_GPIO_PORT GPIOA
#define SWITCH_GPIO_CLK RCC_APB2Periph_GPIOA

#define CHARGING_PIN GPIO_Pin_3
#define DISCHARGING_PIN GPIO_Pin_4

/*******************************************************************************************************/

#define CHARGING_SWITCH_1() GPIO_SetBits(SWITCH_GPIO_PORT,CHARGING_PIN)
#define CHARGING_SWITCH_0() GPIO_ResetBits(SWITCH_GPIO_PORT,CHARGING_PIN)

#define DISCHARGING_SWITCH_1() GPIO_SetBits(SWITCH_GPIO_PORT,DISCHARGING_PIN)
#define DISCHARGING_SWITCH_0() GPIO_ResetBits(SWITCH_GPIO_PORT,DISCHARGING_PIN)
/*******************************************************************************************************/

void Switch_GPIO_Config(void);

#endif /* __SWITCH_H */

(2)switch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
******************************************************************************
* @file switch.c
* @author Soso
* @date 2019-08-23
* @brief GPIO applicatiom function interface.
******************************************************************************
*/

#include "switch.h"

void Switch_GPIO_Config(void)
{
// Enable GPIO port clock
RCC_APB2PeriphClockCmd(SWITCH_GPIO_CLK,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;

// GPIO configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Pin = CHARGING_PIN;
GPIO_Init(SWITCH_GPIO_PORT,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = DISCHARGING_PIN;
GPIO_Init(SWITCH_GPIO_PORT,&GPIO_InitStructure);

GPIO_SetBits(SWITCH_GPIO_PORT,CHARGING_PIN);
GPIO_ResetBits(SWITCH_GPIO_PORT,DISCHARGING_PIN);
}

/*********************************************END OF FILE*********************************************/

6,定时器计时模块

(1)tim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __TIM_H
#define __TIM_H

#include "stm32f10x.h"

#define COUNTER_TIM TIM1
#define COUNTER_TIM_CLK RCC_APB2Periph_TIM1

#define COUNTER_TIM_IRQn TIM1_UP_IRQn
#define COUNTER_TIM_IRQHandler TIM1_UP_IRQHandler

void TIM1_Config(void);

#endif /* __TIM_H */

(2)tim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
******************************************************************************
* @file tim.c
* @author Soso
* @date 2019-08-23
* @brief Configure timer 1 as the count mode.
*
******************************************************************************
*/

#include "tim.h"


// Configuring nested vectored interrupt controller(NVIC)
static void TIM1_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure NVIC as a priority group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Configure interrupt source*/
NVIC_InitStructure.NVIC_IRQChannel = COUNTER_TIM_IRQn;
/* Configuration preemption priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* Configure sub-priority: 1 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* Enable interrupt channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


void TIM1_Config(void)
{
SystemInit();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// Enable TIM3_CLK
RCC_APB1PeriphClockCmd(COUNTER_TIM_CLK,ENABLE);

/* 1, PSC(prescaler) = 720-1
timer frequency = 72M/(PSC + 1) = 100KHZ
calculate the time of one pulse = 1/1000,000 s
2, ARR (automatic reload register) = 100-1, counted from 0 to 99, then counted 100 times.
3, Time = 100/100,000 = 0.001s = 10ms */
TIM_TimeBaseStructure.TIM_Period = 100-1;
TIM_TimeBaseStructure.TIM_Prescaler = 720-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// Initialization TIM3
TIM_TimeBaseInit(COUNTER_TIM,&TIM_TimeBaseStructure);

// Clear timer update interrupt flag
TIM_ClearITPendingBit(COUNTER_TIM,TIM_IT_Update);
// Enable timer update interrupt
TIM_ITConfig(COUNTER_TIM,TIM_IT_Update,ENABLE);
// Initialization disable timer
TIM_Cmd(COUNTER_TIM,DISABLE);

TIM1_NVIC_Config();

}
/*********************************************END OF FILE*********************************************/

7,串口调试模块

(1)usart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef __USART_H
#define __USART_H

#include "stm32f10x.h"
#include "stdio.h"

//Pin definition
/*******************************************************/
#define DEBUG_USART USART3
#define DEBUG_USART_CLK RCC_APB1Periph_USART3
#define DEBUG_USART_AF RCC_APB2Periph_AFIO
#define DEBUG_USART_GPIO_CLK RCC_APB2Periph_GPIOB
#define DEBUG_USART_BAUDRATE 115200

#define DEBUG_USART_RX_GPIO_PORT GPIOB
#define DEBUG_USART_RX_PIN GPIO_Pin_11

#define DEBUG_USART_TX_GPIO_PORT GPIOB
#define DEBUG_USART_TX_PIN GPIO_Pin_10


#define DEBUG_USART_IRQ USART3_IRQn
#define DEBUG_USART_IRQHandler USART3_IRQHandler

/************************************************************/

void USART_Config(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);

void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);

#endif /* __USART_H */

(2)usart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
******************************************************************************
* @file uasrt.c
* @author Soso
* @date 2019-08-23
* @brief Redirect c library printf function to usart port, interrupt receiving mode.
******************************************************************************
*/

#include "usart.h"


/* Configuring nested vectored interrupt controller NVIC */
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Nested vector interrupt controller group selection */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

/* Preemptio Priority is 1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* Sub Priority is 1 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* Enable interrupt */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

/* Configure USART as the interrupt source */
NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
/* Initialize configuration NVIC */
NVIC_Init(&NVIC_InitStructure);
}

void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* Enable port alternate function and enable USART GPIO clock */
RCC_APB2PeriphClockCmd(DEBUG_USART_AF|DEBUG_USART_GPIO_CLK,ENABLE);
/* Enable USART Clock */
RCC_APB1PeriphClockCmd(DEBUG_USART_CLK,ENABLE);

/* Configure the Tx pin for alternate function */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

/* Configure the Rx pin for the alternate function */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);

/* Configuration USART mode */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
/* USART mode control: simultaneous enable send and receive */
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;

USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
USART_Init(DEBUG_USART, &USART_InitStructure);

/* Configure the serial port to receive interrupts. */
NVIC_Configuration();

/* Enable serial port receive interrupt */
USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);

/* Enable serial */
USART_Cmd(DEBUG_USART, ENABLE);
}


/***************** Send a character **********************/
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
/* Send a byte of data to the USART */
USART_SendData(pUSARTx,ch);

/* Waiting for the transmit data register to be empty */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}

/***************** Send string **********************/
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(*(str + k)!='\0');

/* Waiting for transmission to complete */
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
{}
}

/***************** Send a 16-digit number **********************/
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t temp_h, temp_l;

/* Take out the high eight */
temp_h = (ch&0XFF00)>>8;
/* Take the lower eight */
temp_l = ch&0XFF;

/* Send high eight */
USART_SendData(pUSARTx,temp_h);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);

/* Send the lower eight digits */
USART_SendData(pUSARTx,temp_l);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}

/* Redirect the c library function printf to the serial port,
use the printf function after redirection. */
int fputc(int ch, FILE *f)
{
/* Send a byte of data to the serial port */
USART_SendData(DEBUG_USART, (uint8_t) ch);

/* Waiting to send */
while (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_TXE) == RESET);

return (ch);
}

/* Redirect the c library function scanf to the serial port,
rewrite backwards can use scanf, getchar and other functions. */
int fgetc(FILE *f)
{
/* Waiting for serial input data */
while (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) == RESET);

return (int)USART_ReceiveData(DEBUG_USART);
}
/*********************************************END OF FILE*********************************************/

8,延时函数配置模块

(1)systick.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef __SYSTICK_H
#define __SYSTICK_H

#include"stm32f10x.h"

void delay_us(u32 i);
void delay_ms(u32 i);


#endif /* __SYSTICK_DELAY_H */

(2)systick.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "systick.h"

void delay_us(u32 i)
{
u32 temp;
SysTick->LOAD=9*i; //Set the reload value, at 72MHZ
SysTick->CTRL=0X01; //Enable, reduce to zero is no action, use external clock source
SysTick->VAL=0; //Clear counter
do
{
temp=SysTick->CTRL; //Read current countdown value
}
while((temp&0x01)&&(!(temp&(1<<16)))); //Waiting time to arrive
SysTick->CTRL=0; //Close counter
SysTick->VAL=0; //Empty counter
}
void delay_ms(u32 i)
{
u32 temp;
SysTick->LOAD=9000*i; //Set the reload value, at 72MHZ
SysTick->CTRL=0X01; //Enable, reduce to zero is no action, use external clock source
SysTick->VAL=0; //Clear counter
do
{
temp=SysTick->CTRL; //Read current countdown value
}
while((temp&0x01)&&(!(temp&(1<<16)))); //Waiting time to arrive
SysTick->CTRL=0; //Close counter
SysTick->VAL=0; //Empty counter
}

9,中断配置模块

把下面的代码添加到 stm32f10x_it.c 文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "stm32f10x_it.h"
#include "systick.h"
#include "hcsr04.h"
#include "mg996r.h"
#include "usart.h"


extern float HCdistance;
extern float disBuf[15];

float distSum = 0;
uint32_t usHcCount = 0,HcTrigCount = 0,Hcsr04_TrigTime = 0;
uint8_t filterCount = 0;


extern uint8_t RxBuf,Rxflag;

void DEBUG_USART_IRQHandler(void)
{
uint8_t ucTemp;

if(USART_GetITStatus(DEBUG_USART,USART_IT_RXNE) != RESET)
{
ucTemp = USART_ReceiveData(DEBUG_USART);
if(Rxflag == 1 )
{
RxBuf = ucTemp;
}
else if(ucTemp == 'P')
RxBuf = ucTemp;
}

}


//extern uint16_t Pwm1,Pwm2;
///**
// * @brief This function handles PPP interrupt request.
// * @param None
// * @retval None
// */
//void DEBUG_USART_IRQHandler(void)
//{
// uint8_t ucTemp;
// if(USART_GetITStatus(DEBUG_USART,USART_IT_RXNE) != RESET)
// {
// ucTemp = USART_ReceiveData(DEBUG_USART);
// USART_SendData(DEBUG_USART,ucTemp);
//
// if(ucTemp == 'q')
// Pwm1 += 10;
// else if(ucTemp == 'w')
// Pwm1 -= 10;
// else if(ucTemp == 'a')
// Pwm1 += 5;
// else if(ucTemp == 's')
// Pwm1 -= 5;
// else if(ucTemp == 'z')
// Pwm1 += 1;
// else if(ucTemp == 'x')
// Pwm1 -= 1;
// else if(ucTemp == 'e')
// Pwm2 += 10;
// else if(ucTemp == 'r')
// Pwm2 -= 10;
// else if(ucTemp == 'd')
// Pwm2 += 1;
// else if(ucTemp == 'f')
// Pwm2 -= 1;
//
// MG996R_TIM_SetPWM_Num(1,Pwm1);
// MG996R_TIM_SetPWM_Num(2,Pwm2);
// }
//}

/* 1ms */
void HCSR04_TRIG_TIM_IRQHandler(void)
{
if ( TIM_GetITStatus( HCSR04_TRIG_TIM, TIM_IT_Update) != RESET )
{

HcTrigCount++;
if(HcTrigCount >= 60)
{
Hcsr04_Trig();
HcTrigCount = 0;
}

TIM_ClearITPendingBit(HCSR04_TRIG_TIM , TIM_IT_Update);
}
}

/* 1us */
void HCSR04_TIM_IRQHandler(void)
{
if ( TIM_GetITStatus( HCSR04_TIM, TIM_IT_Update) != RESET )
{
usHcCount++;
TIM_ClearITPendingBit(HCSR04_TIM , TIM_IT_Update);
}
}



void HCSR04_EXTI_IRQHandler(void)
{
static uint8_t flagState = 0;

if(EXTI_GetITStatus(HCSR04_EXTI_LINE) != RESET)
{
EXTI_ClearITPendingBit(HCSR04_EXTI_LINE);
if(GPIO_ReadInputDataBit(HCSR04_PORT,HCSR04_ECHO) == 1)
{
TIM_SetCounter(HCSR04_TIM,0);
flagState = 1;
TIM_Cmd(HCSR04_TIM,ENABLE);
}
else
{
TIM_Cmd(HCSR04_TIM,DISABLE);
if(flagState)
{
/**************** Bubble filter ****************/
disBuf[filterCount] = usHcCount/58.0; //cm
filterCount++;
if(filterCount == 10)
{
HCdistance = bubble_sort(disBuf,filterCount)*2.0;

if(HCdistance > 400)
HCdistance = 400;

filterCount = 0;
}
usHcCount = 0 ;
}
flagState = 0;
}

}
}

10,main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/**
******************************************************************************
* @file main.c
* @author Soso
* @date 2019-08-23
* @brief Electromagnetic gun.
*
******************************************************************************
*/

#include "stm32f10x.h"
#include "usart.h"
#include "keyboard.h"
#include "systick.h"
#include "lcd12864.h"
#include "hcsr04.h"
#include "mg996r.h"
#include "switch.h"

uint8_t count_flag = 0,Rxflag;
uint8_t key_flag = 0,Model_flag = 0,RxBuf = 0;
uint8_t key_value = 0;
char Dis_valueBuf[3]={'o'},Angle_valueBuf[3]={'o'};
uint16_t pos_value = 5;
float HCdistance = 0,disBuf[15] = {0};

//uint16_t Pwm1 = 1400,Pwm2 = 1500;

void Display_Main_dire(void);
void Display_Running(void);
void Key_Number(uint8_t value_flag);
void Display_Model1_dire(void);
void Display_Model1_Contr(void);
void Display_Model2_dire1(void);
void Display_Model2_dire2(void);
void Display_Model2_Contr(void);
void Display_Model3_Contr(void);
void Display_Model4_Contr(void);
uint16_t Distance_to_pwm(uint16_t dis);


int main(void)
{
MG996R_Init();
KeyBoard_Init();
USART_Config();
LCD_Init();
HCSR04_Init();
Switch_GPIO_Config();


MG996R_Initial_Position(); // 设置舵机初始位置
Display_Main_dire(); // 显示主界面
// printf("Test\r\n");

while(1)
{
// printf("Pwm1:%d Pwm2:%d Angle1:%0.2f Angle2:%0.2f\r\n",Pwm1,Pwm2,(Pwm1-500)*0.09,(Pwm2-500)*0.09);

key_value = Key_scan(); //按键扫描

if(key_flag == 1)
{
key_flag = 0;

if(key_value == 1 || key_value == 2 || key_value == 3 || key_value == 4)
{
Display_Main_dire();
LCD_Display_Words(key_value-1,0,"★");
}
if(key_value == 15)
{
if(Model_flag == 1)
{
Display_Model1_Contr();
}
else if(Model_flag == 2)
{
Display_Model2_Contr();
}
else if(Model_flag == 3)
{
Display_Model3_Contr();
}
else if(Model_flag == 4)
{
Display_Model4_Contr();
}
}
}
}
}




uint16_t Distance_to_pwm(uint16_t dis)
{
uint16_t pwm = 0;

if(dis >= 200 && dis <= 205)
pwm = 1355;
else if(dis > 205 && dis <= 215)
pwm = 1350;
else if(dis > 215 && dis <= 225)
pwm = 1345;
else if(dis > 225 && dis <= 235)
pwm = 1335;
else if(dis > 235 && dis <= 245)
pwm = 1330;
else if(dis > 245 && dis <= 255)
pwm = 1315;
else if(dis > 255 && dis <= 265)
pwm = 1310;
else if(dis > 265 && dis <= 275)
pwm = 1305;
else if(dis > 275 && dis <= 285)
pwm = 1300;
else if(dis > 285 && dis <= 295)
pwm = 1290;
else if(dis > 295 && dis <= 300)
pwm = 1280;

return pwm;
}

/*********************************************************** 显示函数 **********************************************************/

void Display_Main_dire(void)
{
LCD_Display_Words(0,0,"1)模式1:输入距离");
LCD_Display_Words(1,0,"2)模式2:自动瞄准");
LCD_Display_Words(2,0,"3)模式3:自动搜寻");
LCD_Display_Words(3,0,"4)模式4:自动扫描");

MG996R_Initial_Position(); // 设置舵机初始位置
}

void Display_Running(void)
{
LCD_Display_Words(0,0,"正在运行.....");
LCD_Display_Words(3,6,"结束");
}


void Display_Model1_dire(void)
{
LCD_Display_Words(0,0,"输入距离(cm):");
LCD_Display_Words(3,0,"确定");
LCD_Display_Words(3,6,"退出");

pos_value = 5;
Set_Display_Pos(1,pos_value);
}


void Display_Model2_dire1(void)
{
LCD_Display_Words(0,0,"输入距离(cm):");
LCD_Display_Words(3,0,"下一步");
LCD_Display_Words(3,6,"退出");

pos_value = 5;
Set_Display_Pos(1,pos_value);
}


void Display_Model2_dire2(void)
{
LCD_Display_Words(0,0,"输入偏离角度:");
LCD_Display_Words(3,0,"确定");
LCD_Display_Words(3,5,"上一步");

pos_value = 5;
Set_Display_Pos(1,pos_value);
}


void Display_Model1_Contr(void)
{
LCD_Clear_Scr();
Display_Model1_dire(); // 显示模式1界面
Write_LCD_Command(0x0e); // 显示器开,光标开

while(1)
{
key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

Key_Number(1); // 输入数字

if(key_value == 15) // “确定”键
{

if( Dis_valueBuf[0] != 'o' && Dis_valueBuf[1] != 'o' && Dis_valueBuf[2] != 'o')
{
uint16_t dis_temp = 0;

dis_temp = (Dis_valueBuf[2]-'0')*100 + (Dis_valueBuf[1]-'0')*10 + (Dis_valueBuf[0]-'0');

if(dis_temp >= 200 && dis_temp <= 300)
{
LCD_Clear_Scr();
Display_Running(); // 显示运行界面

uint16_t pwm_temp = 0;
pwm_temp = Distance_to_pwm(dis_temp);
/* 改变舵机1的角度 */
MG996R_TIM_SetPWM_Num(1,pwm_temp); // 改变仰角的角度,控制发射距离

/* 充电 */
CHARGING_SWITCH_0();
delay_ms(1000);
delay_ms(1000);
CHARGING_SWITCH_1();

/* 发射 */
delay_ms(500);
DISCHARGING_SWITCH_1();
delay_ms(1000);
delay_ms(1000);
DISCHARGING_SWITCH_0();


while(1)
{
key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Model1_dire(); // 显示模式1界面
MG996R_Initial_Position();

delay_ms(100);
Dis_valueBuf[0] = 'o';
Dis_valueBuf[1] = 'o';
Dis_valueBuf[2] = 'o';
break;
}
}
}
}

}
}
else if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Main_dire();
Write_LCD_Command(0x0c); // 显示器开,光标关闭

Model_flag = 0;
Dis_valueBuf[0] = 'o';
Dis_valueBuf[1] = 'o';
Dis_valueBuf[2] = 'o';

break;
}
}
}
}


void Display_Model2_Contr(void)
{
LCD_Clear_Scr();
Display_Model2_dire1(); // 显示模式2_1界面
Write_LCD_Command(0x0e); // 显示器开,光标开

while(1)
{
key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

Key_Number(1);
if(key_value == 15)
{
if( Dis_valueBuf[0] != 'o' && Dis_valueBuf[1] != 'o' && Dis_valueBuf[2] != 'o')
{
uint16_t dis_temp = 0;

dis_temp = (Dis_valueBuf[2]-'0')*100 + (Dis_valueBuf[1]-'0')*10 + (Dis_valueBuf[0]-'0');

if(dis_temp >= 200 && dis_temp <= 300)
{
LCD_Clear_Scr();
Display_Model2_dire2(); // 显示模式2_2界面

while(1)
{
key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

Key_Number(2); // 输入数字

if(key_value == 15) // "确定"键
{
if( Angle_valueBuf[1] != 'o' && Angle_valueBuf[2] != 'o')
{
uint16_t angle_temp = 0;

if(Angle_valueBuf[0] == 'o')
angle_temp = Angle_valueBuf[1]-'0';
else
angle_temp = (Angle_valueBuf[1]-'0')*10 + (Angle_valueBuf[0]-'0');

if(angle_temp <= 30)
{
LCD_Clear_Scr();
Display_Running(); // 显示运行界面


/* 水平转动角度 */
uint16_t pwm2 = 0;
if(Angle_valueBuf[2] == '-')
pwm2 = (90-angle_temp)/0.09 + 500;
else
pwm2 = (90+angle_temp)/0.09 + 500;
MG996R_TIM_SetPWM_Num(2,pwm2);

uint16_t pwm_temp = 0;
pwm_temp = Distance_to_pwm(dis_temp);
/* 改变舵机1的角度 */
MG996R_TIM_SetPWM_Num(1,pwm_temp); // 改变仰角的角度,控制发射距离

/* 充电 */
CHARGING_SWITCH_0();
delay_ms(1000);
delay_ms(1000);
CHARGING_SWITCH_1();

/* 发射 */
delay_ms(500);
DISCHARGING_SWITCH_1();
delay_ms(1000);
delay_ms(1000);
DISCHARGING_SWITCH_0();

while(1)
{
key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Model2_dire2(); // 显示模式2_2界面
MG996R_Initial_Position();
break;
}
}
}
}
}
}
else if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Model2_dire1(); // 显示模式2_1界面
Angle_valueBuf[0] = 'o';
Angle_valueBuf[1] = 'o';
Angle_valueBuf[2] = 'o';
break;
}
}
}
}
}
}
else if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Main_dire();
Write_LCD_Command(0x0c); // 显示器开,光标关闭

Model_flag = 0;
Dis_valueBuf[0] = 'o';
Dis_valueBuf[1] = 'o';
Dis_valueBuf[2] = 'o';
Angle_valueBuf[0] = 'o';
Angle_valueBuf[1] = 'o';
Angle_valueBuf[2] = 'o';
break;
}

}
}
}



void Display_Model3_Contr(void)
{
RxBuf = 0;
Usart_SendByte(DEBUG_USART,'c'); // 发送设置 OpenMV 为模式3
LCD_Clear_Scr();
Display_Running(); // 显示运行界面
HCdistance = 0;
count_flag = 1;
Rxflag = 1;
TIM_Cmd(HCSR04_TRIG_TIM,ENABLE); // 开启超声波测距

uint16_t temp1 = 0,temp2 = 0,flag = 1;


while(1)
{
if(RxBuf == 'L' && flag == 1)
{
MG996R_TIM_SetPWM_Num(2,1500+temp1);
temp1 += 1;
if(temp1 > 333)
temp1 = 333;

delay_ms(10);
}
else if(RxBuf == 'R' && flag == 1)
{
MG996R_TIM_SetPWM_Num(2,1500-temp2);
temp2 += 1;
if(temp2 > 333)
temp2 = 333;

delay_ms(10);
}
else if(RxBuf == 'S' && flag == 1)
{
flag = 0;
Usart_SendByte(DEBUG_USART,'e');

delay_ms(1000);
delay_ms(1000);
uint16_t pwm_temp = 0;
pwm_temp = Distance_to_pwm(HCdistance-30);
/* 改变舵机1的角度 */
MG996R_TIM_SetPWM_Num(1,pwm_temp); // 改变仰角的角度,控制发射距离

/* 充电 */
CHARGING_SWITCH_0();
delay_ms(1000);
delay_ms(1000);
CHARGING_SWITCH_1();

/* 发射 */
delay_ms(500);
DISCHARGING_SWITCH_1();
delay_ms(1000);
delay_ms(1000);
DISCHARGING_SWITCH_0();
}

key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Main_dire(); // 显示主界面
Model_flag = 0;
Rxflag = 0;

Usart_SendByte(DEBUG_USART,'e');
TIM_Cmd(HCSR04_TRIG_TIM,DISABLE); // 关闭超声波测距
break;
}
}
}
}


//void Display_Model4_Contr(void)
//{
// LCD_Clear_Scr();
// Display_Running(); // 显示运行界面
//
//
// /* 充电 */
// CHARGING_SWITCH_0();
// delay_ms(1000);
// delay_ms(1000);
// CHARGING_SWITCH_1();

//
// /* 发射 */
// delay_ms(500);
// DISCHARGING_SWITCH_1();
// delay_ms(1000);
// delay_ms(1000);
// DISCHARGING_SWITCH_0();

//
// while(1)
// {
//
// key_value = Key_scan(); // 按键扫描

// if(key_flag == 1)
// {
// key_flag = 0;
//
// if(key_value == 16) // “退出”键
// {
// LCD_Clear_Scr();
// Display_Main_dire(); // 显示主界面
// MG996R_Initial_Position();
// Model_flag = 0;
//
// break;
// }
// }
// }
//}


void Display_Model4_Contr(void)
{
uint16_t pwm_temp = 0;
pwm_temp = Distance_to_pwm(250);
/* 改变舵机1的角度 */
MG996R_TIM_SetPWM_Num(1,pwm_temp); // 改变仰角的角度,控制发射距离
MG996R_TIM_SetPWM_Num(2,1167); // -30°

Usart_SendByte(DEBUG_USART,'d'); // 发送设置 OpenMV 为模式4
LCD_Clear_Scr();
Display_Running(); // 显示运行界面

/* 充电 */
CHARGING_SWITCH_0();
delay_ms(1000);
// delay_ms(500);
// CHARGING_SWITCH_1();

// Usart_SendByte(DEBUG_USART,'d');
uint16_t temp = 0,flag = 0;
RxBuf = 0;
Rxflag = 1;
while(1)
{
if(flag == 0)
{
MG996R_TIM_SetPWM_Num(2,1167+temp);
temp++;

// if(temp < 166)
// Rxflag = 0;
// else
// Rxflag = 1;

if(temp >= 660)
{
flag = 1;
temp = 0;
}

delay_ms(6);
}
else
{
MG996R_TIM_SetPWM_Num(2,1833-temp);
temp++;
if(temp >= 660)
{
flag = 0;
temp = 0;
}

delay_ms(6);
}

if(RxBuf == 'P')
{
/* 关闭充电 */
CHARGING_SWITCH_1();

RxBuf = 0;
}
else if(RxBuf == 'S')
{
/* 发射 */
DISCHARGING_SWITCH_1();
}


key_value = Key_scan(); // 按键扫描

if(key_flag == 1)
{
key_flag = 0;

if(key_value == 16) // “退出”键
{
LCD_Clear_Scr();
Display_Main_dire(); // 显示主界面
Model_flag = 0;

Usart_SendByte(DEBUG_USART,'b');
/* 关闭充电 */
CHARGING_SWITCH_1();
DISCHARGING_SWITCH_0();
break;
}
}
}
}


void Key_Number(uint8_t value_flag)
{
if(key_value == 1) // 1
{
LCD_Display_Words(1,pos_value,"1");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '1';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '1';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 2) // 2
{
LCD_Display_Words(1,pos_value,"2");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '2';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '2';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 3) // 3
{
LCD_Display_Words(1,pos_value,"3");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '3';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '3';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 4) // 4
{
LCD_Display_Words(1,pos_value,"4");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '4';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '4';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 5) // 5
{
LCD_Display_Words(1,pos_value,"5");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '5';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '5';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 6) // 6
{
LCD_Display_Words(1,pos_value,"6");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '6';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '6';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 7) // 7
{
LCD_Display_Words(1,pos_value,"7");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '7';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '7';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 8) // 8
{
LCD_Display_Words(1,pos_value,"8");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '8';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '8';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 9) // 9
{
LCD_Display_Words(1,pos_value,"9");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '9';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '9';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 10) // 0
{
LCD_Display_Words(1,pos_value,"0");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '0';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '0';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 11) // 删除
{
LCD_Display_Words(1,pos_value," ");

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = 'o';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = 'o';

pos_value--;
if(pos_value < 5)
{
pos_value = 5;
}
Set_Display_Pos(1,pos_value);
}
else if(key_value == 13)
{
LCD_Display_Words(1,pos_value,"+"); // "+"号

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '+';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '+';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
else if(key_value == 14)
{
LCD_Display_Words(1,pos_value,"-"); // "-"号

if(value_flag == 1)
Dis_valueBuf[7-pos_value] = '-';
else if(value_flag == 2)
Angle_valueBuf[7-pos_value] = '-';

pos_value++;
if(pos_value > 7)
pos_value = 7;
}
}
/*********************************************END OF FILE*********************************************/

六、元器件接线说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
【*】 引脚分配

STM32 MG996R
PB6 -> ①
PB7 -> ②
+5V -> Vcc
GND -> GND

STM32 串口模块
PB11 -> Tx
PB10 -> Rx
+3.3V -> +3.3V
GND -> GND
波特率:115200

STM32 LCD12864
PA0 -> CS
PA1 -> SID
PA2 -> SCLK
+3.3v -> RST
+3.3V -> Vcc
GND -> GND
GND -> PSB
+3.3v -> BLA
GND -> BLK

STM32 矩阵键盘
PB12 -> C4
PB13 -> C3
PB14 -> C2
PB15 -> C1
PA8 -> R1
PA9 -> R2
PA10 -> R3
PA11 -> R4

STM32 超声波模块
PB8 -> Trig
PB9 -> Echo
+5V -> Vcc
GND -> GND

STM32 继电器
PA3 -> IN

STM32 可控硅
PA4 -> I/O


模拟电磁曲射炮设计
https://kevinloongc.github.io/posts/9e207.html
作者
Kevin Loongc
发布于
2019年8月23日
许可协议