⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elevator.c

📁 MAGE128下用RS485进行通讯并进行CRC校验以及中断的处理
💻 C
字号:
//还要考虑采样子程
#include <io.h>
#include <interrupt.h>
#include <signal.h>
#include <sig-avr.h>

#define F_CPU              14745600    /* 4Mhz 4000000*/
#define UART_BAUD_RATE      9600     /* 9600 baud */

#define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16l)-1)

typedef unsigned char   u08;
typedef          char   s08;
typedef unsigned short  u16;
typedef          short  s16;

u08 instruction1,instruction0,store12[7],floor,state,u,v,good;
u08  begin[4]={0},store1[7]={0x4d,0xdf,0xde,0x0d,0x1d,0x2d,0x3d};
u08 store13[7]={0};
u08 store0[2]={0xbf,0xaf};
u08 store03[2]={0};
u08 (*putin1)[2],(*takeout1)[2],(*putin0)[2],(*takeout0)[2];
u08 store2[50][2]={{0}};
u08 *rx,*array1,*array0;
u08 send_buf1[6]={0x55,0xbb,0x00,0x00,0x00,0xaa};
u08 send_buf0[6]={0x55,0x00,0xcc,0x00,0x00,0xaa};
u08 r1,r0,k,l,led[6],lck[6],byt1[4],byt0[4],ok1,err1,wrong1,ok0,err0,wrong0,pin,shield[32];
u16 counter1,counter0;
u08 d1,d0;
u08  opentip,closetip,motorman,motor,light,openbutton,closebutton,straight,overload;
u08  load,unload,normalload,unopen,unclose,inevenfloor,outevenfloor,evenfloor,container;


void shutdown(void)
{;}

void opendoor(void)
{;}



void change1(void)
{
byt1[0]=send_buf1[1];
byt1[1]=send_buf1[2];
byt1[2]=send_buf1[3];
byt1[3]=send_buf1[4];
}

void change0(void)
{
byt0[0]=send_buf0[1];
byt0[1]=send_buf0[2];
byt0[2]=send_buf0[3];
byt0[3]=send_buf0[4];
}

void chain1(u08 lookup)   //将此程序放入串口一的接收子程中
{
int x;
for(x=0;x<=6;x++)
	{
	if(lookup==store1[x])
		{
		store13[x]=1;
	        break;
		}
	
	}
}


void  chain0(u08 lookup0)
{
int y;
for(y=0;y<=1;y++)
	{
	if(lookup0==store0[y])
		{
		store03[y]=1;
		break;
		}
	}
}


unsigned char crc_gen(u08 *data,int bytes)
{
int i;
int j;
u08 genpoly=0xe1;
u08 crc=0;
for(i=0;i<bytes;i++)
	{
	crc^=data[i];
	for(j=0;j<8;j++)
		{
		if(crc&0x80)
			crc^=genpoly;
		crc<<=1;
		}
	}
if(crc&0x80)
	crc^=genpoly;

return crc;
}





void USART_Transmit1(u08 *buf1,u16 size1)
{
array1=buf1;
counter1=size1;
outp(*buf1,UDR1);
}


SIGNAL(SIG_UART1_TRANS) 
{
array1++;
if(--counter1)
	{
	outp(*array1,UDR1);
	}
}

void USART_Transmit0(u08 *buf0,u16 size0)
{
array0=buf0;
counter0=size0;
outp(*buf0,UDR1);
}



SIGNAL(SIG_UART0_TRANS)
{
array0++;
if(--counter0)
	{
	outp(*array0,UDR1);
	}
}



SIGNAL(SIG_UART1_RECV)      
{
led[k]= inp(UDR1);        /* read byte for UART data buffer */
if(k==0)
	{
        if(led[k]==0x55)
        	k++;
        }
else if(k==1)
	{
        if(led[k]!=0xdd&&led[k]<=0x01&&led[k]>=0x60&&led[k]!=0xcc&&led[k]!=0xbc&&led[k]!=0xcb)
        	{
        	k=0;
        	led[0]=0;
        	}
        else
        	k++;
        }
else if(k==2)
	{
        k++;
	}
else if(k==3)
	{
        k++;
        if(led[3]==0x6a)  //&&led[1]=0xcc
        	{
        	ok1=1;
        	}
        else if(led[3]==0x4a)  //&&led[1]=0xcc
        	{
        	err1=1;
        	}
        else
        	{
        	ok1=0;
        	err1=0;
        	}
        }
else if(k==4)
        {
        byt1[0]=led[1];
        byt1[1]=led[2];
        byt1[2]=led[3];
        byt1[3]=led[4];
        d1=crc_gen(byt1,4);//CRC校验
        k++;
        }
else if(k==5)
        {
        k=0;
       	led[0]=0;
       	if(led[k]==0xaa)
       		{
       		if(ok1!=1&&err1!=1)
       			{
       			byt1[0]=0xbb;
       			if(d1==0)
       				{
    				if(led[1]==0xcc)
    					{
    					if(led[2]==0xbc)
        					{
        					floor=led[3];
        					}
    					else if(led[2]==0xcb)
    						{
    						shield[pin]=led[3];
    						pin++;
    						}
    					else
    						{
       						if(state==0)
       							{
       							chain1(led[3]);
	       						instruction1++;	
      	                                		}						
       						else
       							{
       							*(*putin1+0)=led[1];
       							*(*putin1+1)=led[3];
       							putin1++;
       							r1++;
       							if(r1>=50)
       								{
       								r1=0;
       								putin1=store2;
       								}
       							}
       						}
       					}
       			        else 
       					{
       					if(led[1]==0xdd)
       						{send_buf0[1]=0xdd;}
       					else
       						{send_buf0[1]=led[1];}
       					send_buf0[3]=led[3];
       					byt0[0]=send_buf0[1];
       					byt0[1]=send_buf0[2];
       					byt0[2]=send_buf0[3];
       					send_buf0[4]=crc_gen(byt0,3);
       					USART_Transmit0(send_buf0,6);
       					}
       				
       				send_buf1[3]=0x6a;
    				byt1[2]=send_buf1[3];

       				}
       			else
       				{
       				send_buf1[3]=0x4a;
       				byt1[2]=send_buf1[3];
       				}
       			send_buf1[2]=0xcc;
       			byt1[1]=send_buf1[2];
			send_buf1[4]=crc_gen(byt1,3);
       			USART_Transmit1(send_buf1,6);
       			}
       		else if(err1==1)
       			{
       			wrong1++;
			if(wrong1<=3)
       				{
       				USART_Transmit1(send_buf1,6);
       				err1=0;
       				}
       			else
       				{
       				wrong1=0;
       				err1=0;
       				}
       			}
       		else 
       			{
       			ok1=0;
       			wrong1=0;
       			}       		
       		}
       	}
else
	{
	led[0]=0;
	ok1=0;
	err1=0;
	k=0;
	wrong1=0;
	}
}


SIGNAL(SIG_UART0_RECV)
{
lck[l]= inp(UDR0); 
if(l==0)
	{
        if(lck[l]==0x55)
        	l++;
        }
else if(l==1)
	{
        if(led[l]!=0xcc)
        	{
        	k=0;
        	lck[0]=0;
        	}
        else
        	l++;
        }
else if(l==2)
	{
        l++;
	}
else if(l==3)
	{
        l++;
        if(lck[3]==0x6a)  //&&led[1]=0xcc
        	{
        	ok0=1;
        	}
        else if(led[3]==0x4a)  //&&led[1]=0xcc
        	{
        	err0=1;
        	}
        else
        	{
        	ok0=0;
        	err0=0;
        	}
        }
else if(l==4)
        {
        byt0[0]=lck[1];
        byt0[1]=lck[2];
        byt0[2]=lck[3];
        byt0[3]=lck[4];
        d0=crc_gen(byt0,4);//CRC校验
        l++;
        }
else if(l==5)
        {
        l=0;
       	lck[0]=0;
       	if(lck[l]==0xaa)
       		{
       		if(ok0!=1&&err0!=1)
       			{
       			byt0[0]=lck[2];
       			if(d0==0)
       				{
    				   	
       				if(state==0)
       					{
       					chain0(lck[3]);
	       				instruction0++;	
      	                               	}						
       				else                     //此处要检查屏蔽层
       					{
       					/**(*putin0+0)=lck[1];
       					*(*putin0+1)=lck[3];
       					putin0++;
       					r0++;
       					if(r1>=50)
       						{
       						r1=0;
       						putin1=store2;
       						}
       					*/
       					send_buf0[1]=lck[2];
       					send_buf0[3]=0xe0;
       					byt0[0]=send_buf0[1];
       					byt0[1]=send_buf0[2];
       					byt0[2]=send_buf0[3];
       					send_buf0[4]=crc_gen(byt0,3);
       					USART_Transmit0(send_buf0,6);
       	
       					
       					}
       					
       				
       				send_buf1[3]=0x6a;
    				byt1[2]=send_buf1[3];

       				}
       			else
       				{
       				send_buf1[3]=0x4a;
       				byt1[2]=send_buf1[3];
       				}
       			send_buf1[2]=0xcc;
       			byt1[1]=send_buf1[2];
			send_buf1[4]=crc_gen(byt1,3);
       			USART_Transmit1(send_buf1,6);
       			}
       		else if(err0==1)
       			{
       			wrong0++;
			if(wrong0<=3)
       				{
       				USART_Transmit0(send_buf1,6);
       				err0=0;
       				}
       			else
       				{
       				wrong0=0;
       				err0=0;
       				}
       			}
       		else 
       			{
       			ok0=0;
       			wrong0=0;
       			}
       		
       		}
       	}
else
	{
	lck[0]=0;
	ok0=0;
	err0=0;
	l=0;
	wrong0=0;
	}	

} 







void USART_Init1(u16 baud1)
{
UBRR1H=(unsigned char)(baud1>>8);
UBRR1L=(unsigned char)baud1;
UCSR1B=(1<<RXEN)|(1<<RXCIE)|(1<<TXEN)|(1<<TXCIE); 
}


void USART_Init0(u16 baud0)
{
UBRR0H=(u08)(baud0>>8);
UBRR0L=(u08)baud0;
UCSR0B=(1<<RXEN)|(1<<RXCIE)|(1<<TXEN)|(1<<TXCIE);
}


void examself(void)
{

container=PINB;
if(container&&0x04)    //判断是司机还是自动
	{
	motorman=1;
	begin[0]=0xc5;
	}
else
	{
	motor=1;
	begin[0]=0xc6;
	}

container=PINF;
if(container&&0x08)   //判断门的情况
	{
	closetip=1;
	begin[1]=0xc1;
	}
else
	{
	unclose=1;
	begin[1]=0x8c;
	}

if(container&&0x40)
	{
	opentip=1;
	begin[2]=0xc0;
	}
else
	{
	unopen=1;
	begin[2]=0x9c;
	}

container=PINB;       //判断载重情况
if(container&&0x20)
	{
	load=1;
	begin[3]=0xc8;
	}
else if(container&&0x40)
	{
	unload=1;
	begin[3]=0xc9;
	}
else
	{
	normalload=1;
	begin[3]=0xc4;
	}

}


void handshake(void)
{
state=0;
while(!instruction1)
	{;}
if(store13[0])
	{
	send_buf1[2]=0xcc;
	send_buf1[3]=0xac;   //发送握手指令
	change1();
	send_buf1[4]=crc_gen(byt1,3);
	USART_Transmit1(send_buf1,6);
        instruction1=0;
        store13[0]=0;
        }
while(!instruction1)
	{;}
send_buf0[1]=0x01;  //暂时先用0x01代表内呼板地址
send_buf0[3]=0xef;
change0();
send_buf0[4]=crc_gen(byt0,3);
USART_Transmit0(send_buf0,6);
while(!instruction0)
	{;}
if(store03[0]==1)
	{
	instruction0=0;
	send_buf1[2]=0x01;
	send_buf1[3]=0x1c;
	change1();
	send_buf1[4]=crc_gen(byt1,3);
	USART_Transmit1(send_buf1,6);
	store03[0]=0;
	}
else
	{
	instruction0=0;
	send_buf1[2]=0x01;
	send_buf1[3]=0x3c;
	change1();
	send_buf1[4]=crc_gen(byt1,3);
	USART_Transmit1(send_buf1,6);
	}
v=0x21;
for(u=0;u<=floor-1;u++)
	{
	send_buf0[1]=v;  //暂时先用0x01代表内呼板地址
	send_buf0[3]=0x03;
	change0();
	send_buf0[4]=crc_gen(byt0,3);
	USART_Transmit0(send_buf0,6);
	while(!instruction0)
		{;}
	if(store03[1]==1)
		{
		instruction0=0;
		good++;
		}
	else
		{
		instruction0=0;
		good=0x4c;
		send_buf1[2]=v;
		send_buf1[3]=0x4c;
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
		}	
	v++;
	}
if(good==floor)
	{
	send_buf1[2]=0x21;
	send_buf1[3]=0x2c;
	change1();
	send_buf1[4]=crc_gen(byt1,3);
	USART_Transmit1(send_buf1,6);
	}
for(u=0;u<=3;u++)
	{
	send_buf1[2]=0xcc;
	send_buf1[3]=begin[u];
	change1();
	send_buf1[4]=crc_gen(byt1,3);
	USART_Transmit1(send_buf1,6);
	}
state=1;
}


