Basic_sine_wave_generator

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos AD5206

Files at this revision

API Documentation at this revision

Comitter:
hanjiex
Date:
Wed Dec 02 19:48:56 2015 +0000
Parent:
10:159f38636ed4
Commit message:
Missing part:; 1, touch pad control; 2, integrated with RPC

Changed in this revision

AD5206.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
menu.cpp Show annotated file Show diff for this revision Revisions of this file
menu.h Show annotated file Show diff for this revision Revisions of this file
para.cpp Show annotated file Show diff for this revision Revisions of this file
para.h Show annotated file Show diff for this revision Revisions of this file
wave.cpp Show diff for this revision Revisions of this file
wave.h Show diff for this revision Revisions of this file
diff -r 159f38636ed4 -r d21c2da8b290 AD5206.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AD5206.lib	Wed Dec 02 19:48:56 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/taoqiuyang/code/AD5206/#d2adf1f7e548
diff -r 159f38636ed4 -r d21c2da8b290 main.cpp
--- a/main.cpp	Tue Dec 01 18:59:42 2015 +0000
+++ b/main.cpp	Wed Dec 02 19:48:56 2015 +0000
@@ -8,24 +8,39 @@
                 Yuqing Peng
 */
 
-#include "wave.h"
+//#include "wave.h"
 #include "mbed.h"
 #include "menu.h"
 #include "para.h"
 #include "uLCD_4DGL.h"
+#include <AD5206.h>
 //----- Objects for devices----------------------
-
+Serial funGen(p28, p27);
 DigitalOut led1(LED1);
+AD5206 digipot(p5, p6, p7,p8);//MOSI, MISO, CLK, CS'
+Serial pc(USBTX, USBRX);
 
+para thispara;
+
+int vol;
 int main(void){
+    pc.printf("1111");
     int  start = begain_first();
     if (start) {
-        para_setting();
-        generate_waveform_datapoints();
+        thispara = para_setting();
+        int type=thispara.get_type();
+        vol = thispara.get_amp() * 100;
+        float freq = thispara.get_freq();
         while (1){
-            led1 = 1;
-            output_waveform_datapoints();
+            //funGen.printf("%d,%f\n", type, freq);
+            pc.printf("%d",type);
+        //change gain of the opamp, in order to change output amplitude
+            
+            digipot.write_AD5206(0, vol);
+            //val=val+10;
+            //if (val>=255){val=0;}
+            wait(1);
         }
-        
     }
 }
+
diff -r 159f38636ed4 -r d21c2da8b290 menu.cpp
--- a/menu.cpp	Tue Dec 01 18:59:42 2015 +0000
+++ b/menu.cpp	Wed Dec 02 19:48:56 2015 +0000
@@ -1,6 +1,6 @@
 /*
 * menu.cpp
-* The menu is displayed first when the funtion generator starts
+* Author: Hanjie Xie
 */
 
 #include "mbed.h"
@@ -143,13 +143,13 @@
     }
 }
 
-int para_setting() {
+para para_setting() {
     mode_set();
     freq_set();
     amp_set();
-    offset_set();
+    //offset_set();
     para_display();
-    return 1;
+    return mypara;
 }
 
 void mode_set() {
@@ -204,12 +204,12 @@
     uLCD.printf("\n  unit:\n"); 
     */
     int choose = 0;
-    int increment = 100;
+    float increment = 100;
     uLCD.locate(1,3);
     uLCD.printf("\nSet the frequency:\n");
     while(choose == 0){
         uLCD.locate(4,5);
-        uLCD.printf("\n%d    \n", mypara.get_freq());
+        uLCD.printf("\n%.0f    \n", mypara.get_freq());
         if(left_pb1 == 0){
             choose = 1;
             uLCD.cls();
@@ -254,31 +254,7 @@
     }
 }
 
-void offset_set() {
-    int choose = 0;
-    uLCD.locate(4,3);
-    uLCD.printf("\nSet the offset:\n");
-    float increment = 0.5;
-    while(choose == 0){
-        uLCD.locate(4,5);
-        uLCD.printf("\n%.2f    \n", mypara.get_offset());
-        if(left_pb1 == 0){
-            choose = 1;
-            uLCD.cls();
-        } 
-        if(fire_pb1 == 0){
-           float temp = mypara.get_offset();
-           temp += increment;
-           mypara.set_offset(temp);
-        }   
-        if(right_pb1 == 0){
-           float temp = mypara.get_offset();
-           temp -= increment;
-           mypara.set_offset(temp);
-        }
-        wait(0.2);
-    }
-}
+  
 
 void para_display() {
     uLCD.locate(4,3);
@@ -287,6 +263,6 @@
     uLCD.printf("\n 2.Frequency: %d\n", mypara.get_freq());
     uLCD.locate(4,7);
     uLCD.printf("\n 3.Amplitude: %.2f\n", mypara.get_amp());
-    uLCD.locate(4,9);
-    uLCD.printf("\n 4.Offset: %.2f\n", mypara.get_offset());
+    //uLCD.locate(4,9);
+    //uLCD.printf("\n 4.Offset: %.2f\n", mypara.get_offset());
 }
\ No newline at end of file
diff -r 159f38636ed4 -r d21c2da8b290 menu.h
--- a/menu.h	Tue Dec 01 18:59:42 2015 +0000
+++ b/menu.h	Wed Dec 02 19:48:56 2015 +0000
@@ -1,9 +1,12 @@
 /*
 * menu.h
+* the control menu on mbed side
+* Author: Hanjie Xie
 */
 #ifndef MENU_H
 #define MENU_H
 
+#include "para.h"
 int begain_first();
 
 int return_menu();
@@ -16,11 +19,11 @@
 
 void check();
 
-int para_setting();
+para para_setting();
 void mode_set();
 void amp_set();
 void freq_set();
-void offset_set();
+//void offset_set();
 void para_display();
 
 #endif //MENU_H
\ No newline at end of file
diff -r 159f38636ed4 -r d21c2da8b290 para.cpp
--- a/para.cpp	Tue Dec 01 18:59:42 2015 +0000
+++ b/para.cpp	Wed Dec 02 19:48:56 2015 +0000
@@ -1,17 +1,21 @@
+/*
+* Author: Hanjie Xie
+* para.cpp
+*/
 #include "para.h"
 #include "mbed.h"
 
 para::para() {
     waveform_type=0; //0 for sine, 
-    freq=1000; //Hz
+    freq=2000; //Hz
     amp=1.65; //Sacle factor, 1=full range
-    offset=1.65;   
+    //offset=1.65;   
 }
 void para::set_type(int a)
 {
     waveform_type = a;
 }
-void para::set_freq(int a)
+void para::set_freq(float a)
 {
     freq = a;
 }
@@ -19,11 +23,8 @@
 {
     amp = a;
 }
-void para::set_offset(float a)
-{
-    offset = a;
-}
+
 int para::get_type(){return waveform_type;}
-int para::get_freq(){return freq;}
+float para::get_freq(){return freq;}
 float para::get_amp(){return amp;}
-float para::get_offset(){return offset;}
\ No newline at end of file
+//float para::get_offset(){return offset;}
\ No newline at end of file
diff -r 159f38636ed4 -r d21c2da8b290 para.h
--- a/para.h	Tue Dec 01 18:59:42 2015 +0000
+++ b/para.h	Wed Dec 02 19:48:56 2015 +0000
@@ -1,22 +1,28 @@
+/*
+* Author: Hanjie Xie
+* para.cpp
+* a class to store the parameter which is used
+* to change the wave output
+*/
 #ifndef PARA_H
 #define PARA_H
 
 class para {
 public:
     int waveform_type;
-    int freq;
+    float freq;
     float amp;
-    float offset;
+    //float offset;
 public:
     para();
     void set_type(int);
-    void set_freq(int);
+    void set_freq(float);
     void set_amp(float);
-    void set_offset(float);
+    //void set_offset(float);
     int get_type();
-    int get_freq();
+    float get_freq();
     float get_amp();
-    float get_offset();
+    //float get_offset();
 };
 
 #endif //PARA_H
\ No newline at end of file
diff -r 159f38636ed4 -r d21c2da8b290 wave.cpp
--- a/wave.cpp	Tue Dec 01 18:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-/*
-* wave.cpp
-* 
-*/
-
-#include "mbed.h"
-#include "wave.h"
-#include "para.h"
-#include "menu.h"
-Serial pc(USBTX, USBRX);
-AnalogOut  aout(p18);
-
-// from global variable
-extern para mypara;
-int waveform_type = mypara.get_type();
-int freq = mypara.get_freq();
-float amp = mypara.get_amp();
-float offset = mypara.get_offset();
-
-
-int datapoint_counter=0;
-float duty_cycle=0.2;
-int length_waveform=100;
-float time_interv=1.0/freq/length_waveform;
-uint16_t waveform[100];
-int x;
-const double pi = 3.14;
-void generate_waveform_datapoints(){
-    float t=0;
-     
-    //------if sine wave--------
-    if (waveform_type==0){  
-        for (int counter=0; counter<length_waveform; counter++){
-            x=amp/3.3*cos(2*pi*freq*t)+offset/3.3;
-            waveform[counter]=(uint16_t) (x*65535);      
-            t += time_interv;
-        }
-    }
-    
-    //------if square wave--------   
-    if (waveform_type==1){  
-        for (int counter=0; counter<length_waveform; counter++){
-            if (counter<=length_waveform*duty_cycle){
-                x=amp/3.3+offset/3.3;
-            }else{
-                x=-1.0*amp/3.3+offset/3.3;
-            }
-            waveform[counter]=(uint16_t) (x*65535);   
-        }           
-    }
-    
-    //------if triangle wave---------  
-    if (waveform_type==2){  
-        for (int counter=0; counter<length_waveform/2; counter++){
-            x=amp/1.65*(2.0*counter/length_waveform-0.5)+offset/3.3;
-            waveform[counter]=(uint16_t) (x*65535);   
-        } 
-        
-        for (int counter=length_waveform/2; counter<length_waveform; counter++){
-            waveform[counter]=waveform[length_waveform-counter-1];
-        } 
-                  
-    }
-}
-
-
-
-void output_waveform_datapoints(){
-    pc.printf("%d",freq);
-    for (int counter=0; counter<length_waveform; counter++){
-        aout.write_u16(waveform[counter]);
-        wait(time_interv);
-    } 
-}
-
-
-void output_waveform_datapoints_timer_ISR(){ 
-        aout.write_u16(waveform[datapoint_counter]);
-        datapoint_counter++;
-        
-        if (datapoint_counter==length_waveform-1){datapoint_counter=0;}
-}
\ No newline at end of file
diff -r 159f38636ed4 -r d21c2da8b290 wave.h
--- a/wave.h	Tue Dec 01 18:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-#ifndef WAVE_H
-#define WAVE_H
-
-void generate_waveform_datapoints(); //Generate data points for the waveform
-void output_waveform_datapoints(); //Output the data points using DAC (pin18)
-void output_waveform_datapoints_timer_ISR();//Output the data points using DAC (pin18)
-
-#endif //WAVE_H
\ No newline at end of file