S32K144之GPIO

程序初始化前线使用Components工具对时钟和GPIO进行配置,然后再main函数里面初始化。

时钟配置参考:

S32K144之时钟配置 - 明明1109 - 博客园 (cnblogs.com)

gpio配置

S32K SDK使用详解之PinSettings组件配置与使用详解(S32K1xx PORT 和GPIO模块)_嵌入式与汽车电子开发-商业新知 (shangyexinzhi.com)

 

 

 

1,S32K144编程第一步先初始化时钟

/* Initialize and configure clocks
   * 	-	see clock manager component for details
   */
  CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
						g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
  CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

2,初始化GPIO 

PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr); //初始化IO

初始化之后就可以,在程序用调用相关GPIO的驱动程序了。

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 */
  //  WDOG_disable();/* Disable Watchdog in case it is not done in startup code */
  /* Initialize and configure clocks
    * 	-	see clock manager component for details
    */

   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); //初始化IO
   /* Output direction for LED0 & LED1 */
  // PINS_DRV_SetPinsDirection(GPIO_PORT, (1 << LED1));

    /* Set Output value LED0 & LED1 */
   //PINS_DRV_SetPins(GPIO_PORT, 1 << LED1);

   LPUART_DRV_Init(INST_LPUART0, &lpuart0_State, &lpuart0_InitConfig0); //初始化串口
   // PINS_DRV_ClearPins(GPIO_PORT, 1 << LED2);
  /* For example: for(;;) { } */
  while(1){
	  // PINS_DRV_SetPins(PTA,1<<15);//将PTA15设置为高
	  /* Toggle output value LED0 & LED1 */
		//PINS_DRV_TogglePins(GPIO_PORT, ((1 << LED1)));
	  	LED0(0);
	  	delay_ms(1000);
	  	LED0(1);
	  	delay_ms(1000);
	  	u0_printf("KEY1 按下\r\n");
	  //PINS_DRV_ClearPins(PTA,1<<15);//将PTA15设置为低
	  //PINS_DRV_ClearPins(PTE,1<<6);//将PTE6设置为低
  }

  /*** 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!!! ***/

3,常用函数介绍


1,初始化函数
/*FUNCTION**********************************************************************
 *
 * Function Name : PINS_DRV_Init
 * Description   : This function configures the pins with the options provided
 * in the given structure.
 *
 * Implements    : PINS_DRV_Init_Activity
 *END**************************************************************************/
status_t PINS_DRV_Init(uint32_t pinCount,
                       const pin_settings_config_t config[])
{
    uint32_t i;
    for (i = 0U; i < pinCount; i++)
    {
        PINS_Init(&config[i]);
    }

    return STATUS_SUCCESS;
}

2,将GPIO输出置高
/*FUNCTION**********************************************************************
 *
 * Function Name : PINS_DRV_SetPins
 * Description   : This function configures output pins listed in parameter pins (bits that are
 * '1') to have a value of 'set' (HIGH). Pins corresponding to '0' will be
 * unaffected.
 *
 * Implements    : PINS_DRV_SetPins_Activity
 *END**************************************************************************/
void PINS_DRV_SetPins(GPIO_Type * const base,
                      pins_channel_type_t pins)
{
    PINS_GPIO_SetPins(base, pins);
}

3,将GPIO输出置低
/*FUNCTION**********************************************************************
 *
 * Function Name : PINS_DRV_ClearPins
 * Description   : This function configures output pins listed in parameter pins (bits that are
 * '1') to have a 'cleared' value (LOW). Pins corresponding to '0' will be
 * unaffected.
 *
 * Implements    : PINS_DRV_ClearPins_Activity
 *END**************************************************************************/
void PINS_DRV_ClearPins(GPIO_Type * const base,
                        pins_channel_type_t pins)
{
    PINS_GPIO_ClearPins(base, pins);
}

4,将GPIO状态输出反转
/*FUNCTION**********************************************************************
 *
 * Function Name : PINS_DRV_TogglePins
 * Description   : This function toggles output pins listed in parameter pins (bits that are
 * '1'). Pins corresponding to '0' will be unaffected.
 *
 * Implements    : PINS_DRV_TogglePins_Activity
 *END**************************************************************************/
void PINS_DRV_TogglePins(GPIO_Type * const base,
                         pins_channel_type_t pins)
{
    PINS_GPIO_TogglePins(base, pins);
}

5,输入时,读取GPIO状态
/*FUNCTION**********************************************************************
 *
 * Function Name : PINS_DRV_ReadPins
 * Description   : This function returns the current input values from a port. Only pins
 * configured as input will have meaningful values.
 *
 * Implements    : PINS_DRV_ReadPins_Activity
 *END**************************************************************************/
pins_channel_type_t PINS_DRV_ReadPins(const GPIO_Type * const base)
{
    return PINS_GPIO_ReadPins(base);
}

/* Initialize pins
   *    -    See PinSettings component for more info
 */

 //GPIO初始化
  PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

  /* Output direction for LED0 & LED1 */
  PINS_DRV_SetPinsDirection(PTA, (1 << 15)|(1 << 14));

  /* Set Output value LED0 & LED1 */
  PINS_DRV_SetPins(PTA, (1 << 15)|(1 << 14));  
  PINS_DRV_ClearPins(PTA, (1 << 15)|(1 << 14));

        uint32_t gpio_state=PINS_DRV_ReadPins(PTD);
        if(gpio_state&((1<<8)|(1<<9)|(1<<10)|(1<<11)|(1<<12)))//5个引脚输入口
		{
			PINS_DRV_ClearPins(PTA, 1 << 15);
		}else{
			PINS_DRV_SetPins(PTA, 1 << 15);
		}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值