13
13
*********************************************************************/
14
14
15
15
/*
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
19
18
* - 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
26
23
* compared to LED BLUE (PWM2) --> They fade in opposite direction.
27
24
*/
28
25
29
26
#include < Arduino.h>
30
27
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
-
40
28
/* *************************************************************************/
41
29
/* !
42
30
@brief The setup function runs once when reset the board
43
31
*/
44
32
/* *************************************************************************/
45
33
void 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 );
53
40
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
55
42
HwPWM0.begin ();
56
43
HwPWM0.setResolution (15 );
57
- HwPWM0.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_1); // default : freq = 16Mhz
44
+ HwPWM0.setClockDiv (PWM_PRESCALER_PRESCALER_DIV_1); // freq = 16Mhz
58
45
59
46
HwPWM1.begin ();
60
47
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
66
49
}
67
50
68
51
/* *************************************************************************/
@@ -73,39 +56,24 @@ void setup()
73
56
void loop ()
74
57
{
75
58
const int maxValue = bit (15 ) - 1 ;
76
- bool inverted;
77
59
78
60
// fade in from min to max
79
- // inverted PWM0 (false), PWM1 (true), PWM2 (false)
80
61
for (int fadeValue = 0 ; fadeValue <= maxValue; fadeValue += 1024 )
81
62
{
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
+
92
67
// wait for 30 milliseconds to see the dimming effect
93
68
delay (30 );
94
69
}
95
70
96
71
// fade out from max to min
97
- // inverted PWM0 (false), PWM1 (true), PWM2 (false)
98
72
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 );
109
77
110
78
// wait for 30 milliseconds to see the dimming effect
111
79
delay (30 );
0 commit comments