|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
买错芯片了,求大神指导哈,程序如下,多谢
#include<reg51.h>
#include<intrins.h>
#define u8 unsigned char
#define u16 unsigned int
//----------------------------------------------------------
//STC15¼Ä´æÆ÷ÉùÃ÷
sfr P3M1=0xb1;
sfr P3M0=0xb2;
sfr AUXR=0x8e;
sfr T2H=0xd6;
sfr T2L=0xd7;
sfr IE2=0xaf;
//----------------------------------------------------------
//Òý½Å¶¨Òå
sbit A1=P3^0;
sbit A2=P3^1;
sbit B1=P3^2;
sbit B2=P3^3;
sbit IN1=P3^5;
sbit IN2=P3^4;
//----------------------------------------------------------
//Èí¼þPWM
u8 PWM_count;
u8 PWM_duty_A;
u8 PWM_duty_B;
u8 PWM_value;
//----------------------------------------------------------
//¿ØÖÆ
u16 timer1,timer2;
u16 pulse;
u16 IN1_H_time,IN2_H_time;
bit IN1_last,IN2_last;
bit get_pulse1,get_pulse2;
bit get_new;
int mix_x,mix_y;
bit Direction_A,Direction_B;
int pulse1=150,pulse2=150;
u8 lose_A=20,lose_B=20; //¸üУºÉϵçĬÈÏ´¦ÓÚʧ¿Ø״̬£¬·ÀÖ¹µÚÒ»´ÎÐźżì²â´íÎó
//----------------------------------------------------------
//»ì¿ØÉèÖÃ
bit mix_en=0; //¹Ø±Õ»ì¿Ø£¬Á½Â·¶ÀÁ¢
//bit mix_en=1; //¿ªÆô»ì¿Ø
void delay_ms(u16 ms) //STC15 @12.000MHz
{
u16 i;
do{
i = 12000000 / 13022;
_nop_(),_nop_(),_nop_();
while(--i) ;
}while(--ms);
}
void delay_us(u8 us) //@12.000MHz
{
while(--us)
_nop_(),_nop_();
}
void shock( u8 n) //Õ𶯷¢Éù
{
u8 i;
for(i=0;i<250;i++)
{
A1=0,A2=0;
B1=0,B2=0;
delay_us(n);
A1=1;B1=1;
delay_us(n);
A1=0;B1=0;
delay_us(n);
A2=1;B2=1;
delay_us(n);
}
A1=0,A2=0;
B1=0,B2=0;
delay_us(n);
}
main()
{
P3=0xF0; //Éϵç¹Ø±ÕÊä³ö
P3M0=0x0F; //IOÍÆÍìÊä³ö
delay_ms(400);
shock(200); //ÐÂÔö ÉϵçÒôÀÖ£¬±íÃ÷Õý³£¹¤×÷
shock(150);
shock(100);
IE=0x80;
IE2=0x04;
T2L=0xF6;T2H=0xFF; //¿ªÆôÈí¼þPWM
AUXR=0x10;
while(1)
{
if(get_pulse1)
{
get_pulse1=0;
pulse =IN1_H_time;
IN1_H_time=0;
if(pulse >85 && pulse <215) //Ö»ÊÜÀíºÏÀí¶æÁ¿·¶Î§
{
timer1=0;
if(pulse <105)pulse =105;
if(pulse >195)pulse =195;
if(lose_A)lose_A--; //¶ªÐźÅÖØÁ¬±£»¤
else get_new=1,pulse1=pulse;
}
}
if(get_pulse2)
{
get_pulse2=0;
pulse =IN2_H_time;
IN2_H_time=0;
if(pulse >85 && pulse <215)
{
timer2=0;
if(pulse <105)pulse =105;
if(pulse >195)pulse =195; //¶æÁ¿ÏÞ·ù 1050~1950
if(lose_B)lose_B--; //¶ªÐźÅÖØÁ¬±£»¤
else get_new=1,pulse2=pulse;
}
}
if(get_new)
{
get_new=0;
if(mix_en) //Òý½Å¸ßµçƽ ʹÄÜ»ì¿Ø
{
if(pulse2>152 || pulse2<148) //ͨµÀ2Ϊ²îÏòÊäÈ룬ÖÐλËÀÇø
{
mix_x=pulse1 + pulse2 -150;
mix_y=pulse1 - pulse2 +150;
}
else
{
mix_x=pulse1; //ͨµÀ1ΪͬÏòÊäÈ룬
mix_y=pulse1;
}
}
else //Òý½ÅµÍµçƽ Á½Í¨µÀ¶ÀÁ¢
{
mix_x=pulse1;
mix_y=pulse2;
}
if(mix_x<105)mix_x=105;
if(mix_x>195)mix_x=195;
if(mix_y<105)mix_y=105;
if(mix_y>195)mix_y=195;
if(mix_x>=153 ) //Õýת
{
PWM_duty_A= (mix_x-151)*100/44; //²¹³¥Ä¦²ÁÁ¦
Direction_A=0;
}
else if(mix_x<=147 ) //·´×ª
{
PWM_duty_A= (149-mix_x)*100/44;
Direction_A=1;
}
else //Í£Ö¹
{
PWM_duty_A=0;
}
if(mix_y>=153 ) //Õýת
{
PWM_duty_B= (mix_y-151)*100/44; //²¹³¥Ä¦²ÁÁ¦
Direction_B=0;
}
else if(mix_y<=147 ) //·´×ª
{
PWM_duty_B= (149-mix_y)*100/44;
Direction_B=1;
}
else //Í£Ö¹
{
PWM_duty_B=0;
}
}
}
}
void T2_isr()interrupt 12//Èí¼þPWM 1Khz
{
if(IN1) //ͨµÀ1Âö¿í¼ì²â
{
IN1_last=1;
IN1_H_time++;
}
else
{
if(IN1_last)get_pulse1=1;
IN1_last=0;
}
if(IN2) //ͨµÀ2Âö¿í¼ì²â
{
IN2_last=1;
IN2_H_time++;
}
else
{
if(IN2_last)get_pulse2=1;
IN2_last=0;
}
PWM_count++;
if(PWM_count==100) //100¼¶·Ö±æÂÊ
{
PWM_count=0;
timer1++;
if(timer1>500)//0.5SÎÞÐźű£»¤
{
timer1=0; lose_A=30;
pulse1=150,get_new=1;
}
timer2++;
if(timer2>500)//0.5SÎÞÐźű£»¤
{
timer2=0; lose_B=30;
pulse2=150,get_new=1;
}
}
if(PWM_duty_A > PWM_count)
{
if(Direction_A)A1=1;
else A2=1;
}
else A1=0,A2=0;
if(PWM_duty_B > PWM_count)
{
if(Direction_B)B1=1;
else B2=1;
}
else B1=0,B2=0;
}
|
| |