📄 controlboard.c
字号:
/*******************************************************************************
Update date information
2004.02.20
This issue is modified for P520 poly phase meter. The modified item is
listed as below:
1. The timer out value for checking MPPC flash programming result is modified
from 18s to 30s. Timer base is modified from 100ms to 1000ms.
2. The version is modified from "200202190003" to "200402200004"
2004.03.01
1. The time out waiting processing during programming is only done while reset
the MPPC OK.
2. The version is modified from "200202190004" to "200402200005"
********************************************************************************/
//******************************************************************************
#include "At89x52.h"
#include "Intrins.h"
//******************************************************************************
//******************************************************************************
#define uchar unsigned char
#define HIGH 1
#define LOW 0
sbit MppcReq1=P0^0;
sbit MppcRst1=P0^1;
sbit MppcReq2=P0^2;
sbit MppcRst2=P0^3;
sbit MppcReq3=P0^4;
sbit MppcRst3=P0^5;
sbit MppcReq4=P0^6;
sbit MppcRst4=P0^7;
sbit inRtcErr =P1^0;
sbit inFramErr =P1^1;
sbit inFramRtcBusy =P1^2;
sbit inFramRtcEnd =P1^3;
sbit _inFlashEnd =P1^4;
sbit _inFlashBusy =P1^5;
sbit _inFlashErr1 =P1^6;
sbit _inFlashErr2 =P1^7;
sbit SEL0 =P2^0;
sbit SEL1 =P2^1;
sbit _outEn9V =P3^2;
sbit _outEn5V =P3^3;
sbit _outRstPcb =P3^4;
//******************************************************************************
//******************************************************************************
volatile uchar MsgOk=0x00;
volatile uchar Rs232State=0x00;
volatile uchar Rs232Timer=0; //单位:100ms
uchar ReceiveBuffer[2];
uchar SendBuffer[13];
bit w25msf; //2.5ms标志
volatile uchar Timer1s=0;
volatile uchar Timer100ms=0;
volatile uchar Timer2500us=0;
uchar MppcEnd=0;
// 本变量使用时按位定义,各位在RESET检测时置初值为全1,表示各结果已经出来,复位成功后清0表示
//要继续写入程序、数据和校正RTC等。在进入编程进程开始时,若对应位为1表示复位不成功,不进行对应
//的程序写入、数据写入或时钟校正。当对应位为0时,即进行相应操作,并于确定时间内检测是否正确完成
//相应操作,并置上相应相应结果位
//bit0 for MPPC1.FLSH
//bit1 for MPPC2.FLSH
//bit2 for MPPC3.FLSH
//bit3 for MPPC4.FLSH
//bit4 for MPPC1.FRAM/RTC
//bit5 for MPPC2.FRAM/RTC
//bit6 for MPPC3.FRAM/RTC
//bit7 for MPPC4.FRAM/RTC
uchar FlashResult=0; //FLASH编程结果
//bit0 MPPC1.FlashErr1
//bit1 MPPC2.FlashErr1
//bit2 MPPC3.FlashErr1
//bit3 MPPC4.FlashErr1
//bit4 MPPC1.FlashErr2
//bit5 MPPC2.FlashErr2
//bit6 MPPC3.FlashErr2
//bit7 MPPC4.FlashErr2
uchar FramRtcResult=0; //FRAM/RTC编程校正结果
//bit0 MPPC1.FramErr
//bit1 MPPC2.FramErr
//bit2 MPPC3.FramErr
//bit3 MPPC4.FramErr
//bit4 MPPC1.RtcErr
//bit4 MPPC2.RtcErr
//bit4 MPPC3.RtcErr
//bit4 MPPC4.RtcErr
bit flaresf1,frmresf1,flaresf2,frmresf2,flaresf3,frmresf3,flaresf4,frmresf4;
//******************************************************************************
//******************************************************************************
void Send(len)
//功能:发送一帧消息,消息格式参见设计说明
//入口:len=正文信息长度
{
uchar *p;
uchar CheckSum=0;
// uchar code MSG_VER[]="200402200005";
//发送用查询方式,关闭串口中断
ES=0;
//发送2字节起始同符0xA5,0x5A
SBUF=0xA5;while(!TI);TI=0;
SBUF=0x5A;while(!TI);TI=0;
//发送长度字节
// SBUF=12+len;while(!TI);TI=0;
SBUF=len;while(!TI);TI=0;
//发送版本信息
/* p=MSG_VER;
while(*p)
{
SBUF=*p;
CheckSum+=*p++;
while(!TI);TI=0;
} */
//发送正文信息
p=SendBuffer;
do
{
SBUF=*p;
CheckSum+=*p++;
while(!TI);TI=0;
}while(--len);
//发送检测和字节
SBUF=CheckSum;
while(!TI);
TI=0;
//恢复串口中断
ES=1;
}
//******************************************************************************
//******************************************************************************
void ResetMppc()
{
uchar FlashResetCount[4]={30,30,30,30};
uchar FramRtcResetCount[4]={30,30,30,30};
//延时100ms等系统稳定再复位MPPC
Timer2500us=40;
while(Timer2500us); //40*2500us=40*2.5ms=100ms
//复位MPPC
MppcRst1=0;
MppcRst2=0;
MppcRst3=0;
MppcRst4=0;
Timer2500us=40;
while(Timer2500us); //40*2500us=100ms
MppcRst1=1;
MppcRst2=1;
MppcRst3=1;
MppcRst4=1;
MppcEnd=0xFF;
//复位后的超时时间为2.5s=100ms*25;
Timer100ms=25;
while(Timer100ms)
{
uchar channel;
if(!w25msf) continue;
w25msf=0;
for(channel=0;channel<4;channel++)
{
P2=0xFC|channel;
if(FlashResetCount[channel])
{
if
(
_inFlashBusy && //MPPC.FLASH已不忙
(!_inFlashEnd) && //MPPC.FLASH工作已完成
_inFlashErr1 && //MPPC.FLASH错误信号1无效
_inFlashErr2 //MPPC.FLASH错误信号2无效
)
{
if(--FlashResetCount[channel]==0) MppcEnd&=(~(0x01<<channel));
}
else
{
FlashResetCount[channel]=30;
}
}
if(FramRtcResetCount[channel])
{
if
(
inFramRtcBusy ||
inFramErr ||
inRtcErr ||
inFramRtcEnd
)
{
FramRtcResetCount[channel]=30;
}
else
{
if(--FramRtcResetCount[channel]==0) MppcEnd&=(~(0x10<<channel));
}
}
}
if(MppcEnd==0x00) break; // jump out while loop
}
}
//******************************************************************************
//******************************************************************************
void StartAndCheckMppc()
{
uchar FlashState[4]={0,0,0,0}; //(1,2,3,4,赋初值)
uchar FramRtcState[4]={0,0,0,0};
uchar FlashCount[4];
uchar FramRtcCount[4];
FlashResult=FramRtcResult=0x00;
if(MppcEnd!=0xFF) //复位完成
{
//MppcEnd=0x00;
//向MPPC发出请求信号(100ms低脉冲信号)
MppcReq1=0;
MppcReq2=0;
MppcReq3=0;
MppcReq4=0;
Timer2500us=40;
while(Timer2500us); //40*2500us=40*2.5ms=100ms
MppcReq1=1;
MppcReq2=1;
MppcReq3=1;
MppcReq4=1;
//延时一定时间
Timer2500us=40;
while(Timer2500us); //40*2500us=40*2.5ms=100ms
//设定MPPC的编程、写数据、校RTC的总共超时时间为30s=1000ms*30
Timer1s=42;
while(Timer1s)
{
uchar channel;
if(!w25msf) continue;
w25msf=0;
for(channel=0;channel<4;channel++)
{
P2=0xFC|channel;
if( !(MppcEnd&(0x10<<channel)) ) //本通道FRAM/RTC完成吗?
{ //没未成
switch(FramRtcState[channel])
{
case 0: //等待inFramRtcBusy有效
{
if(inFramRtcBusy)
{
FramRtcCount[channel]=0;
}
else
{
if( ++FramRtcCount[channel]>40)
{
++FramRtcState[channel];
FramRtcCount[channel]=0;
}
}
break;
}
case 1: //等待inFramRtcBusy无效 且inFramRtcEnd有效
{
if( (!inFramRtcBusy) && inFramRtcEnd )
{
if(++FramRtcCount[channel]>20)
{ //inFramRtcBusy 连续50ms无效
++FramRtcState[channel];
FramRtcCount[channel]=0;
}
}
else
{
FramRtcCount[channel]=0;
}
break;
}
case 2: //读inFramErr和inRtcErr
{
uchar tmp=0x00;
if(!inFramErr) tmp|=0x01;
if(!inRtcErr) tmp|=0x10;
tmp<<=channel;
if
(
inFramRtcBusy &&
(!inFramRtcEnd) &&
( (FramRtcResult&(0x11<<channel)) ==tmp )
)
{
if(++FramRtcCount[channel]>20)
{ //连续50ms
++FramRtcState[channel];
FramRtcCount[channel]=0;
}
}
else
{
FramRtcCount[channel]=0;
FramRtcResult&=(~(0x11<<channel));
FramRtcResult|=tmp;
}
break;
}
case 3:
{
MppcEnd|=(0x10<<channel);
break;
}
default:
{
MppcEnd|=(0x10<<channel);
FramRtcResult&=(~(0x11<<channel));
break;
}
}
}
if( !(MppcEnd&(0x01<<channel)) ) //本通道FLASH完成吗?
{ //没完成
switch(FlashState[channel])
{
case 0: //等待_inFlashBusy为0有效
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -