Gpt定时器模块是程序中非常重要的模块,它可以定义不同的时间段在While循环中运行,保证重要程度不同的功能能够稳定工作。(基于S32K312芯片进行开发,采用在不同定时器的时间段去点亮LED灯。来展现Gpt是否配对)
1.首先在EB中创建新工程,并添加以下模块GPT原本不需要定义引脚(Port与Dio是负责点灯)
2.在Port中添加MCU_LED的引脚,在Dio中将它定义出来(可以使用Dio_WriteChannel定义该引脚的高低电平)
3. 首先打开Gpt模块,选择Pre模式,方便初始化,关闭Wakeup相关内容(没有用到wakeup模块,开启后会报错)
4.在Gpt模块中打开PIT0_CH0的中断,并添加相应的通道
5. 添加Gpt所用的时钟,并添加上诉引脚的具体配置(Gpt采用SLOW_CLK,MCU配置可看之前的MCU详解)
6.在详细配置界面,注意中断回调函数的打开与命名。
7. 在Platform模块中打开PIT0通道的中断与中断回调
之后生成代码就可以到DS中去集成了(附带Gpt定义时间段代码)
volatile uint8 timer_0_1ms = 0;
volatile uint8 timer_1ms = 0;
volatile uint8 timer_2ms = 0;
volatile uint8 timer_4ms = 0;
volatile uint8 timer_4_5ms = 0;
volatile uint8 timer_10ms = 0;
volatile uint8 timer_100ms = 0;
volatile uint8 timer_200ms = 0;
volatile uint8 timer_500ms = 0;
volatile uint8 timer_1000ms = 0;
volatile uint8 Count_0_1ms_for_1ms = 0;
volatile uint8 Count_1ms_for_2ms = 0;
volatile uint8 Count_1ms_for_4ms = 0;
volatile uint8 Count_0_1ms_for_4_5ms = 0;
volatile uint8 Count_1ms_for_10ms = 0;
volatile uint8 Count_10ms_for_100ms = 0;
volatile uint8 Count_10ms_for_200ms = 0;
volatile uint8 Count_10ms_for_500ms = 0;
volatile uint8 Count_10ms_for_1000ms = 0;
void GptNotification(void)
{
timer_0_1ms = 1;
//Count for 4.5ms
if(Count_0_1ms_for_4_5ms < 44)
{
Count_0_1ms_for_4_5ms++;
}
else
{
Count_0_1ms_for_4_5ms = 0;
timer_4_5ms = 1;
}
if(Count_0_1ms_for_1ms < 9)
{
Count_0_1ms_for_1ms++;
}
else
{
Count_0_1ms_for_1ms = 0;
timer_1ms = 1;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Icu_Pwm_Capture();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if(Count_1ms_for_2ms < 1)
Count_1ms_for_2ms++;
else
{
timer_2ms = 1;
Count_1ms_for_2ms = 0;
}
if(Count_1ms_for_4ms < 3)
Count_1ms_for_4ms++;
else
{
timer_4ms = 1;
Count_1ms_for_4ms = 0;
}
if(Count_1ms_for_10ms < 9)
Count_1ms_for_10ms++;
else
{
timer_10ms = 1;
Count_1ms_for_10ms = 0;
if(Count_10ms_for_100ms < 9) //count 1ms, 10 * 1ms = 10ms
Count_10ms_for_100ms++;
else
{
Count_10ms_for_100ms = 0;
timer_100ms = 1;
if(Count_10ms_for_200ms < 1)
{
Count_10ms_for_200ms++;
}
else
{
Count_10ms_for_200ms = 0;
timer_200ms = 1;
}
if(Count_10ms_for_500ms < 4) //count 100ms, 5 * 10ms = 500ms
{
Count_10ms_for_500ms++;
}
else
{
Count_10ms_for_500ms = 0;
timer_500ms = 1;
ledRunFlagValue = !ledRunFlagValue;
Dio_WriteChannel(DioConf_DioChannel_MCU_LED, ledRunFlagValue);
}
if(Count_10ms_for_1000ms < 9)
{
Count_10ms_for_1000ms++;
}
else
{
Count_10ms_for_1000ms = 0;
timer_1000ms = 1;
}
}
}
}
}
Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_0, 3000U);
定义最小时间段(时钟除以3000为频率,周期为频率的倒数)。
Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
开启Gpt的中断回调。