S32K148_ProcessorExpert之定时器中断

文章目录

    • 目标
    • 建立工程
    • ProcessorExpert配置
    • 代码拖动和补全
    • 调试设置
    • 工程源码

目标

通过定时器每1s翻转一次 LED_GREEN(引脚PTE22) 的亮灭状态.

建立工程

步骤:

  • File -> New -> S32DS Application Project
  • Processors 选择 S32K148, Project Name 填你自己的工程名
  • Select SDK: SDKs 选择 S32K148_SDK 3.0.0, Finish

ProcessorExpert配置

双击工程名, 点击Components窗口Components目录下的 pin_mux:PinSetting, 设置绿色LED引脚PTE22为输出:
S32K148_ProcessorExpert之定时器中断_第1张图片
导入lpit组件:

S32K148_ProcessorExpert之定时器中断_第2张图片
双击Components窗口Components目录下的 lpit1:lpit:

S32K148_ProcessorExpert之定时器中断_第3张图片
此处周期单元选择了us, 周期1000000us, 也就是1s, 使能中断.

到此配置完毕, 点击生成代码:

S32K148_ProcessorExpert之定时器中断_第4张图片

代码拖动和补全

打开 main.c, 函数可以从Components中的组件展开直接用鼠标拖入, 然后补全代码即可:

/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"

  volatile int exit_code = 0;

/* User includes (#include below this line is not maintained by Processor Expert) */
#define LED0_PIN_INDEX  22U
#define LED_GPIO_PORT   PTE

#define LPIT_CHANNEL        0UL
#define LPIT_Channel_IRQn   LPIT0_Ch0_IRQn

void LPIT_Callback(void) {
	LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1, (1 << LPIT_CHANNEL));
	PINS_DRV_TogglePins(LED_GPIO_PORT, (1 << LED0_PIN_INDEX));
}
/*! 
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  #ifdef PEX_RTOS_INIT
    PEX_RTOS_INIT();                   /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

    PINS_DRV_SetPinsDirection(LED_GPIO_PORT, (1 << LED0_PIN_INDEX));
    PINS_DRV_WritePin(LED_GPIO_PORT, LED0_PIN_INDEX, 1);

    LPIT_DRV_Init(INST_LPIT1, &lpit1_InitConfig);
    LPIT_DRV_InitChannel(INST_LPIT1, LPIT_CHANNEL, &lpit1_ChnConfig0);
    INT_SYS_InstallHandler(LPIT_Channel_IRQn, &LPIT_Callback, (isr_t *)0);
    LPIT_DRV_StartTimerChannels(INST_LPIT1, (1 << LPIT_CHANNEL));

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;) {
    if(exit_code != 0) {
      break;
    }
  }
  return exit_code;
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */

时钟:

    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

IO:

#define LED0_PIN_INDEX  22U
#define LED_GPIO_PORT   PTE

    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
    PINS_DRV_SetPinsDirection(LED_GPIO_PORT, (1 << LED0_PIN_INDEX));
    PINS_DRV_WritePin(LED_GPIO_PORT, LED0_PIN_INDEX, 1);	//点亮LED

定时器:

#define LPIT_CHANNEL        0UL
#define LPIT_Channel_IRQn   LPIT0_Ch0_IRQn

void LPIT_Callback(void) {
	LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1, (1 << LPIT_CHANNEL));
	PINS_DRV_TogglePins(LED_GPIO_PORT, (1 << LED0_PIN_INDEX));
}
  
    LPIT_DRV_Init(INST_LPIT1, &lpit1_InitConfig);
    LPIT_DRV_InitChannel(INST_LPIT1, LPIT_CHANNEL, &lpit1_ChnConfig0);
    INT_SYS_InstallHandler(LPIT_Channel_IRQn, &LPIT_Callback, (isr_t *)0);	//回调函数
    LPIT_DRV_StartTimerChannels(INST_LPIT1, (1 << LPIT_CHANNEL));

调试设置

参考前篇, 此处略.

工程源码

https://download.csdn.net/download/weifengdq/11813519

你可能感兴趣的:(S32K148)