Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2016, Freescale Semiconductor, Inc.
- * Copyright 2016-2019 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "fsl_debug_console.h"
- #include "pin_mux.h"
- #include "board.h"
- #include "fsl_sctimer.h"
- #include <stdbool.h>
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define SCTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
- #define DEMO_SCTIMER_OUT kSCTIMER_Out_2
- #define MAX_LED_LOGO_PROFIL_VALUES 350
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief delay a while.
- */
- void delay(void);
- unsigned int ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES] =
- {
- 1, // Allow to never turn of the logo
- 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,
- 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,
- 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,
- 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,
- 131, 133, 135, 137, 140, 142, 144, 147, 149, 151, 154, 156, 159, 161, 164, 166, 169, 171, 174, 177, 180, 182, 185, 188,
- 191, 194, 197, 200, 203, 206, 209, 212, 215, 219, 222, 225, 228, 232, 235, 239, 242, 246, 250, 253, 257, 261, 265, 268,
- 272, 276, 280, 284, 289, 293, 297, 301, 306, 310, 315, 319, 324, 328, 333, 338, 343, 347, 352, 357, 362, 368, 373, 378,
- 383, 389, 394, 400, 405, 411, 417, 423, 429, 435, 441, 447, 453, 459, 466, 472, 479, 486, 492, 499, 506, 513, 520, 527,
- 535, 542, 549, 557, 565, 572, 580, 588, 596, 604, 613, 621, 630, 638, 647, 656, 665, 674, 683, 692, 701, 711, 721, 730,
- 740, 750, 761, 771, 781, 792, 802, 813, 824, 835, 847, 858, 870, 881, 893, 905, 917, 930, 942, 955, 968, 981, 994, 1007,
- 1020, 1034, 1048, 1062, 1076, 1091, 1105, 1120, 1135, 1150, 1165, 1181, 1197, 1212, 1229, 1245, 1262, 1278, 1295, 1312,
- 1330, 1348, 1365, 1384, 1402, 1421, 1439, 1458, 1478, 1497, 1517, 1537, 1558, 1578, 1599, 1620, 1642, 1663, 1685, 1708,
- 1730, 1753, 1776, 1799, 1823, 1847, 1872, 1896, 1921, 1947, 1972, 1998, 2024, 2051, 2078, 2105, 2133, 2161, 2190, 2218,
- 2248, 2277, 2307, 2337, 2368, 2399, 2431, 2462, 2495, 2528, 2561, 2594, 2628, 2663, 2698, 2733, 2769, 2805, 2842, 2879,
- 2917, 2955, 2994, 3033, 3073
- };
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- volatile bool sctimerIsrFlag = false;
- volatile uint8_t updatedDutycycle = 10U;
- uint32_t eventNumberOutput;
- unsigned int logo_cycle_time = 0;
- unsigned int ledLogoValue = 0;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- void delay(void)
- {
- volatile uint32_t i = 0U;
- for (i = 0U; i < 80000U; ++i)
- {
- __asm("NOP"); /* delay */
- }
- }
- /* The interrupt callback function is used to update the PWM dutycycle */
- void SCTIMER_LED_HANDLER()
- {
- sctimerIsrFlag = true;
- static uint8_t state1 = 0;
- static uint8_t state2 = 0;
- static uint8_t state3 = 0;
- logo_cycle_time++;
- if (logo_cycle_time < 350)//light increases
- {
- ledLogoValue = ledLogoProfilValues[logo_cycle_time];
- // state1 = !state1;
- // GPIO_PinWrite(BOARD_INITPINS_TEST1_GPIO, BOARD_INITPINS_TEST1_PORT, BOARD_INITPINS_TEST1_PIN, state1);
- }
- else if (logo_cycle_time <= 650)//light stabilized (100% dutycycle => LEDs allumée au max)
- {
- ledLogoValue = ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES-1];
- // state2 = !state2;
- // GPIO_PinWrite(BOARD_INITPINS_TEST2_GPIO, BOARD_INITPINS_TEST2_PORT, BOARD_INITPINS_TEST2_PIN, state2);
- }
- else if (logo_cycle_time <= 1000)//light decreases
- {
- ledLogoValue = ledLogoProfilValues[1000 - logo_cycle_time];
- // state3 = !state3;
- // GPIO_PinWrite(BOARD_INITPINS_TEST3_GPIO, BOARD_INITPINS_TEST3_PORT, BOARD_INITPINS_TEST3_PIN, state3);
- }
- else//light stabilized (0% dutycycle => LEDS éteinte)
- {
- ledLogoValue = 0;
- if (logo_cycle_time >= 1000)//minimum time OFF (increase this value to increase time OFF)
- {
- logo_cycle_time = 0;
- }
- }
- updatedDutycycle = (uint8_t)(ledLogoValue * 100 / ledLogoProfilValues[MAX_LED_LOGO_PROFIL_VALUES-1]);
- if (SCTIMER_GetStatusFlags(SCT0) & (1 << eventNumberOutput))
- {
- /* Clear interrupt flag.*/
- SCTIMER_ClearStatusFlags(SCT0, (1 << eventNumberOutput));
- }
- }
- /*!
- * @brief Main function
- */
- int main(void)
- {
- sctimer_config_t sctimerInfo;
- sctimer_pwm_signal_param_t pwmParam;
- uint32_t sctimerClock;
- /* Board pin, clock, debug console init */
- /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- BOARD_InitPins();
- BOARD_BootClockFROHF96M();
- BOARD_InitDebugConsole();
- sctimerClock = SCTIMER_CLK_FREQ;//96MHz
- /* Print a note to terminal */
- PRINTF("\r\nSCTimer example to output center-aligned PWM signal\r\n");
- PRINTF("\r\nYou will see a change in LED brightness if an LED is connected to the SCTimer output pin");
- PRINTF("\r\nIf no LED is connected to the pin, then probe the signal using an oscilloscope");
- SCTIMER_GetDefaultConfig(&sctimerInfo);
- /* Initialize SCTimer module */
- SCTIMER_Init(SCT0, &sctimerInfo);
- /* Configure PWM params with frequency 100HZ from output */
- pwmParam.output = DEMO_SCTIMER_OUT;
- pwmParam.level = kSCTIMER_HighTrue;
- pwmParam.dutyCyclePercent = updatedDutycycle;
- if (SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_EdgeAlignedPwm, 100U, sctimerClock, &eventNumberOutput) ==
- kStatus_Fail)
- {
- return -1;
- }
- /* Enable interrupt flag for event associated with out 4, we use the interrupt to update dutycycle */
- SCTIMER_EnableInterrupts(SCT0, (1 << eventNumberOutput));
- /* Receive notification when event is triggered */
- SCTIMER_SetCallback(SCT0, SCTIMER_LED_HANDLER, eventNumberOutput);
- /* Enable at the NVIC */
- EnableIRQ(SCT0_IRQn);
- /* Start the 32-bit unify timer */
- SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_U);
- /* Code below updates the PWM dutycycle for Out */
- while (1)
- {
- /* Use interrupt to update the PWM dutycycle on output */
- if (true == sctimerIsrFlag)
- {
- /* Disable interrupt to retain current dutycycle for a few seconds */
- SCTIMER_DisableInterrupts(SCT0, (1 << eventNumberOutput));
- sctimerIsrFlag = false;
- /* Update PWM duty cycle */
- SCTIMER_UpdatePwmDutycycle(SCT0, DEMO_SCTIMER_OUT, updatedDutycycle, eventNumberOutput);
- /* Delay to view the updated PWM dutycycle */
- // delay();
- /* Enable interrupt flag to update PWM dutycycle */
- SCTIMER_EnableInterrupts(SCT0, (1 << eventNumberOutput));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement