基于STC89C52的ATT7022E工频三相交流电压电流测量

/*
基于ATT7022E的三相工频交流电测量


Coder : Farman
Date  : 2018-01-02
*/




#include 
#include 
#include 


#include "Delay.h" 
#include "ST7066.h"


#ifndef ATT7022_H
#define ATT7022_H


sbit ATT7022E_CS   = P2^7;
sbit ATT7022E_IRQ  = P2^6;
sbit ATT7022E_SDI  = P2^5;
sbit ATT7022E_SCLK = P2^4;
sbit ATT7022E_SDO  = P2^3;


unsigned char ATT7022E_string[24];
unsigned char V[3] = {0x05,0x55,0x55}, I[3]={0x03,0x33,0x33};
float fV = 0.0, fI = 0.0;


code unsigned char ATT7022E_calibration_data[] = {
    0x01|0x80, 0x00, 0xBA, 0x7F, // Mode


    0x02|0x80, 0x00, 0x00, 0x00, // ADC gain


    0x03|0x80, 0x00, 0x09, 0x08, // EMU


    0x04|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x05|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x06|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x07|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x08|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x09|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x0A|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x0B|0x80, 0x00, 0x00, 0x00, // Power gain offset
    0x0C|0x80, 0x00, 0x00, 0x00, // Power gain offset


    0x0D|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x0E|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x0F|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x10|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x11|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x12|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x61|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x62|0x80, 0x00, 0x00, 0x00, // Phase calibration
    0x63|0x80, 0x00, 0x00, 0x00, // Phase calibration


    0x13|0x80, 0x00, 0x00, 0x00, // Power offset
    0x14|0x80, 0x00, 0x00, 0x00, // Power offset
    0x15|0x80, 0x00, 0x00, 0x00, // Power offset
    0x21|0x80, 0x00, 0x00, 0x00, // Power offset
    0x22|0x80, 0x00, 0x00, 0x00, // Power offset
    0x23|0x80, 0x00, 0x00, 0x00, // Power offset
    0x64|0x80, 0x00, 0x00, 0x00, // Power offset
    0x65|0x80, 0x00, 0x00, 0x00, // Power offset
    0x66|0x80, 0x00, 0x00, 0x00, // Power offset
    0x67|0x80, 0x00, 0x00, 0x00, // Power offset
    0x68|0x80, 0x00, 0x00, 0x00, // Power offset
    0x69|0x80, 0x00, 0x00, 0x00, // Power offset


    0x16|0x80, 0x00, 0x00, 0x00, // Base wave no-power phase calibration


    0x17|0x80, 0x00, 0xFA, 0x00, // Voltage gain calibration
    0x18|0x80, 0x00, 0xFA, 0x00, // Voltage gain calibration
    0x19|0x80, 0x00, 0xFA, 0x00, // Voltage gain calibration


    0x1A|0x80, 0x00, 0x00, 0x00, // Current gain calibration
    0x1B|0x80, 0x00, 0x00, 0x00, // Current gain calibration
    0x1C|0x80, 0x00, 0x00, 0x00, // Current gain calibration
    0x20|0x80, 0x00, 0x00, 0x00, // Current gain calibration


    0x1D|0x80, 0x00, 0x00, 0x00, // Start-up current


    0x1E|0x80, 0x00, 0x0A, 0x00, // High-frequency pulse constant


    0x1F|0x80, 0x00, 0x00, 0x00, // Fault-voltage gauge


    0x24|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x25|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x26|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x27|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x28|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x29|0x80, 0x00, 0x00, 0x00, // Valid value offset
    0x6A|0x80, 0x00, 0x00, 0x00, // Valid value offset


    0x2A|0x80, 0x00, 0x00, 0x00, // ADC offset
    0x2B|0x80, 0x00, 0x00, 0x00, // ADC offset
    0x2C|0x80, 0x00, 0x00, 0x00, // ADC offset
    0x2D|0x80, 0x00, 0x00, 0x00, // ADC offset
    0x2E|0x80, 0x00, 0x00, 0x00, // ADC offset
    0x2F|0x80, 0x00, 0x00, 0x00, // ADC offset


    0x30|0x80, 0x00, 0x00, 0x00, // IRQ mask


    0x31|0x80, 0x00, 0x3C, 0xFF, // Analog module


    0x32|0x80, 0x00, 0x00, 0x00, // All channel gain

    
    0x33|0x80, 0x00, 0x00, 0x00, // Pulse double


    0x34|0x80, 0x00, 0x2C, 0x59, // Base wave gain


    0x35|0x80, 0x00, 0x00, 0x0F, // I/O config


    0x36|0x80, 0x00, 0x00, 0x00, // Power start


    0x37|0x80, 0x00, 0x00, 0x00, // Phase offset area
    0x60|0x80, 0x00, 0x00, 0x00, // Phase offset area


    0x38|0x80, 0x00, 0x00, 0x00, // SAG cycle length


    0x39|0x80, 0x00, 0x13, 0x72, // SAG level


    0x71|0x80, 0x00, 0x5F, 0x71, // Over-current level

    
    0x6B|0x80, 0x00, 0x00, 0x00, // Temperature offset


    //0x6C|0x80, 0x00, 0x00, 0x00, // Temperature gain , not need to set


    0x6D|0x80, 0x00, 0xFF, 0x00, // VREF gain
    0x6E|0x80, 0x00, 0x00, 0xB8, // VREF gain
    0x6F|0x80, 0x00, 0xD1, 0xDA, // VREF gain


    0x6F|0x70, 0x00, 0x02, 0x1E, // Algorithm config


    0x00|0x80, 0x00, 0x00, 0x00  // End of data table
};


void Debug1(unsigned char ch)
{
    sprintf(ATT7022E_string,"DEBUG1 : 0x        ");


    ATT7022E_string[11] = (ch/16)>9 ? (ch/16)-10+'A' : (ch/16) + '0';
    ATT7022E_string[12] = (ch%16)>9 ? (ch%16)-10+'A' : (ch%16) + '0';

    ATT7022E_string[13] = 0x00; 


    ST7066_PutString(2, 0, ATT7022E_string);
}


void Debug3(unsigned char ch[3])
{
    sprintf(ATT7022E_string,"DEBUG3: 0x            ");


    ATT7022E_string[11] = (ch[0]/16)>9 ? (ch[0]/16)-10+'A' : (ch[0]/16) + '0';
    ATT7022E_string[12] = (ch[0]%16)>9 ? (ch[0]%16)-10+'A' : (ch[0]%16) + '0';
    ATT7022E_string[13] = ' '; 
    ATT7022E_string[14] = (ch[1]/16)>9 ? (ch[1]/16)-10+'A' : (ch[1]/16) + '0';
    ATT7022E_string[15] = (ch[1]%16)>9 ? (ch[1]%16)-10+'A' : (ch[1]%16) + '0';
    ATT7022E_string[16] = ' '; 
    ATT7022E_string[17] = (ch[2]/16)>9 ? (ch[2]/16)-10+'A' : (ch[2]/16) + '0';
    ATT7022E_string[18] = (ch[2]%16)>9 ? (ch[2]%16)-10+'A' : (ch[2]%16) + '0';
    ATT7022E_string[16] = ' ';
    ATT7022E_string[20] = 0x00; 


    ST7066_PutString(3, 0, ATT7022E_string);
}


void Debug_string(char str[])
{
    ST7066_PutString(1, 0, str);
}


//-------------------------------------------------




void SPI_delay()
{
    _nop_();
}


void _SPI_init()
{
    ATT7022E_SCLK = 0;
    SPI_delay();
}




void SPI_write_byte(char data_byte)
{
    unsigned char mask = 0x80;
    char n;


    _SPI_init();


    for(n=0; n<8; n++)
    {
        ATT7022E_SDI = (mask & data_byte) ? 1 : 0;
        SPI_delay();


        ATT7022E_SCLK = 1;
        SPI_delay();


        ATT7022E_SCLK = 0;
        SPI_delay();

        mask >>= 1;  
    }
}


void SPI_write_bytes(char data_bytes[], char len)
{
    char n;


    for(n=0; n   0.0   0.00  ");
    ST7066_PutString(2, 0, "  B >   0.0   0.00  ");
    ST7066_PutString(3, 0, "  C >   0.0   0.00  ");


    while(1)
    {
        // Phase A
        ATT7022E_measure_data_read(0x0D, V);
        ATT7022E_measure_data_read(0x10, I);
        ATT7022_data_to_float();


        if (V[0] < 0x7D)
        {
            sprintf(ATT7022E_string,"  A > %5.1f  %5.2f  ", fV, fI);
        }
        else
        {
            sprintf(ATT7022E_string,"A: ERROR!Check Cable");
            need_calibrate = 5;
        }


        ST7066_PutString(1, 0, ATT7022E_string);


        // Phase B
        ATT7022E_measure_data_read(0x0E, V);
        ATT7022E_measure_data_read(0x11, I);
        ATT7022_data_to_float();

        if (V[0] < 0x7D)
        {
            sprintf(ATT7022E_string,"  B > %5.1f  %5.2f  ", fV, fI);
        }
        else
        {
            sprintf(ATT7022E_string,"B: ERROR!Check Cable");
            need_calibrate = 5;
        }


        ST7066_PutString(2, 0, ATT7022E_string);


        // Phase C
        ATT7022E_measure_data_read(0x0F, V);
        ATT7022E_measure_data_read(0x12, I); 
        ATT7022_data_to_float();

        if (V[0] < 0x7D)
        {
            sprintf(ATT7022E_string,"  C > %5.1f  %5.2f  ", fV, fI);
        }
        else
        {
            sprintf(ATT7022E_string,"C: ERROR!Check Cable");
            need_calibrate = 5;
        }

        ST7066_PutString(3, 0, ATT7022E_string);


        if(need_calibrate)
        {
            ATT7022E_calibration_data_write();
            need_calibrate--;
        }


        Delay200ms();
    }
}


#endif

 

你可能感兴趣的:(ATT7022E,STC89C52RC)