查看: 4682|回复: 3

[原创] 使用LPC812 SCT的state特性

[复制链接]

该用户从未签到

715

主题

6374

帖子

0

超级版主

Rank: 8Rank: 8

积分
25166
最后登录
2025-8-12
发表于 2020-1-6 09:28:49 | 显示全部楼层 |阅读模式
使用LPC812 SCT的state特性
Ø 问题
       客户使用LPC812中的SCT模块想实现以下功能:
  1. State 0 -> rising edges detected on input -> counter L started -> advance to state 1
  2.         State 1 -> Counter L reaches limit value -> output set high -> advance to state 2
  3.         State 2- > Falling edge on input -> output set low & counter reset -> set state to 0
复制代码
       根据上述描述,可看出要实现的功能其实并不复杂,所以客户通过参考SDK软件库中multi_state_pwm工程较快的创建了如下工程代码:
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. *
  8. * Modified -- P0_13 now as input to counter to start it,
  9. *
  10. * State 0 -> rising edges detected on input -> counter L started -> advance to state 1
  11. * State 1 -> Counter L reaches limit value -> output set high -> advance to state 2
  12. * State 2- > Falling edge on input -> output set low & counter reset -> set state to 0
  13. */

  14. #include "fsl_debug_console.h"
  15. #include "board.h"
  16. #include "fsl_sctimer.h"

  17. #include "pin_mux.h"
  18. /*******************************************************************************
  19. * Definitions
  20. ******************************************************************************/

  21. #define SCTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_Irc)
  22. #define DEMO_FIRST_SCTIMER_OUT kSCTIMER_Out_2
  23. #define DEMO_SECOND_SCTIMER_OUT kSCTIMER_Out_3

  24. /*******************************************************************************
  25. * Prototypes
  26. ******************************************************************************/

  27. /*******************************************************************************
  28. * Code
  29. ******************************************************************************/
  30. /*!
  31. * @brief Main function
  32. */
  33. int main(void)
  34. {
  35.     sctimer_config_t sctimerInfo;
  36.     uint32_t riseEvent, fallEvent; // Modified
  37.     uint32_t stateNumber; // Modified
  38.     uint32_t eventCounterL;
  39.     uint32_t sctimerClock;
  40.     uint32_t matchValueL;

  41.     /* Board pin, clock, debug console init */
  42.     /* Enable clock of uart0. */
  43.     CLOCK_EnableClock(kCLOCK_Uart0);
  44.     /* Ser DIV of uart0. */
  45.     CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);

  46.     BOARD_InitPins();
  47.     BOARD_BootClockIRC12M();
  48.     BOARD_InitDebugConsole();
  49.     /* Enable clock of sct. */
  50.     CLOCK_EnableClock(kCLOCK_Sct);

  51.     sctimerClock = SCTIMER_CLK_FREQ;

  52.     /* Print a note to terminal */
  53. //   PRINTF("\r\nSCTimer example to use it in 16-bit mode\r\n");
  54. //   PRINTF("\r\nThe example shows both 16-bit counters running and toggling an output periodically  ");

  55.     SCTIMER_GetDefaultConfig(&sctimerInfo);
  56. /* Add judgment for change clock source*/
  57. #if defined(SCTIMER_NEED_CHANGE_CLOCK_SOURCE)
  58.     sctimerInfo.clockMode   = DEMO_CLOCK_MODE;
  59.     sctimerInfo.clockSelect = DEMO_CLOCK_SEL;
  60. #endif

  61.     /* Switch to 16-bit mode */
  62.     sctimerInfo.enableCounterUnify = false;

  63.     /* Calculate prescaler and match value for Counter L for 10ms */
  64.     matchValueL            = MSEC_TO_COUNT(100U, sctimerClock);
  65.     sctimerInfo.prescale_l = matchValueL / 65536;
  66.     matchValueL            = matchValueL / (sctimerInfo.prescale_l + 1) - 1;

  67.     // Switch off synchronization for inputs to speed up TODO: Necessary??
  68.   //  sctimerInfo.inputsync = 0x0U;

  69.     /* Initialize SCTimer module */
  70.     SCTIMER_Init(SCT0, &sctimerInfo);

  71.     stateNumber = SCTIMER_GetCurrentState(SCT0);
  72.     // STATE 0 /////////////////////////////////////////////////////////////////////////////////

  73.      // event 0
  74.      /* Schedule an event to look for a rising edge on input 1 in this state */
  75.      if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputRiseEvent, 0u, kSCTIMER_Input_1, kSCTIMER_Counter_L,
  76.                                                   &riseEvent) == kStatus_Fail)
  77.      {
  78.           return -1;
  79.      }

  80.      /* Transition to next state when a rising edge is detected on input 1 */
  81.      SCTIMER_SetupNextStateAction(SCT0, stateNumber + 1, riseEvent);

  82.      // Goto next state
  83.      SCTIMER_IncreaseState(SCT0);

  84.      // STATE 1 /////////////////////////////////////////////////////////////////////////////////

  85.      // event 1
  86.      /* Schedule a single match event for Counter L */
  87.      if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_MatchEventOnly, matchValueL, 0, kSCTIMER_Counter_L,
  88.                                                 &eventCounterL) == kStatus_Fail)
  89.      {
  90.           return -1;
  91.      }

  92.      // Set output in this state
  93.      SCTIMER_SetupOutputSetAction(SCT0, kSCTIMER_Out_0, eventCounterL);

  94.      /* Reset Counter L when Counter L event occurs TODO: Should counter be reset?? */
  95.      //SCTIMER_SetupCounterLimitAction(SCT0, kSCTIMER_Counter_L, eventCounterL);

  96.      /* Transition to next state when a rising edge detected on input 1 */
  97.      SCTIMER_SetupNextStateAction(SCT0, stateNumber + 2, eventCounterL);

  98.      // Goto next state
  99.      SCTIMER_IncreaseState(SCT0);

  100.      // STATE 2 /////////////////////////////////////////////////////////////////////////////////

  101.      // event 2
  102.      /* Schedule an event to look for a falling edge on input 1 in this state */
  103.      if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputFallEvent, 0u, kSCTIMER_Input_1, kSCTIMER_Counter_L,
  104.                                                 &fallEvent) == kStatus_Fail)
  105.      {
  106.           return -1;
  107.      }

  108.      // Clear output when event occurs in this state
  109.      SCTIMER_SetupOutputClearAction(SCT0, kSCTIMER_Out_0, fallEvent);

  110.      // Transition to next state when event occurs in this state, i.e. loop back to initial state
  111.      SCTIMER_SetupNextStateAction(SCT0, 0u, fallEvent);

  112.      ///////////////////////////////////////////////////////////////////////////////////////////


  113.      /* Start the L counter */
  114.      SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_L);

  115.      while (1)
  116.      {

  117.           __asm("NOP"); /* delay */

  118.      }
  119. }
