文章目录
- 目标
- 建立工程
- 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为输出:
导入lpit组件:
双击Components窗口Components目录下的 lpit1:lpit
:
此处周期单元选择了us, 周期1000000us, 也就是1s, 使能中断.
到此配置完毕, 点击生成代码:
代码拖动和补全
打开 main.c
, 函数可以从Components中的组件展开直接用鼠标拖入, 然后补全代码即可:
#include "Cpu.h"
volatile int exit_code = 0;
#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));
}
int main(void)
{
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT();
#endif
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));
#ifdef PEX_RTOS_START
PEX_RTOS_START();
#endif
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
}
时钟:
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