📄 main.c
字号:
if(ParaError3)
{
fPara[45] = 1;
fPara[46] = 0;
}
WriteFlash();
}
//----------------------------------------------------
DisplayMainMenu(0,0); //显示主功能界面
// SetWrithe(800);
// SPEEDTIME = 2000;
EA = 1;
P4 |= 0x01;
//------------------------------------------------------
// SPEEDTIME=2000;
// fTimeParaMul=(float)2000/2000;
// ResetStepCounter();
//------------------------------------------------------
while(1)
{
if(FunctionCode == 4)
{
if(bAhead2) //提前角的DAC输出
{
if(bAhead3) //提前角的DAC输出
{
if(mainfunccounter>=0 )//&& mainfunccounter<1000)
{
PressValue ++;
DAC0L = PressValue;
DAC0H = PressValue>>8;
}
// if(mainfunccounter>=0 )//&& mainfunccounter<1000)
// {
// DAC0L = PCACPAL[mainfunccounter];
// DAC0H = PCACPAH[mainfunccounter];
// }
mainfunccounter++;
}
else
{
// if(mainfunccounter>0)
// {
// mainfunccounter--;
// DAC0L = PCACPAL[mainfunccounter];
// DAC0H = PCACPAH[mainfunccounter];
// }
if(PressValue>PressValueZero)
{
PressValue --;
DAC0L = PressValue;
DAC0H = PressValue>>8;
}
mainfunccounter++;
}
}
else
{
if(bAhead0)
COUNTER++;
else if(bAhead1)
{
TermCounters = COUNTER/CYLINDERNUMBER;
SetFireAngle();
bAhead2 = 1;
bAhead1 = 0;
// bAhead0 = 0;
}
}
}
else
{
if(AD0ProcessEnd)
{
fCurrent = (float)ADC0_SUM/4096; //除以4096
fCurrent = fCurrent*2.4;
fCurrent /= fGain0;
fCurrent /= fRSample;
fCurrent *= 500;
fCurrent /= COUNTER;
fCurrent *= fCurrentPara;
Putf(18,68,"52",fCurrent,0);
COUNTER = 0;
ADC0_SUM = 0;
AD0ProcessEnd = 0;
AD0BUSY = 1;//Start ADC0
}
if(AD1ProcessEnd)
{
Putf(18,36,"42",fVoltage,0);
AD1ProcessEnd = 0;
ADC1CN |= 0x10;//Start ADC1
}
}
} */
}
/*void Timer0(void) interrupt 1 //using 3 //IRQ_VECT_CT0
{
//DAC0输出
if(m == 1)
{
z++;
if(z>=1000 && z<1501)//z可以代表提前角
{
DAC0L = CylinderPressValue[z-1000];
DAC0H = CylinderPressValue[z-1000]>>8;
}
}
else
{
z = 0;
}
}*/
/*void PCAInterrupt(void) interrupt 9 //using 3 //IRQ_VECT_CT0
{
if(CCF1)
{
CCF1 = 0;
PCA0L = 0;
PCA0H = 0;
// CEX1=!CEX1;
if(Cex1Flag)//(CEX1==1)
{
if(SwitchUp)
{
if(COUNTER<StepCounter[0]) //加速过程
{
PCA0CPL1 = PCACPAL[COUNTER];
PCA0CPH1 = PCACPAH[COUNTER];
if(COUNTER == BDBegin)
P4 &= 0xf7;
if(COUNTER == BDEnd)
P4 |= 0x08;
COUNTER++;
}
else if(COUNTER<=StepCounter[1]) //下降过程
{
PCA0CPL1 = PCACPAL[StepCounter[1]-COUNTER];
PCA0CPH1 = PCACPAH[StepCounter[1]-COUNTER];
COUNTER++;
}
else
{
SwitchUp=0;
bUp = 1;
COUNTER=0;
StateCode = 0;
PCA0CPL1 = 34560;
PCA0CPH1 = 34560>>8;
Putf(18,68,"62",fSpeedTime,0);
Putstr(PromptX3,PromptY3,"空确认键开始加速空",0,0);
}
}
else if(FunctionCode == 3) //闭合角检定时改变比较器值
{
P4 |= 0x08;
PCA0CPL1 = pcacpLH;
PCA0CPH1 = pcacpHH;
}
CylinderCounter++;
if(CylinderCounter > CylinderNumberEnd)
{
CylinderCounter = 1;//101;
P4 |= 0x01;
if(FunctionCode == 4)
{
if(bAhead2)//已经取得主程序计数值
{
// if(!bAhead3)
// {
bAhead3 = 1;
mainfunccounter = AheadCounters;
PressValue = PressValueStart;
P4 &= 0xf7;
TMR3L = TMR3RLL;
TMR3H = TMR3RLH;
TMR3CN = 0x05;
// }
}
else
{
//第一步,先通知主程序对这个脉冲周期进行计数并记录计数值
if(!bAhead0)
{
COUNTER = 0;
bAhead0 = 1;
}
else if(!bAhead1)//停止计数
{
bAhead1 = 1;
bAhead0 = 0;
}
}
}
else if(FunctionCode == 7) //闪光角检定时标志信号
{
if(bFlash)
{
P4 &= 0xf7;
TMR3L = TMR3RLL;
TMR3H = TMR3RLH;
TMR3CN = 0x05;
bFlash = 0;
}
}
}
// else
// {
// if(FunctionCode == 4)
// PressValue = 0;
// }
}
else
{
if(FunctionCode == 3)
{
P4 &= 0xf7;
PCA0CPL1 = pcacpLL;
PCA0CPH1 = pcacpHL;
}
P4 &= 0xfe;
}
}
else if (CCF0)
{
CCF0 = 0;
PCA0L = 0;
PCA0H = 0;
}
else if (CCF2)
CCF2 = 0;
else if (CCF3)
CCF3 = 0;
else if (CCF4)
CCF4 = 0;
else if (CF)
CF = 0;
}
void Timer3(void) interrupt 14 //using 3 //用timer3来计时,优先级要置高
{
TMR3CN &= 0x7f;
// COUNTER = AheadCounterLimit;
P4 |= 0x08;
bAhead3 = 0; //标志
TMR3CN = 0x01; //timer3外部时钟的8分频 ,停止
}
void SetFreq(float freq)
{
uchar temp; //Timer0高字节重载值
CKCON &= 0xf0; //Timer0使用系统时钟/12
TMOD = 0x02; //Timer0设定为8位重载
PCADRVCLOCK = SYSCLOCK/12;
PCADRVCLOCK = PCADRVCLOCK/256;//
temp = PCADRVCLOCK/2/freq;
PCA0CN = 0x40; //pca使能
PCA0MD = 0x84; //选用TIMER0的溢出为驱动时钟
PCA0CPM1 = 0x46; //pca1为频率输出方式,禁止CCFn中断
PCA0CPL1 = 0;
PCA0CPH1 = temp;
TL0 = 0;
TH0 = 0;
TR0 = 1;
}
void SetWrithe()
{
float freq;
uchar Timer0H; //Timer0高字节重载值
// uint outCounter;
uint outCounter1;
// float k=1.0008807750820722235567299223317;
// float b=0.20017615501641444471134598446633; //调整系数
// WRITHE = withe;
// EIE1 &= 0xf7;//禁止PCA中断
freq = (float)WRITHE*CYLINDERNUMBER/120;
freq = freq*fSpeedParaMul+fSpeedParaAdd;
DisplayCounter = (int)(freq+0.5);//间隔1秒左右显示转速
FlashDisplayCounter = DisplayCounter/CYLINDERNUMBER;
if(freq >= 11)
{
//校正频率
// k = 1.0021620139889821291195430147937;
// b = 0.02782397050110974542174747322905;
// freq = freq*k-b;
PCADRVCLOCK = SYSCLOCK/8;
outCounter = (float)PCADRVCLOCK/freq/2+0.5;
outCounter -= 6;
PCA0CN = 0x40;
PCA0MD = 0x8a;
PCA0CPM1 = 0x4D;
//标准缸信号
// PCA0MD = 0x0a;
// PCA0CPM0 = 0x4D;
}
else if(freq>=0.8)
{
CKCON |= 0x08; //Timer0使用系统时钟
TMOD = 0x02; //Timer0设定为8位重载
Timer0H = 256 - 111;//SYSCLOCK/PCADRVCLOCK;// 设置定时器T0 重载值,
PCADRVCLOCK = SYSCLOCK/111;//
outCounter = (float)PCADRVCLOCK/freq/2+0.5;
// outCounter += 6;
// outCounter = (float)outCounter*k+b;
PCA0CN = 0x40;
PCA0MD = 0x84; //选用TIMER0的溢出为驱动时钟
PCA0CPM1 = 0x4D;
//标准缸信号
// PCA0MD = 0x0a;
// PCA0CPM0 = 0x4D;
TL0 = Timer0H;
TH0 = Timer0H;
TR0 = 1;
}
if(FunctionCode == 3)
{
// outCounter += 6;
freq = (float)CLOSEANGLE*fCloseAngleParaMul+fCloseAngleParaAdd;
outCounter *=2;
outCounter1 = outCounter/360*freq*CYLINDERNUMBER+0.5;
// outCounter1 = (float)outCounter/360*CLOSEANGLE*CYLINDERNUMBER+0.5;
pcacpLL = outCounter1;
pcacpHL = outCounter1>>8;
outCounter1 = outCounter-outCounter1;
pcacpLH = outCounter1;
pcacpHH = outCounter1>>8;
PCA0CPL1 = pcacpLH;
PCA0CPH1 = pcacpHH;
fCloseAngle = CLOSEANGLE;
Putf(20,68,"42",fCloseAngle,0);
}
else
{
PCA0CPL1 = outCounter;
PCA0CPH1 = outCounter>>8;
// outCounter *=
// PCA0CPL0 = outCounter;
// PCA0CPH1 = outCounter>>8;
}
// PCA0CPM0 = PCA0CPM1; //pca1为频率输出方式,禁止CCFn中断
// PCA0CPL0 = PCA0CPL1;
// PCA0CPH0 = PCA0CPH1;
// CEX1 = 1;
CylinderCounter = 0;
CylinderNumberEnd = CYLINDERNUMBER;//+100;
EIE1 |= 0x08;//允许PCA中断
EIP1 = 0x08; //PCA中断优先级置高
}
void ResetStepCounter()
{
///////////////////////////////////////////////////////////////////
//用等比数列使周期比例递减,则转速比例递增,是直线
//等比数列和:Sn=(A0*K-An)/(K-1)
//K=(Sn-An)/(Sn-A0)
//A0=800,An=1100
//转速=120/缸数*周期
//
uint i;
float k;//周期比例递减系数
uint n;//递减计数,即等比数列长度
float HalfSpeedTime;
float DownSpeed,UpSpeed;
uint T0,Tn,random,TUp,TDown;
uint TempPoint;
ulong PointSum;
ulong RangePoint;
ulong dis;
float timeAdjusted;
//加了标定系数
timeAdjusted = (float)SPEEDTIME*fTimeParaMul+fTimeParaAdd;
for(i=0;i<300;i++)
{
PCACPAL[i] = 0;
PCACPAH[i] = 0;
}
//-------------800~1100----------------------
HalfSpeedTime = 1.0f;//2秒钟的一半
RangePoint = HalfSpeedTime*PCADRVCLOCK+0.5;
DownSpeed = 600;
UpSpeed = 1000;
T0 = 120/CYLINDERNUMBER/DownSpeed/2*PCADRVCLOCK+0.5;
Tn = 120/CYLINDERNUMBER/UpSpeed/2*PCADRVCLOCK+0.5;
k = (float)(RangePoint-Tn)/(RangePoint-T0);
random = T0;
i = 0;
while(1)//求n算法
{
i++;
TempPoint = random;
random = (float)random/k+0.5;//random=T0/k的i次方
// random /= k;
if(random <= Tn)
{
//判断这个和上一个谁更近
if((TempPoint-Tn) < (Tn-random))
i--;
break;
}
}
n = i+1;
random = T0;
PointSum = 0;
i = 0;
for(i=0;i<n;i++)
{
PCACPAL[i] = random;
PCACPAH[i] = random>>8;
PointSum += random;
random = (float)random/k+0.5;
}
BDBegin = n;
//---------------1300~2300------------------
//注意,检测仪对转速的识别要落后一个脉冲周期
HalfSpeedTime = timeAdjusted/2000;
RangePoint = HalfSpeedTime*PCADRVCLOCK+0.5;
TUp = (float)120/CYLINDERNUMBER/2500/2*PCADRVCLOCK+0.5;
RangePoint -= TUp;//减去2500转的半周期点数
TDown = (float)120/CYLINDERNUMBER/1300/2*PCADRVCLOCK+0.5;
// RangePoint += TDown;//加上1300转的半周期点数
PCACPAL[BDBegin] = TDown;
PCACPAH[BDBegin] = TDown>>8;
BDBegin = n+1;
DownSpeed = 1350;
UpSpeed = 2100;
T0 = 120/CYLINDERNUMBER/DownSpeed/2*PCADRVCLOCK+0.5;
Tn = 120/CYLINDERNUMBER/UpSpeed/2*PCADRVCLOCK+0.5;
k = (float)(RangePoint-Tn)/(RangePoint-T0);
random = T0;
i = 0;
while(1)//求n算法
{
i++;
TempPoint = random;
// random = (float)random/k+0.5;//random=T0/k的i次方
random /= k;
if(random <= Tn)
{
//判断这个和上一个谁更近
if((TempPoint-Tn) < (Tn-random))
i--;
break;
}
}
n = i+1;
random = T0;
PointSum = 0;
i = 0;
for(i=0;i<n;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -