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

180it 2022-04-09 PM 610℃ 0条
/*
基于ATT7022E的三相工频交流电测量
Coder : Farman
Date  : 2018-01-02
*/
 
 
 
 
#include <STC89C5xRC.H>
#include <intrins.h>
#include <stdio.h>
 
 
#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<len; n++)
    {
        SPI_write_byte(data_bytes[n]);  
    }
}
 
 
 
 
char SPI_read_byte()
{
    char data_byte = 0;
    char n;
 
 
    _SPI_init();
 
 
    for(n=0; n<8; n++)
    {
        ATT7022E_SCLK = 1;
        SPI_delay();
 
 
        data_byte = (data_byte << 1) + (ATT7022E_SDO ? 1 : 0);
        SPI_delay();
 
 
        ATT7022E_SCLK = 0;
        SPI_delay();
    }
 
 
    return data_byte;
}
 
 
void SPI_read_bytes(char data_buf[3])
{
    char n;
 
    
    for(n=0; n<3; n++)
    {
        data_buf[n] = SPI_read_byte();
    }
 
 
    return;
}
 
 
//------------------------------------------------
 
 
 
 
void ATT7022E_write_command_and_data(char command, char data_bytes[3])
{
    ATT7022E_CS = 0;
    SPI_delay();
 
 
    SPI_write_byte(command);
    SPI_write_bytes(data_bytes, 3);
 
    ATT7022E_CS = 1;
    SPI_delay();
}
 
 
 
 
void ATT7022E_read_data(char addr, char data_bytes[3])
{
    ATT7022E_CS = 0;
    SPI_delay();
 
 
    SPI_write_byte(addr);
    SPI_read_bytes(data_bytes);
 
    ATT7022E_CS = 1;
    SPI_delay();
}
 
 
//------------------------------------------------------
 
 
 
 
void ATT7022E_soft_reset()
{
    char db[3] = {0x00, 0x00, 0x00};
    ATT7022E_write_command_and_data(0xD3, db);
}
 
 
 
 
void ATT7022E_calibration_data_reset()
{
    char db[3] = {0x00, 0x00, 0x00};
    ATT7022E_write_command_and_data(0xC3, db);
}
 
 
void ATT7022E_calibration_data_write_enable()
{
    char db[3] = {0x00, 0x00, 0x5A};
    ATT7022E_write_command_and_data(0xC9, db);
}
 
 
void ATT7022E_calibration_data_write_disable()
{
    char db[3] = {0x00, 0x00, 0x00};
    ATT7022E_write_command_and_data(0xC9, db);
}
 
 
void ATT7022E_calibration_data_write()
{
    unsigned int n;
    unsigned char command, db[3];
 
 
    ATT7022E_calibration_data_reset();
    ATT7022E_calibration_data_write_enable();
 
 
    //----------------------------------------
 
    for(n=0; ;n++)
    {
        command = ATT7022E_calibration_data[n*4 + 0];
        db[0]   = ATT7022E_calibration_data[n*4 + 1];
        db[1]   = ATT7022E_calibration_data[n*4 + 2];
        db[2]   = ATT7022E_calibration_data[n*4 + 3];
 
 
        if(command == 0x80)
        {
            break;
        }
        else
        {
            ATT7022E_write_command_and_data(command, db);
        }
    }
 
 
    //----------------------------------------
 
 
    ATT7022E_calibration_data_write_disable();
}
 
 
void ATT7022E_calibration_data_read(char addr, char data_bytes[3])
{
    char db[3] = {0x00, 0x00, 0x5A};
    ATT7022E_write_command_and_data(0xC6, db);
    ATT7022E_read_data(addr, data_bytes);
}
 
 
void ATT7022E_measure_data_read(char addr, char data_bytes[3])
{
    char db[3] = {0x00, 0x00, 0x00};
    ATT7022E_write_command_and_data(0xC6, db);
    ATT7022E_read_data(addr, data_bytes);
}
 
 
 
 
//--------------------------------------------------------------
 
 
void ATT7022_data_to_float()
{
    fV = (V[0] * 256.0 + V[1])/32;
    fI = (I[0] * 256.0 + I[1])/32;
} 
 
 
 
void ATT7022E_main()
{
    char need_calibrate = 0;
 
 
    ST7066_Init();
 
 
    // LOGO 
    ST7066_PutString(0, 0, "ATT7022E  Multimeter");
    ST7066_PutString(1, 0, "      3-Phase       ");
    ST7066_PutString(2, 0, "Design : Farman     ");
    ST7066_PutString(3, 0, "Version: 20180102Tue");   
    Delay1s();
    Delay1s();
    Delay1s();
 
    // Soft reset
    ST7066_PutString(0, 0, "                    ");
    ST7066_PutString(1, 0, " Soft-reseting..... ");
    ST7066_PutString(2, 0, "                    ");
    ST7066_PutString(3, 0, "                    ");
    ATT7022E_soft_reset();
    Delay1s();
 
 
    ST7066_PutString(0, 0, "                    ");
    ST7066_PutString(1, 0, " Calibrating....... ");
    ST7066_PutString(2, 0, "                    ");
    ST7066_PutString(3, 0, "                    ");
    ATT7022E_calibration_data_write();
    Delay1s();
 
 
    ST7066_PutString(0, 0, "Phase  V(V)   I(A)  ");
    ST7066_PutString(1, 0, "  A >   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

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

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