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@7:33cb5f2db1ee, 2020-05-05 (annotated)
- Committer:
- lukeocarwright
- Date:
- Tue May 05 14:32:04 2020 +0000
- Revision:
- 7:33cb5f2db1ee
- Parent:
- 6:3a4e9ce12911
- Child:
- 8:f305ea78b2b1
Added initial GUI for front and sorted menu bug
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 | 7:33cb5f2db1ee | 11 | Last Edited: 04/05/2020 |
eencae | 0:b7f1f47bb26a | 12 | */ |
eencae | 0:b7f1f47bb26a | 13 | |
lukeocarwright | 5:e785b9cd58c9 | 14 | /* TO DO: |
lukeocarwright | 5:e785b9cd58c9 | 15 | LEDS INSTALL |
lukeocarwright | 6:3a4e9ce12911 | 16 | pass variables between functions using pointers |
lukeocarwright | 7:33cb5f2db1ee | 17 | Settings tab |
lukeocarwright | 7:33cb5f2db1ee | 18 | Other wavetables |
lukeocarwright | 7:33cb5f2db1ee | 19 | Generate sound |
lukeocarwright | 7:33cb5f2db1ee | 20 | ADSR |
lukeocarwright | 7:33cb5f2db1ee | 21 | Startup.h |
lukeocarwright | 5:e785b9cd58c9 | 22 | |
lukeocarwright | 5:e785b9cd58c9 | 23 | */ |
lukeocarwright | 2:07cef563acdf | 24 | // Includes |
eencae | 0:b7f1f47bb26a | 25 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 26 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 27 | #include "N5110.h" |
lukeocarwright | 6:3a4e9ce12911 | 28 | #include "menu.h" |
lukeocarwright | 7:33cb5f2db1ee | 29 | #include "startup.h" |
lukeocarwright | 7:33cb5f2db1ee | 30 | #include "Front.h" |
eencae | 0:b7f1f47bb26a | 31 | |
lukeocarwright | 2:07cef563acdf | 32 | // Objects |
eencae | 0:b7f1f47bb26a | 33 | Gamepad pad; |
eencae | 0:b7f1f47bb26a | 34 | N5110 lcd; |
lukeocarwright | 6:3a4e9ce12911 | 35 | menu menu; |
lukeocarwright | 7:33cb5f2db1ee | 36 | startup start; |
lukeocarwright | 7:33cb5f2db1ee | 37 | //Front front; |
lukeocarwright | 4:9b7ea5528a5c | 38 | Ticker down; |
lukeocarwright | 4:9b7ea5528a5c | 39 | Ticker period; |
lukeocarwright | 4:9b7ea5528a5c | 40 | |
lukeocarwright | 4:9b7ea5528a5c | 41 | DigitalOut rca(PTC4); |
lukeocarwright | 2:07cef563acdf | 42 | |
lukeocarwright | 2:07cef563acdf | 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 | 5:e785b9cd58c9 | 48 | //void down_isr(); |
lukeocarwright | 5:e785b9cd58c9 | 49 | //void up_isr(); |
lukeocarwright | 2:07cef563acdf | 50 | |
lukeocarwright | 2:07cef563acdf | 51 | |
lukeocarwright | 2:07cef563acdf | 52 | //Global Variables |
lukeocarwright | 4:9b7ea5528a5c | 53 | // all times drastically slowed to attempt debugging in teraterm |
lukeocarwright | 7:33cb5f2db1ee | 54 | //float g_period = 0.002; |
lukeocarwright | 7:33cb5f2db1ee | 55 | //float g_unused= g_period*1000000.0f; |
lukeocarwright | 7:33cb5f2db1ee | 56 | //int g_period_us=g_unused; |
lukeocarwright | 7:33cb5f2db1ee | 57 | //volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value |
lukeocarwright | 4:9b7ea5528a5c | 58 | //isr interrupt flags |
lukeocarwright | 7:33cb5f2db1ee | 59 | //volatile int g_upflag=1; |
lukeocarwright | 7:33cb5f2db1ee | 60 | //volatile int g_downflag=1; |
eencae | 0:b7f1f47bb26a | 61 | |
eencae | 0:b7f1f47bb26a | 62 | int main() |
eencae | 0:b7f1f47bb26a | 63 | { |
lukeocarwright | 2:07cef563acdf | 64 | printf("RUNNING CODE \n"); |
lukeocarwright | 7:33cb5f2db1ee | 65 | start.initialise(lcd,pad); //initialises board and displays start screen |
lukeocarwright | 7:33cb5f2db1ee | 66 | menu.mainmenu(lcd,pad); |
lukeocarwright | 7:33cb5f2db1ee | 67 | //menu.mainmenu(lcd, pad); //generates main menu |
lukeocarwright | 5:e785b9cd58c9 | 68 | //squareWave(); //generates pulse wave modulated by sin wave |
lukeocarwright | 4:9b7ea5528a5c | 69 | //sinspeak(); |
lukeocarwright | 2:07cef563acdf | 70 | } |
lukeocarwright | 2:07cef563acdf | 71 | |
lukeocarwright | 5:e785b9cd58c9 | 72 | /* |
lukeocarwright | 4:9b7ea5528a5c | 73 | void squareWave() |
lukeocarwright | 4:9b7ea5528a5c | 74 | { |
lukeocarwright | 4:9b7ea5528a5c | 75 | printf("Generating SIN PWM \n"); |
lukeocarwright | 4:9b7ea5528a5c | 76 | |
lukeocarwright | 4:9b7ea5528a5c | 77 | int i=0; //int based iterator |
lukeocarwright | 4:9b7ea5528a5c | 78 | float ifl=0; //float based itterator |
lukeocarwright | 4:9b7ea5528a5c | 79 | float f=50; //frequency of sin wave produced |
lukeocarwright | 4:9b7ea5528a5c | 80 | period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca |
lukeocarwright | 4:9b7ea5528a5c | 81 | down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca |
lukeocarwright | 5:e785b9cd58c9 | 82 | |
lukeocarwright | 5:e785b9cd58c9 | 83 | |
lukeocarwright | 4:9b7ea5528a5c | 84 | printf("g_period_us: %d \n", g_period_us); |
lukeocarwright | 4:9b7ea5528a5c | 85 | printf("sin Frequency: %f \n", f); |
lukeocarwright | 4:9b7ea5528a5c | 86 | |
lukeocarwright | 4:9b7ea5528a5c | 87 | while (1) { //continual loop for pulse production |
lukeocarwright | 4:9b7ea5528a5c | 88 | float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse |
lukeocarwright | 5:e785b9cd58c9 | 89 | |
lukeocarwright | 4:9b7ea5528a5c | 90 | g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs |
lukeocarwright | 4:9b7ea5528a5c | 91 | if (g_uptime_us<1) { |
lukeocarwright | 4:9b7ea5528a5c | 92 | g_uptime_us=g_period_us/100; |
lukeocarwright | 4:9b7ea5528a5c | 93 | } //sets to be value for timebeing to eliminate 0 error |
lukeocarwright | 4:9b7ea5528a5c | 94 | //float f=440*(pad.read_pot1()+1); //removed for simplification |
lukeocarwright | 5:e785b9cd58c9 | 95 | |
lukeocarwright | 4:9b7ea5528a5c | 96 | if (g_upflag==0) { |
lukeocarwright | 5:e785b9cd58c9 | 97 | |
lukeocarwright | 4:9b7ea5528a5c | 98 | ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function |
lukeocarwright | 4:9b7ea5528a5c | 99 | if (ifl>4096) { |
lukeocarwright | 4:9b7ea5528a5c | 100 | ifl= ifl-4096; |
lukeocarwright | 4:9b7ea5528a5c | 101 | } |
lukeocarwright | 4:9b7ea5528a5c | 102 | i=ifl; |
lukeocarwright | 5:e785b9cd58c9 | 103 | |
lukeocarwright | 4:9b7ea5528a5c | 104 | g_upflag=1; //reset flag |
lukeocarwright | 4:9b7ea5528a5c | 105 | //printf("iterate i: i= %d, ifl= %f \n", i, ifl); |
lukeocarwright | 4:9b7ea5528a5c | 106 | } |
lukeocarwright | 5:e785b9cd58c9 | 107 | |
lukeocarwright | 4:9b7ea5528a5c | 108 | //printf("DR: %f \n",dutyratio); |
lukeocarwright | 4:9b7ea5528a5c | 109 | //printf("sleep \n"); |
lukeocarwright | 4:9b7ea5528a5c | 110 | sleep(); //sleeps till next isr |
lukeocarwright | 4:9b7ea5528a5c | 111 | } |
lukeocarwright | 4:9b7ea5528a5c | 112 | } |
lukeocarwright | 4:9b7ea5528a5c | 113 | |
lukeocarwright | 4:9b7ea5528a5c | 114 | void down_isr() //sets rca to 0 |
lukeocarwright | 4:9b7ea5528a5c | 115 | { |
lukeocarwright | 4:9b7ea5528a5c | 116 | //printf("downISR \n"); |
lukeocarwright | 4:9b7ea5528a5c | 117 | if (g_downflag==0) {//stops error trigger |
lukeocarwright | 4:9b7ea5528a5c | 118 | rca.write(0); |
lukeocarwright | 4:9b7ea5528a5c | 119 | g_downflag=1; |
lukeocarwright | 4:9b7ea5528a5c | 120 | g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0 |
lukeocarwright | 4:9b7ea5528a5c | 121 | //printf("0 \n");//print only in while v. slow freguency |
lukeocarwright | 4:9b7ea5528a5c | 122 | } |
lukeocarwright | 4:9b7ea5528a5c | 123 | } |
lukeocarwright | 4:9b7ea5528a5c | 124 | |
lukeocarwright | 4:9b7ea5528a5c | 125 | void up_isr() //sets rca=1 |
lukeocarwright | 4:9b7ea5528a5c | 126 | { |
lukeocarwright | 4:9b7ea5528a5c | 127 | //printf("upISR \n"); |
lukeocarwright | 4:9b7ea5528a5c | 128 | rca.write(1); |
lukeocarwright | 4:9b7ea5528a5c | 129 | down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed |
lukeocarwright | 4:9b7ea5528a5c | 130 | g_upflag=0; //sets flag to iterate |
lukeocarwright | 4:9b7ea5528a5c | 131 | g_downflag=0; //sets flag to allow set to 0 |
lukeocarwright | 4:9b7ea5528a5c | 132 | //printf("1 \n"); //only in while at v low frequency |
lukeocarwright | 4:9b7ea5528a5c | 133 | } |
lukeocarwright | 4:9b7ea5528a5c | 134 | |
lukeocarwright | 5:e785b9cd58c9 | 135 | */ |
lukeocarwright | 4:9b7ea5528a5c | 136 | /* |
lukeocarwright | 2:07cef563acdf | 137 | void sinspeak () |
lukeocarwright | 2:07cef563acdf | 138 | { |
lukeocarwright | 2:07cef563acdf | 139 | float f1=440.0; |
lukeocarwright | 2:07cef563acdf | 140 | float i=0; |
lukeocarwright | 3:b7df72682b81 | 141 | unsigned short v=0; |
lukeocarwright | 2:07cef563acdf | 142 | int inc=0; |
lukeocarwright | 2:07cef563acdf | 143 | pad.reset_buttons(); |
lukeocarwright | 2:07cef563acdf | 144 | f1 = pad.read_pot1(); |
lukeocarwright | 3:b7df72682b81 | 145 | f1 = 440.0f+440.0f*f1; |
lukeocarwright | 3:b7df72682b81 | 146 | //printf("f1= %f \n",f1); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 147 | |
lukeocarwright | 2:07cef563acdf | 148 | while (inc<3000) { |
lukeocarwright | 2:07cef563acdf | 149 | int inti = i; |
lukeocarwright | 2:07cef563acdf | 150 | v = o[inti]; |
lukeocarwright | 4:9b7ea5528a5c | 151 | printf("OUTPUT: %u \n", v); |
lukeocarwright | 4:9b7ea5528a5c | 152 | //pad.write_u16(v); |
lukeocarwright | 3:b7df72682b81 | 153 | wait_us(230); //fs= 4k Ts=250us |
lukeocarwright | 3:b7df72682b81 | 154 | |
lukeocarwright | 3:b7df72682b81 | 155 | i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts) |
lukeocarwright | 2:07cef563acdf | 156 | if (i>=4096.0f) { |
lukeocarwright | 2:07cef563acdf | 157 | i=i-4096.0f; |
lukeocarwright | 3:b7df72682b81 | 158 | } |
lukeocarwright | 3:b7df72682b81 | 159 | else { |
lukeocarwright | 4:9b7ea5528a5c | 160 | wait_us(3); // used to attempt note stabilisation to match other loop |
lukeocarwright | 2:07cef563acdf | 161 | } |
lukeocarwright | 2:07cef563acdf | 162 | inc++; |
lukeocarwright | 2:07cef563acdf | 163 | } |
lukeocarwright | 2:07cef563acdf | 164 | } |
lukeocarwright | 4:9b7ea5528a5c | 165 | */ |