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

📄 elevator.c

📁 MAGE128下用RS485进行通讯并进行CRC校验以及中断的处理
💻 C
📖 第 1 页 / 共 2 页
字号:
//还要考虑采样子程   //定时子程
#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,n,m;
u08  opentip,closetip,motorman,motor,light,openbutton,closebutton,straight,overload;
u08  load,unload,normalload,unopen,unclose,inevenfloor,outevenfloor,evenfloor,container;
u08  repair,one,five;       //one表示2s
u16  inroom,timef;


void shutdown(void)
{
PORTC=PORTC|0x01;     //关门指令
while(!closetip)
	{
	if(five==1)
		{
		send_buf1[1]=0xbb;
		send_buf1[3]=0xce;    //关门故障
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf0,6);
		five=0;		
		}
	}
closetip=0;
outp((0<<TOIE1),TIMSK);
}


void opendoor(void)
{
timef=0;    //2s
PORTC=PORTC|0x02;
TCNT1H=0xf0;
TCNT1L=0xbe;                  
outp((1<<TOIE1),TIMSK);
while(!opentip)
	{
	if(one==1)
		{
		send_buf1[1]=0xbb;
		send_buf1[3]=0xcf;
		change1();
		send_buf1[4]=crc_gen(byt1,3);
		USART_Transmit1(send_buf0,6);
		one=0;
		}
	}
opentip=0;
outp((0<<TOIE1),TIMSK);
timef=1;
TCNT1H=0xf0;
TCNT1L=0xbe;                  
outp((1<<TOIE1),TIMSK);
while(!closetip)
	{
	if(five==1)
		{
		shutdown();
		}
	}
}



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;
}


SIGNAL(SIG_OVERFLOW1)
{
if(timef==0)
  {
  if(count_t0>=1)       //2s
    {
    //outp((0<<TOIE1),TIMSK);
    count_t0=0;
    outp((0<<TOIE1),TIMSK);
    TCNT1H=0xf0;
    TCNT1L=0xbe;
    one=1;
    }
  else  
    {
    //outp((0<<TOIE1),TIMSK);
    count_t0++;
    TCNT1H=0xf0;
    TCNT1L=0xbe;                  
    //outp((1<<TOIE1),TIMSK);
    }  
  }
else if(timef==1)
  {
  if(count_t0>=4)       //5s
    {
    //outp((0<<TOIE1),TIMSK);
    count_t0=0;
    outp((0<<TOIE1),TIMSK);
    TCNT1H=0xf0;
    TCNT1L=0xbe;
    five=1;
    }
  else  
    {
    //outp((0<<TOIE1),TIMSK);
    count_t0++;
    TCNT1H=0xf0;
    TCNT1L=0xbe;                  
    //outp((1<<TOIE1),TIMSK);
    }  
  
  }


else
  {//outp((0<<TOIE1),TIMSK);
  outp(0xa8,PORTB);
  outp((0<<TOIE1),TIMSK);  //1s
  TCNT1H=0xf0;
  TCNT1L=0xbe;
  }
}





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)
	{

⌨️ 快捷键说明

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