*/
#include<STC12C5A60S2.h>
#define uchar unsigned char
#define uint unsigned int
#define Delaytime 100 //消抖
#define Delaybeemp 10
typedef signed long INT64S;
typedef unsigned long INT64U;
typedef signed int INT32S;
typedef unsigned int INT32U;
typedef signed short INT16S;
typedef unsigned short INT16U;
typedef signed char INT8S;
typedef unsigned char INT8U;
/*IO口定义*/
sbit out_upper=P0^0; //接电磁阀
sbit out_lower = P0^2; //不用
sbit Buzzer = P0^1; //不用
sbit Upper_limit=P2^4; //(接光膜传感器)
sbit Lower_limit = P2^5; //检测电磁到位开关
sbit button = P2^6; //按钮S2
sbit button1 = P2^7; //按钮S1
typedef struct tagTYPE_UART_BUFFER
{
INT8U ucFlag;
INT8U ucIndex;
INT8U ucLength;
INT8U ucBuffer[20];
}TYPE_UART_BUFFER, *pTYPE_UART_BUFFER;
typedef struct tagTYPE_UART_INFO
{
TYPE_UART_BUFFER sRX;
TYPE_UART_BUFFER sTX;
}TYPE_UART_INFO, *pTYPE_UART_INFO;
// ---------------------------------------------
unsigned char button_flag,jishu_flag,Upper_limit_flag,Lower_limit_flag;
unsigned char delay = Delaytime;
TYPE_UART_INFO xdata sUART0_Info = {0};
void sendonebyte(void);
/*
函数功能:串口初始化,
波特率:38400 工作在1T模式,使用定时器1计数
*/
void InitUART(void)
{
TMOD = 0x21;
SCON = 0x50;
AUXR |= 0x40;//使其工作在1T模式下
TH1 = 0xF7;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
//定时器0 定时时间:10ms
TH0 = 0x0FF;
TL0 = 0x0F6;
EA = 1;
ET0 = 1;
TR0 = 1;
//初始化输入输出的io
P0M0 = 0xff;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0xff;
}
/*
函数功能:延时时间为10ms
*/
void delay_10ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void main(void)
{
uchar i,flag=0;
// jishu_flag = 0;
InitUART();
out_upper = 1;//此时汽缸向上动作
out_lower = 1;
Upper_limit_flag = 0; // 光幕传感器
Lower_limit_flag = 0; //到位开关
button_flag = 0; //按钮S1
button1_flag = 0; //按钮S2
while(1)
{
// if(sUART0_Info.sRX.ucFlag == 1)
// {
// sUART0_Info.sRX.ucFlag = 0;
// out_upper = 1;//此时汽缸向上动作
// out_lower = 1;
// for(i = 0;i<sUART0_Info.sRX.ucLength;i++)
// {
// sUART0_Info.sRX.ucBuffer = 0;
// }
// }
if(Upper_limit_flag == 1) //光幕传感器
{
Upper_limit_flag = 0;
out_upper = 1;//此时汽缸向上动作
out_lower = 1;
flag = 0;
}
if((Lower_limit_flag == 1)&&(flag == 0)) //限位开关和光幕传感器做逻辑与运算
{
Lower_limit_flag = 0; //限位开关为低电位
flag = 1;
out_upper = 1;//此时汽缸向下动作
out_lower = 0;
sendonebyte();
}
if(button_flag == 1)&&(button1_flag == 1) //按钮按下
{
button_flag = 0;
out_upper = 0;//此时汽缸向下动作
out_lower = 1;
}
// if((input==0)&&(jishu_flag == 0))
// {
// delay_10ms();
// if((input==0)&&(jishu_flag == 0))
// {
// output = 0;//把汽缸合上
// jishu_flag = 1;
// overtime = 0;
// while((input_test_qg==1)&&(overtime++ < Delaybeemp-1))
// {
//
// for(i = 0;i< 100;i++)
// {
// delay_10ms();
// }
// }
// if(overtime>=Delaybeemp-1)
// {
// Buzzer = 0;
// }
// else if(input==0)
// {
// sendonebyte();
// }
// }
// }
// if(button_flag == 1)
// {
// button_flag = 0;
// Buzzer = 1;
// output = 1;
// // jishu_flag = 0;
// }
// if(input==1)
// {
// delay_10ms();
// if(input==1)
// {
// jishu_flag = 0;
// Buzzer = 1;
// }
// }
}
}
void ISR_UART0(void)interrupt 4
{
INT8U ucTemp;// 暂存变量
// 接收
if (RI){
// ---------------------------------------------
// 启动接收
RI = 0;
ucTemp = SBUF;
// 判断数据
switch (sUART0_Info.sRX.ucIndex)
{
case 0:// "Z"
if (ucTemp == 'Z')
{
// "Z"
sUART0_Info.sRX.ucBuffer[sUART0_Info.sRX.ucIndex ++] = ucTemp;
}
else
{
// 无效数据
sUART0_Info.sRX.ucIndex = 0;
}
break;
case 1:// "U"
if (ucTemp == 'U')
{
sUART0_Info.sRX.ucBuffer[sUART0_Info.sRX.ucIndex ++] = ucTemp;
}
else
{
// 无效数据
sUART0_Info.sRX.ucIndex = 0;
}
break;
case 2:// "Z"
if (ucTemp == 'Z')
{
// "Z"
sUART0_Info.sRX.ucBuffer[sUART0_Info.sRX.ucIndex ++] = ucTemp;
}
else
{
// 无效数据
sUART0_Info.sRX.ucIndex = 0;
}
break;
default:// 通信数据
// 接收通信数据,
sUART0_Info.sRX.ucBuffer[sUART0_Info.sRX.ucIndex ++] = ucTemp;
// 接收通信数据,判断回车键
if(((ucTemp == '\r')||((ucTemp == '\n')))&&(sUART0_Info.sRX.ucIndex >= 3))
{
sUART0_Info.sRX.ucLength = sUART0_Info.sRX.ucIndex;
sUART0_Info.sRX.ucIndex = 0; //接收完,清零索引
sUART0_Info.sRX.ucFlag = 1;
}
else
{
}
break;
}
// ---------------------------------------------
}
// ---------------------------------------------
if (TI){
// ---------------------------------------------
TI = 0;
// ---------------------------------------------
if (sUART0_Info.sTX.ucIndex < sUART0_Info.sTX.ucLength)
{
SBUF = sUART0_Info.sTX.ucBuffer[sUART0_Info.sTX.ucIndex ++];
}
else
{
sUART0_Info.sTX.ucIndex = 0;
sUART0_Info.sTX.ucLength = 0;
}
// ---------------------------------------------
}
// ---------------------------------------------
}
//10 ms扫描按键
void Timer0Interrupt(void) interrupt 1
{
// 清除标志
TF0 = 0;
TR0 = 0;
TH0 = 0x0FF;
TL0 = 0x0F6;
TR0 = 1;
if((button == 0)||(Upper_limit == 0)||(Lower_limit == 0)) //按钮、光幕传感器、到位开关存在逻辑或的关系
{
delay -- ; //延时消抖
if(!delay)
{
delay = Delaytime;
if(button == 0)
{
button_flag =1;
}
else if(Upper_limit == 0)
{
Upper_limit_flag = 1;
}
else if(Lower_limit == 0)
{
Lower_limit_flag = 1;
}
}
}
//add your code here!
}
void sendonebyte(void)
{
sUART0_Info.sTX.ucBuffer[0] = 'Z';
sUART0_Info.sTX.ucBuffer[1] = 'f';
sUART0_Info.sTX.ucBuffer[2] = 'Z';
sUART0_Info.sTX.ucBuffer[3] = '\r';
sUART0_Info.sTX.ucIndex = 0;
sUART0_Info.sTX.ucLength = 4;
SBUF = sUART0_Info.sTX.ucBuffer[sUART0_Info.sTX.ucIndex ++];
}