summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-05-12 12:45:16 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-05-12 12:45:16 +0100
commit5a4a5e01f75e31b1a5210f7a6ce2642c62a685a4 (patch)
treebb85de17444159c84f8ee2e8190f933413a5bdd2
parentaa30a51f3e2d9120aa819c1bc8162793662ccf32 (diff)
downloadWaveGenerator-5a4a5e01f75e31b1a5210f7a6ce2642c62a685a4.tar.gz
WaveGenerator-5a4a5e01f75e31b1a5210f7a6ce2642c62a685a4.zip
Finished project
-rw-r--r--function_tables.h2
-rwxr-xr-xlook_up_table_genbin0 -> 9016 bytes
-rw-r--r--look_up_table_gen.cpp35
-rw-r--r--main.cpp245
4 files changed, 195 insertions, 87 deletions
diff --git a/function_tables.h b/function_tables.h
new file mode 100644
index 0000000..87b37a7
--- /dev/null
+++ b/function_tables.h
@@ -0,0 +1,2 @@
+int sine_table[16]={32768,45307,55937,63040,65535,63040,55937,45307,32767,20227,9597,2494,0,2494,9597,20227,};
+int triangle_table[16]={0,8191,16383,24575,32767,40959,49151,57343,65535,57343,49151,40959,32767,24575,16383,8191,};
diff --git a/look_up_table_gen b/look_up_table_gen
new file mode 100755
index 0000000..6d206b1
--- /dev/null
+++ b/look_up_table_gen
Binary files differ
diff --git a/look_up_table_gen.cpp b/look_up_table_gen.cpp
new file mode 100644
index 0000000..6f3867f
--- /dev/null
+++ b/look_up_table_gen.cpp
@@ -0,0 +1,35 @@
+#include <cmath>
+#include <iostream>
+
+int main()
+{
+ const int SAMPLES=16;
+
+ std::cout<<"int sine_table["<<SAMPLES<<"]={";
+
+ for(int i=0; i<SAMPLES; ++i)
+ {
+ if(i==0)
+ std::cout<<(int)(0.5*pow(2, 16))<<",";
+ else
+ std::cout<<(int)(((1+std::sin((long double)i*(long double)2*M_PIl/(long double)SAMPLES))/2)*(pow(2, 16)-1))<<",";
+ }
+
+ std::cout<<"};\nint triangle_table["<<SAMPLES<<"]={";
+
+ for(int i=0; i<SAMPLES; ++i)
+ {
+ if(i<SAMPLES/2)
+ {
+ std::cout<<(int)((long double)i/(long double)SAMPLES*2*(pow(2, 16)-1))<<",";
+ }
+ else
+ {
+ std::cout<<(int)((long double)(SAMPLES-i)/(long double)SAMPLES*2*(pow(2, 16)-1))<<",";
+ }
+ }
+
+ std::cout<<"};\n";
+
+ return 0;
+}
diff --git a/main.cpp b/main.cpp
index 8132692..e8702ce 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,17 +1,16 @@
#include "mbed.h"
#include "Adafruit_SSD1306.h"
+#include "function_tables.h"
//Switch input definition
#define PIN21 p21
#define PIN22 p22
#define PIN23 p23
#define PIN24 p24
-//#define PIN18 p25
#define PIN18 p18
//Sampling period for the switch oscillator (us)
#define SW_PERIOD 20000
-#define LOW_PERIOD 100
//Display interface pin definitions
#define D_MOSI_PIN p5
@@ -38,19 +37,18 @@ void sedge3();
void sedge4();
void tout();
void checkButtonInput();
-void select();
-void m_abort();
+void setTimer();
void increment();
-void gen_square(uint16_t avg1_threshold);
-void gen_wave();
-void gen_sine();
+void decrement();
+void cycleAmount();
+void genWave();
+void genSine();
+void genTriangle();
//Output for the alive LED
DigitalOut alive(LED1);
DigitalOut ind(LED4);
-DigitalOut square(p17);
-AnalogOut aout(p18);
-PwmOut pwm_pin(p25);
+AnalogOut analog_pin(PIN18);
//External interrupt input from the switch oscillator
InterruptIn p_21(PIN21);
@@ -60,22 +58,30 @@ InterruptIn p_24(PIN24);
//Switch sampling timer
Ticker swtimer;
+// generates the wave
Ticker tck;
-//Registers for the switch counter, switch counter latch register and update flag
-volatile uint16_t scounter1=0;
-volatile uint16_t scount1=0;
-volatile uint16_t scounter2=0;
-volatile uint16_t scount2=0;
-volatile uint16_t scounter3=0;
-volatile uint16_t scount3=0;
-volatile uint16_t scounter4=0;
-volatile uint16_t scount4=0;
-volatile uint16_t update=0;
-volatile uint16_t value=0;
-volatile uint16_t counter=0;
+// different switche counters as globals
+uint16_t scounter1=0;
+uint16_t scount1=0;
+uint16_t scounter2=0;
+uint16_t scount2=0;
+uint16_t scounter3=0;
+uint16_t scount3=0;
+uint16_t scounter4=0;
+uint16_t scount4=0;
+uint16_t update=0;
-volatile uint32_t frequency=0;
+uint32_t frequency=0;
+uint32_t generated_frequency=0;
+uint32_t period=0;
+
+uint16_t amount=1;
+uint16_t wave_count=0;
+uint8_t alternating=0;
+
+extern int sine_table[16];
+extern int triangle_table[16];
//Initialise SPI instance for communication with the display
SPIPreInit gSpi(D_MOSI_PIN,NC,D_CLK_PIN); //MOSI,MISO,CLK
@@ -83,13 +89,6 @@ SPIPreInit gSpi(D_MOSI_PIN,NC,D_CLK_PIN); //MOSI,MISO,CLK
//Initialise display driver instance
Adafruit_SSD1306_Spi gOled1(gSpi,D_DC_PIN,D_RST_PIN,D_CS_PIN,64,128); //SPI,DC,RST,CS,Height,Width
-
-const double pi = 3.141592653589793238462;
-const double amplitude = 0.5f;
-const double offset = 65535/2;
-double rads = 0.0;
-uint16_t sample = 0;
-
int main() {
//Initialisation
gOled1.setRotation(2); //Set display rotation
@@ -102,8 +101,6 @@ int main() {
//Attach switch sampling timer ISR to the timer instance with the required period
swtimer.attach_us(&tout, SW_PERIOD);
- tck.attach_us(&gen_wave, LOW_PERIOD);
- pwm_pin.period(0.000001);
gOled1.clearDisplay();
@@ -119,7 +116,66 @@ int main() {
checkButtonInput();
gOled1.printf("freq = %10u Hz\n",frequency);
- gOled1.printf("T = %10u us",(unsigned)(1/(float)frequency*1000000));
+ switch(amount)
+ {
+ case 1:
+ gOled1.printf(" ^\n");
+ break;
+ case 10:
+ gOled1.printf(" ^ \n");
+ break;
+ case 100:
+ gOled1.printf(" ^ \n");
+ break;
+ case 1000:
+ gOled1.printf(" ^ \n");
+ break;
+ default:;
+ }
+
+ if(frequency!=0)
+ {
+ period=(uint32_t)1/(float)frequency*1000000;
+ }
+
+ switch(alternating)
+ {
+ case 0:
+ gOled1.printf("wave: square \n");
+ break;
+ case 1:
+ gOled1.printf("wave: sine \n");
+ break;
+ case 2:
+ gOled1.printf("wave: triangle\n");
+ break;
+ default:;
+ }
+ gOled1.printf("gen = %10u Hz\n", generated_frequency);
+
+ gOled1.drawLine(0, 50, 128, 50, WHITE);
+ gOled1.drawLine(0, 63, 128, 63, WHITE);
+
+ gOled1.drawLine(0, 50, 0, 64, WHITE);
+ gOled1.drawLine(32, 50, 32, 64, WHITE);
+ gOled1.drawLine(64, 50, 64, 64, WHITE);
+ gOled1.drawLine(96, 50, 96, 64, WHITE);
+ gOled1.drawLine(127, 50, 127, 64, WHITE);
+
+ gOled1.setTextCursor(10, 40);
+ gOled1.printf("Button Operations");
+
+ gOled1.setTextCursor(8, 54);
+ gOled1.printf("set");
+
+ gOled1.setTextCursor(40, 54);
+ gOled1.printf("sel");
+
+ gOled1.setTextCursor(78, 54);
+ gOled1.printf("-");
+
+ gOled1.setTextCursor(110, 54);
+ gOled1.printf("+");
//Copy the display buffer to the display
gOled1.display();
@@ -127,19 +183,19 @@ int main() {
//Toggle the alive LED
alive = !alive;
}
- gen_sine();
- //wait(0.15);
}
}
//Interrupt Service Routine for rising edge on the switch oscillator input
-void sedge1() {
+void sedge1()
+{
//Increment the edge counter
scounter1++;
}
-void sedge2() {
+void sedge2()
+{
//Increment the edge counter
scounter2++;
}
@@ -155,13 +211,14 @@ void sedge4() {
}
//Interrupt Service Routine for the switch sampling timer
-void tout() {
- //Read the edge counter into the output register
+void tout()
+{
+ //Read the edge counters into the output register
scount1 = scounter1;
scount2 = scounter2;
scount3 = scounter3;
scount4 = scounter4;
- //Reset the edge counter
+ //Reset the edge counters
scounter1 = 0;
scounter2 = 0;
scounter3 = 0;
@@ -173,88 +230,102 @@ void tout() {
void checkButtonInput()
{
ind=0;
-
- static volatile const uint16_t AVG1=scount1;
- static volatile const uint16_t AVG2=scount2;
- static volatile const uint16_t AVG3=scount3;
- static volatile const uint16_t AVG4=scount4;
+
+ // defines the rest state of each switch
+ static const uint16_t AVG1=scount1;
+ static const uint16_t AVG2=scount2;
+ static const uint16_t AVG3=scount3;
+ static const uint16_t AVG4=scount4;
float threshold=0.97;
- if(scount1<AVG1*threshold)// && scount2>=AVG2*threshold && scount3>=AVG3*threshold && scount4>=AVG4*threshold)
+ if(scount1<AVG1*threshold && scount2>=AVG2*threshold && scount3>=AVG3*threshold && scount4>=AVG4*threshold)
{
- //m_abort();
+ if(alternating==0)
+ tck.attach_us(&genWave, period/2);
+ else if(alternating==1)
+ tck.attach_us(&genSine, period/16);
+ else if(alternating==2)
+ tck.attach_us(&genTriangle, period/16);
+
+ if(generated_frequency!=frequency)
+ generated_frequency=frequency;
+ else
+ {
+ if(scount1<AVG1*(threshold-0.1))
+ {
+ alternating++;
+ if(alternating>2)
+ alternating=0;
+ wait_ms(250);
+ }
+ }
ind=1;
}
- if(scount2<AVG2*threshold)// && scount1>=AVG1*threshold && scount3>=AVG3*threshold && scount4>=AVG4*threshold)
+ if(scount2<AVG2*threshold && scount1>=AVG1*threshold && scount3>=AVG3*threshold && scount4>=AVG4*threshold)
{
- //gen_square(AVG1*threshold);
+ cycleAmount();
ind=1;
+ wait_ms(150);
}
- if(scount3<AVG3*threshold)// && scount2>=AVG2*threshold && scount1>=AVG1*threshold && scount4>=AVG4*threshold)
+ if(scount3<AVG3*threshold && scount2>=AVG2*threshold && scount1>=AVG1*threshold && scount4>=AVG4*threshold)
{
- select();
+ decrement();
ind=1;
}
- if(scount4<AVG4*threshold)// && scount2>=AVG2*threshold && scount3>=AVG3*threshold && scount1>=AVG1*threshold)
+ if(scount4<AVG4*threshold && scount2>=AVG2*threshold && scount3>=AVG3*threshold && scount1>=AVG1*threshold)
{
increment();
ind=1;
}
}
-void select()
+void increment()
{
- frequency*=10;
+ if(frequency<(unsigned)-1)
+ {
+ frequency+=amount;
+ }
}
-void m_abort()
+void decrement()
{
- frequency=0;
+ if(frequency>0)
+ {
+ frequency-=amount;
+ }
}
-void increment()
+void cycleAmount()
{
- if(frequency%10==9)
- frequency=frequency-9;
+ if(amount%1000==0)
+ {
+ amount=1;
+ }
else
- ++frequency;
+ {
+ amount*=10;
+ }
}
-
-void gen_square(uint16_t avg1_threshold)
+void genWave()
{
- for(;; wait_us((unsigned)(1/(float)frequency*1000000)/2))
- {
- //square=!square;
-
- if(scount1<avg1_threshold)
- {
- m_abort();
- break;
- }
- }
+ analog_pin=!analog_pin;
}
-void gen_wave()
+void genSine()
{
- if(counter*LOW_PERIOD>=(unsigned)1/(float)frequency*500000)
- {
- value=!value;
- pwm_pin.write(value);
- counter=0;
- }
- counter++;
+ analog_pin.write_u16(sine_table[wave_count++]);
+ if(wave_count>15)
+ wave_count=0;
}
-void gen_sine()
+void genTriangle()
{
- for (int i = 0; i < 360; i++) {
- rads = (pi * i) / 180.f;
- sample = (uint16_t)(amplitude * (offset * (cos(rads*2 + pi))) + offset);
- aout.write_u16(sample);
- }
-} \ No newline at end of file
+ analog_pin.write_u16(triangle_table[wave_count++]);
+ if(wave_count>15)
+ wave_count=0;
+}