From 7db28bff0e4b04bb0ae1e200e1654cf4bbbfb86b Mon Sep 17 00:00:00 2001 From: Stephen Olesen Date: Thu, 2 Jul 2015 19:30:53 -0600 Subject: [PATCH] Change DDS to using signed integers to try to keep our DC bias averaged out. --- DDS.cpp | 10 +++++++--- DDS.h | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/DDS.cpp b/DDS.cpp index b4cf79e..6d86f80 100644 --- a/DDS.cpp +++ b/DDS.cpp @@ -140,9 +140,13 @@ uint8_t DDS::getDutyCycle() { uint8_t phAng; #endif phAng = (accumulator >> ACCUMULATOR_BIT_SHIFT); - uint8_t position = pgm_read_byte_near(ddsSineTable + phAng)>>(8-COMPARATOR_BITS); + int8_t position = pgm_read_byte_near(ddsSineTable + phAng)>>(8-COMPARATOR_BITS); // Apply scaling and return - uint16_t scaled = position; + int16_t scaled = position; + // output = ((duty * amplitude) / 256) + 128 + // This keeps amplitudes centered around 50% duty scaled *= amplitude; - return scaled>>8; + scaled >>= 8; + scaled += 128; + return scaled; } diff --git a/DDS.h b/DDS.h index e18f1e9..551c585 100644 --- a/DDS.h +++ b/DDS.h @@ -128,7 +128,28 @@ static const uint8_t ddsSineTable[1024] PROGMEM = { }; #else #define ACCUMULATOR_BIT_SHIFT (ACCUMULATOR_BITS-8) -static const uint8_t ddsSineTable[256] PROGMEM = { +static const int8_t ddsSineTable[256] PROGMEM = { + 0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, + 51, 54, 57, 60, 63, 65, 68, 71, 73, 76, 78, 81, 83, 85, 88, 90, + 92, 94, 96, 98, 100, 102, 104, 106, 107, 109, 111, 112, 113, 115, + 116, 117, 118, 120, 121, 122, 122, 123, 124, 125, 125, 126, 126, + 126, 127, 127, 127, 127, 127, 127, 127, 126, 126, 126, 125, 125, + 124, 123, 122, 122, 121, 120, 118, 117, 116, 115, 113, 112, 111, + 109, 107, 106, 104, 102, 100, 98, 96, 94, 92, 90, 88, 85, 83, 81, + 78, 76, 73, 71, 68, 65, 63, 60, 57, 54, 51, 49, 46, 43, 40, 37, + 34, 31, 28, 25, 22, 19, 16, 12, 9, 6, 3, 0, -3, -6, -9, -12, -16, + -19, -22, -25, -28, -31, -34, -37, -40, -43, -46, -49, -51, -54, + -57, -60, -63, -65, -68, -71, -73, -76, -78, -81, -83, -85, -88, + -90, -92, -94, -96, -98, -100, -102, -104, -106, -107, -109, -111, + -112, -113, -115, -116, -117, -118, -120, -121, -122, -122, -123, + -124, -125, -125, -126, -126, -126, -127, -127, -127, -127, -127, + -127, -127, -126, -126, -126, -125, -125, -124, -123, -122, -122, + -121, -120, -118, -117, -116, -115, -113, -112, -111, -109, -107, + -106, -104, -102, -100, -98, -96, -94, -92, -90, -88, -85, -83, + -81, -78, -76, -73, -71, -68, -65, -63, -60, -57, -54, -51, -49, + -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -12, -9, -6, -3 + }; +/*static const uint8_t ddsSineTable[256] PROGMEM = { 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173, 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215, 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244, @@ -145,7 +166,7 @@ static const uint8_t ddsSineTable[256] PROGMEM = { 10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35, 37,40,42,44,47,49,52,54,57,59,62,65,67,70,73,76, 79,82,85,88,90,93,97,100,103,106,109,112,115,118,121,124 -}; +};*/ #endif /* DDS_TABLE_LARGE */ class DDS {