Advertisement
Guest User

sctimer_update_dutycycle

a guest
Oct 12th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.05 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3.  * Copyright 2016-2019 NXP
  4.  * All rights reserved.
  5.  *
  6.  * SPDX-License-Identifier: BSD-3-Clause
  7.  */
  8.  
  9. #include "fsl_debug_console.h"
  10. #include "pin_mux.h"
  11. #include "board.h"
  12. #include "fsl_sctimer.h"
  13.  
  14. #include <stdbool.h>
  15. /*******************************************************************************
  16.  * Definitions
  17.  ******************************************************************************/
  18.  
  19. #define SCTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
  20. #define DEMO_SCTIMER_OUT kSCTIMER_Out_2
  21. #define MAX_LED_LOGO_PROFIL_VALUES 350
  22.  
  23. /*******************************************************************************
  24.  * Prototypes
  25.  ******************************************************************************/
  26. /*!
  27.  * @brief delay a while.
  28.  */
  29. void delay(void);
  30.  
  31. unsigned int ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES] =
  32. {
  33.         1,  // Allow to never turn of the logo
  34.         1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 12, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 19,
  35.         20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 31, 32, 33, 34, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  36.         46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 59, 61, 62, 63, 64, 66, 67, 68, 70, 71, 72, 74, 75, 76, 78, 79, 81, 82,
  37.         84, 85, 87, 89, 90, 92, 93, 95, 97, 98, 100, 102, 104, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129,
  38.         131, 133, 135, 137, 140, 142, 144, 147, 149, 151, 154, 156, 159, 161, 164, 166, 169, 171, 174, 177, 180, 182, 185, 188,
  39.         191, 194, 197, 200, 203, 206, 209, 212, 215, 219, 222, 225, 228, 232, 235, 239, 242, 246, 250, 253, 257, 261, 265, 268,
  40.         272, 276, 280, 284, 289, 293, 297, 301, 306, 310, 315, 319, 324, 328, 333, 338, 343, 347, 352, 357, 362, 368, 373, 378,
  41.         383, 389, 394, 400, 405, 411, 417, 423, 429, 435, 441, 447, 453, 459, 466, 472, 479, 486, 492, 499, 506, 513, 520, 527,
  42.         535, 542, 549, 557, 565, 572, 580, 588, 596, 604, 613, 621, 630, 638, 647, 656, 665, 674, 683, 692, 701, 711, 721, 730,
  43.         740, 750, 761, 771, 781, 792, 802, 813, 824, 835, 847, 858, 870, 881, 893, 905, 917, 930, 942, 955, 968, 981, 994, 1007,
  44.         1020, 1034, 1048, 1062, 1076, 1091, 1105, 1120, 1135, 1150, 1165, 1181, 1197, 1212, 1229, 1245, 1262, 1278, 1295, 1312,
  45.         1330, 1348, 1365, 1384, 1402, 1421, 1439, 1458, 1478, 1497, 1517, 1537, 1558, 1578, 1599, 1620, 1642, 1663, 1685, 1708,
  46.         1730, 1753, 1776, 1799, 1823, 1847, 1872, 1896, 1921, 1947, 1972, 1998, 2024, 2051, 2078, 2105, 2133, 2161, 2190, 2218,
  47.         2248, 2277, 2307, 2337, 2368, 2399, 2431, 2462, 2495, 2528, 2561, 2594, 2628, 2663, 2698, 2733, 2769, 2805, 2842, 2879,
  48.         2917, 2955, 2994, 3033, 3073
  49.  
  50. };
  51.  
  52. /*******************************************************************************
  53.  * Variables
  54.  ******************************************************************************/
  55. volatile bool sctimerIsrFlag      = false;
  56. volatile uint8_t updatedDutycycle = 10U;
  57. uint32_t eventNumberOutput;
  58. unsigned int logo_cycle_time = 0;
  59. unsigned int ledLogoValue = 0;
  60. /*******************************************************************************
  61.  * Code
  62.  ******************************************************************************/
  63. void delay(void)
  64. {
  65.     volatile uint32_t i = 0U;
  66.     for (i = 0U; i < 80000U; ++i)
  67.     {
  68.         __asm("NOP"); /* delay */
  69.     }
  70. }
  71.  
  72. /* The interrupt callback function is used to update the PWM dutycycle */
  73. void SCTIMER_LED_HANDLER()
  74. {
  75.     sctimerIsrFlag = true;
  76.     static uint8_t state1 = 0;
  77.     static uint8_t state2 = 0;
  78.     static uint8_t state3 = 0;
  79.  
  80.     logo_cycle_time++;
  81.  
  82.     if (logo_cycle_time < 350)//light increases
  83.     {
  84.         ledLogoValue = ledLogoProfilValues[logo_cycle_time];
  85.         //          state1 = !state1;
  86.         //          GPIO_PinWrite(BOARD_INITPINS_TEST1_GPIO, BOARD_INITPINS_TEST1_PORT, BOARD_INITPINS_TEST1_PIN, state1);
  87.     }
  88.     else if (logo_cycle_time <= 650)//light stabilized (100% dutycycle => LEDs allumée au max)
  89.     {
  90.         ledLogoValue = ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES-1];
  91.         //          state2 = !state2;
  92.         //          GPIO_PinWrite(BOARD_INITPINS_TEST2_GPIO, BOARD_INITPINS_TEST2_PORT, BOARD_INITPINS_TEST2_PIN, state2);
  93.     }
  94.     else if (logo_cycle_time <= 1000)//light decreases
  95.     {
  96.         ledLogoValue = ledLogoProfilValues[1000 - logo_cycle_time];
  97.         //          state3 = !state3;
  98.         //          GPIO_PinWrite(BOARD_INITPINS_TEST3_GPIO, BOARD_INITPINS_TEST3_PORT, BOARD_INITPINS_TEST3_PIN, state3);
  99.     }
  100.     else//light stabilized (0% dutycycle => LEDS éteinte)
  101.     {
  102.         ledLogoValue = 0;
  103.         if (logo_cycle_time >= 1000)//minimum time OFF (increase this value to increase time OFF)
  104.         {
  105.             logo_cycle_time = 0;
  106.         }
  107.     }
  108.     updatedDutycycle = (uint8_t)(ledLogoValue * 100 / ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES-1]);
  109.  
  110.  
  111.     if (SCTIMER_GetStatusFlags(SCT0) & (1 << eventNumberOutput))
  112.     {
  113.         /* Clear interrupt flag.*/
  114.         SCTIMER_ClearStatusFlags(SCT0, (1 << eventNumberOutput));
  115.     }
  116. }
  117.  
  118. /*!
  119.  * @brief Main function
  120.  */
  121. int main(void)
  122. {
  123.     sctimer_config_t sctimerInfo;
  124.     sctimer_pwm_signal_param_t pwmParam;
  125.     uint32_t sctimerClock;
  126.  
  127.     /* Board pin, clock, debug console init */
  128.     /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
  129.     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
  130.  
  131.     BOARD_InitPins();
  132.     BOARD_BootClockFROHF96M();
  133.     BOARD_InitDebugConsole();
  134.  
  135.     sctimerClock = SCTIMER_CLK_FREQ;//96MHz
  136.  
  137.     /* Print a note to terminal */
  138.     PRINTF("\r\nSCTimer example to output center-aligned PWM signal\r\n");
  139.     PRINTF("\r\nYou will see a change in LED brightness if an LED is connected to the SCTimer output pin");
  140.     PRINTF("\r\nIf no LED is connected to the pin, then probe the signal using an oscilloscope");
  141.  
  142.     SCTIMER_GetDefaultConfig(&sctimerInfo);
  143.  
  144.     /* Initialize SCTimer module */
  145.     SCTIMER_Init(SCT0, &sctimerInfo);
  146.  
  147.     /* Configure PWM params with frequency 100HZ from output */
  148.     pwmParam.output           = DEMO_SCTIMER_OUT;
  149.     pwmParam.level            = kSCTIMER_HighTrue;
  150.     pwmParam.dutyCyclePercent = updatedDutycycle;
  151.     if (SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_EdgeAlignedPwm, 100U, sctimerClock, &eventNumberOutput) ==
  152.             kStatus_Fail)
  153.     {
  154.         return -1;
  155.     }
  156.  
  157.     /* Enable interrupt flag for event associated with out 4, we use the interrupt to update dutycycle */
  158.     SCTIMER_EnableInterrupts(SCT0, (1 << eventNumberOutput));
  159.  
  160.     /* Receive notification when event is triggered */
  161.     SCTIMER_SetCallback(SCT0, SCTIMER_LED_HANDLER, eventNumberOutput);
  162.  
  163.     /* Enable at the NVIC */
  164.     EnableIRQ(SCT0_IRQn);
  165.  
  166.     /* Start the 32-bit unify timer */
  167.     SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_U);
  168.  
  169.     /* Code below updates the PWM dutycycle for Out */
  170.     while (1)
  171.     {
  172.         /* Use interrupt to update the PWM dutycycle on output */
  173.         if (true == sctimerIsrFlag)
  174.         {
  175.             /* Disable interrupt to retain current dutycycle for a few seconds */
  176.             SCTIMER_DisableInterrupts(SCT0, (1 << eventNumberOutput));
  177.  
  178.             sctimerIsrFlag = false;
  179.  
  180.             /* Update PWM duty cycle */
  181.             SCTIMER_UpdatePwmDutycycle(SCT0, DEMO_SCTIMER_OUT, updatedDutycycle, eventNumberOutput);
  182.  
  183.             /* Delay to view the updated PWM dutycycle */
  184.             //          delay();
  185.  
  186.             /* Enable interrupt flag to update PWM dutycycle */
  187.             SCTIMER_EnableInterrupts(SCT0, (1 << eventNumberOutput));
  188.         }
  189.     }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement