www.pudn.com > 030425.rar > 030425.c
/*
2004.09.09 汽油发电机油门控制电路源程序,采用16MHz晶振,
四相步进电机,双4拍控制方式,1:25电机
转速=(fosc/12)*VV_JCOU*n/js_zhuanshu
fosc=16*10^6 n=60/96:齿速比 VV_JCOU=脉冲数 js_zhuanshu=计数值
*/
#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int
#define sint signed int
#define ulong unsigned long
#define VV_JCOU 38
#define VV_STEPMAX 40
/* 8052 Extensions */
sfr T2CON = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
sfr T2MOD = 0xC9;
/* T2CON */
sbit TF2 = 0xCF;
sbit EXF2 = 0xCE;
sbit RCLK = 0xCD;
sbit TCLK = 0xCC;
sbit EXEN2 = 0xCB;
sbit TR2 = 0xCA;
sbit C_T2 = 0xC9;
sbit CP_RL2= 0xC8;
sbit SMSTB=P3^0;
sbit SMDIN=P3^1;
sbit SMCLK=P3^2;
sbit K_YM=P3^3;
uchar data Out_P1;
uchar data Step_time; //步进延迟时间0-255
uchar data Step_timcou;
uchar data Step_cou; //步进数 0-255
uchar data Step_cll; //步进拍 0-4
uint data js_zhuanshu; /* use for disper */
uchar data tmp_TH2;
uchar data tmp_TL2;
uchar data fcount;
uchar bdata F_byte;
sbit F_di=F_byte^0; //步进方向,0=关油门,1=开油门
sbit F_jszs=F_byte^1;
//sbit a=P1^0;/*橙*/ //2004.09.09
//sbit b=P1^1;/*红*/
//sbit c=P1^2;/*蓝*/
//sbit d=P1^3;/*黄*/
unsigned char code STG10_smg[16]={
0xBD,0xA0,0x3E,0xB6,0xA3,0x97,0x9F,0xB0,0xBF,0xB7,
0,0,0,0,0,0
};
void pout(unsigned char cc);
void disp_time();
void OFF_YM();
void ON_YM();
void ON_YM005();
/*开油门程序*/
void on(void)
{
switch (Out_P1&0x0F){
case 0x03: Out_P1=(Out_P1&0xF0)|0x06;break;
case 0x06: Out_P1=(Out_P1&0xF0)|0x0C;break;
case 0x0C: Out_P1=(Out_P1&0xF0)|0x09;break;
default: Out_P1=(Out_P1&0xF0)|0x03;break;
}
P1=Out_P1;
}
/*关油门程序*/
void off(void)
{
switch (Out_P1&0x0F){
case 0x03: Out_P1=(Out_P1&0xF0)|0x09;break;
case 0x09: Out_P1=(Out_P1&0xF0)|0x0C;break;
case 0x0C: Out_P1=(Out_P1&0xF0)|0x06;break;
default: Out_P1=(Out_P1&0xF0)|0x03;break;
}
P1=Out_P1;
}
void service_int_T0(void) interrupt 1 using 3
{
ET0=0;
TL0=0x00;
TH0=0xfb;
if(Step_timcou>0) Step_timcou--;
else{ Step_timcou=Step_time;
if(Step_cou>0 && Step_cll==0){Step_cll=4;Step_cou--;}
if(Step_cll>0){
if(F_di==0) off();
else on();
Step_cll--;
}
}
ET0=1;
}
void service_int_T1(void) interrupt 3 using 2
{
TR2=0;
tmp_TH2=TH2;
tmp_TL2=TL2;
TH2=0;
TL2=0;
F_jszs=1;
TR2=1;
}
main()
{
P1=0x00;
Out_P1=0x03;
Step_cll=0;
Step_cou=100;
fcount=0;
TMOD=0x61;
PCON=0;
TL1=256-VV_JCOU;
TH1=256-VV_JCOU;
TL0=0x00;
TH0=0xfb;
ET0=1;
PT1=1;
ET1=1;
TR0=1;
TR1=1;
T2MOD=0;
TL2=0;
TH2=0;
RCAP2H = 0x00; //0xF6
RCAP2L = 0x00;
CP_RL2=1;
C_T2=0;
TR2=1;
EA=1;
js_zhuanshu=12345;
F_jszs=1;
OFF_YM();
//ON_YM();
ON_YM005();
disp_time();
for (;;) {
//if(Step_cou==0 && Step_cll==0) {F_di=~F_di;Step_cou=30;}
if(F_jszs==1){F_jszs=0;disp_time();}
disp_time();
}
}
void disp_time()
{
if(fcount==0){js_zhuanshu=tmp_TH2*256;
js_zhuanshu=js_zhuanshu+tmp_TL2;
}
else{
js_zhuanshu=js_zhuanshu+tmp_TH2*256;
js_zhuanshu=js_zhuanshu+tmp_TL2;
js_zhuanshu=js_zhuanshu/2;
}
fcount++;
if(fcount>10){
fcount=0;
//js_zhuanshu=(80000000*10/(3*16))/js_zhuanshu;
js_zhuanshu=32000000/js_zhuanshu;
if(K_YM==1) pout(0);
else pout(1);
pout(0);
pout(0);
//pout(STG10_smg[(js_zhuanshu/100000)]);
pout(STG10_smg[(js_zhuanshu%100000)/10000]);
pout(STG10_smg[(js_zhuanshu%10000)/1000]);
pout(STG10_smg[(js_zhuanshu%1000)/100]);
pout(STG10_smg[(js_zhuanshu%100)/10]);
pout(STG10_smg[js_zhuanshu%10]);
SMSTB=1;
SMSTB=0;
}
}
void pout(unsigned char cc)
{
unsigned char data k;
for(k=0;k<8;k++){
SMCLK=0;
cc<<=1;
SMDIN=~CY;
SMCLK=1;
}
}
void OFF_YM()
{
Step_time=3;
F_di=1;
if(K_YM==1) Step_cou=VV_STEPMAX+20;
while(K_YM==1){;}
Step_cou=0;
}
void ON_YM()
{
Step_time=3;
F_di=0;
Step_cou=VV_STEPMAX;
}
void ON_YM005()
{
Step_time=3;
F_di=0;
Step_cou=5;
}