N20带编码器减速电机是一种小型的直流电机,通常用于一些较小的电子设备和机器人,它可以通过减速器(齿轮传动)来提供更大的扭矩和更低的速度。编码器是一种用于记录电机旋转角度和速度的传感器,可以帮助控制电机的位置和速度。因此,带编码器的N20减速电机常用于需要准确控制位置和旋转角度的应用,比如机械臂、小车和舵机等。通过编码器的反馈信号,可以精确地控制电机的运动,从而提高机器人或设备的精度和稳定性。
一、模块来源
二、规格参数
驱动电压:根据采购的电机确定
驱动电流:根据电机输入电压决定
控制方式:PWM
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够控制电机旋转速度的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
直流有刷减速电机,转速越高,力量越小。转速越小,力量越大。
2、引脚选择
电机电源 M | GPIO6(EPWM4-A) |
编码器底线 GND | GND |
编码器信号 A相 | GPIO50(EQEP1-A) |
编码器信号 B相 | GPIO51(EQEP1-B) |
编码器电源 VCC | 3V3 |
电机电源线 P | GPIO14(EPWM8-A) |
接下来我们配置 SYSCONFIG
- 双击 c2000.syscfg 文件,打开它。
- 点击ADD添加EQEP配置
- 配置EQEP
- 点击 ADD 添加 PWM 配置
- 配置 PWM
Ctrl + S
保存配置文件Ctrl + B
构建工一次工程(可能会报错,我们不用管!)然后我们所有设定的引脚和功能就会在 board.h 中定义。因为这个文件我们包含进了 tjx_init.h 所以我们只需要引用 tjx_init.h 即可。【这里的 tjx_init.h 就充当了芯片头文件的作用】
3、代码编写
我们在工程中新建 module_driver
,并在其中新建两个文件 bsp_motor_hallencoder.c
和 bsp_motor_hallencoder.h
,并且将头文件路径添加到编译器中。
在文件bsp_motor_hallencoder.c中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_motor_hallencoder.h"
volatile uint32_t Encoder_Count = 0; // 编码器计数,用于用户使用。
volatile uint32_t Encoder_Last_Count = 0; // 存储上一次中断时的编码器位置值
volatile int32_t Motor_dir = 0; // 电机旋转方向(正转/反转)
bool Encoder_InitDone = false;
void Encoder_Init(void)
{
Encoder_Last_Count = EQEP_getPositionLatch(Module_EQEP_BASE);
Encoder_InitDone = false;
}
/******************************************************************
* 函 数 名 称:Motor_Set_PWM
* 函 数 说 明:左右电机速度控制
* 函 数 形 参:Mode: 1左转 2右转 0停止
Speed: 速度
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:PWM值范围0-9999
******************************************************************/
void Motor_Set_PWM(uint8_t Mode, uint16_t Speed)
{
if(Mode == 1){
EPWM_setCounterCompareValue(Module_PWM_M_BASE, EPWM_COUNTER_COMPARE_A, 0);
EPWM_setCounterCompareValue(Module_PWM_P_BASE, EPWM_COUNTER_COMPARE_A, Speed);
}else if(Mode == 2){
EPWM_setCounterCompareValue(Module_PWM_M_BASE, EPWM_COUNTER_COMPARE_A, Speed);
EPWM_setCounterCompareValue(Module_PWM_P_BASE, EPWM_COUNTER_COMPARE_A, 0);
}else if (Mode == 0) {
EPWM_setCounterCompareValue(Module_PWM_M_BASE, EPWM_COUNTER_COMPARE_A, 0);
EPWM_setCounterCompareValue(Module_PWM_P_BASE, EPWM_COUNTER_COMPARE_A, 0);
}else {
EPWM_setCounterCompareValue(Module_PWM_M_BASE, EPWM_COUNTER_COMPARE_A, 0);
EPWM_setCounterCompareValue(Module_PWM_P_BASE, EPWM_COUNTER_COMPARE_A, 0);
}
}
/******************************************************************
* 函 数 名 称:Get_Encoder_Dir
* 函 数 说 明:获取方向
* 函 数 形 参:
* 函 数 返 回:
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int32_t Get_Encoder_Dir(void)
{
return Motor_dir;
}
/******************************************************************
* 函 数 名 称:Get_Encoder_Value
* 函 数 说 明:获取编码器的值
* 函 数 形 参:
* 函 数 返 回:
* 作 者:LCKFB
* 备 注:无
******************************************************************/
uint32_t Get_Encoder_Value(void)
{
return Encoder_Count;
}
//
// 每周期触发一次中断
//
__interrupt void INT_Module_EQEP_ISR(void)
{
uint32_t newCount = EQEP_getPositionLatch(Module_EQEP_BASE);
int32_t newDir = EQEP_getDirection(Module_EQEP_BASE);
if (!Encoder_InitDone) {
Encoder_Last_Count = newCount;
Encoder_InitDone = true;
EQEP_clearInterruptStatus(Module_EQEP_BASE,EQEP_INT_UNIT_TIME_OUT|EQEP_INT_GLOBAL);
Interrupt_clearACKGroup(INT_Module_EQEP_INTERRUPT_ACK_GROUP);
return;
}
int32_t diff = (int32_t)(newCount - Encoder_Last_Count);
uint32_t delta = (uint32_t)ABS(diff);
Encoder_Count = delta;
Encoder_Last_Count = newCount;
Motor_dir = newDir;
EQEP_clearInterruptStatus(Module_EQEP_BASE,EQEP_INT_UNIT_TIME_OUT|EQEP_INT_GLOBAL);
Interrupt_clearACKGroup(INT_Module_EQEP_INTERRUPT_ACK_GROUP);
}
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
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
在文件bsp_motor_hallencoder.h中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef __BSP_MOTOR_HALLENCODER_H__
#define __BSP_MOTOR_HALLENCODER_H__
#include "tjx_init.h"
// 获得绝对值
#define ABS(a) (a>0 ? a:(-a))
void Encoder_Init(void);
int32_t Get_Encoder_Dir(void);
uint32_t Get_Encoder_Value(void);
void Motor_Set_PWM(uint8_t Mode, uint16_t Speed);
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
四、移植验证
在 empty_driverlib_main.c
中输入代码如下:
c
#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "c2000ware_libraries.h"
#include "tjx_init.h"
#include "bsp_motor_hallencoder.h"
// extern volatile uint32_t Encoder_Count; // 编码器计数,用于用户使用。
// extern volatile uint32_t Encoder_Last_Count; // 存储上一次中断时的编码器位置值
// extern volatile int32_t Motor_dir; // 电机旋转方向(正转/反转)
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");
Encoder_Init();
while(1)
{
// 电流太小,驱不动,尽量使用驱动板
// Motor_Set_PWM(1, 5000);
DINT;
uint32_t value = Get_Encoder_Value();
int32_t dir = Get_Encoder_Dir();
EINT;
lc_printf("\r\n");
lc_printf("Encoder_Count = [%lu]\r\n", value);
lc_printf("Motor_dir = [%d]\r\n", dir);
// RGB的B灯亮起,G灯熄灭
GPIO_writePin(RGB_B, 0);
GPIO_writePin(RGB_G, 1);
delay_ms(50);
// RGB的G灯亮起,B灯熄灭
GPIO_writePin(RGB_B, 1);
GPIO_writePin(RGB_G, 0);
delay_ms(50);
// RGB的B和G都熄灭
GPIO_writePin(RGB_B, 1);
GPIO_writePin(RGB_G, 1);
delay_ms(50);
}
}
__interrupt void INT_Debug_Serial_RX_ISR(void)
{
//清除接收中断标志位
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_RXFF);
//清除中断标志位
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
}
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
【代码下载】
- 跳转到
下载中心
去下载CCS模块移植代码:【点击跳转🚀】