📄 circle.c
字号:
#include <reg51.h>
#include <math.h>
#include <intrins.h>
#define pa (*(char xdata *) 0xa000)
#define pb (*(char xdata *) 0xa001)
#define cr (*(char xdata *) 0xa003)
#define pc (*(char xdata *) 0xa002)
#define dispdata (char xdata *) 0x8004
#define dispkey (char xdata *) 0x8000
#define dispbit (*(char xdata *) 0xa000)
#define uc unsigned char
#define ui unsigned int
#define step1 0.0320
#define step2 0.0323
sbit ledflash=P1^0;
sbit ring=P1^1;
void initial(void);
void keyoption(uc key);
uc keytest(void);
uc keyscan(void);
void serialr(void);
void data_option(void);
void delay(ui tt);
void ldriver(uc ldirect,uc t1);
void rdriver(uc rdirect,uc t2);
void drawcircle(uc x0,uc y0,uc r);
void drawRectangle(uc x1,uc y1,uc width,uc high);
void moveto(uc xx1 ,uc yy1 ,uc xx2 ,uc yy2);
ui countl(uc xa,uc ya,uc xb,uc yb);
ui countr(uc xa,uc ya,uc xb,uc yb);
void deriver(ui lnum,ui rnum);
uc rec[19];
uc inputbuff[8];
uc t2mscou,tled;
ui t100mscou,t15scou;
uc recbuff,n;
bit recable,movestop,timestop,ld,rd;
bit inputstate;
//uc code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};/*共阴极 */
void main()
{ uc key;
initial();
while(1)
{
if(movestop==0)
{
data_option();
}
else
{
if(keytest())
{
delay(5);
if(keytest())
{ t15scou=0;
key=keyscan();
keyoption(key);
}
}
}
}
}
//-------------------------------
void serialr() interrupt 4 using 0
{
ES=0;
if(RI==1)
{ RI=0;
recbuff=SBUF;
recbuff=recbuff&0x7f;
if(recbuff==0x05)
{
if(movestop==1)
{ TI=0;
SBUF=0x0a;
while(!TI)
{;}
TI=0;
}
}
if(recbuff==0x02)
{
recable=1;
}
if(recable==1)
{
rec[n]=recbuff;
n++;
if(recbuff==0x0d)
{
movestop=0;
n=0;
recable=0;
}
}
}
ES=1;
return;
}
//---------------------------------
void initial()
{
TMOD=0x21;
PCON=0x80;
TH1=0xf3;
TL1=0xf3;
TH0=-1000/256;
TL0=-1000%256;
SCON=0x50;
TR0=1;
EA=1;
EX0=0;
EX1=0;
ET1=0;
ET0=1;
ES=1;
cr=0x81;
movestop=1;
timestop=1;
ledflash=1;
ring=1;
TR1=1;
return;
}
//---------------------------------
void delay(ui tt)
{ ui j;
while(tt)
{
for(j=0;j<248;j++)
{;}
tt--;
}
return;
}
//---------------------------------
//---------------------------------
void data_option()
{ ui i,impl,impr;
for(i=0;i<19;i++)
{ TI=0;
SBUF=rec[i];
while(!TI)
{;}
TI=0;
}
switch(rec[17])
{
case(0x07):{t100mscou=0;timestop=0;break;}
case(0x12):{timestop=1;break;}
case(0x09):{timestop=1;t100mscou=0;break;}
default:{break;}
}
for(i=2;i<7;i++)
{
rec[i]=rec[i] & 0x0f;
}
for(i=8;i<13;i++)
{
rec[i]=rec[i] & 0x0f;
}
impl=rec[2]*10000+rec[3]*1000+rec[4]*100+rec[5]*10+rec[6];
impr=rec[8]*10000+rec[9]*1000+rec[10]*100+rec[11]*10+rec[12];
i=0;
while((i<impl)||(i<impr))
{
if(i<=impl)
{
if(rec[1]==0x06)
{
ldriver(1,2);
}
else
{
ldriver(0,2);
}
}
if(i<=impr)
{
if(rec[7]==0x06)
{
rdriver(1,2);
}
else
{
rdriver(0,2);
}
}
i++;
}
movestop=1;
return;
}
//---------------------------------
void timer0() interrupt 1 using 1
{
uc h;
ES=0;
TH0=-1000/256;
TL0=-1000%256;
tled++;
t15scou++;
if((timestop==0)||(movestop==0))
{
t2mscou++;
if(t2mscou==50)
{
t100mscou++;
t2mscou=0;
*dispdata=((t100mscou/10)/10)/10;
*(dispdata+1)=((t100mscou/10)/10)%10;
*(dispdata+2)=(t100mscou/10)%10;
*(dispdata+3)=t100mscou%10;
}
}
if(inputstate==1)
{
if(tled==125)
{tled=0;
ledflash=~ledflash;}
}
else if(tled==250)
{
tled=0;
ledflash=~ledflash;
}
if((t15scou==7500)&&(inputstate==1))
{
t15scou=0;
inputstate=0;
for(h=0;h<8;h++)
{
inputbuff[h]=0;
*(dispdata+h)=0;
}
}
ES=1;
return;
}
//---------------------------------
void ldriver(uc ldirect,uc t1)
{
static uc lcode=0x11;
pa=lcode;
if(ldirect==0)
{lcode=_crol_(lcode,1);}
else
{lcode=_cror_(lcode,1);}
delay(t1);
return;
}
//---------------------------------
void rdriver(uc rdirect,uc t2)
{
static uc rcode=0x11;
pb=rcode;
if(rdirect==1)
{rcode=_crol_(rcode,1);}
else
{rcode=_cror_(rcode,1);}
delay(t2);
return;
}
//---------------------------------
uc keytest(void)
{ uc temp;
pc=0x0f;
temp=pc;
temp=(temp&0x0f)|0xf0;
return(~temp);
}
//---------------------------------
uc keyscan(void)
{
uc sch,rch,temp;
sch=0x7f;
while((sch&0x08)!=0)
{
pc=sch;
temp=pc;
if((temp&0x0f)!=0x0f)
{
rch=(temp&0x0f)|0xf0;
return((~sch)+(~rch));
}
else {sch=(sch>>1)|0x80;}
}
return(0);
}
//---------------------------------
void keyoption(uc key)
{ uc keyvalue;
static uc movetype=0;
uc h=0;
switch(key)
{
case(0x88):keyvalue=13;key=0;break;
case(0x84):keyvalue=7;key=0;break;
case(0x82):keyvalue=4;key=0;break;
case(0x81):keyvalue=1;key=0;break;
case(0x48):keyvalue=0;key=0;break;
case(0x44):keyvalue=8;key=0;break;
case(0x42):keyvalue=5;key=0;break;
case(0x41):keyvalue=2;key=0;break;
case(0x28):keyvalue=14;key=0;break;
case(0x24):keyvalue=9;key=0;break;
case(0x22):keyvalue=6;key=0;break;
case(0x21):keyvalue=3;key=0;break;
case(0x18):keyvalue=15;key=0;break;
case(0x14):keyvalue=12;key=0;break;
case(0x12):keyvalue=11;key=0;break;
case(0x11):keyvalue=10;key=0;
inputstate=~inputstate;
for(h=0;h<8;h++)
{inputbuff[h]=0;
*(dispkey+h)=0;}
for(h=0;h<4;h++)
{
ring=~ring;
delay(150);
}
ring=1;
while(keytest())
{;}
break;
}
if(inputstate==0)
{
switch(keyvalue)
{
case(13):ldriver(1,1);ldriver(1,1);ldriver(1,1);break;
case(0):ldriver(0,1);ldriver(0,1);ldriver(0,1);break;
case(14):rdriver(0,1);rdriver(0,1);rdriver(0,1);break;
case(15):rdriver(1,1);rdriver(1,1);rdriver(1,1);break;
default:break;
}
}
else if(keyvalue==11)
{
movetype++;
if(movetype==3)
{movetype=0;}
*dispkey=movetype;
ring=0;
delay(30);
ring=1;
while(keytest())
{;}
}
else if(keyvalue==14)
{
inputstate=0;
movestop=0;
t100mscou=0;
ring=0;
delay(30);
ring=1;
switch(movetype)
{
case(0):moveto((inputbuff[0]*10+inputbuff[1]),(inputbuff[2]*10+inputbuff[3]),(inputbuff[4]*10+inputbuff[5]),(inputbuff[6]*10+inputbuff[7]));break;
case(1):drawcircle((inputbuff[2]*10+inputbuff[3]),(inputbuff[4]*10+inputbuff[5]),(inputbuff[6]*10+inputbuff[7]));break;
case(2):drawRectangle((inputbuff[0]*10+inputbuff[1]),(inputbuff[2]*10+inputbuff[3]),(inputbuff[4]*10+inputbuff[5]),(inputbuff[6]*10+inputbuff[7]));break;
}
movestop=1;
for(h=0;h<8;h++)
{
ring=~ring;
delay(150);
}
ring=1;
while(keytest())
{;}
}
else if(keyvalue==12)
{
for(h=0;h<7;h++)
{
inputbuff[7-h]=inputbuff[6-h];
}
inputbuff[0]=0;
for(h=0;h<8;h++)
{*(dispkey+h)=inputbuff[h];}
ring=0;
delay(30);
ring=1;
while(keytest())
{;}
}
else if(keyvalue<10)
{
for(h=0;h<7;h++)
{
inputbuff[h]=inputbuff[h+1];
}
inputbuff[7]=keyvalue;
for(h=0;h<8;h++)
{*(dispkey+h)=inputbuff[h];}
ring=0;
delay(30);
ring=1;
while(keytest())
{;}
}
else
{;}
return;
}
void drawcircle(uc x0,uc y0,uc r)
{
uc xvalue[2];
uc yvalue[2];
uc i,xs,ys;
xs=x0-r;
ys=y0;
xvalue[0]=x0-r;
yvalue[0]=y0;
*dispkey=(uc)xvalue[0]/10;
*(dispkey+1)=(uc)xvalue[0]%10;
*(dispkey+2)=(uc)yvalue[0]/10;
*(dispkey+3)=(uc)yvalue[0]%10;
for(i=1;i<=r;i++)
{ //第2象限
xvalue[1]=xs+i;
yvalue[1]=ys+(uc)sqrt(2*r*i-i*i);
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
xvalue[0]=xvalue[1];
yvalue[0]=yvalue[1];
}
xs=x0;
ys=y0;
for(i=1;i<=r;i++) //第1象限
{
xvalue[1]=xs+i;
yvalue[1]=ys+(uc)sqrt(r*r-i*i);
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
xvalue[0]=xvalue[1];
yvalue[0]=yvalue[1];
}
xs=x0+r; //第4象限
ys=y0;
for(i=1;i<=r;i++)
{
xvalue[1]=xs-i;
yvalue[1]=ys-(uc)sqrt(2*r*i-i*i);
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
xvalue[0]=xvalue[1];
yvalue[0]=yvalue[1];
}
xs=x0; //第3象限
ys=y0;
for(i=1;i<=r;i++)
{
xvalue[1]=xs-i;
yvalue[1]=ys-(uc)sqrt(r*r-i*i);
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
xvalue[0]=xvalue[1];
yvalue[0]=yvalue[1];
}
return;
}
ui countl(uc xa,uc ya,uc xb,uc yb)
{
float l1,l2;
ui lk;
l1=sqrt((xa+15)*(xa+15)+(115-ya)*(115-ya));
l2=sqrt((xb+15)*(xb+15)+(115-yb)*(115-yb));
if(l2>l1)
{
ld=1;
lk=(ui)((l2-l1)/step1);
}
else
{
ld=0;
lk=(ui)((l1-l2)/step1);
}
return(lk);
}
ui countr(uc xa,uc ya,uc xb,uc yb)
{
float l1,l2;
ui rk;
l1=sqrt((95-xa)*(95-xa)+(115-ya)*(115-ya));
l2=sqrt((95-xb)*(95-xb)+(115-yb)*(115-yb));
if(l2>l1)
{
rd=1;
rk=(ui)((l2-l1)/step2);
}
else
{
rd=0;
rk=(ui)((l1-l2)/step2);
}
return(rk);
}
void deriver(ui lnum,ui rnum)
{
ui j;
j=0 ;
while((j<lnum)||(j<rnum))
{
if(j<=lnum)
{
ldriver(ld,4);
}
if(j<=rnum)
{
rdriver(rd,4);
}
j++;
}
return;
}
void drawRectangle(uc x1,uc y1,uc width,uc high)
{
uc xvalue[2];
uc yvalue[2];
uc j;
xvalue[0]=x1;
yvalue[0]=y1;
yvalue[1]=y1;
*dispkey=(uc)xvalue[0]/10;
*(dispkey+1)=(uc)xvalue[0]%10;
*(dispkey+2)=(uc)yvalue[0]/10;
*(dispkey+3)=(uc)yvalue[0]%10;
for(j=1;j<=width;j++)
{
xvalue[1]=x1+j;
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
xvalue[0]=xvalue[1];
}
for(j=1;j<=high;j++)
{
yvalue[1]=y1+j;
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
yvalue[0]=yvalue[1];
}
for(j=1;j<=width;j++)
{
xvalue[1]=x1+width-j;
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*dispkey=(uc)xvalue[1]/10;
*(dispkey+1)=(uc)xvalue[1]%10;
xvalue[0]=xvalue[1];
}
for(j=1;j<=high;j++)
{
yvalue[1]=y1+high-j;
moveto(xvalue[0],yvalue[0],xvalue[1],yvalue[1]);
*(dispkey+2)=(uc)yvalue[1]/10;
*(dispkey+3)=(uc)yvalue[1]%10;
xvalue[0]=xvalue[1];
yvalue[0]=yvalue[1];
}
}
void moveto(uc xx1 ,uc yy1 ,uc xx2 ,uc yy2)
{ ui lim,rim;
lim=countl(xx1,yy1,xx2,yy2);
rim=countr(xx1,yy1,xx2,yy2);
deriver(lim,rim);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -