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.
main.cpp@18:204cd747b54a, 2020-05-21 (annotated)
- Committer:
- lukeocarwright
- Date:
- Thu May 21 22:59:59 2020 +0000
- Revision:
- 18:204cd747b54a
- Parent:
- 17:358987e9f30e
- Child:
- 19:08862f49cd9e
Got attack working with hardware glitch (slew rate issue and distortion likely due to loading and amp module). Generates CSV file to check output with envelope.
Who changed what in which revision?
User | Revision | Line number | New 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 | 17:358987e9f30e | 15 | Sort Squarewave Sound |
lukeocarwright | 17:358987e9f30e | 16 | Add Menu from front.cpp |
lukeocarwright | 17:358987e9f30e | 17 | Settings tab functionality |
lukeocarwright | 17:358987e9f30e | 18 | ADSR (functionality) (adjust stops sound) |
lukeocarwright | 8:f305ea78b2b1 | 19 | MIDI |
lukeocarwright | 8:f305ea78b2b1 | 20 | LEDS INSTALL |
lukeocarwright | 5:e785b9cd58c9 | 21 | */ |
lukeocarwright | 2:07cef563acdf | 22 | // Includes |
eencae | 0:b7f1f47bb26a | 23 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 24 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 25 | #include "N5110.h" |
lukeocarwright | 8:f305ea78b2b1 | 26 | #include "Menu.h" |
lukeocarwright | 7:33cb5f2db1ee | 27 | #include "startup.h" |
lukeocarwright | 7:33cb5f2db1ee | 28 | #include "Front.h" |
eencae | 0:b7f1f47bb26a | 29 | |
lukeocarwright | 9:f6ba53e355a0 | 30 | #ifdef DEBUG |
lukeocarwright | 9:f6ba53e355a0 | 31 | # include "Debug.h" |
lukeocarwright | 9:f6ba53e355a0 | 32 | #endif |
lukeocarwright | 9:f6ba53e355a0 | 33 | |
lukeocarwright | 2:07cef563acdf | 34 | // Objects |
eencae | 0:b7f1f47bb26a | 35 | Gamepad pad; |
eencae | 0:b7f1f47bb26a | 36 | N5110 lcd; |
lukeocarwright | 8:f305ea78b2b1 | 37 | Menu menu; |
lukeocarwright | 7:33cb5f2db1ee | 38 | startup start; |
lukeocarwright | 14:9cfe0041cc4e | 39 | Ticker tick; |
lukeocarwright | 4:9b7ea5528a5c | 40 | |
lukeocarwright | 14:9cfe0041cc4e | 41 | //DigitalOut rca(PTC4); |
lukeocarwright | 14:9cfe0041cc4e | 42 | //Serial pc(USBTX, USBRX); |
lukeocarwright | 2:07cef563acdf | 43 | |
lukeocarwright | 2:07cef563acdf | 44 | //Functions |
lukeocarwright | 5:e785b9cd58c9 | 45 | //void squareWave(); |
lukeocarwright | 4:9b7ea5528a5c | 46 | //void sinspeak(); |
lukeocarwright | 14:9cfe0041cc4e | 47 | void noise_isr(); |
lukeocarwright | 2:07cef563acdf | 48 | |
lukeocarwright | 2:07cef563acdf | 49 | |
lukeocarwright | 2:07cef563acdf | 50 | //Global Variables |
lukeocarwright | 14:9cfe0041cc4e | 51 | volatile extern int g_isr_flag; //may need extern |
lukeocarwright | 10:258a1eca02cc | 52 | |
lukeocarwright | 15:1c67f064278e | 53 | |
eencae | 0:b7f1f47bb26a | 54 | int main() |
eencae | 0:b7f1f47bb26a | 55 | { |
lukeocarwright | 2:07cef563acdf | 56 | printf("RUNNING CODE \n"); |
lukeocarwright | 7:33cb5f2db1ee | 57 | start.initialise(lcd,pad); //initialises board and displays start screen |
lukeocarwright | 14:9cfe0041cc4e | 58 | |
lukeocarwright | 14:9cfe0041cc4e | 59 | #ifdef DEBUG |
lukeocarwright | 14:9cfe0041cc4e | 60 | printf("DEBUG COMPILE RUNNING\n"); |
lukeocarwright | 14:9cfe0041cc4e | 61 | run_LUTs_debug(); |
lukeocarwright | 14:9cfe0041cc4e | 62 | #endif |
lukeocarwright | 14:9cfe0041cc4e | 63 | |
lukeocarwright | 14:9cfe0041cc4e | 64 | #ifdef SLOW_TIME //ATTACH TICKERS FOR SOUND |
lukeocarwright | 15:1c67f064278e | 65 | printf("Running in SLOW_TIME\n"); |
lukeocarwright | 15:1c67f064278e | 66 | tick.attach(&noise_isr, 1); //extended for debug (1Hz) |
lukeocarwright | 15:1c67f064278e | 67 | #endif |
lukeocarwright | 18:204cd747b54a | 68 | #ifdef CSV |
lukeocarwright | 18:204cd747b54a | 69 | printf("Running in SLOW_TIME\n"); |
lukeocarwright | 18:204cd747b54a | 70 | tick.attach_us(&noise_isr, 5000); //extended for debug (1Hz) |
lukeocarwright | 18:204cd747b54a | 71 | #endif |
lukeocarwright | 15:1c67f064278e | 72 | #ifndef SLOW_TIME |
lukeocarwright | 18:204cd747b54a | 73 | #ifndef CSV |
lukeocarwright | 18:204cd747b54a | 74 | tick.attach_us(&noise_isr, 63); //normal mode SR=16kHz |
lukeocarwright | 18:204cd747b54a | 75 | #endif |
lukeocarwright | 14:9cfe0041cc4e | 76 | #endif |
lukeocarwright | 14:9cfe0041cc4e | 77 | |
lukeocarwright | 14:9cfe0041cc4e | 78 | menu.mainmenu(lcd,pad); |
lukeocarwright | 9:f6ba53e355a0 | 79 | |
lukeocarwright | 14:9cfe0041cc4e | 80 | //UNUSED FUNCTIONS:------------------------------------------------------------- |
lukeocarwright | 5:e785b9cd58c9 | 81 | //squareWave(); //generates pulse wave modulated by sin wave |
lukeocarwright | 4:9b7ea5528a5c | 82 | //sinspeak(); |
lukeocarwright | 2:07cef563acdf | 83 | } |
lukeocarwright | 2:07cef563acdf | 84 | |
lukeocarwright | 14:9cfe0041cc4e | 85 | void noise_isr() { |
lukeocarwright | 14:9cfe0041cc4e | 86 | #ifdef SLOW_TIME |
lukeocarwright | 14:9cfe0041cc4e | 87 | if (g_isr_flag==0) { |
lukeocarwright | 18:204cd747b54a | 88 | //printf("ISR INTTERUPT = 1\n"); |
lukeocarwright | 14:9cfe0041cc4e | 89 | } |
lukeocarwright | 14:9cfe0041cc4e | 90 | #endif |
lukeocarwright | 14:9cfe0041cc4e | 91 | g_isr_flag=1; |
lukeocarwright | 14:9cfe0041cc4e | 92 | } |
lukeocarwright | 14:9cfe0041cc4e | 93 | |
lukeocarwright | 5:e785b9cd58c9 | 94 | /* |
lukeocarwright | 4:9b7ea5528a5c | 95 | void squareWave() |
lukeocarwright | 4:9b7ea5528a5c | 96 | { |
lukeocarwright | 4:9b7ea5528a5c | 97 | printf("Generating SIN PWM \n"); |
lukeocarwright | 4:9b7ea5528a5c | 98 | |
lukeocarwright | 4:9b7ea5528a5c | 99 | int i=0; //int based iterator |
lukeocarwright | 4:9b7ea5528a5c | 100 | float ifl=0; //float based itterator |
lukeocarwright | 4:9b7ea5528a5c | 101 | float f=50; //frequency of sin wave produced |
lukeocarwright | 4:9b7ea5528a5c | 102 | period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca |
lukeocarwright | 4:9b7ea5528a5c | 103 | down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca |
lukeocarwright | 5:e785b9cd58c9 | 104 | |
lukeocarwright | 5:e785b9cd58c9 | 105 | |
lukeocarwright | 4:9b7ea5528a5c | 106 | printf("g_period_us: %d \n", g_period_us); |
lukeocarwright | 4:9b7ea5528a5c | 107 | printf("sin Frequency: %f \n", f); |
lukeocarwright | 4:9b7ea5528a5c | 108 | |
lukeocarwright | 4:9b7ea5528a5c | 109 | while (1) { //continual loop for pulse production |
lukeocarwright | 4:9b7ea5528a5c | 110 | float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse |
lukeocarwright | 5:e785b9cd58c9 | 111 | |
lukeocarwright | 4:9b7ea5528a5c | 112 | g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs |
lukeocarwright | 4:9b7ea5528a5c | 113 | if (g_uptime_us<1) { |
lukeocarwright | 4:9b7ea5528a5c | 114 | g_uptime_us=g_period_us/100; |
lukeocarwright | 4:9b7ea5528a5c | 115 | } //sets to be value for timebeing to eliminate 0 error |
lukeocarwright | 4:9b7ea5528a5c | 116 | //float f=440*(pad.read_pot1()+1); //removed for simplification |
lukeocarwright | 5:e785b9cd58c9 | 117 | |
lukeocarwright | 4:9b7ea5528a5c | 118 | if (g_upflag==0) { |
lukeocarwright | 5:e785b9cd58c9 | 119 | |
lukeocarwright | 4:9b7ea5528a5c | 120 | ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function |
lukeocarwright | 4:9b7ea5528a5c | 121 | if (ifl>4096) { |
lukeocarwright | 4:9b7ea5528a5c | 122 | ifl= ifl-4096; |
lukeocarwright | 4:9b7ea5528a5c | 123 | } |
lukeocarwright | 4:9b7ea5528a5c | 124 | i=ifl; |
lukeocarwright | 5:e785b9cd58c9 | 125 | |
lukeocarwright | 4:9b7ea5528a5c | 126 | g_upflag=1; //reset flag |
lukeocarwright | 4:9b7ea5528a5c | 127 | //printf("iterate i: i= %d, ifl= %f \n", i, ifl); |
lukeocarwright | 4:9b7ea5528a5c | 128 | } |
lukeocarwright | 5:e785b9cd58c9 | 129 | |
lukeocarwright | 4:9b7ea5528a5c | 130 | //printf("DR: %f \n",dutyratio); |
lukeocarwright | 4:9b7ea5528a5c | 131 | //printf("sleep \n"); |
lukeocarwright | 4:9b7ea5528a5c | 132 | sleep(); //sleeps till next isr |
lukeocarwright | 4:9b7ea5528a5c | 133 | } |
lukeocarwright | 4:9b7ea5528a5c | 134 | } |
lukeocarwright | 4:9b7ea5528a5c | 135 | |
lukeocarwright | 2:07cef563acdf | 136 | void sinspeak () |
lukeocarwright | 2:07cef563acdf | 137 | { |
lukeocarwright | 2:07cef563acdf | 138 | float f1=440.0; |
lukeocarwright | 2:07cef563acdf | 139 | float i=0; |
lukeocarwright | 3:b7df72682b81 | 140 | unsigned short v=0; |
lukeocarwright | 2:07cef563acdf | 141 | int inc=0; |
lukeocarwright | 2:07cef563acdf | 142 | pad.reset_buttons(); |
lukeocarwright | 2:07cef563acdf | 143 | f1 = pad.read_pot1(); |
lukeocarwright | 3:b7df72682b81 | 144 | f1 = 440.0f+440.0f*f1; |
lukeocarwright | 3:b7df72682b81 | 145 | //printf("f1= %f \n",f1); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 146 | |
lukeocarwright | 2:07cef563acdf | 147 | while (inc<3000) { |
lukeocarwright | 2:07cef563acdf | 148 | int inti = i; |
lukeocarwright | 2:07cef563acdf | 149 | v = o[inti]; |
lukeocarwright | 4:9b7ea5528a5c | 150 | printf("OUTPUT: %u \n", v); |
lukeocarwright | 4:9b7ea5528a5c | 151 | //pad.write_u16(v); |
lukeocarwright | 3:b7df72682b81 | 152 | wait_us(230); //fs= 4k Ts=250us |
lukeocarwright | 3:b7df72682b81 | 153 | |
lukeocarwright | 3:b7df72682b81 | 154 | i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts) |
lukeocarwright | 2:07cef563acdf | 155 | if (i>=4096.0f) { |
lukeocarwright | 2:07cef563acdf | 156 | i=i-4096.0f; |
lukeocarwright | 3:b7df72682b81 | 157 | } |
lukeocarwright | 3:b7df72682b81 | 158 | else { |
lukeocarwright | 4:9b7ea5528a5c | 159 | wait_us(3); // used to attempt note stabilisation to match other loop |
lukeocarwright | 2:07cef563acdf | 160 | } |
lukeocarwright | 2:07cef563acdf | 161 | inc++; |
lukeocarwright | 2:07cef563acdf | 162 | } |
lukeocarwright | 2:07cef563acdf | 163 | } |
lukeocarwright | 4:9b7ea5528a5c | 164 | */ |