1313*********************************************************************/
1414
1515/*
16- * This sketch use all 12 channels of 3 Hardware PWM.
17- * Also change the resolution to max 15-bit.
18- * Each PWM Hardware group (4 pin) also run with different frequency
16+ * This sketch use different Hardware PWMs for LED Blue and Red
17+ * running with different frequency
1918 * - PWM0 : clock/1 ~ 16Mhz
20- * - PWM1 : clock/4 ~ 4Mhz
21- * - PMW2 : clock/16 ~ 1Mhz
22- *
23- * Since LED RED and BLUE are on different Hardware PWM,
24- * LED BLUE will blink (due to lower freq) while fading while LED RED
25- * looks more solid. Furthermore LED RED group write value (PWM1) is inverted
19+ * - PWM1 : clock/16 ~ 1Mhz
20+ *
21+ * While LED RED looks solid, LED BLUE will blink while fading
22+ * (due to its lower freq). Furthermore LED RED is inverted
2623 * compared to LED BLUE (PWM2) --> They fade in opposite direction.
2724 */
2825
2926#include < Arduino.h>
3027
31- // Maximum 12 pins can be used for 3 PWM modules ( 4 channel each )
32- // Note: nRF52832 has 3 PWM modules, nRF52840 has 4 PWM modules
33- int pins[12 ] =
34- {
35- A0 , A1 , A2 , A3,
36- A4 , A5 , A6 , LED_RED, /* avoid A7 (VBAT) */
37- 27 , LED_BLUE, PIN_WIRE_SDA, PIN_WIRE_SCL
38- };
39-
4028/* *************************************************************************/
4129/* !
4230 @brief The setup function runs once when reset the board
4331*/
4432/* *************************************************************************/
4533void setup ()
46- {
47- // Add 4 pins into a group
48- // It is better to add Pin before call .begin()
49- for (int i=0 ; i<12 ; i++)
50- {
51- HwPWMx[i/4 ]->addPin ( pins[i] );
52- }
34+ {
35+ // Add LED RED to PWM0
36+ HwPWM0.addPin ( LED_RED );
37+
38+ // Add LED BLUE to PWM1
39+ HwPWM1.addPin ( LED_BLUE );
5340
54- // Enable all 3 PWM modules with 15-bit resolutions(max) but different clock div
41+ // Enable PWM modules with 15-bit resolutions(max) but different clock div
5542 HwPWM0.begin ();
5643 HwPWM0.setResolution (15 );
57- HwPWM0.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_1); // default : freq = 16Mhz
44+ HwPWM0.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_1); // freq = 16Mhz
5845
5946 HwPWM1.begin ();
6047 HwPWM1.setResolution (15 );
61- HwPWM1.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_4); // default : freq = 4Mhz
62-
63- HwPWM2.begin ();
64- HwPWM2.setResolution (15 );
65- HwPWM2.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_16); // default : freq = 1Mhz
48+ HwPWM1.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_16); // freq = 1Mhz
6649}
6750
6851/* *************************************************************************/
@@ -73,39 +56,24 @@ void setup()
7356void loop ()
7457{
7558 const int maxValue = bit (15 ) - 1 ;
76- bool inverted;
7759
7860 // fade in from min to max
79- // inverted PWM0 (false), PWM1 (true), PWM2 (false)
8061 for (int fadeValue = 0 ; fadeValue <= maxValue; fadeValue += 1024 )
8162 {
82- inverted = false ;
83-
84- for (int i=0 ; i<12 ; i++)
85- {
86- // Inverted for each PWM group
87- if (i%4 ) inverted = !inverted;
88-
89- HwPWMx[i/4 ]->writePin ( pins[i], fadeValue, inverted);
90- }
91-
63+ // Write same value but inverted for Led Blue
64+ HwPWM0.writePin (LED_RED, fadeValue, false );
65+ HwPWM1.writePin (LED_BLUE, fadeValue, true );
66+
9267 // wait for 30 milliseconds to see the dimming effect
9368 delay (30 );
9469 }
9570
9671 // fade out from max to min
97- // inverted PWM0 (false), PWM1 (true), PWM2 (false)
9872 for (int fadeValue = maxValue ; fadeValue >= 0 ; fadeValue -= 1024 )
99- {
100- inverted = false ;
101-
102- for (int i=0 ; i<12 ; i++)
103- {
104- // Inverted for each PWM group
105- if (i%4 ) inverted = !inverted;
106-
107- HwPWMx[i/4 ]->writePin ( pins[i], fadeValue, inverted);
108- }
73+ {
74+ // Write same value but inverted for Led Blue
75+ HwPWM0.writePin (LED_RED, fadeValue, false );
76+ HwPWM1.writePin (LED_BLUE, fadeValue, true );
10977
11078 // wait for 30 milliseconds to see the dimming effect
11179 delay (30 );
0 commit comments