复制代码
       浏览上述代码后,小编并未发现任何错误,但客户在debug时候却发现,进入state 2后,event(Falling edge on input)始终就未被触发,这是什么原因导致的呢??
Ø 问题分析解决
       在对比了LPC MCU的各个参考手册后发现,LPC812最多支持的state数仅为2,而在客户工程代码中,使用了state 0, state 1和state 2,这已然超出了LPC812的性能范围,经客户与小编更进一步的测试后可确认,此为引发问题的原因。
11.jpg



回复

使用道具 举报

  • TA的每日心情
    慵懒
    2023-10-12 08:58
  • 签到天数: 188 天

    连续签到: 1 天

    [LV.7]常住居民III

    1

    主题

    4032

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    18136
    最后登录
    2023-11-2
    发表于 2020-1-6 09:47:47 | 显示全部楼层
    看来要换芯片了~~
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2021-7-5 15:45
  • 签到天数: 664 天

    连续签到: 1 天

    [LV.9]以坛为家II

    42

    主题

    1594

    帖子

    19

    金牌会员

    Rank: 6Rank: 6

    积分
    5624
    最后登录
    2021-12-22
    发表于 2020-1-6 22:50:04 | 显示全部楼层
    state数仅为2   这都不能在称作SCT了
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    715

    主题

    6374

    帖子

    0

    超级版主

    Rank: 8Rank: 8

    积分
    25166
    最后登录
    2025-8-12
     楼主| 发表于 2020-1-7 14:04:55 | 显示全部楼层
    okwh 发表于 2020-1-6 22:50
    state数仅为2   这都不能在称作SCT了

    毕竟是入门级芯片
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-8-15 02:55 , Processed in 0.082700 second(s), 23 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表