#include <REGX51.H>
//#include <AT89X55.H>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//for key
sbit dio=P1^0;
sbit clk=P1^1;
sbit cs=P1^2;
sbit key=P3^2;
bit ctr = 0;
//for moto
sbit direct=P0^5;
sbit pulse=P0^4;
//for light
sbit led=P3^5;
void removedlms(void) //去抖动
{
uchar i;
for(i=200;i>0;i--)
{}
}
void delay(ulong count) //延时函数
{
for(;count>0;count--)
{}
}
void send(unsigned char send_buf,bit cs_sign) //按位发送数据函数
{
unsigned char i;
cs=0;
delay(25);
for(i=0;i<8;i++,send_buf<<=1)
{
dio=(send_buf&0x80)?1:0;
clk=1;
delay(5);
clk=0;
delay(5);
}
delay(10);
cs=cs_sign;
delay(25);
}
unsigned char receive() //接收键码函数
{
unsigned char rece_buf=0;
unsigned char j;
clk=0;
dio=1;
delay(25);
for(j=0;j<8;j++)
{
clk=1;
delay(5);
rece_buf=(rece_buf<<1)|dio;
clk=0;
delay(5);
}
dio=0;
cs=1;
delay(25);
return rece_buf;
}
void zhuan(uchar step)
{
ulong i;
for(i=0;i<2000;i++)//改变转动的角度
{
delay(step);
pulse=!pulse;
}
}
main() //主函数
{
uchar step;
uchar reled;
uchar rekey;
uchar num = 0,num1 =0,num2=0;
send(0xA4,1); //ZLG7289复位
step=4500;
direct=0;
pulse=0;
while(1)
{
reled=led;
if(reled!=led&&led==0)
{
direct=!direct;
num = num+1;
zhuan(step);
delay(50000);//改变数值改变延时时间
direct=!direct;
//反转
zhuan(step);
num1=num/10;
num2=num%10;
send(0x86,0);
send(num1,1);
delay(25);
send(0x87,0);
send(num2,1);
delay(25);
}
if(key==0)
{
removedlms();
send(0x15,0);
rekey=receive();
while(key==0);
switch(rekey)
{
case 36:
num = 0;
send(0x86,0);
send(0,1);
delay(25);
send(0x87,0);
send(0,1);
delay(25);
break;
default:
break;
}
}
}
}
评论0