#include <htc.h>
#include <pic18f4523.h>
#include <pic18.h>
#include "v_def.h"
#include "att7022.h"
#include <Math.h>
#include "meter.h"
#include "storage.h"
/*校表参数*/
u8 common_buf[256];
extern const u32 correct_value[];
u32 att7022_chksum;
void delay(u32 s)
{
while(s-->0)
NOP();
}
void att_init(void)
{
PORTA = 0xff;
LATA = 0xff;
TRISA &= 0xf0;
TRISA |= 0x01;
TRISB &= ~(1 << 5);
TRISB |= (1 << 4);
}
u32 ATTRead(u8 addr)
{
s8 n = 0;
u32 result = 0;
u32 h = 0;
CS = 1;
SCLK = 0;
CS = 0;
delay(SPI_SPEED);
for(n = 0;n < 8;n++)
{
SCLK = 1;
if(((addr >> (7-n))&0x01))
{
DIN = 1;
}
else
{
DIN = 0;
}
delay(SPI_SPEED);
SCLK = 0;
delay(SPI_SPEED);
}
delay(SPI_SPEED*3);
for(n = 0;n <= 23;n++)
{
SCLK = 1;
delay(SPI_SPEED);
SCLK = 0;
delay(SPI_SPEED);
if(DOUT == 1)
h = 1;
else
h = 0;
result = (result << 1)|h;
delay(SPI_SPEED);
}
CS = 1;
return result;
}
void ATTWrite(u8 addr,u32 data)
{
s8 n = 0;
CS = 1;
SCLK = 0;
CS = 0;
delay(SPI_SPEED);
for(n = 0;n < 8;n++)
{
SCLK = 1;
DIN = ((addr >> (7-n))&0x01);
delay(SPI_SPEED);
SCLK = 0;
delay(SPI_SPEED);
}
for(n = 23;n >= 0;n--)
{
SCLK = 1;
delay(SPI_SPEED_H);
DIN = (data >> n)&0x01;
delay(SPI_SPEED_H);
SCLK = 0;
delay(SPI_SPEED);
}
CS = 1;
}
void att7022_reset(void)
{
ATT_RESET = 0;
delay_ms(500);
ATT_RESET = 1;
delay_ms(500);
}
void get_correct_value(void)
{
u32 i;
for(i = 0;i < sizeof(Correct_Type);i++)
{
common_buf[i] = FLASH_READ((u8*)correct_value + i);
}
}
void write_correct_to_att7022(void)
{
Correct_Type *correct = (Correct_Type*)common_buf;
ATTWrite(w_CorrectEn,SET_ZERO); //允许写操作
delay_ms(50);
ATTWrite( w_ClearCorrect,SET_ZERO); //清校表寄存器
delay_ms(50);
ATTWrite( w_HFConst,correct->HFConst);
ATTWrite( w_UADCPga,PGAIN_FOUR); //电压倍率4
ATTWrite( w_EAddMode,correct->EAddMode); //合相累加模式选择
//功率校正 pf = 1
ATTWrite( w_PgainA0,correct->PgainA0); //校正a功率
ATTWrite( w_PgainA1,correct->PgainA1); //校正a功率
ATTWrite( w_PgainB0,correct->PgainB0); //校正b功率
ATTWrite( w_PgainB1,correct->PgainB1); //校正b功率
ATTWrite( w_PgainC0,correct->PgainC0); //校正c功率
ATTWrite( w_PgainC1,correct->PgainC1); //校正c功率
//功率校正 pf = 1
ATTWrite( w_PhsregA0,correct->PhsregA0); //校正a相位
ATTWrite( w_PhsregB0,correct->PhsregB0); //校正b相位
ATTWrite( w_PhsregC0,correct->PhsregC0); //校正c相位
//电压,电流校正
ATTWrite( w_UgainA,correct->UgainA); //校正a电压
ATTWrite( w_UgainB,correct->UgainB); //校正b电压
ATTWrite( w_UgainC,correct->UgainC); //校正c电压
ATTWrite( w_IgainA,correct->IgainA); //校正a电流
ATTWrite( w_IgainB,correct->IgainB); //校正b电流
ATTWrite( w_IgainC,correct->IgainC); //校正c电流
ATTWrite( w_EnLineFreq,correct->EnLineFreq); //视在能量选择
ATTWrite( w_Istartup,correct->Istartup); //启动电流设置
ATTWrite( w_CorrectEn,WRITE_PROTECT); //bu允许写操作
delay_ms(500);
}
void att7022_Init(void)
{
// u8 i = 0;
// for(i = 0;i < sizeof(Correct_Type);i++)
// common_buf[i] = FLASH_READ(METER_PARA_IAP_ADDRESS+i);
// correct = (Correct_Type *)(common_buf); //get correct data
// correct = (Correct_Type *)(correct_value); //get correct data
att_init();
att7022_reset();
get_correct_value();
write_correct_to_att7022();
CheckCorrect();
}
void correct_init(void)
{
Correct_Type *correct = (Correct_Type *)correct_value;
ATTWrite(w_CorrectEn,SET_ZERO);
delay_ms(100);
/*清校表寄存器*/
ATTWrite(w_ClearCorrect,SET_ZERO);
delay_ms(100);
correct->HFConst = (u32)(5760000000*0.648*0.648*4*UNi*IBi/(UN*IB*P_pulse_const_h));
ATTWrite(w_HFConst,correct->HFConst);
delay_ms(100);
/*电压倍率4*/
ATTWrite(w_UADCPga,0x465501);
/*关闭7022写操作*/
ATTWrite(w_CorrectEn,WRITE_PROTECT);
/*合相能量累加模式 三相四线:1:代数和, 0:绝对值和
三相三线:1:绝对值和, 0:代数和
*/
correct->EAddMode = 1;/*三相四线:1:代数和, 0:绝对值和*/
// correct.EAddMode = 0;/*三相三线:1:绝对值和, 0:代数和*/
/*视在能量累加选择,此值为0x008127选择视在能量累加*/
correct->EnLineFreq = 0x008127;
}