commit b16c6796f167e253997ec3aea8d925553cffe9e1
parent c3ff610aec4705de6bf1906171591f2572f2e32a
Author: Henry Wilson <m3henry@googlemail.com>
Date: Fri, 16 Feb 2018 11:59:02 +0000
Stereo 8-bit sine wave sound generation
Externally swapped pins D[5,6]
Diffstat:
M | hidModule.h | | | 2 | +- |
M | main.cpp | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
M | utils.h | | | 24 | +++++++++++++++++++++++- |
3 files changed, 83 insertions(+), 12 deletions(-)
diff --git a/hidModule.h b/hidModule.h
@@ -53,7 +53,7 @@ constexpr auto buttonD = bit(6);
constexpr auto ledRED = bit(7);
constexpr auto ledYEL = bit(4);
-constexpr auto ledGRN = bit(6);
+constexpr auto ledGRN = bit(5);
void init();
diff --git a/main.cpp b/main.cpp
@@ -2,6 +2,7 @@
#include "lcd.h"
#include "ctrl.h"
+#include <avr/interrupt.h>
#include <util/delay.h>
namespace timer
@@ -44,10 +45,30 @@ auto& outputCompareB()
{
return memory(0xB4);
}
+auto& interruptMask()
+{
+ return memory(0x70);
+}
+auto const& interruptFlags()
+{
+ return memory(0x37);
+}
}
}
+uint8_t const sinTable[] [[gnu::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
+};
+
int main(void)
{
ctrl::disableJTAG();
@@ -66,18 +87,46 @@ int main(void)
timer::two::outputCompareA() = 0x80;
timer::two::outputCompareB() = 0x80;
timer::two::control() = 0b00'00'0'001'1111'00'11;
-
- uint8_t x = 0;
- uint16_t y = 0;
+
for (;;)
{
- timer::two::outputCompareA() = x++;
-
- timer::two::outputCompareB() = y-- >> 8;
- //io::pin::D() = bit(5);
- _delay_us(10);
- //io::pin::D() = bits(5, 7);
- //_delay_ms(1);
+ constexpr uint8_t midPoint = 127;
+ constexpr auto begin = flashPtr(sinTable);
+ constexpr auto end = begin + 64;
+ auto ptr = flashPtr(sinTable);
+ do
+ {
+ auto val = midPoint + ptr.load_post_inc();
+ timer::two::outputCompareA() = val;
+ timer::two::outputCompareB() = val;
+ _delay_us(10);
+ }
+ while (ptr != end);
+
+ while (ptr != begin)
+ {
+ auto val = midPoint + (--ptr).load();
+ timer::two::outputCompareA() = val;
+ timer::two::outputCompareB() = val;
+ _delay_us(10);
+ }
+
+ do
+ {
+ auto val = midPoint - ptr.load_post_inc();
+ timer::two::outputCompareA() = val;
+ timer::two::outputCompareB() = val;
+ _delay_us(10);
+ }
+ while (ptr != end);
+
+ while (ptr != begin)
+ {
+ auto val = midPoint - (--ptr).load();
+ timer::two::outputCompareA() = val;
+ timer::two::outputCompareB() = val;
+ _delay_us(10);
+ }
}
}
diff --git a/utils.h b/utils.h
@@ -38,7 +38,7 @@ inline constexpr uint8_t mix(const uint8_t a, const uint8_t b, const uint8_t mas
struct flashPtr
{
- inline flashPtr(uint8_t const* const p) : ptr(p) {}
+ inline constexpr flashPtr(uint8_t const* const p) : ptr(p) {}
inline uint8_t load() const
{
uint8_t result;
@@ -63,6 +63,28 @@ struct flashPtr
);
return result;
}
+ inline constexpr flashPtr& operator ++()
+ {
+ ++ptr;
+ return *this;
+ }
+ inline constexpr flashPtr& operator --()
+ {
+ --ptr;
+ return *this;
+ }
+ inline constexpr flashPtr operator +(const uint16_t delta) const
+ {
+ return flashPtr(ptr + delta);
+ }
+ inline constexpr flashPtr operator -(const uint16_t delta) const
+ {
+ return flashPtr(ptr - delta);
+ }
+ inline constexpr bool operator !=(const flashPtr& that) const
+ {
+ return ptr != that.ptr;
+ }
static_assert(sizeof(uint16_t) == sizeof(uint8_t const *), "Flash ptr should be a word");