for learning

Dependencies:   mbed FastPWM

Revision:
11:82d8768d7351
Parent:
10:83a6baa77a2e
Child:
12:6f2531038ea4
diff -r 83a6baa77a2e -r 82d8768d7351 main.cpp
--- a/main.cpp	Fri Aug 23 01:02:32 2019 +0000
+++ b/main.cpp	Fri Aug 23 12:28:08 2019 +0000
@@ -5,6 +5,7 @@
 #include "SPI_EEP_ENC.h"
 #include "I2C_AS5510.h"
 #include "setting.h"
+#include "function_utilities.h"
 
 // dac & check ///////////////////////////////////////////
 DigitalOut check(PC_2);
@@ -27,7 +28,7 @@
 DigitalOut eeprom_cs(PB_12);
 SPI enc(PC_12,PC_11,PC_10);
 DigitalOut enc_cs(PD_2);
-DigitalOut indi_led(PA_15);
+DigitalOut LED(PA_15);
 
 // UART ///////////////////////////////////////////
 Serial pc(PA_9,PA_10); //  _ UART
@@ -35,6 +36,10 @@
 // CAN ///////////////////////////////////////////
 CAN can(PB_8, PB_9, 1000000);
 CANMessage msg;
+void onMsgReceived()
+{
+    CAN_RX_HANDLER();
+}
 
 // Variables ///////////////////////////////////////////
 State pos;
@@ -50,7 +55,7 @@
 double V_MAX = 12000.0; // Maximum Voltage : 12V = 12000mV
 
 double PWM_out=0.0;
-
+int ID_index_array[100] = {0};
 // =============================================================================
 // =============================================================================
 // =============================================================================
@@ -61,44 +66,61 @@
     ***     Initialization
     *********************************/
         
-    indi_led = 0;
+    LED = 1;
     pc.baud(9600);
-    
-    //Timer t;
-    //t.start();
-    //t.stop();
-    //pc.printf("The time taken was %f seconds\n",t.read());
 
     // i2c init
     i2c.frequency(400 * 1000);          // 0.4 mHz
     wait_ms(2);                         // Power Up wait
     look_for_hardware_i2c();            // Hardware present
     init_as5510(i2c_slave_addr1);
+    make_delay();
 
 //    // spi init
     eeprom.format(8,3);
     eeprom.frequency(5000000); //5M
     enc.format(8,0);
     enc.frequency(5000000); //5M
+    make_delay();
     
     // ADC init
     Init_ADC();
+    make_delay();
 
     // Pwm init
     Init_PWM();
     TIM4->CR1 ^= TIM_CR1_UDIS;
+    make_delay();
     
-//    //SPI
-//    spi_eeprom_ready();
-//    spi_eeprom_write(0x1,0x112);
-//    spi_eeprom_ready();
-//    int i = spi_eeprom_read(0x1);
+    // TMR3 init
+    Init_TMR3();
+    TIM3->CR1 ^= TIM_CR1_UDIS;
+    make_delay();
 
     // CAN
     can.attach(&CAN_RX_HANDLER);
+    CAN_ID_INIT();
+    make_delay();
     
     // spi _ enc
     spi_enc_set_init();
+    make_delay();
+    
+    //eeprom
+    ROM_INIT_DATA();
+    make_delay();
+    
+    //DAC init
+    dac_1 = PRES_A_VREF/3.3;
+    dac_2 = PRES_B_VREF/3.3;
+    make_delay();
+    
+    for (int i=0; i<100; i++){
+        if(i%2==0)
+            ID_index_array[i] = - i * 0.5;
+        else
+            ID_index_array[i] =  (i+1) * 0.5;
+    }
     
     
     /************************************
@@ -107,16 +129,10 @@
     while(1) {
         
         //spi _ enc
-        int a = spi_enc_read();
+        //int a = spi_enc_read();
+        
         //i2c
         read_field(i2c_slave_addr1);
-
-        check_2=0;
-
-        pc.printf("%f\n",1234567);
-//        pc.printf("%d\n",a1);
-        
-//        wait(0.01f);
     }
 }
 
@@ -138,31 +154,39 @@
         ********************************************************/
         
         if((CNT_TMR4%2)==0){
-            //spi
-    //      eeprom.write(0xff);
-    //      eeprom.write(0xff);
-    //      ready();
-    //      read(1);
-    
-            //i2c
-    ////    read_field(i2c_slave_addr1);
     
             //ADC
             ADC3->CR2  |= 0x40000000;                        // adc _ 12bit
-    //      a1=ADC1->DR;
-    //      a1=ADC2->DR;
-//            int raw_cur = ADC3->DR;
+//          a1=ADC1->DR;
+//          a1=ADC2->DR;
+//          int raw_cur = ADC3->DR;
             while((ADC3->SR & 0b10));
                     
             double alpha_update_cur = 1.0/(1.0+(FREQ_TMR4/2.0)/(2.0*3.14*1000.0)); // f_cutoff : 500Hz
             double cur_new = ((double)ADC3->DR-2048.0)*20.0/4096.0; // unit : mA
             cur.sen=cur.sen*(1.0-alpha_update_cur)+cur_new*(alpha_update_cur);
-
         }
 
         //DAC
 //      dac_1 = ADC1->DR;
 //      dac_2 = ADC2->DR;
+
+        
+        
+        /*******************************************************
+        ***     Timer Counting & etc.
+        ********************************************************/
+        CNT_TMR4++; 
+    }
+    TIM4->SR = 0x0;  // reset the status register
+}
+
+unsigned long CNT_TMR3 = 0;
+double FREQ_TMR3 = (double)FREQ_5k;
+double DT_TMR3 = (double)DT_5k;
+extern "C" void TIM3_IRQHandler(void)
+{
+    if ( TIM3->SR & TIM_SR_UIF ) {
         
         /*******************************************************
         ***     Valve Control 
@@ -199,30 +223,47 @@
         /*******************************************************
         ***     Data Send (CAN) & Print out (UART)
         ********************************************************/
-        if((CNT_TMR4%40)==0){
-            msg.id = 50;
-            msg.len = 4;
-            int temp_CUR = (int)(cur.sen*1000.0);
-            msg.data[0]=0x00FF&temp_CUR;
-            msg.data[1]=0x00FF&(temp_CUR>>8);
-            int temp_PWM = (int)(V_out);
-            msg.data[2]=0x00FF&temp_PWM;
-            msg.data[3]=0x00FF&(temp_PWM>>8);
-            can.write(msg);
+        //if((CNT_TMR3%40)==0){
+//            msg.id = 50;
+//            msg.len = 4;
+//            int temp_CUR = (int)(cur.sen*1000.0);
+//            msg.data[0]=0x00FF&temp_CUR;
+//            msg.data[1]=0x00FF&(temp_CUR>>8);
+//            int temp_PWM = (int)(V_out);
+//            msg.data[2]=0x00FF&temp_PWM;
+//            msg.data[3]=0x00FF&(temp_PWM>>8);
+//            can.write(msg);
+//        }
+
+        if((CNT_TMR3%5000)==0){
+            if(LED==1)
+            {
+                LED=0;
+            }
+            else
+                LED = 1;
+//            LED != LED;
         }
 
-//        if((CNT_TMR4%4000)==0){
-//            pc.printf("%d\n",a1);
-//        }
-
         /*******************************************************
         ***     Timer Counting & etc.
         ********************************************************/
-        CNT_TMR4++; 
+        CNT_TMR3++; 
     }
-    TIM4->SR = 0x0;  // reset the status register
+    TIM3->SR = 0x0;  // reset the status register
 }
 
+/*******************************************************************************
+ *  REFERENCE MODE
+ ******************************************************************************/
+enum _REFERENCE_MODE{
+    MODE_REF_NO_ACT = 0,                                //0 
+    MODE_REF_DIRECT,                                //1
+    MODE_REF_COS_INC,                                  //2
+    MODE_REF_LINE_INC,                                 //3
+    MODE_REF_SIN_WAVE,                                  //4
+    MODE_REF_SQUARE_WAVE,                                  //5
+};
 
 /*******************************************************************************
  *  CONTROL MODE
@@ -252,6 +293,10 @@
     MODE_PRESSURE_SENSOR_NULLING,                       //24
     MODE_PRESSURE_SENSOR_CALIB,                         //25
     MODE_ROTARY_FRICTION_TUNING,                        //26
+    
+    MODE_DDV_POS_VS_PWM_ID = 30,                           //30
+    MODE_DDV_DEADZONE_AND_CENTER,                       //31
+    MODE_DDV_POS_VS_FLOWRATE,                           //32
 };
 
 void ValveControl(unsigned int ControlMode)