Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of HelloWorld_LED61A1 by
LED Control with the X-NUCLEO-LED61A1 Expansion Board
This application provides a simple example of usage of the X-NUCLEO-LED61A1 LED Control Expansion Board.
It shows how to control a LED stripe load connected to the board by means of a sinusoidal wave form injected into the PWM dimming control pin.
main.cpp
- Committer:
- nikapov
- Date:
- 2016-02-04
- Revision:
- 9:23accaba4f4a
- Parent:
- 7:7a608b07fa9f
- Child:
- 13:97083451e538
File content as of revision 9:23accaba4f4a:
/**
******************************************************************************
* @file main.cpp
* @author Davide Aliprandi, STMicroelectronics
* @version V1.0.0
* @date February 4h, 2016
* @brief mbed test application for the STMicroelectronics X-NUCLEO-LED61A1
* LED expansion board.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
/* mbed specific header files. */
#include "mbed.h"
/* Component specific header files. */
#include "led6001_class.h"
/* Definitions ---------------------------------------------------------------*/
/* PI. */
#ifndef M_PI
#define M_PI (3.14159265358979323846f)
#endif
/* Loop period in micro-seconds. */
#define LOOP_PERIOD_us (5E5) /* 0.5 seconds. */
/* Sin period in micro-seconds. */
#define PWM_SIN_PERIOD_us (1E7) /* 10 seconds. */
/* Duration of initialization interval in milli-seconds. */
#define INITIALIZATION_INTERVAL_ms (2E3) /* 2 seconds. */
/* Variables -----------------------------------------------------------------*/
/* Main loop's ticker. */
static Ticker ticker;
/* LED Control Component. */
LED6001 *led;
/* Interrupt flags. */
static volatile bool ticker_irq_triggered;
static volatile bool xfault_irq_triggered;
/* PWM dimming values. */
static float pwm_dimming;
/* Functions -----------------------------------------------------------------*/
/**
* @brief Initilizing the demo.
* @param None.
* @retval None.
*/
void LEDInit(void)
{
/* Printing to the console. */
printf("Initializing LED driver...");
/* Initializing Interrupt flags. */
xfault_irq_triggered = false;
ticker_irq_triggered = false;
/* Initializing PWM dimming to maximum values. */
pwm_dimming = 1.0f;
/* Start-up sequence. */
led->PowerOFF();
wait_ms(INITIALIZATION_INTERVAL_ms);
led->PowerON();
/* Printing to the console. */
printf("Done.\r\n\n");
}
/**
* @brief Handling the LED capabilities.
* @param None.
* @retval None.
*/
void LEDHandler(void)
{
static int tick = 0;
/* Handling the LED dimming when powered ON. */
pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f;
tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us);
/* Printing to the console. */
printf("Sinusoidal PWM Dimming --> %0.2f\r", pwm_dimming);
/*
Writing PWM dimming values to the LED.
Notes:
+ Replace "SetPWMDimming()" with "SetAnalogDimming()" for an analog control.
*/
led->SetPWMDimming(pwm_dimming);
}
/**
* @brief Interrupt Request for the main loop's ticker related interrupt.
* @param None.
* @retval None.
*/
void TickerIRQ(void)
{
ticker_irq_triggered = true;
}
/**
* @brief Interrupt Request for the component's XFAULT interrupt.
* @param None.
* @retval None.
*/
void XFaultIRQ(void)
{
xfault_irq_triggered = true;
led->DisableXFaultIRQ();
}
/**
* @brief Interrupt Handler for the component's XFAULT interrupt.
* @param None.
* @retval None.
*/
void XFaultHandler(void)
{
/* Printing to the console. */
printf("XFAULT Interrupt detected! Re-initializing LED driver...\r\n");
/* Re-initializing the demo. */
LEDInit();
led->EnableXFaultIRQ();
}
/* Main ----------------------------------------------------------------------*/
int main()
{
/*----- Initialization. -----*/
/* Printing to the console. */
printf("LED Control Application Example\r\n\n");
/* Initializing LED Control Component. */
led = new LED6001(D4, A3, D6, D5);
if (led->Init() != COMPONENT_OK)
exit(EXIT_FAILURE);
/* Attaching interrupt request functions. */
led->AttachXFaultIRQ(&XFaultIRQ);
led->EnableXFaultIRQ();
ticker.attach_us(TickerIRQ, LOOP_PERIOD_us);
/* Initializing the demo. */
LEDInit();
/*----- LED Control. -----*/
/* Either performing the component handler, interrupt handlers, or waiting for events. */
while (true)
{
if (ticker_irq_triggered)
{
ticker_irq_triggered = false;
LEDHandler();
} else if (xfault_irq_triggered)
{
xfault_irq_triggered = false;
XFaultHandler();
} else {
/* It is recommended that SEVONPEND in the System Control Register is NOT set. */
__WFE();
}
}
}

X-NUCLEO-LED61A1 LED Driver