首先,小弟是真小白,完全靠蒙。
根据上次改点火的经验,改了部分程序,输出有反应。
油门最低0v……推15% led闪1.6v……推89% led亮3.2v。期间亮度无变化(看不出有pwm效果)
推油门过程,万用表测得只有0v、1.6v和3.2v三个电平(这样叫不知道对不对),没有线性变化。
问题1:怎么改?才能线性变化。
问题2:15w104运行频率是12m,换8g1k改成24m甚至35m,又该怎么改。
源码如下:
#include<stc8g1k08a.h>
#define u8 unsigned char
#define u16 unsigned int
//sfr P3M1=0xb1;
//sfr P3M0=0xb2;
//sfr IAP_DATA=0xc2;
//sfr IAP_ADDRH=0xc3;
//sfr IAP_ADDRL=0xc4;
//sfr IAP_CMD=0xc5;
//sfr IAP_TRIG=0xc6;
//sfr IAP_CONTR=0xc7;
//sfr AUXR=0x8e;
//sfr T2H=0xd6;
//sfr T2L=0xd7;
//sfr IE2=0xaf;
sbit LED=P3^2;
sbit PWM=P3^3;
sbit IN=P5^4;
u8 H_l,H_h,L_l,L_h;
u16 time,proportion,tt,MAX,min,k; //min改MIN,编译会语法错误,什么鬼
u8 low,top,ok,lose,calabration,MARK1,MARK2,over=1,error;
bit turn;
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void delay(u16 t)
{
while(t--)
Delay1ms();
}
u8 EEPROM_read(u8 address)
{
IAP_CMD=0x01;
IAP_ADDRH=0;
IAP_ADDRL=address;
IAP_TRIG=0x5a;
IAP_TRIG=0xa5;
return IAP_DATA;
}
void EEPROM_write(u8 address,u8 byte)
{
IAP_CMD=0x02;
IAP_DATA=byte;
IAP_ADDRH=0;
IAP_ADDRL=address;
IAP_TRIG=0x5a;
IAP_TRIG=0xa5;
}
void EEPROM_clean(u8 address)
{
IAP_CMD=0x03;
IAP_ADDRH=0;
IAP_ADDRL=address;
IAP_TRIG=0x5a;
IAP_TRIG=0xa5;
}
void DATA_read()
{
IAP_CONTR=0x82;
MARK1=EEPROM_read(0);
MARK2=EEPROM_read(1);
MAX=EEPROM_read(2);
MAX<<=8;
MAX+=EEPROM_read(3);
min=EEPROM_read(4);
min<<=8;
min+=EEPROM_read(5);
IAP_CONTR=0;
}
void DATA_save()
{
IAP_CONTR=0x82;
EEPROM_clean(0);
EEPROM_write(0,0x0f);
EEPROM_write(1,0xa5);
EEPROM_write(2,MAX>>8);
EEPROM_write(3,MAX);
EEPROM_write(4,MIN>>8);
EEPROM_write(5,min);
IAP_CONTR=0;
}
void measure()
{
time=0;
TL0=0,TH0=0;
while(!IN);
TR0=1;
while(IN);
TR0=0;
time=TH0;
time<<=8;
time+=TL0;
}
void LED_flash(u8 m)
{
while(m--)
{
LED=1;delay(50);
LED=0;delay(50);
}
}
void initial()
{
u8 i;
P3M0 = 0xff; P3M1 = 0x00;
P5M0 = 0x00; P5M1 = 0x00;
P3=0x0c;
TMOD=0x11; //T0、T1工作模式12T
TL0=0;
TH0=0;
TL1=0;
TH1=0;
delay(100);
measure();
measure();
measure();
measure();
measure();
while(PCON)
{
PCON=0,delay(10); //低压保护,如果1S电池的电压低于
} //下载时的设定值,则在此死循环
DATA_read();
if(MARK1!=0x0f||MARK2!=0xa5)//新增 16bit验证,降低错误率
{
MAX=1950;min=1000;//新增 开机自带1ms-2ms标准行程
}
over=1;
while(over)
{
measure();
if(time>1700)//重新校准行程判断
{
i++;
if(i>100)over=0,calabration=1;
}
else //
{
if(time<min+100)over=0;
}
}
if(MAX>2200||min<800)//新增 数据验证,降低错误率
{
calabration=1;
if(MAX<1700||min>1300)calabration=1;
}
if(calabration)
{
while(over)
{
measure();
if(time>1700)over=0;
}
delay(1000);
LED_flash(10);
measure();
measure();
if(time>800&&time<2200)MAX=time;
else error=1;
over=1;
while(over)
{
measure();
if(time<1300)over=0;
}
delay(1000);
LED_flash(10);
measure();
measure();
if(time>800&&time<2200)min=time;
else error=1;
if(error==0)DATA_save();
}
}
main()
{
initial();
k=MAX-min;
IE=0x80;
ET1=1; //开T1中断
while(1)
{
measure();
if(lose)
{
delay(1000);
measure();
measure();
}
if(time>800&&time<2260)
{
ok=1;lose=0;TR1=1;
if(time>MAX-20)time=MAX-20,top=1;
else top=0;
if(time<min+100)time=min+100,low=1;
else low=0;
proportion=(long)(time-min)*1000/k;
H_l=65535-proportion ;
H_h=((65535-proportion)>>8);
L_l=64535+proportion;
L_h=((64535+proportion)>>8);
}
}
}
void et1()interrupt 3
{
turn=!turn;
if(turn)
{
if(top)PWM=1,T2L=0x0b,T2H=0xfe;
else PWM=0,T2L=H_l,T2H=H_h;
}
else
{
if(low)PWM=0,T2L=0x0b,T2H=0xfe;
else PWM=1,T2L=L_l,T2H=L_h;
}
LED=PWM;//新增LED亮度随油门变化
tt++;
if(ok)tt=0;
else {if(tt>2000)tt=2000,PWM=0,turn=0,TR1=0,LED=0,lose=1;}
ok=0;
}
|