STM32_ADE7758驅(qū)動
- /*
- *ade7758.c
- *
- *Createdon:2014年10月11日
- *Author:Lzy
- */
- //#include
- #include"ade7758.h"
- #include"sys.h"
- #define ADE_CS_PIN PBout(12)
- #define ADE_RCC RCC_APB2Periph_GPIOB
- #define ADE_GPIO GPIOB
- #define ADE_PIN(GPIO_Pin_12)
- unsigned char bWorkModel=0;//工作模式標(biāo)志位 1:校準(zhǔn)模式;0:正常工作模式;
- unsigned char bit_1s=0;//1s鐘標(biāo)志,在時鐘中斷函數(shù)中置位
- static unsigned char divider=1;//電能分頻器,默認(rèn)值為零,視在功率超出一定值時,自動將該值提高
- static unsignedintenergy[9];//用于累加電能值 36
- struct all_data working;//正常工作模式下存放的電參量 95
- static unsignedintvo_buffer[5][3];//用于電壓的積分慮波 36
- static unsignedintio_buffer[5][3];//用于電流的積分慮波 36
- /**
- *功能:片選使能
- */
- void ADE_CS(unsigned char cs)
- {
- ADE_CS_PIN=cs;
- delay_ms(1);
- }
- /**
- *功能:延時函數(shù) 50us
- */
- void ADE_udelay(void)
- {
- delay_ms(1);
- }
- /**
- *功能:通過SPI寫入數(shù)據(jù)至芯片
- *入口參數(shù):
- *buf->數(shù)據(jù)緩沖區(qū)
- *len->數(shù)據(jù)長度
- */
- void ADE_SPIWrite(unsigned char*buf,unsigned charlen)
- {
- SPI2_Write(buf,len);
- }
- /**
- *功能:通過SPI讀芯片數(shù)據(jù)
- *入口參數(shù):len->數(shù)據(jù)長度
- *出口參數(shù):buf->數(shù)據(jù)緩沖區(qū)
- *
- */
- void ADE_SPIRead(unsigned char*buf,unsigned charlen)
- {
- SPI2_Read(buf,len);
- }
- /**
- *功能:7758寫數(shù)據(jù)函數(shù)
- *入口參數(shù):
- *type:目標(biāo)寄存器的地址
- *wdata:寫進(jìn)寄存器的內(nèi)容
- *databit:目標(biāo)寄存器的寬度
- *出口參數(shù):NULL
- *返回值:NULL
- */
- void ADE_Write(unsigned char type,unsignedintwdata,unsigned char databit)
- {
- unsigned char data[4];
- ADE_CS(0);
- type=type|0x80;
- data[0]=type;
- ADE_SPIWrite(data,1);
- ADE_udelay();
- if(databit==8)
- {
- data[0]=wdata;
- ADE_SPIWrite(data,1);
- }
- elseif(databit==16)
- {
- data[0]=(wdata&0xff00)>>8;/*高8位*/
- data[1]=(wdata&0x00ff);/*底8位*/
- ADE_SPIWrite(data,2);
- }
- elseif(databit==24)
- {
- data[0]=(wdata&0xff0000)>>16;/*高8位*/
- data[1]=(wdata&0xff00)>>8;
- data[2]=(wdata&0xff);
- ADE_SPIWrite(data,3);
- }
- else
- printf("ADE write databit Error:%dn",databit);
- ADE_CS(1);
- }
- /**
- *功能:7758讀寄存器函數(shù)
- *入口參數(shù):
- * type:目標(biāo)寄存器的地址
- *databit:目標(biāo)寄存器的寬度
- *出口參數(shù):指定寄存器的內(nèi)容
- *返回值:指定寄存器的內(nèi)容
- */
- unsignedintADE_Read(unsigned char type,unsigned char databit)
- {
- unsigned char data[4]={0,0,0,0};
- unsignedintrtdata=0;
- ADE_CS(0);
- type=type&0x7F;
- data[0]=type;
- ADE_SPIWrite(data,1);
- ADE_udelay();
- if(databit==8)
- {
- ADE_SPIRead(data,1);
- rtdata=data[0];
- }
- elseif(databit==12)
- {
- ADE_SPIRead(data,2);
- rtdata=(data[0]&0x0f)<<8;
- rtdata+=data[1];
- }
- elseif(databit==16)
- {
- ADE_SPIRead(data,2);
- rtdata=data[0]<<8;
- rtdata+=data[1];
- }elseif(databit==24)
- {
- ADE_SPIRead(data,3);
- rtdata=data[0]<<16;
- rtdata+=(data[1]<<8);
- rtdata+=data[2];
- }
- else
- printf("ADE Read databit Error:%dn",databit);
- ADE_CS(1);
- return(rtdata);
- }
- /**
- *功能:檢測異常
- */
- void ADE_AuCheck(void)
- {
- unsigned char i;
- unsignedinttemp_data[5];//存放運算過程的中間變量
- unsignedinttemp_v,temp_i;
- //自動檢測ADE7758是否出現(xiàn)異常
- if(working.voltage[0]>ERR_VOLTAGE||
- working.voltage[1]>ERR_VOLTAGE||
- working.voltage[2]>ERR_VOLTAGE)
- {
- //ADE_Check7758();
- printf("ADE Errorn");
- }
- //自動設(shè)置分頻器的大小
- for(i=0;i<3;i++)
- {
- temp_v=working.voltage[i];
- temp_i=working.current[i];
- temp_data[i]=((temp_v*temp_i)/DIVI_VALUE)&0x000000ff;
- }
- temp_data[3]=(temp_data[0]>temp_data[1])?
- ((temp_data[0]>temp_data[2])?temp_data[0]:temp_data[2]):
- ((temp_data[1]>temp_data[2])?temp_data[1]:temp_data[2]);
- if(divider!=(char)temp_data[3])
- {
- //writetoade7758
- divider=(char)temp_data[3]+1;
- for(i=0;i<3;i++)
- ADE_Write(ADD_WDIV+i,((int)divider<<8),8);
- }
- }
- /**
- *功能:每秒讀取功率
- */
- void ADE_ReadHR(void)
- {
- unsigned char i;
- unsignedinttemp_data[9];//存放運算過程的中間變量
- //有功
- temp_data[ADD_AWATTHR-1]=ADE_Read(ADD_AWATTHR,16);
- temp_data[ADD_BWATTHR-1]=ADE_Read(ADD_BWATTHR,16);
- temp_data[ADD_CWATTHR-1]=ADE_Read(ADD_CWATTHR,16);
- //無功
- temp_data[ADD_AVARHR-1]=ADE_Read(ADD_AVARHR,16);
- temp_data[ADD_BVARHR-1]=ADE_Read(ADD_BVARHR,16);
- temp_data[ADD_CVARHR-1]=ADE_Read(ADD_CVARHR,16);
- //視在
- temp_data[ADD_AVAHR-1]=ADE_Read(ADD_AVAHR,16);
- temp_data[ADD_BVAHR-1]=ADE_Read(ADD_BVAHR,16);
- temp_data[ADD_CVAHR-1]=ADE_Read(ADD_CVAHR,16);
- for(i=0;i<9;i++)
- {
- if(temp_data[i]>0x7fff)
- temp_data[i]=0xffff-temp_data[i]+1;
- }
- if(divider>1)
- {
- for(i=0;i<9;i++)
- temp_data[i]=temp_data[i]*divider;//乘上分頻器的值
- }
- //能量的計算
- for(i=0;i<9;i++)
- energy[i]+=temp_data[i];//累加電能值,單位為 WS(瓦秒)
- //轉(zhuǎn)換成千瓦時
- for(i=0;i<3;i++)
- {
- working.watt_hour[i]+=(energy[i]/3600000);//轉(zhuǎn)換成千瓦時
- energy[i]=energy[i]%3600000;
- }
- working.watt_hour[3]=working.watt_hour[0]+working.watt_hour[1]+working.watt_hour[2];//總和
- //轉(zhuǎn)換成千伏安時
- for(i=0;i<3;i++)
- {
- working.va_hour[i]+=(energy[i+6]/3600000);//轉(zhuǎn)換成千瓦時
- energy[i+6]=energy[i+6]%3600000;
- }
- working.va_hour[3]=working.va_hour[0]+working.va_hour[1]+working.va_hour[2];//總和
- for(working.watt[3]=0,i=0;i<3;i++)
- {
- working.watt[i]=temp_data[i]/1000;//千瓦
- working.watt[3]+=working.watt[i];
- }
- for(working.var[3]=0,i=0;i<3;i++)
- {
- working.var[i]=temp_data[i+3]/1000;
- working.var[3]+=working.var[i];
- }
- for(working.va[3]=0,i=0;i<3;i++)
- {
- working.va[i]=temp_data[i+6]/1000;//千伏安
- if(working.va[i]
- working.va[i]=working.watt[i];
- working.va[3]+=working.va[i];
- }
- }
- /**
- *功能:實時讀取電流電壓值
- */
- void ADE_ReadVC(void)
- {
- unsigned char i,j;
- for(i=0;i<3;i++)
- {
- working.voltage[i]=0;
- working.current[i]=0;
- }
- for(i=0;i<3;i++)
- {
- for(j=0;j<5;j++)
- {
- working.voltage[i]+=vo_buffer[j][i];
- working.current[i]+=io_buffer[j][i];
- }
- }
- for(i=0;i<3;i++)
- {
- working.voltage[i]=working.voltage[i]/5;
- working.current[i]=working.current[i]/5;
- }
- //電壓電流的三相平均值
- working.voltage[3]=(working.voltage[0]+working.voltage[1]+working.voltage[2])/3;
- working.current[3]=(working.current[0]+working.current[1]+working.current[2])/3;
- printf("voltage=%x current=%xn",working.voltage[0],working.current[0]);
- }
- /**
- *功能:從ADE7758中取出三相電壓電流功率等電參量
- */
- void ADE_Update(void)
- {
- static unsigned char sample_cycle=0;//電壓采樣周期,5次取平均
- static unsigned char bit_3s=0;
- unsigned char j;
- if(!bWorkModel)//正常工作模式
- {
- if(bit_1s)
- {
- bit_1s=0;
- ADE_ReadHR();
- if((bit_3s++)>=3)/*三秒檢測一次異常*/
- {
- //ADE_AuCheck();
- bit_3s=0;
- }
- }
- for(j=0;j<3;j++)
- {
- vo_buffer[sample_cycle][j]=ADE_Read(ADD_AVRMS+j,24)/*>>12*/;//voltage
- io_buffer[sample_cycle][j]=ADE_Read(ADD_AIRMS+j,24)/*>>13*/;//current
- }
- if(sample_cycle==4)/*讀取5次取平均值*/
- ADE_ReadVC();
- }
- if(sample_cycle<4)
- sample_cycle+=1;
- else
- sample_cycle=0;
- }
- /**
- *測試硬件連接是否正確
- */
- u8 ADE_TestHard(void)
- {
- unsignedintrdata,wdata=0xaa5577;//AEHF=1,VAEHF=1,低8位無用
- u8 ret=0;
- ADE_Write(ADD_MASK,wdata,24);
- rdata=ADE_Read(ADD_MASK,24);//驗證通訊是否有問題
- if(rdata!=wdata)
- printf("ADE errorrn");
- else
- {
- ret=1;
- printf("ADE OKrn");
- }
- return ret;
- }
- /**
- *功能:7758初始化函數(shù)
- */
- void ADE_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(ADE_RCC,ENABLE);
- GPIO_InitStructure.GPIO_Pin=ADE_PIN;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽輸出
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度為50MHz
- GPIO_Init(ADE_GPIO,&GPIO_InitStructure);
- ADE_CS(1);
- if(ADE_TestHard())
- {
- ADE_Write(ADD_OPMODE,0x44,8);//軟件復(fù)位
- ADE_udelay();//添加延時 確保復(fù)位成功
- }
- }
- void ADE_thread_entry(void)
- {
- SPI2_Init();
- ADE_Init();
- while(1)
- {
- ADE_Update();
- delay_ms(50);/*等待,讓出cpu權(quán)限,切換到其他線程*/
- }
- }
評論