?? 基本實現2.txt
字號:
/*****************************************************************/
/*
/* 熱釋紅外探測人體,USB攝像頭進行觀察
/*
/*
/*
/*
/******************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
uchar code FFW1[4]={0x03,0x09,0x0c,0x06};//正轉數組
uchar code REV1[4]={0x06,0x0c,0x09,0x03};//反轉數組
uchar code FFW2[4]={0x30,0x90,0xc0,0x60};//正轉數組
uchar code REV2[4]={0x60,0xc0,0x90,0x30};//反轉數組
bit StartFlag1=0;
bit StartFlag2=0;
bit SingalFlag=0;
sbit sensor_signal=P3^3;
sbit photoelectric_switch=P3^2;
uchar rate=0xff;
uchar motorstep1=0;
uchar motorstep2=0;
uchar con_step;
uchar count1;
uchar count2;
uchar j=0;
bit con_direction;
uchar m,n;
/********************************************************/
/*
/* 延時 1ms
/* 11.0592MHz時鐘,
/*
/********************************************************/
void delay()
{
uchar k;
uint s;
k = rate;
do
{
for(s = 0 ; s <100 ; s++) ;
}while(--k);
}
/********************************************************/
/*
/*步進電機正轉
/*
/********************************************************/
void motor2_ffw()
{
uchar i;
for (i=0; i<4; i++) //一個周期轉30度
{ motorstep2++;
if(motorstep2==1)EX1=1;
P1 = FFW2[i];//取數據
delay(); //調節轉速
}
}
/********************************************************/
/*
/*步進電機反轉
/*
/********************************************************/
void motor2_rev()
{
uchar i;
for (i=0; i<4; i++) //一個周期轉30度
{ motorstep2++;
if(motorstep2==1)EX1=1;
P1 = REV2[i]; //取數據
delay(); //調節轉速
}
}
/********************************************************
*
* 主程序
*
*********************************************************/
void main(void)
{
uint a,b,c;
/*******************定時器0初始化************************/
count1=0;
TMOD=0x61;
TH0=-50000/256;
TL0=-50000%256;
PT0=0;
ET0=1;
TR0=1;
EA=1;
/*****************定時器1初始化**************************/
TL1=0xff;
TH1=0xff;
ET1=1;
TR1=1;
PT1=1;
/******************外部中斷0初始化——光電對管1**********************/
EX0=1;
PX0=0;
IT0=0;
EA=1;
/******************外部中斷1初始化——光電對管2********************/
EX1=1;
PX1=0;
IT1=0;
EA=1;
/******************************************************/
while(1)
{
if(SingalFlag==1)
{
EA=0;
con_direction=StartFlag1;
con_step=motorstep1;
//MOTOR2轉至信號輸入方向
if(con_direction==1) {
do{motor2_rev();}while(motorstep2<=con_step);
}
else {
do
{motor2_ffw();}while(motorstep2<=con_step);
}
EA=1;
//延時1000*10ms=10s
for(a=0;a<=1000;a++)
for(b=0;b<=10;b++)
for(c=0;c<=120;c++);
for(a=0;a<=20;a++) {ET0=0; ET1=0;}
//返回致零角度
if(con_direction==1) { do{motor2_ffw();}while(motorstep2!=0) ;
}
else if(con_direction==0) { do{motor2_rev();}while(motorstep2!=0);}
for(a=0;a<=20;a++){ET0=1;ET1=1;}
SingalFlag=0;
}
}
}
/********************************************************外部0中斷服務——MOTOR1零角度中斷*****************/
int0_srv() interrupt 0 using 0 //電機起始定位ISP
{
EX0=0; //關中斷0
StartFlag1^=1;
motorstep1=0;
//開中斷
}
/**********************************************************外部中斷1服務—— **********************************/
int1_srv() interrupt 2 using 2
{
EX1=0;
StartFlag2^=1;
motorstep2=0;
}
/********************************************定時器0中斷服務——MOTOR1步進中斷***************************************************/
void timer0(void) interrupt 1 using 1
{
TH0=-50000/256;
TL0=-50000%256;
if( StartFlag1==1)P1 = REV1[j];
else P1 = FFW1[j];
count1++;
if(count1==10)
{count1=0;
motorstep1++;
if(motorstep1==12)EX0=1;
j++;
if(j==4){j=0;}
}
}
/********************************定時器3——熱釋紅外輸入服務**************************************/
void timer1(void) interrupt 3 using 3
{
SingalFlag=1;
con_step=motorstep1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -