Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue May 19 18:38:33 2020 +0000
Revision:
15:1c67f064278e
Parent:
14:9cfe0041cc4e
Child:
16:14c231624f3e
Fixed Tri wavetable issue to get tri wave out. Added more slow debug elements.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukeocarwright 2:07cef563acdf 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
lukeocarwright 1:766a293c9b07 7 Name: Luke Cartwright
lukeocarwright 1:766a293c9b07 8 Username: el18loc
lukeocarwright 1:766a293c9b07 9 Student ID Number: 201225242
lukeocarwright 2:07cef563acdf 10 Start Date: 06/02/2020
lukeocarwright 14:9cfe0041cc4e 11 Last Edited: 19/05/2020
eencae 0:b7f1f47bb26a 12 */
eencae 0:b7f1f47bb26a 13
lukeocarwright 5:e785b9cd58c9 14 /* TO DO:
lukeocarwright 14:9cfe0041cc4e 15 Generate sound
lukeocarwright 8:f305ea78b2b1 16 add Menu functionality from front.cpp
lukeocarwright 8:f305ea78b2b1 17 front.h warning sort
lukeocarwright 7:33cb5f2db1ee 18 Settings tab
lukeocarwright 14:9cfe0041cc4e 19 ADSR (functionaluty)
lukeocarwright 8:f305ea78b2b1 20 MIDI
lukeocarwright 8:f305ea78b2b1 21 LEDS INSTALL
lukeocarwright 5:e785b9cd58c9 22 */
lukeocarwright 2:07cef563acdf 23 // Includes
eencae 0:b7f1f47bb26a 24 #include "mbed.h"
eencae 0:b7f1f47bb26a 25 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 26 #include "N5110.h"
lukeocarwright 8:f305ea78b2b1 27 #include "Menu.h"
lukeocarwright 7:33cb5f2db1ee 28 #include "startup.h"
lukeocarwright 7:33cb5f2db1ee 29 #include "Front.h"
eencae 0:b7f1f47bb26a 30
lukeocarwright 9:f6ba53e355a0 31 #ifdef DEBUG
lukeocarwright 9:f6ba53e355a0 32 # include "Debug.h"
lukeocarwright 9:f6ba53e355a0 33 #endif
lukeocarwright 9:f6ba53e355a0 34
lukeocarwright 2:07cef563acdf 35 // Objects
eencae 0:b7f1f47bb26a 36 Gamepad pad;
eencae 0:b7f1f47bb26a 37 N5110 lcd;
lukeocarwright 8:f305ea78b2b1 38 Menu menu;
lukeocarwright 7:33cb5f2db1ee 39 startup start;
lukeocarwright 14:9cfe0041cc4e 40 Ticker tick;
lukeocarwright 4:9b7ea5528a5c 41
lukeocarwright 14:9cfe0041cc4e 42 //DigitalOut rca(PTC4);
lukeocarwright 14:9cfe0041cc4e 43 //Serial pc(USBTX, USBRX);
lukeocarwright 2:07cef563acdf 44
lukeocarwright 2:07cef563acdf 45 //Functions
lukeocarwright 5:e785b9cd58c9 46 //void squareWave();
lukeocarwright 4:9b7ea5528a5c 47 //void sinspeak();
lukeocarwright 14:9cfe0041cc4e 48 void noise_isr();
lukeocarwright 2:07cef563acdf 49
lukeocarwright 2:07cef563acdf 50
lukeocarwright 2:07cef563acdf 51 //Global Variables
lukeocarwright 14:9cfe0041cc4e 52 volatile extern int g_isr_flag; //may need extern
lukeocarwright 10:258a1eca02cc 53
lukeocarwright 15:1c67f064278e 54
eencae 0:b7f1f47bb26a 55 int main()
eencae 0:b7f1f47bb26a 56 {
lukeocarwright 2:07cef563acdf 57 printf("RUNNING CODE \n");
lukeocarwright 7:33cb5f2db1ee 58 start.initialise(lcd,pad); //initialises board and displays start screen
lukeocarwright 14:9cfe0041cc4e 59
lukeocarwright 14:9cfe0041cc4e 60 #ifdef DEBUG
lukeocarwright 14:9cfe0041cc4e 61 printf("DEBUG COMPILE RUNNING\n");
lukeocarwright 14:9cfe0041cc4e 62 run_LUTs_debug();
lukeocarwright 14:9cfe0041cc4e 63 #endif
lukeocarwright 14:9cfe0041cc4e 64
lukeocarwright 14:9cfe0041cc4e 65 #ifdef SLOW_TIME //ATTACH TICKERS FOR SOUND
lukeocarwright 15:1c67f064278e 66 printf("Running in SLOW_TIME\n");
lukeocarwright 15:1c67f064278e 67 tick.attach(&noise_isr, 1); //extended for debug (1Hz)
lukeocarwright 15:1c67f064278e 68 #endif
lukeocarwright 15:1c67f064278e 69 #ifndef SLOW_TIME
lukeocarwright 15:1c67f064278e 70 tick.attach_us(&noise_isr, 63); //normal mode SR=20kHz
lukeocarwright 14:9cfe0041cc4e 71 #endif
lukeocarwright 14:9cfe0041cc4e 72
lukeocarwright 14:9cfe0041cc4e 73 menu.mainmenu(lcd,pad);
lukeocarwright 9:f6ba53e355a0 74
lukeocarwright 14:9cfe0041cc4e 75 //UNUSED FUNCTIONS:-------------------------------------------------------------
lukeocarwright 5:e785b9cd58c9 76 //squareWave(); //generates pulse wave modulated by sin wave
lukeocarwright 4:9b7ea5528a5c 77 //sinspeak();
lukeocarwright 2:07cef563acdf 78 }
lukeocarwright 2:07cef563acdf 79
lukeocarwright 14:9cfe0041cc4e 80 void noise_isr() {
lukeocarwright 14:9cfe0041cc4e 81 #ifdef SLOW_TIME
lukeocarwright 14:9cfe0041cc4e 82 if (g_isr_flag==0) {
lukeocarwright 14:9cfe0041cc4e 83 printf("ISR INTTERUPT = 1\n");
lukeocarwright 14:9cfe0041cc4e 84 }
lukeocarwright 14:9cfe0041cc4e 85 #endif
lukeocarwright 14:9cfe0041cc4e 86 g_isr_flag=1;
lukeocarwright 14:9cfe0041cc4e 87 }
lukeocarwright 14:9cfe0041cc4e 88
lukeocarwright 5:e785b9cd58c9 89 /*
lukeocarwright 4:9b7ea5528a5c 90 void squareWave()
lukeocarwright 4:9b7ea5528a5c 91 {
lukeocarwright 4:9b7ea5528a5c 92 printf("Generating SIN PWM \n");
lukeocarwright 4:9b7ea5528a5c 93
lukeocarwright 4:9b7ea5528a5c 94 int i=0; //int based iterator
lukeocarwright 4:9b7ea5528a5c 95 float ifl=0; //float based itterator
lukeocarwright 4:9b7ea5528a5c 96 float f=50; //frequency of sin wave produced
lukeocarwright 4:9b7ea5528a5c 97 period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca
lukeocarwright 4:9b7ea5528a5c 98 down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca
lukeocarwright 5:e785b9cd58c9 99
lukeocarwright 5:e785b9cd58c9 100
lukeocarwright 4:9b7ea5528a5c 101 printf("g_period_us: %d \n", g_period_us);
lukeocarwright 4:9b7ea5528a5c 102 printf("sin Frequency: %f \n", f);
lukeocarwright 4:9b7ea5528a5c 103
lukeocarwright 4:9b7ea5528a5c 104 while (1) { //continual loop for pulse production
lukeocarwright 4:9b7ea5528a5c 105 float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse
lukeocarwright 5:e785b9cd58c9 106
lukeocarwright 4:9b7ea5528a5c 107 g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs
lukeocarwright 4:9b7ea5528a5c 108 if (g_uptime_us<1) {
lukeocarwright 4:9b7ea5528a5c 109 g_uptime_us=g_period_us/100;
lukeocarwright 4:9b7ea5528a5c 110 } //sets to be value for timebeing to eliminate 0 error
lukeocarwright 4:9b7ea5528a5c 111 //float f=440*(pad.read_pot1()+1); //removed for simplification
lukeocarwright 5:e785b9cd58c9 112
lukeocarwright 4:9b7ea5528a5c 113 if (g_upflag==0) {
lukeocarwright 5:e785b9cd58c9 114
lukeocarwright 4:9b7ea5528a5c 115 ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function
lukeocarwright 4:9b7ea5528a5c 116 if (ifl>4096) {
lukeocarwright 4:9b7ea5528a5c 117 ifl= ifl-4096;
lukeocarwright 4:9b7ea5528a5c 118 }
lukeocarwright 4:9b7ea5528a5c 119 i=ifl;
lukeocarwright 5:e785b9cd58c9 120
lukeocarwright 4:9b7ea5528a5c 121 g_upflag=1; //reset flag
lukeocarwright 4:9b7ea5528a5c 122 //printf("iterate i: i= %d, ifl= %f \n", i, ifl);
lukeocarwright 4:9b7ea5528a5c 123 }
lukeocarwright 5:e785b9cd58c9 124
lukeocarwright 4:9b7ea5528a5c 125 //printf("DR: %f \n",dutyratio);
lukeocarwright 4:9b7ea5528a5c 126 //printf("sleep \n");
lukeocarwright 4:9b7ea5528a5c 127 sleep(); //sleeps till next isr
lukeocarwright 4:9b7ea5528a5c 128 }
lukeocarwright 4:9b7ea5528a5c 129 }
lukeocarwright 4:9b7ea5528a5c 130
lukeocarwright 2:07cef563acdf 131 void sinspeak ()
lukeocarwright 2:07cef563acdf 132 {
lukeocarwright 2:07cef563acdf 133 float f1=440.0;
lukeocarwright 2:07cef563acdf 134 float i=0;
lukeocarwright 3:b7df72682b81 135 unsigned short v=0;
lukeocarwright 2:07cef563acdf 136 int inc=0;
lukeocarwright 2:07cef563acdf 137 pad.reset_buttons();
lukeocarwright 2:07cef563acdf 138 f1 = pad.read_pot1();
lukeocarwright 3:b7df72682b81 139 f1 = 440.0f+440.0f*f1;
lukeocarwright 3:b7df72682b81 140 //printf("f1= %f \n",f1); // Used for Debug
lukeocarwright 2:07cef563acdf 141
lukeocarwright 2:07cef563acdf 142 while (inc<3000) {
lukeocarwright 2:07cef563acdf 143 int inti = i;
lukeocarwright 2:07cef563acdf 144 v = o[inti];
lukeocarwright 4:9b7ea5528a5c 145 printf("OUTPUT: %u \n", v);
lukeocarwright 4:9b7ea5528a5c 146 //pad.write_u16(v);
lukeocarwright 3:b7df72682b81 147 wait_us(230); //fs= 4k Ts=250us
lukeocarwright 3:b7df72682b81 148
lukeocarwright 3:b7df72682b81 149 i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 150 if (i>=4096.0f) {
lukeocarwright 2:07cef563acdf 151 i=i-4096.0f;
lukeocarwright 3:b7df72682b81 152 }
lukeocarwright 3:b7df72682b81 153 else {
lukeocarwright 4:9b7ea5528a5c 154 wait_us(3); // used to attempt note stabilisation to match other loop
lukeocarwright 2:07cef563acdf 155 }
lukeocarwright 2:07cef563acdf 156 inc++;
lukeocarwright 2:07cef563acdf 157 }
lukeocarwright 2:07cef563acdf 158 }
lukeocarwright 4:9b7ea5528a5c 159 */