|
【声明】此程序仅用于学习与参考,引用请注明版权和作者信息!
*********************************************************************/
/*
一次最多发送20组数据,大于这个数据就不再发送
*/
#include <STC12C5A60S2.H>
#define uchar unsigned char
#define uint unsigned int
/**************************************************************/
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
extern void UartDriver(); //串口驱动函数,监测数据帧的接收,调度功能函数,需在主循环中调用
extern void ConfigUART(unsigned int baud); //串口配置函数,baud-通信波特率
extern void ConfigUART1(unsigned int baud1); //串口配置函数,baud-通信波特率
extern void UartRxMonitor(unsigned char ms); //串口接收监控,由空闲时间判定帧结束,需在定时中断中调用,ms-定时间隔
extern void SendString(char *s) ;
extern void UartWrite(unsigned char *buf, unsigned char len); //串口数据写入,即串口发送函数,buf-待发送数据的指针,len-指定的发送长度
extern void fengming();
uchar zh[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uchar jieshou[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uchar jj = 0;
/*****************************************************************/
sbit out0 = P2^0;
sbit out1 = P2^1;
sbit out2 = P2^2;
sbit out3 = P2^3;
sbit in0 = P2^4;
sbit in1 = P2^5;
sbit in2 = P2^6;
sbit in3 = P2^7;
sbit smgk = P3^6;
sbit boma1 = P1^1;
sbit boma2 = P1^0;
bit d1 = 1;
bit d2 = 1;
sbit in4 = P1^6;
sbit in5 = P1^5;
uchar code xianshi [] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};
uchar dangqianzhuangtai[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
void t0 () interrupt 1
{
uchar i;
static uchar lie = 0;
static uchar sao[2] = {1,1};
static uchar saomiaobaocun [4][4] = {{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff}};
TH0 = 0xfc;
TL0 = 0x67;
UartRxMonitor(1); //串口接收监控
sao[0] = (sao[0]<<1) |in4;
sao[1] = (sao[1]<<1) |in5;
saomiaobaocun [lie][0] = (saomiaobaocun [lie][0]<<1) | in0;
saomiaobaocun [lie][1] = (saomiaobaocun [lie][1]<<1) | in1;
saomiaobaocun [lie][2] = (saomiaobaocun [lie][2]<<1) | in2;
saomiaobaocun [lie][3] = (saomiaobaocun [lie][3]<<1) | in3;
if(sao[0] == 0x00)
{
d1 = 0;
}
else if(sao[0] == 0xff)
{
d1 = 1;
}
if(sao[1] == 0x00)
{
d2 = 0;
}
else if(sao[1] == 0xff)
{
d2 = 1;
}
for (i=0;i<4;i++)
{
if ((saomiaobaocun [lie] & 0x0f) == 0x00)
{
dangqianzhuangtai[lie] = 0;
}
else if ((saomiaobaocun [lie] & 0x0f) == 0x0f)
{
dangqianzhuangtai[lie] = 1;
}
}
lie++;
lie = lie & 0x03;
switch (lie)
{
case 0 : out1 = 1; out2 = 1; out3 = 1; out0 = 0; break;
case 1 : out0 = 1; out2 = 1; out3 = 1; out1 = 0; break;
case 2 : out0 = 1; out1 = 1; out3 = 1; out2 = 0; break;
case 3 : out0 = 1; out1 = 1; out2 = 1; out3 = 0; break;
}
}
void main ()
{
uchar i,j;
bit q1 = 1;
bit q2 = 1;
uchar qianyicizhuangtai[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
smgk = 0;
TMOD = 0x01;
TH0 = 0xfc;
TL0 = 0x67;
TR0 = 1;
EA = 1;
ET0 = 1;
P0 = xianshi[16];
// ConfigUART(9600); //配置波特率为9600
while (1)
{
UartDriver(); //调用串口驱动
if((boma1 == 0)&&(boma2 != 0))
{
ConfigUART(2400); //配置波特率为1200
}
if((boma2 == 0)&&(boma1 != 0))
{
ConfigUART(4800); //配置波特率为4800
}
if((boma1 == 0)&&(boma2 == 0))
{
ConfigUART(9600); //配置波特率为9600
}
if((boma1 != 0)&&(boma2 != 0))
{
ConfigUART1(19200); //配置波特率为19200
}
if(d1 != q1)
{
q1 = d1;
if(d1 == 0)
{
if((jj != 0)&&(jj <= 2))
{
zh[0] = jieshou[0] | jieshou[1];
UartWrite(zh, 1);
fengming();
}
if((jj > 2)&&(jj <= 4))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
UartWrite(zh, 2);
fengming();
}
if((jj > 4)&&(jj <= 6))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
UartWrite(zh, 3);
fengming();
}
if((jj > 6)&&(jj <= 8))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
UartWrite(zh, 4);
fengming();
}
if((jj > 8)&&(jj <= 10))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
UartWrite(zh, 5);
fengming();
}
if((jj > 10)&&(jj <= 12))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
zh[5] = jieshou[10] | jieshou[11];
UartWrite(zh, 6);
fengming();
}
if((jj > 12)&&(jj <= 14))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
zh[5] = jieshou[10] | jieshou[11];
zh[6] = jieshou[12] | jieshou[13];
UartWrite(zh, 7);
fengming();
}
if((jj > 14)&&(jj <= 16))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
zh[5] = jieshou[10] | jieshou[11];
zh[6] = jieshou[12] | jieshou[13];
zh[7] = jieshou[14] | jieshou[15];
UartWrite(zh, 8);
fengming();
}
if((jj > 16)&&(jj <= 18))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
zh[5] = jieshou[10] | jieshou[11];
zh[6] = jieshou[12] | jieshou[13];
zh[7] = jieshou[14] | jieshou[15];
zh[8] = jieshou[16] | jieshou[17];
UartWrite(zh, 9);
fengming();
}
if((jj > 18)&&(jj <= 20))
{
zh[0] = jieshou[0] | jieshou[1];
zh[1] = jieshou[2] | jieshou[3];
zh[2] = jieshou[4] | jieshou[5];
zh[3] = jieshou[6] | jieshou[7];
zh[4] = jieshou[8] | jieshou[9];
zh[5] = jieshou[10] | jieshou[11];
zh[6] = jieshou[12] | jieshou[13];
zh[7] = jieshou[14] | jieshou[15];
zh[8] = jieshou[16] | jieshou[17];
zh[9] = jieshou[18] | jieshou[19];
UartWrite(zh, 10);
fengming();
}
}
}
if(d2 != q2)
{
q2 = d2;
if(d2 == 0)
{
jj = 0;
jieshou[0] = 0x00;
jieshou[1] = 0x00;
jieshou[2] = 0x00;
jieshou[3] = 0x00;
jieshou[4] = 0x00;
jieshou[5] = 0x00;
jieshou[6] = 0x00;
jieshou[7] = 0x00;
jieshou[8] = 0x00;
jieshou[9] = 0x00;
P0 = xianshi[16];
fengming();
}
}
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
if(dangqianzhuangtai[j] != qianyicizhuangtai[j] )
{
qianyicizhuangtai[j] = dangqianzhuangtai[j];
if(qianyicizhuangtai[j] != 0)
{
jj++;
if((i*4+j) < 9)
{
P0 = xianshi[i*4+j+1];
}
if((i*4+j) == 9)
{
P0 = xianshi[0];
}
if((i*4+j) > 9)
{
P0 = xianshi[i*4+j];
}
fengming();
/******************1组**************************/
if(jj == 1)
{
//jieshou[0] = i*4+j;
//jieshou[0] = jieshou[0] << 4;
if((i*4+j) < 9)
{
jieshou[0] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[0] = 0;
}
if((i*4+j) > 9)
{
jieshou[0] = i*4+j;
}
jieshou[0] = jieshou[0] << 4;
}
if(jj == 2)
{
//jieshou[1] = i*4+j;
if((i*4+j) < 9)
{
jieshou[1] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[1] = 0;
}
if((i*4+j) > 9)
{
jieshou[1] = i*4+j;
}
}
/******************2组**************************/
if(jj == 3)
{
//jieshou[2] = i*4+j;
if((i*4+j) < 9)
{
jieshou[2] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[2] = 0;
}
if((i*4+j) > 9)
{
jieshou[2] = i*4+j;
}
jieshou[2] = jieshou[2] << 4;
}
if(jj == 4)
{
//jieshou[3] = i*4+j;
if((i*4+j) < 9)
{
jieshou[3] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[3] = 0;
}
if((i*4+j) > 9)
{
jieshou[3] = i*4+j;
}
}
/******************3组**************************/
if(jj == 5)
{
//jieshou[4] = i*4+j;
if((i*4+j) < 9)
{
jieshou[4] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[4] = 0;
}
if((i*4+j) > 9)
{
jieshou[4] = i*4+j;
}
jieshou[4] = jieshou[4] << 4;
}
if(jj == 6)
{
//jieshou[5] = i*4+j;
if((i*4+j) < 9)
{
jieshou[5] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[5] = 0;
}
if((i*4+j) > 9)
{
jieshou[5] = i*4+j;
}
}
/******************4组**************************/
if(jj == 7)
{
//jieshou[6] = i*4+j;
if((i*4+j) < 9)
{
jieshou[6] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[6] = 0;
}
if((i*4+j) > 9)
{
jieshou[6] = i*4+j;
}
jieshou[6] = jieshou[6] << 4;
}
if(jj == 8)
{
//jieshou[7] = i*4+j;
if((i*4+j) < 9)
{
jieshou[7] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[7] = 0;
}
if((i*4+j) > 9)
{
jieshou[7] = i*4+j;
}
}
/******************5组**************************/
if(jj == 9)
{
//jieshou[8] = i*4+j;
if((i*4+j) < 9)
{
jieshou[8] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[8] = 0;
}
if((i*4+j) > 9)
{
jieshou[8] = i*4+j;
}
jieshou[8] = jieshou[8] << 4;
}
if(jj == 10)
{
//jieshou[9] = i*4+j;
if((i*4+j) < 9)
{
jieshou[9] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[9] = 0;
}
if((i*4+j) > 9)
{
jieshou[9] = i*4+j;
}
}
/******************6组**************************/
if(jj == 11)
{
//jieshou[10] = i*4+j;
if((i*4+j) < 9)
{
jieshou[10] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[10] = 0;
}
if((i*4+j) > 9)
{
jieshou[10] = i*4+j;
}
jieshou[10] = jieshou[10] << 4;
}
if(jj == 12)
{
//jieshou[11] = i*4+j;
if((i*4+j) < 9)
{
jieshou[11] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[11] = 0;
}
if((i*4+j) > 9)
{
jieshou[11] = i*4+j;
}
}
/******************7组**************************/
if(jj == 13)
{
//jieshou[12] = i*4+j;
if((i*4+j) < 9)
{
jieshou[12] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[12] = 0;
}
if((i*4+j) > 9)
{
jieshou[12] = i*4+j;
}
jieshou[12] = jieshou[12] << 4;
}
if(jj == 14)
{
//jieshou[13] = i*4+j;
if((i*4+j) < 9)
{
jieshou[13] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[13] = 0;
}
if((i*4+j) > 9)
{
jieshou[13] = i*4+j;
}
}
/******************8组**************************/
if(jj == 15)
{
//jieshou[14] = i*4+j;
if((i*4+j) < 9)
{
jieshou[14] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[14] = 0;
}
if((i*4+j) > 9)
{
jieshou[14] = i*4+j;
}
jieshou[14] = jieshou[14] << 4;
}
if(jj == 16)
{
//jieshou[15] = i*4+j;
if((i*4+j) < 9)
{
jieshou[15] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[15] = 0;
}
if((i*4+j) > 9)
{
jieshou[15] = i*4+j;
}
}
/******************9组**************************/
if(jj == 17)
{
//jieshou[16] = i*4+j;
if((i*4+j) < 9)
{
jieshou[16] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[16] = 0;
}
if((i*4+j) > 9)
{
jieshou[16] = i*4+j;
}
jieshou[16] = jieshou[16] << 4;
}
if(jj == 18)
{
//jieshou[17] = i*4+j;
if((i*4+j) < 9)
{
jieshou[17] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[17] = 0;
}
if((i*4+j) > 9)
{
jieshou[17] = i*4+j;
}
}
/******************10组**************************/
if(jj == 19)
{
//jieshou[18] = i*4+j;
if((i*4+j) < 9)
{
jieshou[18] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[18] = 0;
}
if((i*4+j) > 9)
{
jieshou[18] = i*4+j;
}
jieshou[18] = jieshou[18] << 4;
}
if(jj == 20)
{
//jieshou[19] = i*4+j;
if((i*4+j) < 9)
{
jieshou[19] = i*4+j+1;
}
if((i*4+j) == 9)
{
jieshou[19] = 0;
}
if((i*4+j) > 9)
{
jieshou[19] = i*4+j;
}
}
}
}
}
}
}
}
void UartAction(unsigned char *buf, unsigned char len)
{
//在接收到的数据帧后添加换车换行符后发回
// buf[len++] = '\r';
//buf[len++] = '\n';
UartWrite(buf, (len-2));
}
|
|