双轴按键游戏摇杆模块,采用 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 | 5V0 |
VRX | GPIO-A6 |
VRY | GPIO-A7 |
SW | GPIO54 |
接下来我们配置 SYSCONFIG
- 双击 c2000.syscfg 文件,打开它。
- 点击 ADD 添加配置
- 配置
- 点击ADD添加GPIO配置
- 配置GPIO
Ctrl + S
保存配置文件Ctrl + B
构建工一次工程(可能会报错,我们不用管!)然后我们所有设定的引脚和功能就会在 board.h 中定义。因为这个文件我们包含进了 tjx_init.h 所以我们只需要引用 tjx_init.h 即可。【这里的 tjx_init.h 就充当了芯片头文件的作用】
3、代码编写
我们在工程中新建 module_driver
,并在其中新建两个文件 bsp_rock.c
和 bsp_rock.h
,并且将头文件路径添加到编译器中。
在文件 bsp_rock.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 uint16_t ADC_GET(uint8_t CHx)
{
uint16_t gAdcResult = 0;
uint16_t timeOut = 200;
uint16_t CH_Force = 0;
uint16_t CH_Number = 0;
if(CHx == 0){
CH_Force = Module_ADC_FORCE_SOC0;
CH_Number = Module_ADC_SOC0;
}else if(CHx == 1){
CH_Force = Module_ADC_FORCE_SOC1;
CH_Number = Module_ADC_SOC1;
}else{
lc_printf("CHx Error!!\r\n");
return 1;
}
// 通过软件触发 SOC 转换
ADC_forceMultipleSOC(Module_ADC_BASE, CH_Force);
// 等待ADC总线处理完成
while(ADC_isBusy(Module_ADC_BASE) && timeOut--)
{
delay_us(5);
}
if(!timeOut)
{
lc_printf("ADC_GET Failed!!!\r\n");
return 1;
}
// 获取通道的转换结果
gAdcResult = ADC_readResult(Module_ADC_RESULT_BASE, CH_Number);
// lc_printf("gAdcResult = %d\r\n",gAdcResult);
return gAdcResult;
}
/******************************************************************
* 函 数 名 称:Get_Adc_Joystick_Value
* 函 数 说 明:对保存的数据进行平均值计算后输出
* 函 数 形 参:CHx 那个通道值
* 函 数 返 回:对应扫描的ADC值
* 作 者:LCKFB
* 备 注:无
******************************************************************/
uint16_t Get_Adc_Joystick_Value(uint8_t CHx)
{
uint16_t 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
* 备 注:无
******************************************************************/
uint16_t Get_Joystick_Percentage_value(uint8_t dir)
{
uint16_t adc_new = 0;
uint16_t 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
{
lc_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;
}
}
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
在文件 bsp_rock.h 中,编写如下代码。
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef __BSP_ROCK_H__
#define __BSP_ROCK_H__
#include "tjx_init.h"
#define GET_SW GPIO_readPin(GPIO_SW)
// 采样次数
#define SAMPLES 5
uint16_t Get_Joystick_Percentage_value(uint8_t dir);
uint8_t Get_SW_state(void);
#endif /* __BSP_ROCK_H__ */
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
四、移植验证
在 empty_driverlib_main.c
中输入代码如下:
#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "c2000ware_libraries.h"
#include "tjx_init.h"
#include "bsp_rock.h"
void main(void)
{
/* The initialization code automatically generated by CCS [Start] */
Device_init();
Device_initGPIO();
Interrupt_initModule();
Interrupt_initVectorTable();
Board_init();
C2000Ware_libraries_init();
EINT;
ERTM;
/* The initialization code automatically generated by CCS [End] */
lc_printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
lc_printf("\r\n=== Welcome to use the LC-TJX-TMS320F28P550 ====\r\n");
lc_printf("\r\n============== www.lckfb.com ===================\r\n");
lc_printf("\r\n============== wiki.lckfb.com ==================\r\n");
lc_printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
while(1)
{
if( Get_SW_state() == 0 )
{
lc_printf("Key SW Down!!\r\n");
}
lc_printf("\r\n");
lc_printf("X = [%d]\r\n",Get_Joystick_Percentage_value(0));
lc_printf("Y = [%d]\r\n",Get_Joystick_Percentage_value(1));
// RGB的B灯亮起,G灯熄灭
GPIO_writePin(RGB_B, 0);
GPIO_writePin(RGB_G, 1);
delay_ms(20);
// RGB的G灯亮起,B灯熄灭
GPIO_writePin(RGB_B, 1);
GPIO_writePin(RGB_G, 0);
delay_ms(20);
// RGB的B和G都熄灭
GPIO_writePin(RGB_B, 1);
GPIO_writePin(RGB_G, 1);
delay_ms(20);
}
}
__interrupt void INT_Debug_Serial_RX_ISR(void)
{
//清除接收中断标志位
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_RXFF);
//清除中断标志位
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
}
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
上电效果:
【代码下载】
- 跳转到
下载中心
去下载CCS模块移植代码:【点击跳转🚀】