• S32K144的GPIO使用


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

    时钟配置参考:

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

    gpio配置

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

     

     

     

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

    1. /* Initialize and configure clocks
    2. * - see clock manager component for details
    3. */
    4. CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
    5. g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    6. CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    2,初始化GPIO 

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

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

    1. int main(void)
    2. {
    3. /* Write your local variable definition here */
    4. /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
    5. #ifdef PEX_RTOS_INIT
    6. PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
    7. #endif
    8. /*** End of Processor Expert internal initialization. ***/
    9. /* Write your code here */
    10. // WDOG_disable();/* Disable Watchdog in case it is not done in startup code */
    11. /* Initialize and configure clocks
    12. * - see clock manager component for details
    13. */
    14. CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
    15. g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    16. CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
    17. PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr); //初始化IO
    18. /* Output direction for LED0 & LED1 */
    19. // PINS_DRV_SetPinsDirection(GPIO_PORT, (1 << LED1));
    20. /* Set Output value LED0 & LED1 */
    21. //PINS_DRV_SetPins(GPIO_PORT, 1 << LED1);
    22. LPUART_DRV_Init(INST_LPUART0, &lpuart0_State, &lpuart0_InitConfig0); //初始化串口
    23. // PINS_DRV_ClearPins(GPIO_PORT, 1 << LED2);
    24. /* For example: for(;;) { } */
    25. while(1){
    26. // PINS_DRV_SetPins(PTA,1<<15);//将PTA15设置为高
    27. /* Toggle output value LED0 & LED1 */
    28. //PINS_DRV_TogglePins(GPIO_PORT, ((1 << LED1)));
    29. LED0(0);
    30. delay_ms(1000);
    31. LED0(1);
    32. delay_ms(1000);
    33. u0_printf("KEY1 按下\r\n");
    34. //PINS_DRV_ClearPins(PTA,1<<15);//将PTA15设置为低
    35. //PINS_DRV_ClearPins(PTE,1<<6);//将PTE6设置为低
    36. }
    37. /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
    38. /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
    39. #ifdef PEX_RTOS_START
    40. PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
    41. #endif
    42. /*** End of RTOS startup code. ***/
    43. /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
    44. for(;;) {
    45. if(exit_code != 0) {
    46. break;
    47. }
    48. }
    49. return exit_code;
    50. /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
    51. } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

    3,常用函数介绍

    1. 1,初始化函数
    2. /*FUNCTION**********************************************************************
    3. *
    4. * Function Name : PINS_DRV_Init
    5. * Description : This function configures the pins with the options provided
    6. * in the given structure.
    7. *
    8. * Implements : PINS_DRV_Init_Activity
    9. *END**************************************************************************/
    10. status_t PINS_DRV_Init(uint32_t pinCount,
    11. const pin_settings_config_t config[])
    12. {
    13. uint32_t i;
    14. for (i = 0U; i < pinCount; i++)
    15. {
    16. PINS_Init(&config[i]);
    17. }
    18. return STATUS_SUCCESS;
    19. }
    20. 2,将GPIO输出置高
    21. /*FUNCTION**********************************************************************
    22. *
    23. * Function Name : PINS_DRV_SetPins
    24. * Description : This function configures output pins listed in parameter pins (bits that are
    25. * '1') to have a value of 'set' (HIGH). Pins corresponding to '0' will be
    26. * unaffected.
    27. *
    28. * Implements : PINS_DRV_SetPins_Activity
    29. *END**************************************************************************/
    30. void PINS_DRV_SetPins(GPIO_Type * const base,
    31. pins_channel_type_t pins)
    32. {
    33. PINS_GPIO_SetPins(base, pins);
    34. }
    35. 3,将GPIO输出置低
    36. /*FUNCTION**********************************************************************
    37. *
    38. * Function Name : PINS_DRV_ClearPins
    39. * Description : This function configures output pins listed in parameter pins (bits that are
    40. * '1') to have a 'cleared' value (LOW). Pins corresponding to '0' will be
    41. * unaffected.
    42. *
    43. * Implements : PINS_DRV_ClearPins_Activity
    44. *END**************************************************************************/
    45. void PINS_DRV_ClearPins(GPIO_Type * const base,
    46. pins_channel_type_t pins)
    47. {
    48. PINS_GPIO_ClearPins(base, pins);
    49. }
    50. 4,将GPIO状态输出反转
    51. /*FUNCTION**********************************************************************
    52. *
    53. * Function Name : PINS_DRV_TogglePins
    54. * Description : This function toggles output pins listed in parameter pins (bits that are
    55. * '1'). Pins corresponding to '0' will be unaffected.
    56. *
    57. * Implements : PINS_DRV_TogglePins_Activity
    58. *END**************************************************************************/
    59. void PINS_DRV_TogglePins(GPIO_Type * const base,
    60. pins_channel_type_t pins)
    61. {
    62. PINS_GPIO_TogglePins(base, pins);
    63. }
    64. 5,输入时,读取GPIO状态
    65. /*FUNCTION**********************************************************************
    66.  *
    67.  * Function Name : PINS_DRV_ReadPins
    68.  * Description   : This function returns the current input values from a port. Only pins
    69.  * configured as input will have meaningful values.
    70.  *
    71.  * Implements    : PINS_DRV_ReadPins_Activity
    72.  *END**************************************************************************/
    73. pins_channel_type_t PINS_DRV_ReadPins(const GPIO_Type * const base)
    74. {
    75.     return PINS_GPIO_ReadPins(base);
    76. }

    /* 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));

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

  • 相关阅读:
    python17(pygame)
    Jetpack Compose学习(9)——Compose中的列表控件(LazyRow和LazyColumn)
    吃豆人游戏-第12届蓝桥杯Scratch选拔赛真题精选
    计算机毕业设计Java江西婺源旅游文化推广系统(源码+系统+mysql数据库+lw文档)
    非线性参数的精英学习灰狼优化算法-附代码
    百度工程师眼中的云原生可观测性追踪技术
    【Mongoose笔记】HTTP 服务器
    怎么使用PDF编辑器在PDF中插入图片?PDF插入图片的教程
    【剑指offer】——股票的最大利润
    Wireshark下载、Wireshark使用、Wireshark抓包、ARP抓包、ICMP抓包、TCP抓包、HTTP抓包
  • 原文地址:https://blog.csdn.net/m0_38012497/article/details/127930131