Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 4:9b7ea5528a5c
- Parent:
- 3:b7df72682b81
- Child:
- 5:e785b9cd58c9
--- a/main.cpp Wed Apr 01 16:25:32 2020 +0000 +++ b/main.cpp Tue Apr 07 18:56:28 2020 +0000 @@ -8,7 +8,7 @@ Username: el18loc Student ID Number: 201225242 Start Date: 06/02/2020 -Last Edited: 12/03/2020 +Last Edited: 07/04/2020 */ // Includes @@ -19,7 +19,10 @@ // Objects Gamepad pad; N5110 lcd; -Timer t; +Ticker down; +Ticker period; + +DigitalOut rca(PTC4); Serial pc(USBTX, USBRX); @@ -27,29 +30,33 @@ void startup(); void squareWave(); void wavetable(); -void sinspeak(); +//void sinspeak(); +void down_isr(); +void up_isr(); //Arrays for startup -const int notes[8] = {330,0,330,294,330,247,294,392}; -const int duration[8] = {4,8,4,8,8,4,8,4}; const char CART[4] = {'C','A','R','T'}; -const char SYNTH[5] = {'S','Y','N','T','H'}; //Global Variables -volatile unsigned short o[4096]; +volatile double wavtable[4096]; //Wavetable array +// all times drastically slowed to attempt debugging in teraterm +float g_period = 0.002; +float g_unused= g_period*1000000.0f; +int g_period_us=g_unused; +volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value +//isr interrupt flags +volatile int g_upflag=1; +volatile int g_downflag=1; int main() { printf("RUNNING CODE \n"); - startup(); - wavetable(); - pad.leds_on(); - sinspeak(); - pad.leds_off(); - for (int rep=0; rep<10; rep++) { - sinspeak(); - } -// squareWave(); + startup(); //initialises board and displays start screen + wavetable(); //generates wavtable array + //pad.leds_on(); + squareWave(); //generates pulse wave modulated by sin wave + //pad.leds_off(); + //sinspeak(); } @@ -58,19 +65,15 @@ pad.init(); //initiate Gamepad pad.leds_on(); //turn LEDS on to show starting up printf("Initialising Pad\n"); - pad.play_melody(8,notes,duration,108,0); //play startup tune lcd.init(); //intitates screen lcd.clear(); - lcd.setContrast(0.4f); //contrast setting + lcd.setContrast(0.5); //contrast setting lcd.inverseMode(); //puts screen in -ve - //positions for CART SYNTH + //position for CART const int x = 10; const int y = 2; - const int a = 40; - const int b = 4; //Prints CART SYNTH to display lcd.printString(CART,x,y); - lcd.printString(SYNTH,a,b); lcd.refresh(); wait_ms(1800); //timer to allow theme to play out lcd.clear(); @@ -81,27 +84,84 @@ void wavetable() { - int samples= 4096; - float sinfl[samples]; - int sinf[samples]; + double sin_d[4096]; - pad.leds_on(); //shows computating + pad.leds_on(); //shows computating (debug) printf("Generating Wavetable \n"); - wait_ms(500); //shows reaches stage - - float ifl=0.0; //creates fl type incramenter for (int i=0; i<4096; i++) { - sinfl[i] = 65536.0f*sin(2.0f*PI*(ifl/4096.0f)); - sinf[i] = sinfl[i]; - o[i]= sinf[i]+32767; //generates wave table in uint - // printf("o[i]= %u \n", o[i]); // Used for Debug - ifl=i+1.0f; + sin_d[i] = 0.5f*sin(2.0f*PI*(i/4096.0f)); + wavtable[i]= sin_d[i]+0.5; //generates wave table 0<1 in double + //printf("wav[i]= %f \n", wavtable[i]); // Used for Debug } pad.leds_off(); } +void squareWave() +{ + printf("Generating SIN PWM \n"); + + int i=0; //int based iterator + float ifl=0; //float based itterator + float f=50; //frequency of sin wave produced + period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca + down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca + + + printf("g_period_us: %d \n", g_period_us); + printf("sin Frequency: %f \n", f); + + while (1) { //continual loop for pulse production + float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse + + g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs + if (g_uptime_us<1) { + g_uptime_us=g_period_us/100; + } //sets to be value for timebeing to eliminate 0 error + //float f=440*(pad.read_pot1()+1); //removed for simplification + + if (g_upflag==0) { + + ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function + if (ifl>4096) { + ifl= ifl-4096; + } + i=ifl; + + g_upflag=1; //reset flag + //printf("iterate i: i= %d, ifl= %f \n", i, ifl); + } + + //printf("DR: %f \n",dutyratio); + //printf("sleep \n"); + sleep(); //sleeps till next isr + } +} + +void down_isr() //sets rca to 0 +{ + //printf("downISR \n"); + if (g_downflag==0) {//stops error trigger + rca.write(0); + g_downflag=1; + g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0 + //printf("0 \n");//print only in while v. slow freguency + } +} + +void up_isr() //sets rca=1 +{ + //printf("upISR \n"); + rca.write(1); + down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed + g_upflag=0; //sets flag to iterate + g_downflag=0; //sets flag to allow set to 0 + //printf("1 \n"); //only in while at v low frequency +} + + +/* void sinspeak () { float f1=440.0; @@ -116,8 +176,8 @@ while (inc<3000) { int inti = i; v = o[inti]; - //printf("OUTPUT: %u \n", v); - pad.write_u16(v); + printf("OUTPUT: %u \n", v); + //pad.write_u16(v); wait_us(230); //fs= 4k Ts=250us i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts) @@ -125,31 +185,9 @@ i=i-4096.0f; } else { - wait_us(3); + wait_us(3); // used to attempt note stabilisation to match other loop } inc++; } } - -void squareWave() -{ - // defines local variables - float v = 0.0; - float f = 440.0; - float p1 = 0; - // continual loop for square production - while (1) { - pad.write_dac(v); //wite out value (1/0) - //Statements for switch of out - if (v == 0.0f) { - v=0.1; - } else { - v=0.0; - } - p1 = pad.read_pot1(); //read pot 1 value - f = 440 + 440*p1; //convert to freq (A4->A5) - float T=1/f; //calc Period of wave - wait(T); //timer for switch value (T) - } - -} +*/ \ No newline at end of file