- /*
- %%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%
- Features: ATT7053BU.C
- Description: NONE
- Author: 小ARM菜菜
- Time: 2013/04
- Version: NONE
- Q Q :925295580
- %%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%
- */
- #include <PL3201_Addr_Map.h>
- #include "ATT7053BU.h"
- sbit ATT7053BU_REST=P3^2;
- sbit AT_CS=P2^2; //AT93C56
- sbit SCLK=P1^5 ;
- sbit SDI=P1^4;//sbit SDI=P1^6;
- sbit SDO=P1^7;
- sbit CS=P2^1;
- //如有疑问请参阅王建华的PCB之终端板为依据
- //2013年
- //小ARM菜菜
- void delay1ms(uint32_t t)
- {
- unsigned char a,b;
- while(t)
- {t--;
- for(b=44;b>0;b--)
- for(a=53;a>0;a--);
- }
- }
- void delay10us(void)
- {
- unsigned char a;
- for(a=22;a>0;a--);
- }
- static void ATT7035BU_Write(uint8_t addr,uint32_t dat)
- {
- unsigned char i,j;
- uint8_t temp[3];
- addr|=0x80; //写所以要置高位为1
- dat&=0x00ffffff; //强制首位字节为0,因为无效,只有24个比特,3个字节有效
- temp[2] = dat; //低8为
- dat>>=8 ;
- temp[1] = dat; //中8为
- dat>>=8 ;
- temp[0] = dat; //高8为
-
- SCLK=0;
- CS=0;
- AT_CS=0;//must be 0!
-
- //写命令
- for (i = 0; i < 8; i++)
- {
- if (addr & 0x80)
- SDI=1;
- else
- SDI=0;
- addr <<= 1;
- delay10us();
- SCLK=1;
- delay10us();
- SCLK=0;
- delay10us();
- }
- //写应用数据,从高位开始
- for(j=0;j<3;j++)
- {
- for (i = 0; i < 8; i++)
- {
- if (temp[j] & 0x80)
- SDI=1;
- else
- SDI=0;
- temp[j] <<= 1;
- delay10us();
- SCLK=1;
- delay10us();
-
- SCLK=0;
- delay10us();
- }
- }
-
-
- CS=1;
- SCLK=1;
-
-
- }
-
- static uint32_t ATT7035BU_Read(uint8_t addr)
- {
- unsigned char i,j;
- uint32_t temp=0;
- uint8_t dat;
-
- addr&=0x7f;//高位强制为0.
- SCLK=0;
- CS=0;
- AT_CS=0;//must be 0!
- //写ADDR
- for (i = 0; i < 8; i++)
- {
- if (addr & 0x80)
- SDI=1;
- else
- SDI=0;
- addr <<= 1;
- delay10us();
- SCLK=1;
- delay10us();
- SCLK=0;
- delay10us();
- }
-
- //读取数据
- for(j=0;j<3;j++)
- {
-
-
- for (i = 0; i < 8; i++)
- {
- if (dat & 0x80)
- SDI=1;
- else
- SDI=0;
- dat <<= 1;
- delay10us();
- SCLK=1;
- delay10us();
- if (SDO==1)
- dat |= 0x01;
- else
- dat &= 0xfe;
- SCLK=0;
- delay10us();
- }
- temp|=dat ;//接收
- temp<<=8; //next
-
- // val[i]= dat;//val[0]高位24,val[2]地位0,
-
- }
-
- CS=1;
- SCLK=1;
- return temp; //返回数据
- }
- void Init_ATT7053BU(void)
- {
- /*
- reset ATTA7053B
- */
- ATT7053BU_REST=0;
- delay1ms(5) ;
- ATT7053BU_REST=1;
- }
- extern void SendOneByte(unsigned char c) ;
- char Read_AC_Frequency (void)
- {
- uint32_t xdata temp;
- uint16_t xdata l,m;
- /*
- Read the frequency of the alternating current
- */
- temp = ATT7035BU_Read( Freq_U);
-
- m = temp>>16 ;
- m&=0x00ff;
- l= temp>>8 ;
- l&=0x00ff;
- m<<=8;
- m=m+l;
- return (500000/m);
- }
- uint16_t Read_AC_voltage_RMS(void)
- {
- uint32_t xdata temp;
- /*
- Calculating AC voltage values, voltage returns a hexadecimal value,
- of course, necessary in the case of digital filtering should be considered
- by 小ARM 菜菜
- */
- temp = ATT7035BU_Read( Rms_U );
- temp>>=8;
- return (temp/k);//计算电压
-
-
- }
- uint32_t Read_AC_Current (uint8_t chanl)
- {
-
- uint32_t temp;
-
- if(chanl==1)
- {
- ATT7035BU_Write(WPREG,OWP_40);
- ATT7035BU_Write( ANAEN,OPEN_ALL_ADC);
- ATT7035BU_Write( EMUCFG,MEASUREMENT_CHANNEL_1 ); //计量通道choose
- ATT7035BU_Write(WPREG,CWP_40);
- temp = ATT7035BU_Read( Rms_I1 ); //通道2电流值
- temp>>=8;
- temp=KI*temp ;
- return temp;
-
- }
- else
- if(chanl==2)
- {
- ATT7035BU_Write(WPREG,OWP_40);
- ATT7035BU_Write( ANAEN,OPEN_ALL_ADC);
- ATT7035BU_Write( EMUCFG,MEASUREMENT_CHANNEL_2 ); //计量通道choose
- ATT7035BU_Write(WPREG,CWP_40);
- temp = ATT7035BU_Read( Rms_I2 ); //通道2电流值
- temp>>=8;
- temp=KI*temp ;
- return temp;
- }
-
-
-
- }
- /*
- Calculated active power is actually identified from manual to read data register by 小ARM菜菜
- */
- uint32_t Active_Power_Calculation (uint8_t channl)
- {
-
- uint32_t temp;
-
- if(channl==1)
- {
- temp = ATT7035BU_Read( PowerP1 );//第一通道的有功功率
- return temp>>=8;
- }
- else
- if(channl==2)
- {
-
- temp = ATT7035BU_Read( PowerP2 );//第一通道的有功功率
- return temp>>=8;
- }
-
- }
- /*无功功率测量*/
- uint32_t Reactive_power_measurement(uint8_t channl)
- {
- uint32_t temp;
- if(channl==1)
- {
- temp = ATT7035BU_Read( PowerQ1 );//第一通道的有功功率
- return temp>>=8;
- }
- else
- if(channl==2)
- {
- temp = ATT7035BU_Read( PowerQ2 );//第一通道的有功功率
- return temp>>=8;
-
- }
-
- }
- /*视在功率测量*/
- uint32_t Apparent_power_measurement (uint8_t channl)
- {
- uint32_t temp;
-
- if(channl==1)
- {
- ATT7035BU_Write(WPREG,OWP_40);
- ATT7035BU_Write( ANAEN,OPEN_ALL_ADC);
- ATT7035BU_Write( EMUCFG,MEASUREMENT_CHANNEL_1 ); //计量通道choose
- ATT7035BU_Write(WPREG,CWP_40);
-
- temp = ATT7035BU_Read( Power_S );//第一通道的有功功率
- return temp>>=8;
- }
- else
- if(channl==2)
- {
-
- ATT7035BU_Write(WPREG,OWP_40);
- ATT7035BU_Write( ANAEN,OPEN_ALL_ADC);
- ATT7035BU_Write( EMUCFG,MEASUREMENT_CHANNEL_2 ); //计量通道choose
- ATT7035BU_Write(WPREG,CWP_40);
-
- temp = ATT7035BU_Read( Power_S );//第二通道的有功功率
- return temp>>=8;
- }
-
复制代码
|