#include <STC15F2K60S2.h>
#include "uart.h"
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define JCJ1_setp P21
#define JCJ2_setp P21
#define JCJ3_setp P21
bit JCJ1_guding = 0;
bit JCJ2_guding = 0;
bit JCJ3_guding = 0;
bit xunhuan = 0;
bit INT0_flag = 0;
uint pow(uchar j);
uchar UART_DATA[6] = {0,0,0,0,0,0}; //接收缓存数组
uchar TH1_JCJ1 = 0; //挤出机1四位数据
uchar TL1_JCJ1 = 0;
uchar TH2_JCJ1 = 0;
uchar TL2_JCJ1 = 0;
uchar TH1_JCJ2 = 0; //挤出机2四位数据
uchar TL1_JCJ2 = 0;
uchar TH2_JCJ2 = 0;
uchar TL2_JCJ2 = 0;
uchar TH1_JCJ3 = 0; //挤出机3四位数据
uchar TL1_JCJ3 = 0;
uchar TH2_JCJ3 = 0;
uchar TL2_JCJ3 = 0;
uint JCJ1 = 0; //整合后的十六进制挤出机1数据
uint JCJ2 = 0; //整合后的十六进制挤出机2数据
uint JCJ3 = 0; //整合后的十六进制挤出机3数据
uint count_2ms = 0; //定时2ms
uint count_1min = 0; //定时1min
/***** 定时器初始化 *****/
void Timer0Init(void) //2毫秒@22.1184MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x33; //设置定时初值
TH0 = 0x53; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA = 1; //开总中断
ES = 1; //开串口1中断
ET0 = 1; //开定时器0中断
EX0 = 1; //开外部0中断,p32为外部中断0接口
IT0 = 1;
}
/**************************************************************************
主函数
1、本模块只有在接收到外部工作信号来临时,才会进入工作状态。
2、在工作状态中,若没有相关定时工作,则默认为电机1工作。
3、设定循环时间,只有在触发了循环时,才能进行循环定时操作。
4、在工作状态中,设为循环模式下,若没有相关的定时操作,则默认为电机1工作。
5、锁定模式下,锁定该电机一直运行。
***************************************************************************/
void main()
{
bit flag = 1;
bit JCJ1_count = 0;
bit JCJ2_count = 0;
bit JCJ3_count = 0;
bit JCJ1_flag = 1;
bit JCJ2_flag = 1;
bit JCJ3_flag = 1;
bit JCJ1_count_flag = 1;
bit JCJ2_count_flag = 1;
bit JCJ3_count_flag = 1;
P1M1 = 0x00; P1M0 = 0x00;
P3M1 = 0x00; P3M0 = 0x00;
UART_init();
Timer0Init();
JCJ1_setp = 0; //默认开启电机1
JCJ2_setp = 1;
JCJ3_setp = 1;
while(1)
{
if(INT0_flag) //检测挤出机是否进入工作状态
{
/***** 进入定时或锁定模式 *****/
if(xunhuan == 0)
{
/***** 挤出机1 *****/
count_2ms = 0;
count_1min = 0;
while(((count_1min < JCJ1)&&(JCJ1_flag)&&(JCJ1_count_flag)) || (JCJ1_guding))
{
JCJ1_setp = 0; //开启电机1
JCJ2_setp = 1;
JCJ3_setp = 1;
if((JCJ2 > 0)&&(flag))
{
flag = 0;
JCJ2_flag = 1;
JCJ3_flag = 0;
}
else if((JCJ3 > 0)&&(flag))
{
flag = 0;
JCJ2_flag = 0;
JCJ3_flag = 1;
}
if(JCJ1_guding == 0)
JCJ1_count = 1;
}
if(JCJ1_count)
{
JCJ1_count = 0;
JCJ1 = 0;
JCJ1_count_flag = 0;
}
flag = 1;
/***** 挤出机2 *****/
count_2ms = 0;
count_1min = 0;
while(((count_1min < JCJ2)&&(JCJ2_flag)&&(JCJ2_count_flag)) || (JCJ2_guding))
{
JCJ1_setp = 1; //开启电机2
JCJ2_setp = 0;
JCJ3_setp = 1;
if((JCJ1 > 0)&&(flag))
{
flag = 0;
JCJ1_flag = 1;
JCJ3_flag = 0;
}
else if((JCJ3 > 0)&&(flag))
{
flag = 0;
JCJ1_flag = 0;
JCJ3_flag = 1;
}
if(JCJ2_guding == 0)
JCJ2_count = 1;
}
if(JCJ2_count)
{
JCJ2_count = 0;
JCJ2 = 0;
JCJ2_count_flag = 0;
}
flag = 1;
/***** 挤出机3 *****/
count_2ms = 0;
count_1min = 0;
while(((count_1min < JCJ3)&&(JCJ3_flag)&&(JCJ3_count_flag)) || (JCJ3_guding))
{
JCJ1_setp = 1; //开启电机3
JCJ2_setp = 1;
JCJ3_setp = 0;
if((JCJ1 > 0)&&(flag))
{
flag = 0;
JCJ1_flag = 1;
JCJ2_flag = 0;
}
else if((JCJ2 > 0)&&(flag))
{
flag = 0;
JCJ1_flag = 0;
JCJ2_count_flag = 1;
}
if(JCJ3_guding == 0)
JCJ3_count = 1;
}
if(JCJ3_count)
{
JCJ3_count = 0;
JCJ3 = 0;
JCJ3_count_flag = 0;
}
flag = 1;
}
/***** 进入循环模式 *****/
else if(xunhuan)
{
/***** 挤出机1 *****/
count_2ms = 0;
count_1min = 0;
while(count_1min < JCJ1)
{
JCJ1_setp = 0; //开启电机1
JCJ2_setp = 1;
JCJ3_setp = 1;
}
/***** 挤出机2 *****/
count_2ms = 0;
count_1min = 0;
while(count_1min < JCJ2)
{
JCJ1_setp = 1; //开启电机2
JCJ2_setp = 0;
JCJ3_setp = 1;
}
/***** 挤出机3 *****/
count_2ms = 0;
count_1min = 0;
while(count_1min < JCJ3)
{
JCJ1_setp = 1; //开启电机3
JCJ2_setp = 1;
JCJ3_setp = 0;
}
}
}
}
}
/***** 2ms定时中断函数 *****/
void timer0_Routine() interrupt 1
{
TF0 = 0;
count_2ms++; //定时2ms
if(count_2ms>=500)
{
count_2ms = 0;
count_1min++;
}
}
/***** 外部中断0函数 *****/
void Int0_Routine() interrupt 0
{
EX0 = 0;
IE0 = 0;
INT0_flag = 1;
}
/***** 串口中断函数 *****/
void UART1_Routine() interrupt 4
{
uchar n,k;
uchar a;
if(RI)
{
RI = 0;
UART_DATA[n++] = SBUF;
if(UART_DATA[n-1] == 0xff)
{
/***** 判断是否进入循环模式 *****/
if(UART_DATA[0] == 0xab)
{
xunhuan = 1;
JCJ1 = 0;
JCJ2 = 0;
JCJ3 = 0;
for(k=0;k<6;k++)
{
UART_DATA[k] = 0;
}
n = 0;
}
else if(UART_DATA[0] == 0xac)
{
xunhuan = 0;
JCJ1 = 0;
JCJ2 = 0;
JCJ3 = 0;
for(k=0;k<6;k++)
{
UART_DATA[k] = 0;
}
n = 0;
}
/***** 挤出机1定时 *****/
else if(UART_DATA[0] == 0xa1)
{
if(UART_DATA[1] == 0xb1) //判断是否进入挤出机1锁定模式
{
JCJ1_guding = 1;
JCJ2_guding = 0;
JCJ3_guding = 0;
TH1_JCJ1 = 0x30;
}
else if(UART_DATA[1] != 0xff) //判断是否进入挤出机1定时
{
JCJ1_guding = 0;
JCJ2_guding = 0;
JCJ3_guding = 0;
count_1min = 0;
TH1_JCJ1 = UART_DATA[1];
}
else
{
TH1_JCJ1 = 0x30;
a = 0;
}
if(UART_DATA[2] != 0xff)
{
TL1_JCJ1 = UART_DATA[2];
}
else
{
TL1_JCJ1 = 0x30;
a = 0;
}
if(UART_DATA[3] != 0xff)
{
TH2_JCJ1 = UART_DATA[3];
}
else
{
TH2_JCJ1 = 0x30;
a = 1;
}
if(UART_DATA[4] != 0xff)
{
TL2_JCJ1 = UART_DATA[4];
}
else
{
TL2_JCJ1 = 0x30;
a = 2;
}
if(UART_DATA[5] == 0xff)
{
a = 3;
}
JCJ1 = (TH1_JCJ1-0x30)*pow(a)+(TL1_JCJ1-0x30)*pow(a-1)+(TH2_JCJ1-0x30)*pow(a-2)+(TL2_JCJ1-0x30)*pow(a-3);
for(k=0;k<6;k++)
{
UART_DATA[k] = 0;
}
n = 0;
}
/***** 挤出机2定时 *****/
else if(UART_DATA[0] == 0XA2)
{
if(UART_DATA[1] == 0xb2) //判断是否进入挤出机2锁定模式
{
JCJ2_guding = 1;
JCJ1_guding = 0;
JCJ3_guding = 0;
TH1_JCJ2 = 0x30;
}
else if(UART_DATA[1] != 0xff) //判断是否进入挤出机2定时
{
JCJ1_guding = 0;
JCJ2_guding = 0;
JCJ3_guding = 0;
count_1min = 0;
TH1_JCJ2 = UART_DATA[1];
}
else
{
TH1_JCJ2 = 0x30;
a = 0;
}
if(UART_DATA[2] != 0xff)
{
TL1_JCJ2 = UART_DATA[2];
}
else
{
TL1_JCJ2 = 0x30;
a = 0;
}
if(UART_DATA[3] != 0xff)
{
TH2_JCJ2 = UART_DATA[3];
}
else
{
TH2_JCJ2 = 0x30;
a = 1;
}
if(UART_DATA[4] != 0xff)
{
TL2_JCJ2 = UART_DATA[4];
}
else
{
TL2_JCJ2 = 0x30;
a = 2;
}
if(UART_DATA[5] == 0xff)
{
a = 3;
}
JCJ2 = (TH1_JCJ2-0x30)*pow(a)+(TL1_JCJ2-0x30)*pow(a-1)+(TH2_JCJ2-0x30)*pow(a-2)+(TL2_JCJ2-0x30)*pow(a-3);
for(k=0;k<6;k++)
{
UART_DATA[k] = 0;
}
n = 0;
}
/***** 挤出机3定时 *****/
else if(UART_DATA[0] == 0XA3)
{
if(UART_DATA[1] == 0xb3) //判断是否进入挤出机3锁定模式
{
JCJ3_guding = 1;
JCJ1_guding = 0;
JCJ2_guding = 0;
TH1_JCJ3 = 0x30;
}
else if(UART_DATA[1] != 0xff) //判断是否进入挤出机3定时
{
JCJ1_guding = 0;
JCJ2_guding = 0;
JCJ3_guding = 0;
count_1min = 0;
TH1_JCJ3 = UART_DATA[1];
}
else
{
TH1_JCJ3 = 0x30;
a = 0;
}
if(UART_DATA[2] != 0xff)
{
TL1_JCJ3 = UART_DATA[2];
}
else
{
TL1_JCJ3 = 0x30;
a = 0;
}
if(UART_DATA[3] != 0xff)
{
TH2_JCJ3 = UART_DATA[3];
}
else
{
TH2_JCJ3 = 0x30;
a = 1;
}
if(UART_DATA[4] != 0xff)
{
TL2_JCJ3 = UART_DATA[4];
}
else
{
TL2_JCJ3 = 0x30;
a = 2;
}
if(UART_DATA[5] == 0xff)
{
a = 3;
}
JCJ3 = (TH1_JCJ3-0x30)*pow(a)+(TL1_JCJ3-0x30)*pow(a-1)+(TH2_JCJ3-0x30)*pow(a-2)+(TL2_JCJ3-0x30)*pow(a-3);
for(k=0;k<6;k++)
{
UART_DATA[k] = 0;
}
n = 0;
}
/***** 重置 *****/
else if(UART_DATA[0] == 0xad)
{
IAP_CONTR = 0x20; //软复位,系统重新从用户代码区开始运行程序
}
}
}
}
/***** 10的平方函数 *****/
uint pow(uchar j)
{
uint pow_data = 1;
for(;j>0;j--)
{
pow_data *= 10;
}
return pow_data;
}
|