我的程序有点乱,请大神们批评指出
#include "Function_Init.H"
/**************************************************************
˵Ã÷£º±¾Àý³ÌÑÝʾÁËADC¶ÁÈ¡´«¸ÐÆ÷µÄÊýÖµ£¬È»ºóÅжϵ±Ç°´«¸ÐÆ÷ÊÇ·ñ´¦ÓÚºÚɫѼ£ÏßÇøÓò£¬
Èç¹û´«¸ÐÆ÷ÔÚºÚÉ«ÇøÓò£¬¾ÍµãÁÁ¶ÔÓ¦µÄLED×÷Ϊָʾ¡£
±àд£ºËïºìÁÁ
ʱ¼ä£º2019.4.29
***************************************************************/
extern void ADC_Init(uint Channel);
extern void void Timer_Init(void);
extern void PWM_Init(void);
extern void PWM_ctr(uchar pwmL,uchar pwmR);
extern void ADC_ch();
extern void Uart0_Init(uint Freq,unsigned long int baud);
extern bit Uart0SendFlag ;
extern uint adcL,adcR;
uchar pwmL,pwmR;
#define uint unsigned int
#define uchar unsigned char
#define TRIG P1_1
#define ECHO p1_0
unsigned int time=0;
unsigned int timer=0;
unsigned int k = 0;
unsigned int s = 0;
void main(void)
{
uint m,k;
IO_Init(); //Òý½Å³õʼ»¯
PWM_Init(); //PWM³õʼ»¯
Uart0_Init(16,9600);
pwmL=100,pwmR=100;//Õ¼¿Õ±È³õÖµ
P1CON|=0x01;
P1CON|=~0x02;
TOMD = 0x21;
TH0 = 0;
TL0 = 0;
EA = 1;
IE |=0X44; //¿ªÆôADCÖжÏ
while(1)
{WDTCON |= 0x10; //Çå¿´ÃÅ
ADC_ch();
for(m=0;m<500;m++);//ÑÓʱһ¶Îʱ¼ä£¬ ÒÔÅäºÏС³µÔ˶¯ËٶȽÏÂýµÄÇé¿ö
if((adcR>3300) &&(adcL<2800)) //ÓÒ´«¸ÐÆ÷λÓÚºÚÉ«ÇøÓò£¬×󴫸ÐÆ÷λÓÚ°×É«ÇøÓò
{if(pwmL<250)pwmL++;//Ìá¸ß×óµç»úÇý¶¯ÐźŵÄÕ¼¿Õ±È£¬ÊµÏÖÓÒת
pwmR=400;
P1&=~0X80; //µãÁÁP1.7Òý½ÅµÄLED,D5
P1|=0X40; //¹Ø±ÕLED
PWM_ctr(pwmL,pwmR);
}
if((adcR>3300) &&(adcL>3300)) //×ó,ÓÒ´«¸ÐÆ÷λÓÚºÚÉ«ÇøÓò£¬
{
pwmR=150;
pwmL=pwmR; //Ö±ÐÐ
PWM_ctr(pwmL,pwmR);
P1|=0X80; //¹Ø±ÕLED
P1&=~0X40; //µãÁÁP1.6Òý½ÅµÄLED,D6
}
if((adcR<2800) &&(adcL<2800)) //×ó,ÓÒ´«¸ÐÆ÷λÓÚ°×É«ÇøÓò£¬
{pwmR=0; //ÔµØÐýת
pwmL=0;
PWM_ctr(pwmL,pwmR);
P1&=~0X40; //µãÁÁP1.6Òý½ÅµÄLED,D6
P1&=~0X80; //µãÁÁP1.7Òý½ÅµÄLED,D5
}
if((adcR<2800) &&(adcL>3300)) //×󴫸ÐÆ÷λÓÚºÚÉ«ÇøÓò£¬ÓÒ´«¸ÐÆ÷λÓÚ°×É«ÇøÓò
{if(pwmR<150)pwmR++;//Ìá¸ßÓÒµç»úÇý¶¯ÐźŵÄÕ¼¿Õ±È£¬×óʵÏÖת
pwmL=800;
PWM_ctr(pwmL,pwmR);
P1|=0X80; //¹Ø±ÕLED
P1|=0X40; //¹Ø±ÕLED
}
k++;
if(k>=100) //ÿѻ·1000´Î£¬·¢ËÍÒ»´Î¹âµç´«¸ÐÆ÷Êý¾Ý
{ SS0DAT = adcL>>8; //×󴫸ÐÆ÷Êý¾Ý¸ß×Ö½Ú
while(!Uart0SendFlag);
Uart0SendFlag = 0;
SS0DAT = (uchar)adcL; //×󴫸ÐÆ÷Êý¾ÝµÍ×Ö½Ú
while(!Uart0SendFlag);
Uart0SendFlag = 0;
SS0DAT = adcR>>8; //ÓÒ´«¸ÐÆ÷Êý¾Ý¸ß×Ö½Ú
while(!Uart0SendFlag);
Uart0SendFlag = 0;
SS0DAT = (uchar)adcR; //ÓÒ´«¸ÐÆ÷Êý¾ÝµÍ×Ö½Ú
while(!Uart0SendFlag);
Uart0SendFlag = 0;
k=0;
}
void Timer_Count(void);
if(s==)
}
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.88)/10; //Ëã³öÀ´ÊÇMM
//³¬³ö²âÁ¿·¶Î§ÏÔʾ¡°-¡±
}
void Delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=587;j>0;j--)
}
void Micro Wait( int timeout )
{
while(timeout--)
{
TRIG = 1;
asm"NOP";
asm"NOP";
asm"NOP";
asm"NOP";
asm"NOP";
asm"NOP";
asm"NOP";
asm"NOP";
TRIG = 0;
}
}
void delay_10us(void)
{
Micro Wait(10);
}
void Timer_Count(void)
{
TR0=0; //¿ªÆô¼ÆÊý
while(ECHO==0); //µ±RXΪ1¼ÆÊý²¢µÈ´ý
TR0=1; //¹Ø±Õ¼ÆÊý
Conut(); //¼ÆËã
}
void zd0() interrupt 1
{
TR0 = 0;
TL0 = 0;
} |