-
Notifications
You must be signed in to change notification settings - Fork 7
working with pwm
Dave Glover edited this page Oct 19, 2021
·
2 revisions
- Code example, working with PWM and LEDs
- Code example, working with PWM and LEDs and PWM MikroE BUZZ Click
bool dx_pwmSetDutyCycle(DX_PWM_BINDING *pwmBinding, uint32_t hertz, uint32_t dutyCyclePercentage);
bool dx_pwmStop(DX_PWM_BINDING *pwmBinding);
bool dx_pwmOpen(DX_PWM_BINDING *pwmBinding);
void dx_pwmSetOpen(DX_PWM_BINDING **pwmSet, size_t pwmSetCount);
PWM binding structure.
typedef struct DX_PWM_CONTROLLER
{
int fd;
PWM_ControllerId controllerId;
bool initialized;
char *name;
} DX_PWM_CONTROLLER;
typedef struct DX_PWM_BINDING
{
DX_PWM_CONTROLLER *pwmController;
PWM_ChannelId channelId;
PwmPolarity pwmPolarity;
char *name;
} DX_PWM_BINDING;
The following example declares a PWM controller and three PWM LEDs that use the same PWM Controller.
static DX_PWM_CONTROLLER pwm_led_controller = {.controllerId = PWM_LED_CONTROLLER,
.name = "PWM Click Controller"};
static DX_PWM_BINDING pwm_red_led = {.pwmController = &pwm_led_controller, .channelId = 0, .name = "led"};
static DX_PWM_BINDING pwm_green_led = {.pwmController = &pwm_led_controller, .channelId = 1, .name = "green led"};
static DX_PWM_BINDING pwm_blue_led = {.pwmController = &pwm_led_controller, .channelId = 2, .name = "blue led"};
static DX_PWM_BINDING pwm_user_led = {.pwmController = &pwm_led_controller, .channelId = 3, .name = "user led on Seeed Mini"};
For convenance you can declare a set of PWM peripherals and use the set based functions to open. Add each PWM peripheral by reference to the set (array) of PWM peripherals.
static DX_PWM_BINDING *pwm_bindings[] = {&pwm_red_led, &pwm_green_led, &pwm_blue_led, &pwm_user_led};
In main.c, open the set of PWMs using the dx_pwmSetOpen function.
static void InitPeripheralsAndHandlers(void)
{
dx_pwmSetOpen(pwm_bindings, NELEMS(pwm_bindings));
dx_timerSetStart(timerSet, NELEMS(timerSet));
...
}
/// <summary>
/// Simple PWM example - 1kHz (1000 Hz), and alter duty cycle
/// </summary>
static void update_led_pwm_handler(EventLoopTimer *eventLoopTimer)
{
if (ConsumeEventLoopTimerEvent(eventLoopTimer) != 0) {
dx_terminate(DX_ExitCode_ConsumeEventLoopTimeEvent);
return;
}
#ifdef OEM_SEEED_STUDIO_MINI
// NO RGB LED so set PWM on the user LED
static DX_PWM_BINDING *pwmLed = &pwm_user_led;
#else
static DX_PWM_BINDING *pwmLed = &pwm_red_led;
#endif
static int duty_cycle = 0;
switch (duty_cycle % 4) {
case 0:
dx_pwmSetDutyCycle(pwmLed, 1000, 100);
break;
case 1:
dx_pwmSetDutyCycle(pwmLed, 1000, 80);
break;
case 2:
dx_pwmSetDutyCycle(pwmLed, 1000, 40);
break;
case 3:
dx_pwmSetDutyCycle(pwmLed, 1000, 0);
break;
default:
break;
}
duty_cycle++;
}
AzureSphereDevX Examples Wiki
- Home
- Build Tools
- Adding the DevX library
- Azure IoT Hub Messaging
- Azure IoT Hub Device Twins
- Azure IoT Hub Direct Methods
- Avnet IoT Connect messaging
- Handling multithreaded async events
- Working with GPIO
- Working with UARTS
- Working with PWM
- Working with Event Timers
- Intercore Messaging
- Application termination
- Deferring updates
- Utility functions
- Tools and scripts
- Hardware Definitions