双轴按键游戏摇杆模块,采用 PS2游戏手柄上金属按键摇杆电位器。模块特设二路模拟输出和一路数字输出接口、输出值分别对应(X、Y)双轴偏移量、其类型为模拟量、按键表示用户是否在Z轴上按下、其类型为数字开关量、用其可以轻松控制物体,在二维空间运动、因此可以通控制器编程、传感器扩展板插接、完成具有创意性遥控互动作品。
一、模块来源
二、规格参数
驱动电压:3.3V~5V
控制方式:ADC+GPIO
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够控制电机旋转速度的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
输出信号:模块特设二路模拟输出(VRX,VRY)和一路数字输出接口(SW),二路模拟输出值分别对应(X,Y)双轴偏移量,其类型为模拟量;按键表示用户是否在Z轴上按下,其类型为数字开关量。
十字摇杆为一个双向的10K电阻器,随着摇杆方向不同,抽头的阻值随着变化。本模块如果使用5V供电,原始状态下X,Y读出电压为2.5V左右,当随箭头方向按下,读出电压值减少,限小为0V。
2、引脚选择
GND | GND |
+5V | 3V3 |
VRX | PA1 |
VRY | PA2 |
SW | PA7 |
3、代码编写
新建两个文件 bsp_rock.c
和 bsp_rock.h
,并且将头文件路径添加到编译器中。
在文件 bsp_rock.c
和 bsp_rock.h
中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_ROCK_H_
#define BSP_CODE_BSP_ROCK_H_
#include "gd32vw55x.h"
#include "systick.h"
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif
#ifndef delay_ms
#define delay_ms(x) delay_1ms(x)
#endif
#ifndef delay_us
#define delay_us(x) delay_1us(x)
#endif
#define Module_RCU_Enable() \
rcu_periph_clock_enable(BSP_ADC_RCU); \
rcu_periph_clock_enable(BSP_ADC_GPIO_X_RCU); \
rcu_periph_clock_enable(BSP_ADC_GPIO_Y_RCU); \
rcu_periph_clock_enable(GPIO_SW_RCU);
/* PA1 ADC_IN1 */
#define BSP_ADC_GPIO_X_RCU RCU_GPIOA
#define BSP_ADC_GPIO_X_PORT GPIOA
#define BSP_ADC_GPIO_X_PIN GPIO_PIN_1
/* PA2 ADC_IN2 */
#define BSP_ADC_GPIO_Y_RCU RCU_GPIOA
#define BSP_ADC_GPIO_Y_PORT GPIOA
#define BSP_ADC_GPIO_Y_PIN GPIO_PIN_2
#define BSP_ADC_RCU RCU_ADC
#define BSP_ADC ADC
#define BSP_ADC_X_CHANNEL ADC_CHANNEL_1
#define BSP_ADC_Y_CHANNEL ADC_CHANNEL_2
/* PA7 SW GPIO */
#define GPIO_SW_RCU RCU_GPIOA
#define GPIO_SW_PORT GPIOA
#define GPIO_SW_PIN GPIO_PIN_7
#define GET_SW gpio_input_bit_get(GPIO_SW_PORT, GPIO_SW_PIN) // 读取按键状态
// 采样次数
#define SAMPLES 5
void Joystick_Init(void);
int Get_Joystick_Percentage_value(uint8_t dir);
uint8_t Get_SW_state(void);
#endif /* BSP_CODE_BSP_ROCK_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_rock.h"
#include "stdio.h"
/**********************************************************
* 函 数 名 称:ADC_GET
* 函 数 功 能:读取一次ADC数据
* 传 入 参 数:CHx:读取通道是X还是Y
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:LP
**********************************************************/
static int ADC_GET(uint8_t CHx)
{
int gAdcResult = 0;
int timeOut = 200;
if(CHx == 0){ // x通道
// 设置采集通道
adc_routine_channel_config(0, BSP_ADC_X_CHANNEL, ADC_SAMPLETIME_14POINT5);
}else if(CHx == 1){ // y通道
// 设置采集通道
adc_routine_channel_config(0, BSP_ADC_Y_CHANNEL, ADC_SAMPLETIME_14POINT5);
}else{
printf("\r\nCHx Error!!\r\n");
return 1;
}
// 开始软件转换
adc_software_trigger_enable(ADC_ROUTINE_CHANNEL);
// 等待ADC总线处理完成
while(!adc_flag_get(ADC_FLAG_EOC) && timeOut--)
{
delay_1us(1);
}
// 清除ADC采样完成标志位
adc_flag_clear(ADC_FLAG_EOC);
if(!timeOut)
{
printf("ADC_GET Failed!!!\r\n");
return 0;
}
// 获取通道的转换结果
gAdcResult = adc_routine_data_read();
// printf("gAdcResult = %d\r\n",gAdcResult);
return gAdcResult;
}
/******************************************************************
* 函 数 名 称:Get_Adc_Joystick_Value
* 函 数 说 明:对保存的数据进行平均值计算后输出
* 函 数 形 参:CHx 那个通道值
* 函 数 返 回:对应扫描的ADC值
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int Get_Adc_Joystick_Value(uint8_t CHx)
{
int Data = 0;
int i;
for(i = 0; i < SAMPLES; i++)
{
Data += ADC_GET(CHx);
delay_ms(10);
}
Data = Data / SAMPLES;
return Data;
}
/******************************************************************
* 函 数 名 称:Get_MQ2_Percentage_value
* 函 数 说 明:读取摇杆值,并且返回百分比
* 函 数 形 参:0=读取摇杆左右值,1=读取摇杆上下值
* 函 数 返 回:返回百分比
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int Get_Joystick_Percentage_value(uint8_t dir)
{
int adc_new = 0;
int Percentage_value = 0;
if( dir == 0 )
{
adc_new = Get_Adc_Joystick_Value(0); // 通道0:X的值
}
else if( dir == 1 )
{
adc_new = Get_Adc_Joystick_Value(1); // 通道1:Y的值
}
else
{
printf("\nCH Error!!\r\n");
}
Percentage_value = (uint16_t)(((float)adc_new/4095.0f) * 100.f);
return Percentage_value;
}
/******************************************************************
* 函 数 名 称:Get_SW_state
* 函 数 说 明:读取摇杆是否有按下
* 函 数 形 参:无
* 函 数 返 回:0摇杆被按下 1摇杆没有按下
* 作 者:LCKFB
* 备 注:无
******************************************************************/
uint8_t Get_SW_state(void)
{
//如果被按下
if( GET_SW == 0 )
{
return 0;
}
else
{
return 1;
}
}
/******************************************************************
* 函 数 名 称:Joystick_Init
* 函 数 说 明:初始化摇杆
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
******************************************************************/
void Joystick_Init(void)
{
/* 使能ADC和GPIO时钟 */
Module_RCU_Enable();
/* 配置摇杆按键为输入模式 */
gpio_mode_set(GPIO_SW_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_SW_PIN);
// 配置ADC时钟为4分频
adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
// 配置引脚为模拟浮空输入模式
gpio_mode_set(BSP_ADC_GPIO_X_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BSP_ADC_GPIO_X_PIN);
gpio_mode_set(BSP_ADC_GPIO_Y_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BSP_ADC_GPIO_Y_PIN);
// 使能扫描模式
adc_special_function_config(ADC_SCAN_MODE, ENABLE);
// 数据右对齐
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
// ADC设置为12位分辨率
adc_resolution_config(ADC_RESOLUTION_12B);
// ADC设置为规则组 一共使用 1 个通道
adc_channel_length_config(ADC_ROUTINE_CHANNEL, 1);
// ADC外部触发禁用, 即只能使用软件触发
adc_external_trigger_config(ADC_ROUTINE_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
// 使能软件触发
adc_software_trigger_enable(ADC_ROUTINE_CHANNEL);
// ADC使能
adc_enable();
}
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
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
四、移植验证
在 src\main.c
中输入代码如下:
c
#include "gd32vw55x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32vw553h_eval.h"
#include "bsp_rock.h"
/*!
\brief toggle the led every 500ms
\param[in] none
\param[out] none
\retval none
*/
void led_spark(void)
{
static __IO uint32_t timingdelaylocal = 0U;
if(timingdelaylocal) {
if(timingdelaylocal < 500U) {
gd_eval_led_on(LED1);
} else {
gd_eval_led_off(LED1);
}
timingdelaylocal--;
} else {
timingdelaylocal = 1000U;
}
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
#ifdef __FIRMWARE_VERSION_DEFINE
uint32_t fw_ver = 0;
#endif /* __FIRMWARE_VERSION_DEFINE */
/* configure systick */
systick_config();
eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
/* initilize the LEDs, USART and key */
gd_eval_led_init(LED1);
gd_eval_com_init(EVAL_COM0);
gd_eval_key_init(KEY_TAMPER_WAKEUP, KEY_MODE_GPIO);
#ifdef __FIRMWARE_VERSION_DEFINE
fw_ver = gd32vw55x_firmware_version_get();
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n\n");
printf("\r\n=== Welcome to use the LC-GD32VW553-HMQ6 development board ====\r\n\n");
printf("\r\n======================= www.lckfb.com =========================\r\n\n");
printf("\r\n======================= wiki.lckfb.com ========================\r\n\n");
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
/* print firmware version */
printf("\r\nGD32VW55X series firmware version: V%d.%d.%d", (uint8_t)(fw_ver >> 24), (uint8_t)(fw_ver >> 16), (uint8_t)(fw_ver >> 8));
#endif /* __FIRMWARE_VERSION_DEFINE */
/* print out the clock frequency of system, AHB, APB1 and APB2 */
printf("\r\nCK_SYS is %d\r\n", rcu_clock_freq_get(CK_SYS));
printf("\r\nCK_AHB is %d\r\n", rcu_clock_freq_get(CK_AHB));
printf("\r\nCK_APB1 is %d\r\n", rcu_clock_freq_get(CK_APB1));
printf("\r\nCK_APB2 is %d\r\n", rcu_clock_freq_get(CK_APB2));
Joystick_Init();
printf("\r\nJoystick Init OK!!\r\n");
while(1)
{
if( Get_SW_state() == 0 )
{
printf("\r\nKey SW Down!!\r\n");
}
printf("\r\n");
printf("X = [%d]\r\n",Get_Joystick_Percentage_value(0));
printf("Y = [%d]\r\n",Get_Joystick_Percentage_value(1));
printf("\r\n");
delay_1ms(100);
}
}
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
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
编译烧录。
【代码下载】
- 跳转到
下载中心
去下载模块移植代码:【点击跳转🚀】