close #105 pwm remove pin

refactor hwpwm sketch
This commit is contained in:
hathach committed 2019-01-10 16:13:04 +07:00
1 parent 36f707a98a
commit 04c45e13d9
3 files changed
+62 -71

No files matched your search

@@ -13,56 +13,39 @@
*********************************************************************/
/*
* This sketch use all 12 channels of 3 Hardware PWM.
* Also change the resolution to max 15-bit.
* Each PWM Hardware group (4 pin) also run with different frequency
* This sketch use different Hardware PWMs for LED Blue and Red
* running with different frequency
* - PWM0 : clock/1 ~ 16Mhz
* - PWM1 : clock/4 ~ 4Mhz
* - PMW2 : clock/16 ~ 1Mhz
*
* Since LED RED and BLUE are on different Hardware PWM,
* LED BLUE will blink (due to lower freq) while fading while LED RED
* looks more solid. Furthermore LED RED group write value (PWM1) is inverted
* - PWM1 : clock/16 ~ 1Mhz
*
* While LED RED looks solid, LED BLUE will blink while fading
* (due to its lower freq). Furthermore LED RED is inverted
* compared to LED BLUE (PWM2) --> They fade in opposite direction.
*/
#include <Arduino.h>
// Maximum 12 pins can be used for 3 PWM modules ( 4 channel each )
// Note: nRF52832 has 3 PWM modules, nRF52840 has 4 PWM modules
int pins[12] =
{
A0 , A1 , A2 , A3,
A4 , A5 , A6 , LED_RED, /* avoid A7 (VBAT) */
27 , LED_BLUE, PIN_WIRE_SDA, PIN_WIRE_SCL
};
/**************************************************************************/
/*!
@brief The setup function runs once when reset the board
*/
/**************************************************************************/
void setup()
{
// Add 4 pins into a group
// It is better to add Pin before call .begin()
for (int i=0; i<12; i++)
{
HwPWMx[i/4]->addPin( pins[i] );
}
{
// Add LED RED to PWM0
HwPWM0.addPin( LED_RED );
// Enable all 3 PWM modules with 15-bit resolutions(max) but different clock div
// Add LED BLUE to PWM1
HwPWM1.addPin( LED_BLUE );
// Enable PWM modules with 15-bit resolutions(max) but different clock div
HwPWM0.begin();
HwPWM0.setResolution(15);
HwPWM0.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_1); // default : freq = 16Mhz
HwPWM0.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_1); // freq = 16Mhz
HwPWM1.begin();
HwPWM1.setResolution(15);
HwPWM1.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_4); // default : freq = 4Mhz
HwPWM2.begin();
HwPWM2.setResolution(15);
HwPWM2.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_16); // default : freq = 1Mhz
HwPWM1.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_16); // freq = 1Mhz
}
/**************************************************************************/
@@ -73,39 +56,24 @@ void setup()
void loop()
{
const int maxValue = bit(15) - 1;
bool inverted;
// fade in from min to max
// inverted PWM0 (false), PWM1 (true), PWM2 (false)
for (int fadeValue = 0 ; fadeValue <= maxValue; fadeValue += 1024)
{
inverted = false;
for (int i=0; i<12; i++)
{
// Inverted for each PWM group
if (i%4) inverted = !inverted;
HwPWMx[i/4]->writePin( pins[i], fadeValue, inverted);
}
// Write same value but inverted for Led Blue
HwPWM0.writePin(LED_RED, fadeValue, false);
HwPWM1.writePin(LED_BLUE, fadeValue, true);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min
// inverted PWM0 (false), PWM1 (true), PWM2 (false)
for (int fadeValue = maxValue ; fadeValue >= 0; fadeValue -= 1024)
{
inverted = false;
for (int i=0; i<12; i++)
{
// Inverted for each PWM group
if (i%4) inverted = !inverted;
HwPWMx[i/4]->writePin( pins[i], fadeValue, inverted);
}
{
// Write same value but inverted for Led Blue
HwPWM0.writePin(LED_RED, fadeValue, false);
HwPWM1.writePin(LED_BLUE, fadeValue, true);
// wait for 30 milliseconds to see the dimming effect
delay(30);