GPT 介绍
地奇星的通用定时器(GPT)是一个具有GPT16E × 6通道的16位定时器。PWM波形可以由控制上行计数器、下行计数器或上行和下行计数器。此外,PWM波形可以生成用于控制无刷直流电机。GPT还可以用作通用计时器,GPT16是16位的定时器,能计数的范围为:0~0xFFFF。
GPT模块支持事件计数、外部输入信号测量、作为通用计时器产生周期性中断、输出周期性或PWM信号至GTIOC引脚,也能通过软件实现单个脉冲输出(需注意,硬件本身不支持One-Shot功能),若使用该模式时必须开启中断,并在脉冲周期结束后的ISR中断服务函数中停止计时器。
GPT 功能表
GPT I/O Pins
GPT 基本定时器
基于GPT的定时功能,设计一个 500Hz 的一个方波。
首先打开 e2 studio 软件新建工程后,在 FSP Configuration -> Pins -> Ports -> P4 -> P402 接下来我们需要配置 Mode 为模式 Output 。如下图所示:
接下来,添加 GPT 定时器的模块, New Stack -> Timers -> Timer,General PWM(r_gpt),如下图所示:
接下来我们选择 g_timer0 模块,然后点击属性,然后放大,需要修改的内容如下图所示:
注:
General 参数说明: Mode:
- Periodic:周期计数
- One-Shot:单次计数
- PWM:脉冲宽度调制输出
- One-Shot Pulse:单脉冲输出
- Triangle-Wave Symmetric PWM:同步三角波输出
- Triangle-Wave Asymmetric PWM:异步三角波输出
- Triangle-Wave Asymmetric PWM(Mode 3):模式三 异步三角波输出
Period:计数周期。
Period Unit:
- Raw Counts:计数模式
- Nanoseconds:计数单位纳米
- Microseconds:计数单位微秒
- Milliseconds:计数单位毫秒
- Seconds:计数单位秒
- Hertz:计数单位HZ
- Kilohertz:计数单位KHz
Interrupts:中断配置
主要配置GPT的中断回调函数和优先级,比如溢出中断优先级、输入捕获优先级等。
设置完成后,按下 Ctrl + S 进行保存,然后点击 Generate Project Content 进行工程生成。如下图所示:
程序编写
然后再 src 下创建一个 gpt 的文件夹,在里面创建 bsp_gpt.c 与 bsp_gpt.h 两个文件,在创建一个 Applay 的文件夹,在里面创建 app.c 与 app.h 两个文件如下图所示:
#include "Apply\app.h"
#include "gpt\bsp_gpt.h"
void Run(void)
{
GPT_Init();
while(1)
{
;
}
}
2
3
4
5
6
7
8
9
10
11
12
#ifndef __APP_H
#define __APP_H
#include "hal_data.h"
void Run(void);
#endif
2
3
4
5
6
7
8
#include "bsp_gpt.h"
void GPT_Init(void)
{
/* 初始化 GPT 模块 */
R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* 设置定时器周期为5000个时钟单位*/
R_GPT_PeriodSet(&g_timer0_ctrl, 50000);
// /* 重置定时器计数器,确保从0开始计数 */
R_GPT_Reset(&g_timer0_ctrl);
/* 启动 GPT 定时器 */
R_GPT_Start(&g_timer0_ctrl);
}
void gpt0_callback(timer_callback_args_t *p_args)
{
if (p_args->event == TIMER_EVENT_CYCLE_END)
{
R_PORT4->PODR ^= 1<<(BSP_IO_PORT_04_PIN_02 & 0xFF);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __BSP_GPT_H
#define __BSP_GPT_H
#include "hal_data.h"
#include <stdio.h>
void GPT_Init(void);
#endif
2
3
4
5
6
7
8
9
#include "hal_data.h"
#include "Apply\app.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
Run();
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
测试结果
GPT PWM输出
使用GPT定时器输出PWM波,频率设置为10KHz
注:工程是 GPT基本定时器的工程基础上进行开发的。
这里我们需要去配置PWM输出到那个GPIO引脚,配置引脚 在 FSP Configuration -> Pins -> Peripherals -> Timers:GPT -> GPT3 接下来我们需要配置 Operation Mode 选择 GTIOCA or GTIOCB ,如下图所示:
接下来,添加 GPT 定时器的模块, New Stack -> Timers -> Timer,General PWM(r_gpt),如下图所示:
接下来我们选择 g_timer1 模块,然后点击属性,然后放大,需要修改的内容如下图所示:
注:
Output:
- Duty Cycle Percent (only applicable in PWM modoe):周期设置,只用于PWM模式
- GTIOCA\GTIOCB Output Enabled :GPT输出A与B输出通道使能
- GTIOCA\GTIOCB Stop Level :停止时IO输出状态的电平设置
设置完成后,按下 Ctrl + S 进行保存,然后点击 Generate Project Content 进行工程生成。如下图所示:
程序编写
#include "Apply\app.h"
#include "gpt\bsp_gpt.h"
void Run(void)
{
uint16_t count = 0;
GPT_Init();
while (1)
{
for (count = 0 ; count<100 ; count ++)
{
GPT_Pwm_Set(count);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __APP_H
#define __APP_H
#include "hal_data.h"
#include <stdio.h>
void Run(void);
#endif
2
3
4
5
6
7
8
9
#include "bsp_gpt.h"
void GPT_Init(void)
{
/* 初始化 GPT 模块 */
R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* 设置定时器周期为5000个时钟单位*/
R_GPT_PeriodSet(&g_timer0_ctrl, 50000);
// /* 重置定时器计数器,确保从0开始计数 */
R_GPT_Reset(&g_timer0_ctrl);
/* 启动 GPT 定时器 */
R_GPT_Start(&g_timer0_ctrl);
/* 初始化 GPT 模块 */
R_GPT_Open(&g_timer3_ctrl, &g_timer3_cfg);
/* 启动 GPT 定时器 */
R_GPT_Start(&g_timer3_ctrl);
}
/* GPT定时器 中断回调函数 */
void gpt0_callback(timer_callback_args_t *p_args)
{
if (p_args->event == TIMER_EVENT_CYCLE_END)
{
R_PORT4->PODR ^= 1<<(BSP_IO_PORT_04_PIN_02 & 0xFF);
}
}
void GPT_Pwm_Set(uint16_t value)
{
timer_info_t info;
uint32_t period_counts;
/*获取GPT3信息*/
R_GPT_InfoGet(&g_timer3_ctrl, &info);
period_counts = info.period_counts;
/*设置PWM周期*/
R_GPT_DutyCycleSet(&g_timer3_ctrl,
(uint32_t)((period_counts * value) / 100),
GPT_IO_PIN_GTIOCA
);
}
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
#ifndef __BSP_GPT_H
#define __BSP_GPT_H
#include "hal_data.h"
#include <stdio.h>
void GPT_Init(void);
void GPT_Pwm_Set(uint16_t value);
#endif
2
3
4
5
6
7
8
9
10
#include "hal_data.h"
#include "Apply\app.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
Run();
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22