EB配置MCAL全模块讲解【Autosar——5 Gpt模块详解】

       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的中断回调。

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
在S32K144芯片中,使用MCAL(Microcontroller Abstraction Layer)模块需要进行以下步骤: 1. 在MCUXpresso IDE中创建一个新的S32K144工程,并选择MCUXpresso SDK为工程模板。 2. 在工程配置中,选择需要使用的MCAL模块,例如GPIO、UART等,并根据具体需求进行相应的配置。 3. 在代码中包含相应的MCAL库文件,并调用相应的MCAL函数来实现对硬件资源的访问和控制。 以下是示例代码: ``` #include "fsl_gpio.h" #include "fsl_uart.h" int main(void) { gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0, }; uart_config_t uart_config; uint8_t buffer[20] = {0}; uint32_t i; // Initialize LED GPIO_PinInit(GPIO, 0U, 19U, &led_config); GPIO_PinWrite(GPIO, 0U, 19U, 1); // Initialize UART UART_GetDefaultConfig(&uart_config); uart_config.baudRate_Bps = 115200U; UART_Init(UART0, &uart_config, CLOCK_GetFreq(kCLOCK_CoreSysClk)); UART_WriteBlocking(UART0, "MCAL example\r\n", 15); // Toggle LED and send UART message while (1) { GPIO_TogglePinsOutput(GPIO, 0U, 1U << 19U); for (i = 0; i < 1000000U; i++) { __asm("NOP"); } UART_WriteBlocking(UART0, "LED toggled\r\n", 13); } } ``` 在代码中,先通过GPIO_PinInit函数初始化GPIO引脚为输出模式,然后通过GPIO_PinWrite函数控制LED的亮灭。同时,通过UART_GetDefaultConfig和UART_Init函数初始化UART模块,并通过UART_WriteBlocking函数发送消息。最后,通过GPIO_TogglePinsOutput函数实现LED的闪烁效果。 需要注意的是,在使用MCAL模块时需要仔细阅读相应的文档,并根据具体需求进行相应的配置和调试。同时,也需要注意硬件资源的安性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值