void sample()
{
if(container&&0x04)    //判断是司机还是自动
	{
	if(!motorman)
		{
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc5;//司机钮有效
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
		motorman=1;
		motor=0;
		}	
	}
else	
	{
	if(!motor)
		{
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc6;  //自动钮有效
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
		motor=1;
		motorman=0;		
		}
	}

if(motorman)//直驶按钮
	{
	if(container&&0x08)
		{
		if(!straight)
			{
			send_buf1[2]=0xcc;
			send_buf1[3]=0xc7;  
			change1();
			send_buf1[4]=crc_gen(byt1,3);
			USART_Transmit1(send_buf1,6);
	        	straight=1;
	        	}
		}
	else
		{
		straight=0;
		}
	}

 //判断载重情况
if(container&&0x10)
	{//超载
	if(!overload)
		{
		outp(0xa0,PORTC);
		overload=1;
		load=0;
		unload=0;
		normalload=0;
		}
	}
else if(container&&0x20)
	{//满载
	if(!load)
		{
		outp(0x40,PORTC);
		overload=0;
		load=1;
		unload=0;
		normalload=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc8;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
	        }
	}
else if(container&&0x40)   //轻载
	{
	if(!unload)
		{
		outp(0x00,PORTC);
		overload=0;
		load=0;
		unload=1;
		normalload=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc9;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
		}    
	}
else                         //常载 
	{
	if(!normalload);
		{
		outp(0x00,PORTC);
		overload=0;
		load=0;
		unload=0;
		normalload=1;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc4;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);		
		}     
	}


//判断开关门按钮
if(container&&0x02)
	{
	if(!openbutton)
		{
		openbutton=1;
		closebutton=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc3;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);		
		
		}
	}
else if(container&&0x01)
	{
	openbutton=0;
	closebutton=1;
	shutdown();
	}
else   
	{;}

container=PINF;
if(container&&0x08)       //判断门的情况
	{                 //关门到位
	if(!closetip)
		{
		closetip=1;
		opentip=0;
		unclose=0;
		unopen=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc1;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);		
		}	   
	}
else
	{    //关门不到位
	if(!unclose)
		{
		closetip=0;
		opentip=0;
		unclose=1;
		unopen=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0x8c;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);		
		}
	}
if(container&&0x40)    //开门到位
	{
	if(!opentip)
		{
		closetip=0;
		opentip=1;
		unclose=0;
		unopen=0;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc0;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);		
		}    
	}
else
	{      //开门不到位
	if(!unopen)
		{
		closetip=0;
		opentip=0;
		unclose=0;
		unopen=1;
		send_buf1[2]=0xcc;
		send_buf1[3]=0xc0;  
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf1,6);
		}   
	}


if(container&&0x20)
	{
	if(!light)
		{
		light=1;
		opendoor();		
		}
	else
		{light=0;}
	}
}
void handle1()
{;}
void handle0()
{;}

int main(void)
{
DDRC=0xff;
DDRB=0x00;
DDRF=0x00;
u16 rate=UART_BAUD_SELECT;
USART_Init1(rate);
USART_Init0(rate);
sei();
ok1=0;
err1=0;
ok0=0;
err0=0;
r1=0;
r0=0;
state=0;
k=0;
wrong1=0;
wrong0=0;
good=0;
opentip=0;
closetip=0;
unopen=0;
unclose=0;
motorman=0;//
motor=0;//
load=0;
unload=0;
normalload=0;
overload=0;
container=0;
light=0;
openbutton=0;
closebutton=0;
straight=0;  //
inevenfloor=0;
outevenfloor=0;
evenfloor=0;

putin1=store2;
takeout1=store2;
examself();//还有平层中断的处理程序
handshake();
for(;;)
	{  
	if(store13[1]||store13[6])       //电梯自动态和自学习态
		{
		sample();
		if(instruction1!=0)
			{
			handle1();
			}
		if(instruction0!=0)
			{	
			handle0();
			}
		}
	else if(store13[2])  //或是将此司机态放入采样时判断处理即可
		{;}
	else if(store13[3])  //检修态
		{;}
	else if(store13[4])  //故障态
		{;}	
	else if(store13[5])  //消防态
		{;}
	else 
		;  
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -