Analog Filters and Oscilloscope

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
kmhatre
Date:
Sat Mar 03 02:53:19 2018 +0000
Commit message:
Analog Filters

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
report.txt Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0981765e0a06 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 03 02:53:19 2018 +0000
@@ -0,0 +1,201 @@
+
+/* 
+ * ENGR E 210 - CYBER PHYSICAL SYSTEMS (DIGITAL SYSTEMS)
+ * LAB 3 - ANALOG FILTERS AND OSCILLOSCOPE
+ * CODE BY KRISH HEMANT MHATRE AND ETHAN ZHANG
+ */
+#include "mbed.h"
+#include <string.h>
+
+#if !DEVICE_ANALOGOUT
+#error You cannot use this example as the AnalogOut is not supported on this device.
+#else
+AnalogOut my_output(PA_4);
+#endif
+
+#define PI        (3.141592653589793238462)
+#define AMPLITUDE (1.0)    // x * 3.3V
+#define PHASE     (PI * 1) // 2*pi is one period
+#define RANGE     (0x7FFF)
+#define OFFSET    (0x7FFF)
+
+// Configuration for sinewave output
+#define BUFFER_SIZE (360)
+uint16_t buffer[BUFFER_SIZE];
+
+Serial pc(USBTX, USBRX);
+DigitalOut led1(LED1);
+DigitalIn button(PC_13);
+
+void Led(int a)
+{
+    led1 = a;
+}
+
+void Blink(int n)
+{
+    int i = 0;
+    for(i=0; i<n; i++)
+    {
+        led1 = 1;
+        wait(0.5);
+        led1= 0;
+        wait(0.5);
+    }
+}
+
+
+void Button()
+{
+    if(button.read() == 1)
+    {
+        pc.printf("RELEASED\n\r");
+    }
+    else
+    {
+        pc.printf("PRESSED\n\r");
+    }
+}
+
+void calculate_sinewave(float amp){
+  for (int i = 0; i < BUFFER_SIZE; i++) {
+     double rads = (PI * i)/180.0; // Convert degree in radian
+     buffer[i] = (uint16_t)(amp * (RANGE * (cos(rads + PHASE))) + OFFSET);
+  }
+}
+
+int main() 
+{
+    pc.printf("Nucleo is running\n\r");
+    pc.printf("cps%\n\r");
+    int size = 32;
+    char str[size];
+    int i = 0;
+    while(1)
+    {
+        str[i] = pc.getc();
+        pc.putc(str[i]);
+        
+        if (i >= size)
+        {
+            pc.printf("OVER BUFFER\n\r");
+            i = 0;
+            for(int j = 0; j < size; j++)
+            {
+                str[j] = 0;
+            }
+            pc.printf("cps%\n\r");
+        }
+        if(str[i] == 13)
+        {
+            pc.printf("\n\rOK\n\r");
+            str[i] = '\0';
+            pc.printf("Command Recieved: %s\n\r", str);
+            char s[5];
+            sscanf(str, "%s", s);
+            if(strcmp(str, "BUTTON") == 0)
+            {
+                Button();
+                pc.printf("Button checked.\n\r");
+            }
+            else if(strcmp("LED ON", str) == 0)
+            {
+                Led(1);
+                pc.printf("LED is on\n\r");
+            }
+            else if(strcmp("LED OFF", str) == 0)
+            {
+                Led(0);
+                pc.printf("LED is off\n\r");
+            }
+            else if(strcmp(s, "BLINK") == 0)
+            {
+                pc.printf("Reading a blink\n\r");
+                char *a = strtok(str, " ");
+                a = strtok(NULL, " ");
+                pc.printf("Start blink\n\r");
+                int n;
+                sscanf(a, " %d", &n);
+                if(n>=1 && n<=10)
+                {
+                   Blink(n);
+                   pc.printf("Blink complete\n\r");
+                }
+                else
+                {
+                    pc.printf("ERROR: Blink cycle should be between 1 and 10\n\r");
+                }
+            }
+            else if(strncmp(str, "DC", 2) == 0)
+            {
+                   pc.printf("Reading DC\n\r");
+                   char *s1 = strtok(str, " ");
+                   s1 = strtok(NULL, " ");
+                   float volt;
+                   sscanf(s1, " %f", &volt);
+                   
+                   if((volt < (float)0.5) || (volt > (float)3.0))
+                   {
+                       pc.printf("Voltage out of range\n\r");    
+                   }
+                   else 
+                   {  
+                       float adjust = 19720;
+                       
+                       pc.printf("Voltage is %f\n\r", volt);
+                       my_output.write_u16(volt*adjust);
+                       pc.printf("Voltage changed\n\r");
+                   }
+            }
+            else if(strncmp(str, "SINE", 4) == 0)
+            {
+                   float x, amp, freq;
+                   char tmp[5];
+
+                   sscanf(str, "%s %f %f\n", tmp, &amp, &freq);
+                   pc.printf("Reading data\n\r");
+                   if((amp < (float)0.5) || (amp > (float)3.0) || (freq < (float)100) || (freq > (float)500))
+                   {
+                       pc.printf("Data out of range\n\r");    
+                   }
+                   else 
+                   {  
+                       pc.printf("Generating sine wave of amplitude %fV and frequency %fHz\n\r", amp, freq);
+                       x = (float)((float)amp/(float)3.3); //3.3V is the given voltage
+                       calculate_sinewave(x);
+                       while(1) 
+                       {      
+                          for (int i = 0; i < BUFFER_SIZE; i++) 
+                          {
+                             my_output.write_u16(buffer[i]);
+                             float delay;
+                             if(freq <= 200)
+                             {
+                                delay = (float)((float)((float)4000-(float)((float)5*freq))/(float)(freq*(float)(PI/2)));
+                             }
+                             else
+                             {
+                                delay = (float)((float)((float)5000-(float)((float)12*(float)freq))/(float)(freq));   
+                             }
+                             delay = ((float)(delay/1000000)); //converting microseconds to seconds
+                             wait(delay);
+                          }
+                       }
+                   }
+            }
+            else
+            {
+                pc.printf("ERR\n\r");
+            }
+            i = 0;
+            for(int j = 0; j < size; j++)
+            {
+                str[j] = '\0';
+            }
+            pc.printf("cps%\n\r");
+        }
+        i++;
+    }
+}
+
+
diff -r 000000000000 -r 0981765e0a06 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Mar 03 02:53:19 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e
\ No newline at end of file
diff -r 000000000000 -r 0981765e0a06 report.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/report.txt	Sat Mar 03 02:53:19 2018 +0000
@@ -0,0 +1,37 @@
+Filter Ideal Analysis
+Frequency(Hz)   V out
+100             0.893
+110             0.875
+120             0.856
+130             0.837
+140             0.818
+150             0.798
+160             0.779
+170             0.76
+180             0.741
+190             0.723
+200             0.7
+
+
+Filter Measurements
+Frequency(Hz)     V out
+100               0.895
+110               0.88
+120               0.858
+130               0.84
+140               0.82
+150               0.8
+160               0.776
+170               0.762
+180               0.746
+190               0.719
+200               0.704
+
+R = 800ohms
+C = 1uF
+f(cutoff) = 200Hz
+
+The major problem in this assignment was to figure out the frequency part. The calculation of the relationship between frequency
+and delay took majority of the time. It was easy to make the analysis on paper, but while working it out on oscilloscope was very
+time consuming as it was completely based on trial and error.
+Finally The code worked properly except that for higher values of frequency the standard deviatoion is pretty high.