cancel
Showing results for 
Search instead for 
Did you mean: 

Resource Library

LED with PWM

Anonymous
Not applicable

LED with PWM

Hello, here is how you can turn on an LED using PWM.


Include Library

#include "pwm.h"
#include "gpiodriver.h"


Define Macros

#define LED_RED    26  /* PWM 0 */
#define LED_BLUE   27  /* PWM 1 */
#define LED_GREEN  28  /* PWM 2 */

#define GPIO_PORT(x)  (x/16)
#define GPIO_PIN(x)   (x%16)

#define ENABLE_LED_RED    PWM0_OUTPUT_ENABLE_P26
#define ENABLE_LED_BLUE   PWM1_OUTPUT_ENABLE_P27
#define ENABLE_LED_GREEN  PWM2_OUTPUT_ENABLE_P28

#define DISABLE_LED_GPIO     GPIO_INPUT_ENABLE | GPIO_PULL_DOWN
#define LED_RED_CHANNEL      (PWM0)
#define LED_BLUE_CHANNEL     (PWM1)
#define LED_GREEN_CHANNEL    (PWM2)

     Note: depending on which chip you are using, the GPIO Pins might change


Enable LED

     // enable
     gpio_configurePin(GPIO_PORT(LED_RED), GPIO_PIN(LED_RED), ENABLE_LED_RED, 0); 
     pwm_start( LED_GREEN_CHANNEL, LHL_CLK, 0x000 , 0);

     // pwm_start( id, clk, toggleCount, initCount);
     // Output Voltage  | Toggle Count (Found these values by trial & error)
     // --------------------------------------------
     // ~ (4/4) V_MAX  |    0x000
     // ~ (3/4) V_MAX  |    0x100
     // ~ (2/4) V_MAX  |    0x210
     // ~ (1/4) V_MAX  |    0x315

     Note: depending on the hardware configuration, the V_MAX may change


Disable LED

     // disable
     gpio_configurePin(GPIO_PORT(LED_RED), GPIO_PIN(LED_RED), DISABLE_LED_GPIO, 0);
     pwm_disableChannel( 1 << LED_RED_CHANNEL );

pastedImage_0.jpg

References

BCM2073XS GPIO Basics

Hardware User Guide (SDK 2.x and TAG3 Board)

How to turn on LED

Different Frequencies for PWM

James

Attachments
0 Likes
Comments
BoonT_56
Employee

thomas.younsi_2228536

LED_green is assigned to p14 but your enablement for green below is P13, and the same for blue. Is this an error?

#define LED_RED    26

#define LED_GREEN  14

#define LED_BLUE   13

#define ENABLE_LED_RED    PWM0_OUTPUT_ENABLE_P26

#define ENABLE_LED_BLUE   PWM2_OUTPUT_ENABLE_P14

#define ENABLE_LED_GREEN  PWM3_OUTPUT_ENABLE_P13

BoonT_56
Employee

P13 (PWM3) and P28 (PWM2) are dual bonded at Pin 39. You have chosen to use P13 for Blue_LED. Someone reported that P28 has to be turn off explicitly, otherwise you will get both PWM3 and PWM2 signals at Pin 39! This could be a reason why you are seeing the wrong colour and/or a mixture. You will need to add the below to turn off P28:

gpio_configurePin((GPIO_PIN_P28) / 16, (GPIO_PIN_P28) % 16, GPIO_INPUT_ENABLE, 0);

Re: How many PWM channels available in BCM20732S  ?

I also noted that in your present configuration, PWM0 and 3 can sink/source up to 16mA whereas PWM2 can sink/source only up to 2mA.

Let me know if it helps.

BoonT_56
Employee

I also noted in pwm_tones app that the configuration of the GPIO (gpio_configurePin) occurred after calling pwm_start. Can you try this as well?

Can you re-post your latest code?

BoonT_56
Employee

Can you state again what is the LED pattern that you want to create?

You have three LEDs, and you want to toggle from one to another using a interrupt?

And what is your test platform? Are you using a tag4 or your own board with the SIP?

I tested out the LED app and another push_button_led_interrupt app separately on a tag4 and there wasn't any issue with those GPIO bonded pins or settings. It is not obvious to me what could be wrong in your code unless I test it out.

MichaelF_56
Moderator
Moderator

thomas.younsi_2228536

Could you create a new discussion as comments on blogs are really not designed to support debug sessions.

Thanks in advance for your understanding.

Mike

btta

ThYo_2228536
New Contributor II

I created Issue Blinking 3 led using PWM on dual bonded pin

I will cleanup the post I did on this thread. Feel free to update some of your comment at the new location

Version history
Revision #:
1 of 1
Last update:
‎Aug 17, 2015 02:29 PM
Updated by:
Anonymous
 
Contributors