Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue Apr 07 18:56:28 2020 +0000
Revision:
4:9b7ea5528a5c
Parent:
3:b7df72682b81
Child:
5:e785b9cd58c9
Generates sine wave using pwm (tested upto 50hz on scope w.o filter)

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 4:9b7ea5528a5c 11 Last Edited: 07/04/2020
eencae 0:b7f1f47bb26a 12 */
eencae 0:b7f1f47bb26a 13
lukeocarwright 2:07cef563acdf 14 // Includes
eencae 0:b7f1f47bb26a 15 #include "mbed.h"
eencae 0:b7f1f47bb26a 16 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 17 #include "N5110.h"
eencae 0:b7f1f47bb26a 18
lukeocarwright 2:07cef563acdf 19 // Objects
eencae 0:b7f1f47bb26a 20 Gamepad pad;
eencae 0:b7f1f47bb26a 21 N5110 lcd;
lukeocarwright 4:9b7ea5528a5c 22 Ticker down;
lukeocarwright 4:9b7ea5528a5c 23 Ticker period;
lukeocarwright 4:9b7ea5528a5c 24
lukeocarwright 4:9b7ea5528a5c 25 DigitalOut rca(PTC4);
lukeocarwright 2:07cef563acdf 26
lukeocarwright 2:07cef563acdf 27 Serial pc(USBTX, USBRX);
lukeocarwright 2:07cef563acdf 28
lukeocarwright 2:07cef563acdf 29 //Functions
lukeocarwright 2:07cef563acdf 30 void startup();
lukeocarwright 2:07cef563acdf 31 void squareWave();
lukeocarwright 2:07cef563acdf 32 void wavetable();
lukeocarwright 4:9b7ea5528a5c 33 //void sinspeak();
lukeocarwright 4:9b7ea5528a5c 34 void down_isr();
lukeocarwright 4:9b7ea5528a5c 35 void up_isr();
lukeocarwright 2:07cef563acdf 36
lukeocarwright 2:07cef563acdf 37 //Arrays for startup
lukeocarwright 2:07cef563acdf 38 const char CART[4] = {'C','A','R','T'};
lukeocarwright 2:07cef563acdf 39
lukeocarwright 2:07cef563acdf 40 //Global Variables
lukeocarwright 4:9b7ea5528a5c 41 volatile double wavtable[4096]; //Wavetable array
lukeocarwright 4:9b7ea5528a5c 42 // all times drastically slowed to attempt debugging in teraterm
lukeocarwright 4:9b7ea5528a5c 43 float g_period = 0.002;
lukeocarwright 4:9b7ea5528a5c 44 float g_unused= g_period*1000000.0f;
lukeocarwright 4:9b7ea5528a5c 45 int g_period_us=g_unused;
lukeocarwright 4:9b7ea5528a5c 46 volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value
lukeocarwright 4:9b7ea5528a5c 47 //isr interrupt flags
lukeocarwright 4:9b7ea5528a5c 48 volatile int g_upflag=1;
lukeocarwright 4:9b7ea5528a5c 49 volatile int g_downflag=1;
eencae 0:b7f1f47bb26a 50
eencae 0:b7f1f47bb26a 51 int main()
eencae 0:b7f1f47bb26a 52 {
lukeocarwright 2:07cef563acdf 53 printf("RUNNING CODE \n");
lukeocarwright 4:9b7ea5528a5c 54 startup(); //initialises board and displays start screen
lukeocarwright 4:9b7ea5528a5c 55 wavetable(); //generates wavtable array
lukeocarwright 4:9b7ea5528a5c 56 //pad.leds_on();
lukeocarwright 4:9b7ea5528a5c 57 squareWave(); //generates pulse wave modulated by sin wave
lukeocarwright 4:9b7ea5528a5c 58 //pad.leds_off();
lukeocarwright 4:9b7ea5528a5c 59 //sinspeak();
lukeocarwright 2:07cef563acdf 60 }
lukeocarwright 2:07cef563acdf 61
lukeocarwright 2:07cef563acdf 62
lukeocarwright 2:07cef563acdf 63 void startup()
lukeocarwright 2:07cef563acdf 64 {
lukeocarwright 2:07cef563acdf 65 pad.init(); //initiate Gamepad
lukeocarwright 2:07cef563acdf 66 pad.leds_on(); //turn LEDS on to show starting up
lukeocarwright 3:b7df72682b81 67 printf("Initialising Pad\n");
lukeocarwright 2:07cef563acdf 68 lcd.init(); //intitates screen
lukeocarwright 2:07cef563acdf 69 lcd.clear();
lukeocarwright 4:9b7ea5528a5c 70 lcd.setContrast(0.5); //contrast setting
lukeocarwright 2:07cef563acdf 71 lcd.inverseMode(); //puts screen in -ve
lukeocarwright 4:9b7ea5528a5c 72 //position for CART
lukeocarwright 2:07cef563acdf 73 const int x = 10;
lukeocarwright 2:07cef563acdf 74 const int y = 2;
lukeocarwright 2:07cef563acdf 75 //Prints CART SYNTH to display
lukeocarwright 2:07cef563acdf 76 lcd.printString(CART,x,y);
lukeocarwright 2:07cef563acdf 77 lcd.refresh();
lukeocarwright 2:07cef563acdf 78 wait_ms(1800); //timer to allow theme to play out
lukeocarwright 2:07cef563acdf 79 lcd.clear();
lukeocarwright 2:07cef563acdf 80 lcd.refresh(); //sets clear screen
lukeocarwright 2:07cef563acdf 81 pad.leds_off(); //turns of leds to show ready
eencae 0:b7f1f47bb26a 82 }
eencae 0:b7f1f47bb26a 83
lukeocarwright 2:07cef563acdf 84
lukeocarwright 2:07cef563acdf 85 void wavetable()
lukeocarwright 2:07cef563acdf 86 {
lukeocarwright 4:9b7ea5528a5c 87 double sin_d[4096];
lukeocarwright 2:07cef563acdf 88
lukeocarwright 4:9b7ea5528a5c 89 pad.leds_on(); //shows computating (debug)
lukeocarwright 2:07cef563acdf 90 printf("Generating Wavetable \n");
lukeocarwright 2:07cef563acdf 91
lukeocarwright 2:07cef563acdf 92 for (int i=0; i<4096; i++) {
lukeocarwright 4:9b7ea5528a5c 93 sin_d[i] = 0.5f*sin(2.0f*PI*(i/4096.0f));
lukeocarwright 4:9b7ea5528a5c 94 wavtable[i]= sin_d[i]+0.5; //generates wave table 0<1 in double
lukeocarwright 4:9b7ea5528a5c 95 //printf("wav[i]= %f \n", wavtable[i]); // Used for Debug
lukeocarwright 2:07cef563acdf 96 }
lukeocarwright 2:07cef563acdf 97 pad.leds_off();
lukeocarwright 2:07cef563acdf 98 }
lukeocarwright 3:b7df72682b81 99
lukeocarwright 3:b7df72682b81 100
lukeocarwright 4:9b7ea5528a5c 101 void squareWave()
lukeocarwright 4:9b7ea5528a5c 102 {
lukeocarwright 4:9b7ea5528a5c 103 printf("Generating SIN PWM \n");
lukeocarwright 4:9b7ea5528a5c 104
lukeocarwright 4:9b7ea5528a5c 105 int i=0; //int based iterator
lukeocarwright 4:9b7ea5528a5c 106 float ifl=0; //float based itterator
lukeocarwright 4:9b7ea5528a5c 107 float f=50; //frequency of sin wave produced
lukeocarwright 4:9b7ea5528a5c 108 period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca
lukeocarwright 4:9b7ea5528a5c 109 down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca
lukeocarwright 4:9b7ea5528a5c 110
lukeocarwright 4:9b7ea5528a5c 111
lukeocarwright 4:9b7ea5528a5c 112 printf("g_period_us: %d \n", g_period_us);
lukeocarwright 4:9b7ea5528a5c 113 printf("sin Frequency: %f \n", f);
lukeocarwright 4:9b7ea5528a5c 114
lukeocarwright 4:9b7ea5528a5c 115 while (1) { //continual loop for pulse production
lukeocarwright 4:9b7ea5528a5c 116 float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse
lukeocarwright 4:9b7ea5528a5c 117
lukeocarwright 4:9b7ea5528a5c 118 g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs
lukeocarwright 4:9b7ea5528a5c 119 if (g_uptime_us<1) {
lukeocarwright 4:9b7ea5528a5c 120 g_uptime_us=g_period_us/100;
lukeocarwright 4:9b7ea5528a5c 121 } //sets to be value for timebeing to eliminate 0 error
lukeocarwright 4:9b7ea5528a5c 122 //float f=440*(pad.read_pot1()+1); //removed for simplification
lukeocarwright 4:9b7ea5528a5c 123
lukeocarwright 4:9b7ea5528a5c 124 if (g_upflag==0) {
lukeocarwright 4:9b7ea5528a5c 125
lukeocarwright 4:9b7ea5528a5c 126 ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function
lukeocarwright 4:9b7ea5528a5c 127 if (ifl>4096) {
lukeocarwright 4:9b7ea5528a5c 128 ifl= ifl-4096;
lukeocarwright 4:9b7ea5528a5c 129 }
lukeocarwright 4:9b7ea5528a5c 130 i=ifl;
lukeocarwright 4:9b7ea5528a5c 131
lukeocarwright 4:9b7ea5528a5c 132 g_upflag=1; //reset flag
lukeocarwright 4:9b7ea5528a5c 133 //printf("iterate i: i= %d, ifl= %f \n", i, ifl);
lukeocarwright 4:9b7ea5528a5c 134 }
lukeocarwright 4:9b7ea5528a5c 135
lukeocarwright 4:9b7ea5528a5c 136 //printf("DR: %f \n",dutyratio);
lukeocarwright 4:9b7ea5528a5c 137 //printf("sleep \n");
lukeocarwright 4:9b7ea5528a5c 138 sleep(); //sleeps till next isr
lukeocarwright 4:9b7ea5528a5c 139 }
lukeocarwright 4:9b7ea5528a5c 140 }
lukeocarwright 4:9b7ea5528a5c 141
lukeocarwright 4:9b7ea5528a5c 142 void down_isr() //sets rca to 0
lukeocarwright 4:9b7ea5528a5c 143 {
lukeocarwright 4:9b7ea5528a5c 144 //printf("downISR \n");
lukeocarwright 4:9b7ea5528a5c 145 if (g_downflag==0) {//stops error trigger
lukeocarwright 4:9b7ea5528a5c 146 rca.write(0);
lukeocarwright 4:9b7ea5528a5c 147 g_downflag=1;
lukeocarwright 4:9b7ea5528a5c 148 g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0
lukeocarwright 4:9b7ea5528a5c 149 //printf("0 \n");//print only in while v. slow freguency
lukeocarwright 4:9b7ea5528a5c 150 }
lukeocarwright 4:9b7ea5528a5c 151 }
lukeocarwright 4:9b7ea5528a5c 152
lukeocarwright 4:9b7ea5528a5c 153 void up_isr() //sets rca=1
lukeocarwright 4:9b7ea5528a5c 154 {
lukeocarwright 4:9b7ea5528a5c 155 //printf("upISR \n");
lukeocarwright 4:9b7ea5528a5c 156 rca.write(1);
lukeocarwright 4:9b7ea5528a5c 157 down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed
lukeocarwright 4:9b7ea5528a5c 158 g_upflag=0; //sets flag to iterate
lukeocarwright 4:9b7ea5528a5c 159 g_downflag=0; //sets flag to allow set to 0
lukeocarwright 4:9b7ea5528a5c 160 //printf("1 \n"); //only in while at v low frequency
lukeocarwright 4:9b7ea5528a5c 161 }
lukeocarwright 4:9b7ea5528a5c 162
lukeocarwright 4:9b7ea5528a5c 163
lukeocarwright 4:9b7ea5528a5c 164 /*
lukeocarwright 2:07cef563acdf 165 void sinspeak ()
lukeocarwright 2:07cef563acdf 166 {
lukeocarwright 2:07cef563acdf 167 float f1=440.0;
lukeocarwright 2:07cef563acdf 168 float i=0;
lukeocarwright 3:b7df72682b81 169 unsigned short v=0;
lukeocarwright 2:07cef563acdf 170 int inc=0;
lukeocarwright 2:07cef563acdf 171 pad.reset_buttons();
lukeocarwright 2:07cef563acdf 172 f1 = pad.read_pot1();
lukeocarwright 3:b7df72682b81 173 f1 = 440.0f+440.0f*f1;
lukeocarwright 3:b7df72682b81 174 //printf("f1= %f \n",f1); // Used for Debug
lukeocarwright 2:07cef563acdf 175
lukeocarwright 2:07cef563acdf 176 while (inc<3000) {
lukeocarwright 2:07cef563acdf 177 int inti = i;
lukeocarwright 2:07cef563acdf 178 v = o[inti];
lukeocarwright 4:9b7ea5528a5c 179 printf("OUTPUT: %u \n", v);
lukeocarwright 4:9b7ea5528a5c 180 //pad.write_u16(v);
lukeocarwright 3:b7df72682b81 181 wait_us(230); //fs= 4k Ts=250us
lukeocarwright 3:b7df72682b81 182
lukeocarwright 3:b7df72682b81 183 i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 184 if (i>=4096.0f) {
lukeocarwright 2:07cef563acdf 185 i=i-4096.0f;
lukeocarwright 3:b7df72682b81 186 }
lukeocarwright 3:b7df72682b81 187 else {
lukeocarwright 4:9b7ea5528a5c 188 wait_us(3); // used to attempt note stabilisation to match other loop
lukeocarwright 2:07cef563acdf 189 }
lukeocarwright 2:07cef563acdf 190 inc++;
lukeocarwright 2:07cef563acdf 191 }
lukeocarwright 2:07cef563acdf 192 }
lukeocarwright 4:9b7ea5528a5c 193 */