/********************************************************************/
#include<stc12.h> //库文件
//#include <intrins.H>
//typedef unsigned char BYTE;
//typedef unsigned int WORD;
#define uchar unsigned char//宏定义无符号字符型
#define uint unsigned int //宏定义无符号整型
//-----------------------------------------------
/* define constants */
#define SYSclk 12000000L
#define MODE1T //Timer clock mode, comment this line is 12T mode, uncomment is 1T mode
#ifdef MODE1T
#define T1MS (65536-SYSclk/1000) //1ms timer calculation method in 1T mode
#else
#define T1MS (65536-SYSclk/12/1000) //1ms timer calculation method in 12T mode
#endif
/********************************************************************
I/O定义
*********************************************************************/
sbit MAX =P3^0;//低电平有效
sbit MIN =P3^1;//低电平有效
sbit GATE =P3^5;//低电平有效
sbit UP =P3^3;//高电平有效
sbit DOWN =P3^4;//高电平有效
//---------------------------------------------------------
#define DELAY_TIME 5 //单位100mS
#define DELAY_TIME2 5 //单位100mS
//---------------------------------------------------------
//---------------------------------------------------------
//-------------------变量定义------------------------------
//---------------------------------------------------------
volatile uint tcnt_100ms = 0;
volatile uint tcnt_1ms = 0;
volatile uint t_delay_cnt = 0;
bit GATE_old;
void Delay100ms() //12.000MHz
{
uchar i, j, k;
// _nop_();
i = 5;
j = 144;
k = 71;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
/*
void Delay1000ms() //@12.000MHz
{
unsigned char i, j, k;
//_nop_();
//_nop_();
i = 46;
j = 153;
k = 245;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
*/
/********************************************************************
定时中断服务函数
*********************************************************************/
void t0(void) interrupt 1 using 0 //定时中断服务函数
{
tcnt_1ms++;
if(tcnt_1ms>=100)
{
tcnt_1ms = 0;
tcnt_100ms++;
if(t_delay_cnt<50)
{
t_delay_cnt++;
}
}
}
/********************************************************************
主函数
*********************************************************************/
//-----------------------------------------------
/* main program */
void main()
{
//开机空运行300mS,等待时间稳定。
Delay100ms();
Delay100ms();
Delay100ms();
#ifdef MODE1T
AUXR = 0x80; //使用1T模式
//timer0 work in 1T mode
#endif
TMOD = 0x00;
//set timer0 as mode0 (16-bit auto-reload)
TL0 = T1MS;
//initial timer0 low byte
TH0 = T1MS >> 8;
//initial timer0 high byte
TR0 = 1;
//timer0 start running
ET0 = 1;
//enable timer0 interrupt
EA = 1;
//open global interrupt switch
P3M0 = 0x1C; // P3.2,P3.3,P3.4是强推挽输出
P3M1 = 0x00;
while(1)
{
if(GATE != GATE_old)
{
t_delay_cnt = 0;
GATE_old = GATE;
}
if(!GATE)//开锁状态(低)--默认为高
{
if(MAX)//限位打开
{
if(t_delay_cnt >= DELAY_TIME)
{
UP=0;
DOWN=0;//停止
}
else
{
UP=1;
DOWN=0;//正转
}
}
else//限位闭合
{
UP=0;
DOWN=0;//停止
}
}
else
{
if(MIN)//限位打开
{
if(t_delay_cnt >= DELAY_TIME2)
{
UP=0;
DOWN=0;//停止
}
else
{
DOWN=1;
UP=0;//反转
}
}
else//限位闭合
{
DOWN=0;
UP=0;//停止
}
}
}
}
/********************************************************************
结束
*********************************************************************/
|