#include
#include
#define uchar unsigned char
#define uint unsigned int
#define ysfs 600 //宏定义 预设风速
#define shuiping 1500 //宏定义 水平位置AD转换结果
#define fuyang 1500 //宏定义 俯仰位置AD转换结果
#define PA XBYTE [0x7CFF] //8255芯片端口4个基地址宏定义
#define PB XBYTE [0x7FFD]
#define PC XBYTE [0x7FFE]
#define CONTROL XBYTE [0x7FFF]
sbit OE=P1^0; //以下为关于AD0808与单片机的八个接口定义
sbit EOC=P1^1;
sbit START=P1^2;
sbit CLK=P1^3;
sbit ALE=P1^4;
sbit C0=P1^5;
sbit C1=P1^6;
sbit C2=P1^7;
sbit gdkg=P2^3; //用于复位光电开关端口定义
sbit clock=P2^0; //定义水平俯仰步进电机的速度端口,频率越快,速度越快,不给频率电机不转
sbit cw1=P2^1; //定义水平步进电机的转向端口,1正转,0反转
sbit enable1=P2^2; //定义水平步进电机的使能端口,1转,0不转
sbit cw2=P2^4; //定义俯仰步进电机的转向端口,1正转,0反转
sbit enable2=P2^5; //定义俯仰步进电机的使能端口,1转,0不转
uint ad_result_fenshu,ad_result_shuiping,ad_result_fuyang;
void timer_init()
{
TMOD=0x11; //定时器模式设置
TH0=(65536-5000)/256;
TL0=(65536-5000)%6;
TH1=0xFF;
TL1=0x9C;
ET0=1; //允许定时器0中断
TR0=1; //启动定时器0
ET1=1; //允许定时器1中断
TR1=1; //启动定时器1
EA=1; //允许总中断
}
void AD_chane(int x)
{
if(x==0)
{
C0=0;
C1=0;
C2=0;
}
else if(x==1)
{
C0=1;
C1=0;
C2=0;
}
else if(x==2)
{
C0=0;
C1=1;
C2=0;
}
ALE=1;
}
int Get_AD_Sd()
{
int Sd;
START=0;START=1;START=0;
while(EOC==0);
OE=1;
Sd=P3;
OE=0;
return Sd;
}
void reset()
{
while(gdkg!=1)
{
cw2=0;
enable2=1;
}
enable2=0;
}
void shuiping_tiaozheng(int date)
{
if(date>shuiping)
{
do
{
cw1=1;
enable1=1;
}
while(date==shuiping);
enable1=0;
}
else if(date {
do
{
cw1=0;
enable1=1;
}
while(date==shuiping);
enable1=0;
}
}
void fuyang_tiaozheng(int date)
{
if(date>fuyang)
{
do
{
cw2=1;
enable2=1;
}
while(date==fuyang);
enable2=0;
}
else if(date {
do
{
cw2=0;
enable2=1;
}
while(date==fuyang);
enable2=0;
}
}
void main()
{
CONTROL = 0x80;
ALE=0;
timer_init();
enable1=0;
enable2=0;
while(1) //进入循环
{
AD_chane(0);
ad_result_fenshu=Get_AD_Sd();
ALE=0;
AD_chane(1);
ad_result_shuiping=Get_AD_Sd();
ALE=0;
AD_chane(2);
ad_result_fuyang=Get_AD_Sd();
ALE=0;
if(ad_result_fenshu>ysfs) //如果风速大于预设风速,则复位
{
reset();
}
shuiping_tiaozheng(ad_result_shuiping);
fuyang_tiaozheng(ad_result_fuyang);
}
}
void timer0() interrupt 1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%6;
CLK=~CLK;
}
void timer1() interrupt 3
{
static uchar i=0;
TH1=0xFF;
TL1=0x9C;
++i;
if (i>10) i=0;
if (i<=6)
clock=0;
else
clock=1;
}