can version

Dependencies:   mbed MCP4725

Fork of dac_controller2 by Eric Hill

Files at this revision

API Documentation at this revision

Comitter:
dzl5187
Date:
Tue Apr 29 03:13:12 2014 +0000
Parent:
2:c0287d9c2226
Commit message:
final_version

Changed in this revision

DAC/DAC.cpp 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
diff -r c0287d9c2226 -r 29aa7212dda4 DAC/DAC.cpp
--- a/DAC/DAC.cpp	Fri Apr 11 19:53:01 2014 +0000
+++ b/DAC/DAC.cpp	Tue Apr 29 03:13:12 2014 +0000
@@ -34,7 +34,7 @@
         if( p_form == FORM_SQUARE ){
             if( p_freq == FREQ_10HZ ){
                 //generate square wave at 10HZ
-                if( (int)time_us % 1000000 <= 500000){ // T = 100 ms
+                if( (int)time_us % 100000 <= 50000){ // T = 100 ms
                     Vout = p_range;
                 }
                 else 
diff -r c0287d9c2226 -r 29aa7212dda4 main.cpp
--- a/main.cpp	Fri Apr 11 19:53:01 2014 +0000
+++ b/main.cpp	Tue Apr 29 03:13:12 2014 +0000
@@ -2,45 +2,69 @@
 #include "log.h"
 #include "DAC.h"
 #include "mcp4725.h"
+#define GROUP_ID 2
 Timer programTimer;
 //Timer DACC_timer;
 //DigitalOut testLed(LED1);
 Ticker read;
 CAN can2(p30,p29);
+DigitalOut led2(LED2);
 
 void canread() {
     CANMessage msg;
      if(can2.read(msg)) {
-        char command = msg.data[0];
-            switch (command) {
-                 case 1:
-                 DAC_set(3.0, FREQ_1HZ, FORM_SQUARE);
-                 break;
-                 case 2:
-                 DAC_set(3.0, FREQ_1HZ, FORM_SIN);
-                 break;
-                 case 3:
-                 DAC_set(3.0, FREQ_10HZ, FORM_SQUARE);
-                 break;
-                 case 4:
-                 DAC_set(3.0, FREQ_10HZ, FORM_SIN);
-                 break;
-                 case 5:
-                 DAC_start();
-                 break;
-                 case 6:
-                 DAC_stop();
-                 break;
-                 default: break;
-                 }
+        led2 =! led2;
+        if( msg.id == GROUP_ID){
+            logMessage( "To our group: Receive command %d %d %d! \n", msg.data[0],msg.data[1],msg.data[2]);
+            // data[0]: 1 -> on , 0->off
+            // data[1]: 1 -> sine, 0->square
+            // data[2]: 1 -> 1HZ, 0->10HZ
+            //DAC-on
+            if( msg.data[0] == 1){
+                DAC_start();
+                //freq 1HZ
+                if( msg.data[1] == 1){
+                    //sine
+                    if(msg.data[2] == 1){
+                        DAC_set(3.0, FREQ_1HZ,FORM_SIN);
+                        logMessage("Set sine wave, 1hz \n");
+                    }
+                    else{
+                        DAC_set(3.0, FREQ_1HZ,FORM_SQUARE);
+                        logMessage("Set square wave, 1hz \n");
+                    }
+                }
+                //freq 10HZ
+                else{
+                    //sine
+                    if(msg.data[2] == 1){
+                        DAC_set(3.0, FREQ_10HZ,FORM_SIN);
+                        logMessage("Set sine wave, 10hz \n");
+                    }
+                    else{
+                        DAC_set(3.0, FREQ_10HZ,FORM_SQUARE);
+                        logMessage("Set square wave, 10hz \n");
+                    }
                 }
             }
+            //DAC-off
+            else{
+                DAC_stop();
+                logMessage("Turn off !\n");
+            }
+        }
+        else{
+            logMessage("Recevie command to group %d \n",msg.id);
+        }
+    }
+}
 
 int main()
-{
-    read.attach(&canread,1);
+{    
     DAC_set(3.0, FREQ_1HZ, FORM_SQUARE);
-    DAC_start();
+    programTimer.start();  
+    logMessage( "Finish initilized! \n");
+    read.attach(&canread,1);      
     while(1) {
         DAC_generate();
     }