#define uint unsigned int
#define uchar unsigned char
uchar num1 = 0
uint v_set = 40
xdata uint pcacap2
xdata uchar dutycycle
uint A=10//float D=0.04float C=5
//int xdata e0=0,e1=0,e2=0
float xdata PWM=0
bit isnewdata
bit DIRECTON
#define POSITIVE DIRECTON = 1
#define NEGATIVE DIRECTON = 0
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE
OSCICN= 0x83
}
void Port_IO_Init()
{
SFRPAGE = 0x0F
XBR0 = 0xF7
XBR2 = 0x40
}
void PCA_Init()
{
SFRPAGE=0x00
PCA0CPM0=0x02
PCA0CPM1=0x02
PCA0CPM2=0x21
PCA0CPL2=0x00
PCA0CPH2=0x00
PCA0MD=0x00
PCA0CN=0x40
EIE1|=0x08
}
void PWM_set(uchar low)
{//占空比设置,高电平占空比为(256-low)/256
SFRPAGE = PCA0_PAGE
if (DIRECTON == 1) {PCA0CPH0 = lowPCA0CPM1 = 0x02PCA0CPM0 = 0x42}
else {PCA0CPH1 = lowPCA0CPM0 = 0x02PCA0CPM1 = 0x42}
}
void PCA_ISR(void) interrupt 9 using 1 {
static xdata uint tmpcnt=0
static xdata uint PCA0CP2=0
xdata uint tmpcnt2
if(CCF2){
tmpcnt2 = PCA0CPH2
tmpcnt2 = tmpcnt2<漏铅带<8
PCA0CP2 = tmpcnt2 + PCA0CPL2
pcacap2 = PCA0CP2-tmpcnt
tmpcnt = PCA0CP2
CCF2=0
}
}
PID_SC(uint v)
{
int Uk_zint e
e = v - v_set
//e1 = v1 - v_set
/激尺/e2 = v2 - v_set
//v2 = v1
//v1 = v
Uk_z = A*e/*+ D*e0 + C*(e0 - 2*e1 + e2)*/返芦
//Uk0 = Uk1 + Uk_z
//Uk1 = Uk0
//PWM = PWM - 256
dutycycle = 0.0256 * Uk_z
}
void main()
{
//Oscillator_Init()
WDTCN = 0x07
WDTCN = 0xDE
WDTCN = 0xAD
Port_IO_Init()
EA=1
num1=0
isnewdata=0
PCA_Init()
POSITIVE
while(1){PID_SC(pcacap2)PWM_set(dutycycle)}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)