⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 实现了dsPIC30f6012a通过SPC3与PLC通信
💻 C
字号:
//************************************************************************
//File Name:   ADCDAC.c
//Include:     ADC_Datatreat  -- The data processing in AD convert
//             ADC -- AD convert
//             DAC_Datatreat  -- The data processing in DA convert
//             DAC(void) -- DA convert
//Description: Sample from sensor, and convert analog signals to digtal;
//    Finally, output the digtal signals to Actuant(Servo-Valve)
//************************************************************************
#include "Headfiles.h"

//************************************************************************
//Func. Name:        ADC data processing(DP)
//Input variables:   None
//Output variables:  None
//Use sub-func.list: None
//Used by list:      ADCDAC.c -- ADC
//Description:       Process the digtal data for PID control and other...
//    functions. Including software filter, and data processing.
//************************************************************************
void ADC_Datatreat(void)
{
    unsigned int i;
    long int temp_lint;

    //----------------------------------------------Soft filter
    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += P1_Filter[0][i];
    }
    P1_Cylinder_AD[0] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //P1.1
    
/*    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += P1_Filter[1][i];
    }
*/    P1_Cylinder_AD[1] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //P1.2
    
    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += P2_Filter[0][i];
    }
    P2_Cylinder_AD[0] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //P2.1
    
/*    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += P2_Filter[1][i];
    }
*/    P2_Cylinder_AD[1] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //P2.2
    
    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += L_Filter[0][i];
    }
    L_Cylinder_AD[0] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //L1
    
/*    temp_lint = 0;
    for (i = 0; i < AD_FILTER_COUNT; i++)
    {
        temp_lint += L_Filter[1][i];
    }
*/    L_Cylinder_AD[1] = temp_lint >> AD_FILTER_POWERE_COEFFICIENT;  //L2

    //-----------------------------------ADC data processing
    temp_lint = P1_Cylinder_AD[0];
    P1_Cylinder[0] = (temp_lint * Channel_Parameter[0][2]) >> 12; //P1.1
//    temp_lint = P1_Cylinder_AD[1];
//    P1_Cylinder[1] = (temp_lint * Channel_Parameter[1][2]) >> 12; //P1.2
    temp_lint = P2_Cylinder_AD[0];
    P2_Cylinder[0] = (temp_lint * Channel_Parameter[0][3]) >> 12; //P2.1
//    temp_lint = P2_Cylinder_AD[1];
//    P2_Cylinder[1] = (temp_lint * Channel_Parameter[1][3]) >> 12; //P2.2

    temp_lint = L_Cylinder_AD[0];
    L_Cylinder[0] = (temp_lint * Channel_Parameter[0][5]) >> 12;  //L1
//    temp_lint = L_Cylinder_AD[1];
//    L_Cylinder[1] = (temp_lint * Channel_Parameter[1][5]) >> 12;  //L2
    temp_lint = I_Sevovalve_AD[0];
    I_Sevovalve[0] = (temp_lint - 2048) * 13 / 40;    //Ibck1
//    temp_lint = I_Sevovalve_AD[1];
//    I_Sevovalve[1] = (temp_lint - 2048) * 13 / 40;    //Ibck2
}

//************************************************************************
//Func. Name:        ADC (Analog to digtal converte)
//Input variables:   None
//Output variables:  None
//Use sub-func.list: ADCDAC.c -- ADC_Datatreat
//Used by list:      MainFunc.c - TIMER_A (ISR)
//Description:       Sample from sensor, and convert the analog signals...
//    to digtal ones. Including 3-point sample function.
//    The meanings of fllowing words:
//      P1.1 -- The bottom cavum without rod of cylinder No.1;
//      P1.2 -- The top cavum with rod of cylinder No.2;
//      L1   -- The position of cylinder No.1;
//      Ibck1-- feedback current of servo valve No.1;
//      P2.1, P2.2, L2, Ibck2 -- the cylinder & servo valve No.2
//         ----------------------
//        | MSP430       P6.0/A0 |59pin<--P1.1
//        |  F167        P6.1/A1 |60pin<--P1.2
//        |              P6.2/A2 |61pin<--L1
//        |              P6.3/A3 |2pin<--Ibck1
//        |                      |
//        |              P6.4/A4 |3pin<--P2.1
//        |              P6.5/A5 |4pin<--P2.2
//        |               VeREF+ |10pin<--L2
//        |         VREF-/VeREF- |11pin<--Ibck2
//************************************************************************
void ADC(void)
{
    //-----------------------------------3-point sample

    _ADCSAMPSTART();
    while (!ADCON1bits.DONE);
    _ADCSAMPOVER();
    P1_sample_recorder[0][0] = ADCBUF2;  //P1.1
    P2_sample_recorder[0][0] = ADCBUF3;  //P2.1
    L_sample_recorder[0][0] = ADCBUF1;   //L1
    I_Sevovalve_AD[0] = ADCBUF0;         //I1  -- 1-point
    
    _ADCSAMPSTART();
    while (!ADCON1bits.DONE);
    _ADCSAMPOVER();
    P1_sample_recorder[0][1] = ADCBUF2;  //P1.1
    P2_sample_recorder[0][1] = ADCBUF3;  //P2.1
    L_sample_recorder[0][1] = ADCBUF1;   //L1
    
    _ADCSAMPSTART();
    while (!ADCON1bits.DONE);
    _ADCSAMPOVER();
    P1_sample_recorder[0][2] = ADCBUF2;  //P1.1
    P2_sample_recorder[0][2] = ADCBUF3;  //P2.1
    L_sample_recorder[0][2] = ADCBUF1;   //L1

    //------------------------------------The pointer for data processing
    //plus one means point the next cell of the array
    Filter_Ptr++;
    if (Filter_Ptr > (AD_FILTER_COUNT - 1))
    {
        Filter_Ptr = 0;
    }

    //-----------------------------3-point sample & covert data Computing
    //Find out the middle one from 3 data, and make it to data processing
    //P1.1
    P1_Filter[0][Filter_Ptr] = Middle_Value(P1_sample_recorder[0][0],
                                            P1_sample_recorder[0][1],
                                            P1_sample_recorder[0][2]);
    //P1.2
//    P1_Filter[1][Filter_Ptr] = Middle_Value(P1_sample_recorder[1][0],
//                                            P1_sample_recorder[1][1],
//                                            P1_sample_recorder[1][2]);
    //P2.1
    P2_Filter[0][Filter_Ptr] = Middle_Value(P2_sample_recorder[0][0],
                                            P2_sample_recorder[0][1],
                                            P2_sample_recorder[0][2]);
    //P2.2
//    P2_Filter[1][Filter_Ptr] = Middle_Value(P2_sample_recorder[1][0],
//                                            P2_sample_recorder[1][1],
//                                            P2_sample_recorder[1][2]);
    //L1
    L_Filter[0][Filter_Ptr] = Middle_Value(L_sample_recorder[0][0],
                                           L_sample_recorder[0][1],
                                           L_sample_recorder[0][2]);
    //L2
//    L_Filter[1][Filter_Ptr] = Middle_Value(L_sample_recorder[1][0],
//                                           L_sample_recorder[1][1],
//                                           L_sample_recorder[1][2]);

    ADC_Datatreat();        //ADC data processing

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -