Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit a373542

Browse files
authored
v1.2.0 to optimize speed, add PWM_SpeedTest, etc
### Releases v1.2.0 1. Optimize speed with new `setPWM_DCPercentageInt_manual` function to improve speed almost 500 times compared to `setPWM` 2. Add example [PWM_manual](https://github.com/khoih-prog/SAMD_PWM/tree/main/examples/PWM_manual) to demo how to correctly use PWM to generate waveform. Check [About DCValue in setPWM_manual #2](khoih-prog/AVR_PWM#2) 3. Add example [PWM_SpeedTest](https://github.com/khoih-prog/SAMD_PWM/tree/main/examples/PWM_SpeedTest) to demo the better speed of new `setPWM_DCPercentageInt_manual` function 4. Breaking change: Modify `setPWM_manual` function to take `16-bit` dutycycle instead from merely `0-100` for better accuracy 5. Modify example [PWM_Waveform](https://github.com/khoih-prog/SAMD_PWM/tree/main/examples/PWM_Waveform) to adapt to breaking change of `setPWM_manual` function 6. Improve `README.md` so that links can be used in other sites, such as `PIO` 7. Bump up to v1.2.0 to sync with other Fast PWM libraries
1 parent e91722c commit a373542

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/PWM_Generic_Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
88
Licensed under MIT license
99
10-
Version: 1.0.1
10+
Version: 1.2.0
1111
1212
Version Modified By Date Comments
1313
------- ----------- ---------- -----------
1414
1.0.0 K Hoang 01/11/2022 Initial coding for SAMD21/SAMD51 boards
1515
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
16+
1.2.0 K Hoang 28/01/2023 Add `PWM_SpeedTest` and `PWM_manual` examples and faster functions
1617
*****************************************************************************************************************************/
1718

1819
#pragma once

src/SAMD_PWM.h

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_PWM
88
Licensed under MIT license
99
10-
Version: 1.0.1
10+
Version: 1.2.0
1111
1212
Version Modified By Date Comments
1313
------- ----------- ---------- -----------
1414
1.0.0 K Hoang 01/11/2022 Initial coding for SAMD21/SAMD51 boards
1515
1.0.1 K Hoang 22/01/2023 Add `PWM_StepperControl` example
16+
1.2.0 K Hoang 28/01/2023 Add `PWM_SpeedTest` and `PWM_manual` examples and faster functions
1617
*****************************************************************************************************************************/
1718

1819
#pragma once
@@ -137,13 +138,13 @@
137138
#include "Arduino.h"
138139

139140
#ifndef SAMD_PWM_VERSION
140-
#define SAMD_PWM_VERSION "SAMD_PWM v1.0.1"
141+
#define SAMD_PWM_VERSION "SAMD_PWM v1.2.0"
141142

142143
#define SAMD_PWM_VERSION_MAJOR 1
143-
#define SAMD_PWM_VERSION_MINOR 0
144-
#define SAMD_PWM_VERSION_PATCH 1
144+
#define SAMD_PWM_VERSION_MINOR 2
145+
#define SAMD_PWM_VERSION_PATCH 0
145146

146-
#define SAMD_PWM_VERSION_INT 1000001
147+
#define SAMD_PWM_VERSION_INT 1002000
147148
#endif
148149

149150
#include "PWM_Generic_Debug.h"
@@ -241,6 +242,8 @@
241242

242243
////////////////////////////////////////
243244

245+
#define MAX_16BIT 65535UL
246+
244247
#define MAX_COUNT_16BIT 65536UL
245248

246249
////////////////////////////////////////
@@ -832,7 +835,8 @@ class SAMD_PWM
832835

833836
bool setPWM(const uint8_t& pin, const float& frequency, const float& dutycycle)
834837
{
835-
_dutycycle = round(map(dutycycle, 0, 100.0f, 0, MAX_COUNT_16BIT));
838+
//_dutycycle = round(map(dutycycle, 0, 100.0f, 0, MAX_COUNT_16BIT));
839+
_dutycycle = round(map(dutycycle, 0, 100.0f, 0, MAX_16BIT));
836840

837841
PWM_LOGDEBUG3(F("setPWM: _dutycycle ="), _dutycycle, F(", frequency ="), frequency);
838842

@@ -853,11 +857,15 @@ class SAMD_PWM
853857
///////////////////////////////////////////
854858

855859
// Must have same frequency
856-
bool setPWM_manual(const uint8_t& pin, const uint16_t& DCValue)
860+
// From v1.0.1-, DCValue = 0-100
861+
// From v1.2.0+, DCValue = 0-65535
862+
bool setPWM_manual(const uint8_t& pin, const uint16_t& dutyCycle)
857863
{
858864
// Not the same pin or overvalue
859-
if ( (_pin != pin) || (DCValue >= (1 << _resolution) ) )
865+
if ( (_pin != pin) || (dutyCycle >= (1 << _resolution) ) )
860866
return false;
867+
868+
uint16_t DCValue = ( (uint32_t) dutyCycle * 100 ) / MAX_16BIT;
861869

862870
#if defined(__SAMD51__)
863871
// Check which timer to use
@@ -906,19 +914,47 @@ class SAMD_PWM
906914

907915
PWM_LOGDEBUG3(F("setPWM_manual TCC: DCValue ="), DCValue, F(", _frequency ="), _frequency);
908916
PWM_LOGDEBUG3(F("setPWM_manual TCC: New DCValue ="), DCValue * _compareValue / 100, F(", _compareValue ="),
909-
_compareValue);
917+
_compareValue);
910918
}
911919

912-
_dutycycle = DCValue * _compareValue / 100;
920+
_dutycycle = dutyCycle;
913921

914922
return true;
915923
}
924+
925+
///////////////////////////////////////////
926+
927+
// Faster than setPWM_DCPercentage_manual by not using float
928+
// DCPercentage from 0-65535 for 0.0f - 100.0f
929+
bool setPWM_DCPercentageInt_manual(const uint8_t& pin, const uint16_t& DCPercentage)
930+
{
931+
_dutycycle = DCPercentage;
932+
933+
// Convert to DCValue based on resolution = MAX_16BIT
934+
PWM_LOGDEBUG3(F("setPWM_DCPercentage_manual: DCPercentage ="), DCPercentage, F(", dc ="), ( DCPercentage * MAX_16BIT ) / 100.0f);
935+
936+
return setPWM_manual(pin, _dutycycle);
937+
}
938+
939+
///////////////////////////////////////////
940+
941+
// DCPercentage from 0.0f - 100.0f
942+
bool setPWM_DCPercentage_manual(const uint8_t& pin, const float& DCPercentage)
943+
{
944+
// Convert to 0-65535
945+
_dutycycle = ( DCPercentage * MAX_16BIT ) / 100.0f;
946+
947+
// Convert to DCValue based on resolution = MAX_16BIT
948+
PWM_LOGDEBUG3(F("setPWM_DCPercentage_manual: DCPercentage ="), DCPercentage, F(", dc ="), ( DCPercentage * MAX_16BIT ) / 100.0f);
949+
950+
return setPWM_manual(pin, _dutycycle);
951+
}
916952

917953
///////////////////////////////////////////
918954

919955
inline float getActualDutyCycle()
920956
{
921-
return ( ( (float) _dutycycle + 1 ) * 100 / (1 << _resolution) );
957+
return ( ( (float) _dutycycle ) * 100 / (1 << _resolution) );
922958
}
923959

924960
///////////////////////////////////////////

0 commit comments

Comments
 (0)