#include<htc.h>
__CONFIG (INTRC & PROTECT & MCLREN & WDTEN);
const unsigned char cs @ 0x1FF;
void DelayUs( int x) // 32US
{while(--x!=0)
{ CLRWDT();
NOP();
unsigned char k=2;
while(--k!=0)
{}
}
}
void main(void)
{ //OSCCAL =cs;
OPTION=68;
TRIS=29;
GPIO=63;
volatile unsigned int tmp0,tmp1,tmp2,tmp3,tmp4,m,y;
volatile int n;
while(GP4==0)
{ ; }
CLRWDT();
y=300;
m=0;
for(;--y>200; )
{
n=y;
while(GP4==1)
{}
DelayUs(n);
CLRWDT();
GP5=1;
DelayUs(10);
GP5=0;
while(GP4==0)
{}
DelayUs(n);
CLRWDT();
GP5=1;
DelayUs(10);
GP5=0;
}
for(m=9;--m; )
{
while(GP4==1)
{}
DelayUs(n);
CLRWDT();
GP5=1;
TMR0=0;
while(GP2==0)
{}
tmp0=TMR0;
DelayUs(10);
GP5=0;
while(GP4==0)
{}
DelayUs(n);
CLRWDT();
GP5=1;
DelayUs(10);
GP5=0;
}
do{
while(GP4==1)
{}
DelayUs(n);
CLRWDT();
GP5=1;
TMR0=0;
while(GP2==0)
{}
tmp1=TMR0;
DelayUs(10);
GP5=0;
while(GP4==0)
{}
DelayUs(n);
CLRWDT();
GP5=1;
DelayUs(10);
GP5=0;
if(tmp0>tmp1)
{tmp2=tmp0-tmp1;
n-=tmp2;
tmp0=tmp1;
if(n<=0)
{ m++;
tmp3=tmp1;
tmp3>>=1;
tmp4=tmp1;
}
else if((n<=0)&&(m>1))
{ if(tmp3<=(tmp2-tmp4))
{
do{ GP5=0;
CLRWDT();
}while(1);
}
else
n=0;
m=1;
}
}
else
{tmp2=tmp1-tmp0;
n+=tmp2;
tmp0=tmp1;
if(n>200)
{ n=200;
}
}
}while(1);
}
此帖出自
小平头技术问答
一周热门 更多>