这一节我们浏览一下void readMea( unsigned char channel)函数, 该函数功能比较简单,就是等待转化结束后,读取转化结果寄存器的值。
/***************************************功能:读取测量数据****************************************/void readMea(unsigned char channel){ unsigned char dat[4];#ifdef TEST char buf[30];#endif #ifdef BAT_TEST char buf[30];#endif int cap;long int pfAD;int pf; //SEL_A_PHASE; //写页寄存器0x00#ifdef TEST switch(channel) { case M_A_PHASE: SEL_A_PHASE;sprintf(buf,"A PHASE measurrn"); break; case M_B_PHASE: SEL_B_PHASE;sprintf(buf,"B PHASE measurrn"); break; case M_C_PHASE: SEL_C_PHASE; sprintf(buf,"C PHASE measurrn"); break; case M_LX_PHASE: SEL_LX_PHASE; sprintf(buf,"zero PHASE measurrn");break;case M_A_LOADER: SEL_A_LOADER;sprintf(buf,"A loader measurrn"); break; case M_B_LOADER: SEL_B_LOADER;sprintf(buf,"B loader measurrn"); break; case M_C_LOADER: SEL_C_LOADER;sprintf(buf,"C loader measurrn"); break; case M_LX_LOADER: SEL_LX_LOADER;sprintf(buf,"zero loader measurrn"); break; case M_CA_LOADER: SEL_AC_V;sprintf(buf,"Uac measurrn"); break; case M_CB_LOADER: SEL_CB_V;sprintf(buf,"Ucb measurrn"); break; case M_AB_LOADER: SEL_AB_V;sprintf(buf,"Uab measurrn"); break; case M_BAT_VOL: SEL_BAT_VOL; sprintf(buf,"Ubat measurrn"); break;default: return; break;} uartSendArray(buf,strlen(buf));#endif dat[0] = (PAGE_REG<<1)|0x40; dat[1] = 0x00; dat[2] = 0x00; dat[3] = 0x00; Write_CS5464(dat,4); //页寄存器置1if((channel>=M_A_PHASE)&&(channel<=M_LX_PHASE)){ //读取电压 Read_5464(V1RMS_REG<<1,dat);#ifdef TEST sprintf(buf,"Volt:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf));#endifcap = (dat[0]<<8)+dat[1];#ifdef TESTsprintf(buf,"Volt:%d,rn",cap); uartSendArray(buf,strlen(buf)); #endifswitch(channel){ case M_A_PHASE: FTU_TEL_MEA[UaDa].value= (cap - FTU_TEL_SET[UaZSetDa].value)/10000.0 *FTU_TEL_SET[UaMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Ua:%d,rn",FTU_TEL_SET[UaZSetDa].value,FTU_TEL_SET[UaMSetDa].value, FTU_TEL_MEA[UaDa].value);#endifbreak; case M_B_PHASE: FTU_TEL_MEA[UbDa].value= (cap - FTU_TEL_SET[UbZSetDa].value)/10000.0 *FTU_TEL_SET[UbMSetDa].value; #ifdef TEST sprintf(buf,"%d,%d,Ub:%d,rn",FTU_TEL_SET[UbZSetDa].value,FTU_TEL_SET[UbMSetDa].value, FTU_TEL_MEA[UbDa].value);#endifbreak; case M_C_PHASE: FTU_TEL_MEA[UcDa].value= (cap - FTU_TEL_SET[UcZSetDa].value)/10000.0 *FTU_TEL_SET[UcMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Uc:%d,rn", FTU_TEL_SET[UcZSetDa].value,FTU_TEL_SET[UcMSetDa].value, FTU_TEL_MEA[UcDa].value);#endifbreak; case M_LX_PHASE: FTU_TEL_MEA[U0Da].value= (cap - FTU_TEL_SET[U0ZSetDa].value)/10000.0 *FTU_TEL_SET[U0MSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,U0x:%d,rn",FTU_TEL_SET[U0ZSetDa].value,FTU_TEL_SET[U0MSetDa].value,FTU_TEL_MEA[U0Da].value);#endifbreak;default: break;} #ifdef TESTuartSendArray(buf,strlen(buf)); #endif //读取电流 Read_5464(I1RMS_REG<<1,dat);#ifdef TESTsprintf(buf,"Ip:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf));#endif cap = (dat[0]<<8)+dat[1];#ifdef TESTsprintf(buf,"Ip:%d,rn",cap);uartSendArray(buf,strlen(buf));#endif switch(channel){ case M_A_PHASE: FTU_TEL_MEA[IapDa].value= (cap - FTU_TEL_SET[IapZSetDa].value)/10000.0 *FTU_TEL_SET[IapMSetDa].value; #ifdef TEST sprintf(buf,"%d,%d,Iap:%d,rn",FTU_TEL_SET[IapZSetDa].value,FTU_TEL_SET[IapMSetDa].value, FTU_TEL_MEA[IapDa].value);#endifbreak; case M_B_PHASE: FTU_TEL_MEA[IbpDa].value= (cap - FTU_TEL_SET[IbpZSetDa].value)/10000.0 *FTU_TEL_SET[IbpMSetDa].value;#ifdef TESTsprintf(buf,"%d,%d,Ibp:%d,rn",FTU_TEL_SET[IbpZSetDa].value, FTU_TEL_SET[IbpMSetDa].value,FTU_TEL_MEA[IbpDa].value);#endifbreak; case M_C_PHASE: FTU_TEL_MEA[IcpDa].value= (cap - FTU_TEL_SET[IcpZSetDa].value)/10000.0 *FTU_TEL_SET[IcpMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Icp:%d,rn", FTU_TEL_SET[IcpZSetDa].value,FTU_TEL_SET[IcpMSetDa].value,FTU_TEL_MEA[IcpDa].value);#endifbreak; case M_LX_PHASE: FTU_TEL_MEA[I0pDa].value= (cap - FTU_TEL_SET[I0pZSetDa].value)/10000.0 *FTU_TEL_SET[I0pMSetDa].value;#ifdef TESTsprintf(buf,"%d,%d,I0p:%d,rn", FTU_TEL_SET[I0pZSetDa].value,FTU_TEL_SET[I0pMSetDa].value,FTU_TEL_MEA[I0pDa].value);#endifbreak;default: break;} #ifdef TESTuartSendArray(buf,strlen(buf)); #endif//读取视在功率Read_5464(S1_REG<<1,dat); #ifdef TESTsprintf(buf,"S:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf)); #endifpfAD = dat[0];pfAD = (pfAD<<8)+dat[1];pfAD = (pfAD<<8)+dat[2];pfAD = pfAD<<8;pfAD = pfAD/256;pfAD = pfAD/256; #ifdef TESTsprintf(buf,"PAD:%ldrn",pfAD);uartSendArray(buf,strlen(buf)); #endif switch(channel){ case M_A_PHASE: FTU_TEL_MEA[SaDa].value= ((pfAD - FTU_TEL_SET[SaZSetDa].value)/100.0) *FTU_TEL_SET[SaMSetDa].value;FTU_TEL_MEA[PsumDa].value = FTU_TEL_MEA[SaDa].value + FTU_TEL_MEA[SbDa].value+FTU_TEL_MEA[ScDa].value; FTU_TEL_MEA[PFsumDa].value = (FTU_TEL_MEA[PaDa].value+FTU_TEL_MEA[PbDa].value+FTU_TEL_MEA[PcDa].value)*1000.0/FTU_TEL_MEA[PsumDa].value;#ifdef TEST sprintf(buf,"%d,%d,Sa:%drn",FTU_TEL_SET[SaZSetDa].value,FTU_TEL_SET[SaMSetDa].value, FTU_TEL_MEA[SaDa].value);#endifbreak; case M_B_PHASE: FTU_TEL_MEA[SbDa].value= (pfAD - FTU_TEL_SET[SbZSetDa].value)/100.0 *FTU_TEL_SET[SbMSetDa].value;FTU_TEL_MEA[PsumDa].value = FTU_TEL_MEA[SaDa].value + FTU_TEL_MEA[SbDa].value + FTU_TEL_MEA[ScDa].value; FTU_TEL_MEA[PFsumDa].value = (FTU_TEL_MEA[PaDa].value+FTU_TEL_MEA[PbDa].value+FTU_TEL_MEA[PcDa].value)*1000.0/FTU_TEL_MEA[PsumDa].value;#ifdef TESTsprintf(buf,"%d,%d,Sb:%drn", FTU_TEL_SET[SbZSetDa].value,FTU_TEL_SET[SbMSetDa].value,FTU_TEL_MEA[SbDa].value);#endifbreak; case M_C_PHASE: FTU_TEL_MEA[ScDa].value= (pfAD - FTU_TEL_SET[ScZSetDa].value)/100.0 *FTU_TEL_SET[ScMSetDa].value; FTU_TEL_MEA[PsumDa].value = FTU_TEL_MEA[SaDa].value + FTU_TEL_MEA[SbDa].value + FTU_TEL_MEA[ScDa].value;FTU_TEL_MEA[PFsumDa].value = (FTU_TEL_MEA[PaDa].value+FTU_TEL_MEA[PbDa].value+FTU_TEL_MEA[PcDa].value)*1000.0/FTU_TEL_MEA[PsumDa].value;#ifdef TESTsprintf(buf,"%d,%d,Sc:%drn",FTU_TEL_SET[ScZSetDa].value, FTU_TEL_SET[ScMSetDa].value,FTU_TEL_MEA[ScDa].value);#endif break; default: break;}#ifdef TESTuartSendArray(buf,strlen(buf));#endif#ifdef TEST sprintf(buf,"PSUM:%drn",FTU_TEL_MEA[PsumDa].value);uartSendArray(buf,strlen(buf));sprintf(buf,"PFsum:%drn",FTU_TEL_MEA[PFsumDa].value);uartSendArray(buf,strlen(buf));#endif//读取有功功率 Read_5464(P1AVG_REG<<1,dat);#ifdef TESTsprintf(buf,"P:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf));#endif pfAD = dat[0];pfAD = (pfAD<<8)+dat[1];pfAD = (pfAD<<8)+dat[2];pfAD = pfAD<<8;pfAD = pfAD/256;pfAD = pfAD/256; #ifdef TESTsprintf(buf,"PAD:%ldrn",pfAD);uartSendArray(buf,strlen(buf)); #endifswitch(channel){ case M_A_PHASE: FTU_TEL_MEA[PaDa].value= ((pfAD - FTU_TEL_SET[PaZSetDa].value)/100.0) *FTU_TEL_SET[PaMSetDa].value; #ifdef TEST sprintf(buf,"%d,%d,Pa:%drn",FTU_TEL_SET[PaZSetDa].value,FTU_TEL_SET[PaMSetDa].value, FTU_TEL_MEA[PaDa].value);#endifbreak; case M_B_PHASE: FTU_TEL_MEA[PbDa].value= (pfAD - FTU_TEL_SET[PbZSetDa].value)/100.0 *FTU_TEL_SET[PbMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Pb:%drn", FTU_TEL_SET[PbZSetDa].value,FTU_TEL_SET[PbMSetDa].value,FTU_TEL_MEA[PbDa].value);#endifbreak; case M_C_PHASE: FTU_TEL_MEA[PcDa].value= (pfAD - FTU_TEL_SET[PcZSetDa].value)/100.0 *FTU_TEL_SET[PcMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Pc:%drn",FTU_TEL_SET[PcZSetDa].value, FTU_TEL_SET[PcMSetDa].value,FTU_TEL_MEA[PcDa].value);#endifbreak; case M_LX_PHASE: FTU_TEL_MEA[P0Da].value= (pfAD - FTU_TEL_SET[P0ZSetDa].value)/10000.0 *FTU_TEL_SET[P0MSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,P0:%drn",FTU_TEL_SET[P0ZSetDa].value,FTU_TEL_SET[P0MSetDa].value,FTU_TEL_MEA[P0Da].value);#endifbreak;default: break;}#ifdef TESTuartSendArray(buf,strlen(buf));#endif //读取PF1 Read_5464(PF1_REG<<1,dat); #ifdef TESTsprintf(buf,"Pf:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf)); #endifpfAD = dat[0];pfAD = (pfAD<<8)+dat[1];pfAD = (pfAD<<8)+dat[2];pfAD = pfAD<<8;pfAD = pfAD/256;//pfAD = pfAD/256;//pf = ((pfAD/256)/0x7f)*1000; pf = (pfAD/(float)0x7fffff)*1000; #ifdef TEST sprintf(buf,"PF:%d,rn",pf);uartSendArray(buf,strlen(buf)); #endifswitch(channel){ case M_A_PHASE: FTU_TEL_MEA[PFaDa].value = pf; #ifdef TEST sprintf(buf,"PFa:%d,rn", FTU_TEL_MEA[PFaDa].value);#endifbreak; case M_B_PHASE: FTU_TEL_MEA[PFbDa].value = pf; #ifdef TESTsprintf(buf,"PFb:%d,rn",FTU_TEL_MEA[PFbDa].value); #endifbreak; case M_C_PHASE: FTU_TEL_MEA[PFcDa].value = pf; #ifdef TESTsprintf(buf,"PFc:%d,rn",FTU_TEL_MEA[PFcDa].value);#endifbreak; case M_LX_PHASE: FTU_TEL_MEA[PF0Da].value = pf; #ifdef TESTsprintf(buf,"PF0:%d,rn", FTU_TEL_MEA[PF0Da].value);#endifbreak;default: break;} #ifdef TESTuartSendArray(buf,strlen(buf)); #endif //读取无功功率 //写页寄存器0x01 dat[0] = (PAGE_REG<<1)|0x40; dat[1] = 0x00; dat[2] = 0x00; dat[3] = 0x01; Write_CS5464(dat,4); //读取Q1AVG_REG Read_5464(Q1WB_REG<<1,dat); #ifdef TESTsprintf(buf,"Q1:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf)); #endifpfAD = dat[0];pfAD = (pfAD<<8)+dat[1];pfAD = (pfAD<<8)+dat[2];pfAD = pfAD<<8;pfAD = pfAD/256;pfAD = pfAD/256; #ifdef TESTsprintf(buf,"Q:%ld,rn",pfAD);uartSendArray(buf,strlen(buf)); #endifswitch(channel){ case M_A_PHASE: FTU_TEL_MEA[QaDa].value= (pfAD - FTU_TEL_SET[QaZSetDa].value)/100.0 *FTU_TEL_SET[QaMSetDa].value; #ifdef TEST sprintf(buf,"%d,%d,Qa:%d,rn", FTU_TEL_SET[QaZSetDa].value,FTU_TEL_SET[QaMSetDa].value,FTU_TEL_MEA[QaDa].value); #endifbreak; case M_B_PHASE: FTU_TEL_MEA[QbDa].value= (pfAD - FTU_TEL_SET[QbZSetDa].value)/100.0 *FTU_TEL_SET[QbMSetDa].value; #ifdef TEST sprintf(buf,"%d,%d,Qb:%d,rn", FTU_TEL_SET[QbZSetDa].value,FTU_TEL_SET[QbMSetDa].value,FTU_TEL_MEA[QbDa].value); #endifbreak; case M_C_PHASE: FTU_TEL_MEA[QcDa].value= (pfAD - FTU_TEL_SET[QcZSetDa].value)/100.0 *FTU_TEL_SET[QcMSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Qc:%d,rn",FTU_TEL_SET[QcZSetDa].value,FTU_TEL_SET[QcMSetDa].value,FTU_TEL_MEA[QcDa].value); #endifbreak; case M_LX_PHASE: FTU_TEL_MEA[Q0Da].value= (pfAD - FTU_TEL_SET[Q0ZSetDa].value)/10000.0 *FTU_TEL_SET[Q0MSetDa].value; #ifdef TESTsprintf(buf,"%d,%d,Q0:%d,rn", FTU_TEL_SET[Q0ZSetDa].value,FTU_TEL_SET[Q0MSetDa].value,FTU_TEL_MEA[Q0Da].value);#endifbreak;default: break;}#ifdef TESTuartSendArray(buf,strlen(buf));#endif return; } if((channel>=M_A_LOADER)&&(channel<=M_LX_LOADER)) {Read_5464(I1RMS_REG<<1,dat); #ifdef TEST sprintf(buf,"loader:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf)); #endifcap = (dat[0]<<8)+dat[1]; #ifdef TESTsprintf(buf,"loader:%d,rn",cap);uartSendArray(buf,strlen(buf));#endif switch(channel){ case M_A_LOADER: FTU_TEL_MEA[IaDa].value= (cap - FTU_TEL_SET[IaZSetDa].value)/10000.0 *FTU_TEL_SET[IaMSetDa].value; correctZero(&FTU_TEL_MEA[IaDa].value);#ifdef TEST sprintf(buf,"%u,%u,A_loader:%u,rn", FTU_TEL_SET[IaZSetDa].value,FTU_TEL_SET[IaMSetDa].value,FTU_TEL_MEA[IaDa].value);#endifbreak; case M_B_LOADER: FTU_TEL_MEA[IbDa].value= (cap - FTU_TEL_SET[IbZSetDa].value)/10000.0 *FTU_TEL_SET[IbMSetDa].value; correctZero(&FTU_TEL_MEA[IbDa].value);#ifdef TESTsprintf(buf,"%u,%u,B_loader:%u,rn",FTU_TEL_SET[IbZSetDa].value,FTU_TEL_SET[IbMSetDa].value,FTU_TEL_MEA[IbDa].value);#endifbreak; case M_C_LOADER: FTU_TEL_MEA[IcDa].value= (cap - FTU_TEL_SET[IcZSetDa].value)/10000.0 *FTU_TEL_SET[IcMSetDa].value; correctZero(&FTU_TEL_MEA[IcDa].value); #ifdef TESTsprintf(buf,"%u,%u,C_loader:%u,rn",FTU_TEL_SET[IcZSetDa].value,FTU_TEL_SET[IcMSetDa].value, FTU_TEL_MEA[IcDa].value);#endifbreak; case M_LX_LOADER: FTU_TEL_MEA[I0Da].value= (cap - FTU_TEL_SET[I0ZSetDa].value)/10000.0 *FTU_TEL_SET[I0MSetDa].value; correctZero(&FTU_TEL_MEA[I0Da].value);#ifdef TESTsprintf(buf,"%u,%u,LX_loader:%u,rn", FTU_TEL_SET[I0ZSetDa].value,FTU_TEL_SET[I0MSetDa].value,FTU_TEL_MEA[I0Da].value);#endifbreak;default: break;}#ifdef TESTuartSendArray(buf,strlen(buf));#endif return; } if((channel>=M_CA_LOADER)&&(channel<=M_AB_LOADER)) {Read_5464(I2RMS_REG<<1,dat); #ifdef TEST sprintf(buf,"linV:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf)); #endifcap = (dat[0]<<8)+dat[1];#ifdef TESTsprintf(buf,"linV:%dn",cap); uartSendArray(buf,strlen(buf));#endif switch(channel) {case M_CA_LOADER:FTU_TEL_MEA[UcaDa].value=(cap - FTU_TEL_SET[UcaZSetDa].value)/10000.0 * FTU_TEL_SET[UcaMSetDa].value;correctZero(&FTU_TEL_MEA[UcaDa].value);#ifdef TEST sprintf(buf,"%d,%d,Uca:%d,rn", FTU_TEL_SET[UcaZSetDa].value,FTU_TEL_SET[UcaMSetDa],FTU_TEL_MEA[UcaDa].value);#endifbreak;case M_CB_LOADER:FTU_TEL_MEA[UcbDa].value= (cap - FTU_TEL_SET[UcbZSetDa].value)/10000.0 * FTU_TEL_SET[UcbMSetDa].value; correctZero(&FTU_TEL_MEA[UcbDa].value);#ifdef TEST sprintf(buf,"%d,%d,Ucb:%d,rn", FTU_TEL_SET[UcbZSetDa].value,FTU_TEL_SET[UcbMSetDa],FTU_TEL_MEA[UcbDa].value);#endif break;case M_AB_LOADER: FTU_TEL_MEA[UabDa].value= (cap - FTU_TEL_SET[UabZSetDa].value)/10000.0 * FTU_TEL_SET[UabMSetDa].value; correctZero(&FTU_TEL_MEA[UabDa].value);#ifdef TEST sprintf(buf,"%d,%d,Uab:%d,rn", FTU_TEL_SET[UabZSetDa].value,FTU_TEL_SET[UabMSetDa],FTU_TEL_MEA[UabDa].value);#endifbreak;default: break; } #ifdef TEST uartSendArray(buf,strlen(buf));#endifreturn; } if(channel==M_BAT_VOL) { P1 ^=0x20; Read_5464(V1RMS_REG<<1,dat); #ifdef TEST sprintf(buf,"VIRMS:%02x,%02x,%02xrn",(unsigned int)dat[0],(unsigned int)dat[1],(unsigned int)dat[2]); uartSendArray(buf,strlen(buf));#endifcap = (dat[0]<<8)+dat[1];FTU_TEL_MEA[VbatDa].value = (cap - FTU_TEL_SET[UbatZSetDa].value)/10000.0 * FTU_TEL_SET[UbatMSetDa].value; #ifdef TESTsprintf(buf,"rncap=%drn",cap); uartSendArray(buf,strlen(buf)); sprintf(buf,"Vbat=%drnrn",FTU_TEL_MEA[VbatDa].value); uartSendArray(buf,strlen(buf));#endifreturn; }}