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

📄 zoubeifuwei.c

📁 这个事自己做的第一个单片机程序
💻 C
📖 第 1 页 / 共 3 页
字号:
        }
        for(i=0;i<30;i++)                     //减速阶段
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(rate<=180)
                rate+=5;
        }
    }
    delay(100,100);
	ClrCurrent();
    M_LOWMOVE;
    return 1;
}
 
//预温电机动作3,不跨区的程序,需要先到零点光耦然后返回 
uchar YW3move(uint mstep)         
{
    uint i;
    uint rate;
    uint high;
    uint step=0;
    rate=180;
    high=100;
    if(START_GO&&(mstep>MAXstepTG)) 		//当前在通光区
    {  
        mstep=MAXstepTG;
        Warn(0x11);
    }
    if(!START_GO&&(mstep>MAXstepDG))		//当前在挡光区
    {  
        mstep=MAXstepDG;
        Warn(0x11);
    }
    
	SetCurrent(10);
    M_ENABLE;
    M_HIGHMOVE;
    
    if(START_GO)                     //在通光区,需要向通光区走
    {
        M_CLOCKWISE;
        while(START_GO)				 //先到零点光耦
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
                rate-=5;
            step++;
            Ystep--;
            if(Ystep<1970)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);   			 //报警程序
                return 0;
            } 
            if(step>MAXstepTG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);   	  		 //报警程序
                return 0;
            } 
        }
		Ystep = 2000;
        for(i=0;i<=30;i++)			 //从零点继续向挡光区走30步
        {
            PORTB &= ~0x02;
            delay1(rate);
            PORTB |= 0x02;
            delay1(rate);
			Ystep--;
        }
        delay(300,300);
        
        M_UNCLOCKWISE;
        rate=180;
		step = 0;
        while(!START_GO)   			 //此时从挡光区再到零点光耦
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
               rate-=5;
            step++;
            Ystep++;
            if(Ystep>2030)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);   			 //报警程序
                return 0;
            }
            if(step>MAXstepDG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);   	  		 //报警程序
                return 0;
            } 
        }
		Ystep=2000;
        if(mstep<=30)  	 			 //再从零点走向通光区,需要分档
        {
            for(i=0;i<mstep;i++)
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep++;
                if(rate>=100)
                    rate-=5;
            }
        }
        else 
        {
            for(i=0;i<mstep-30;i++)	 //加速阶段
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep++;
                if(rate>=100)
                    rate-=5;
            }
            for(i=0;i<30;i++) 		 //减速阶段
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep++;
                if(rate<=180)
                    rate+=5;
            }
        }
    }
	else 	 				  		 //在挡光区,需要向挡光区走
    {
        M_UNCLOCKWISE; 
        while(!START_GO)			 //先到零点光耦
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
                rate-=5;
            step++;
            Ystep++;
            if(Ystep>2030)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);   			 //报警程序
                return 0;
            } 
            if(step>MAXstepDG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);   	  		 //报警程序
                return 0;;
            }
        }
		Ystep = 2000;
        for(i=0;i<=30;i++)			 //从零点继续向通光区走30步
        {
            PORTB &= ~0x02;
            delay1(rate);
            PORTB |= 0x02;
            delay1(rate);
			Ystep++;
        }
        delay(300,300);
        
        M_CLOCKWISE;
        rate=180;
		step = 0;
        while(START_GO)	   			 //此时从通光区再到零点光耦
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
            rate-=5;
            step++;
            Ystep--;
            if(Ystep<1970)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);   			 //报警程序
                return 0;
            }
            if(step>MAXstepTG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);   	  		 //报警程序
                return 0;
            }  
        }
		Ystep=2000;
        if(mstep<=30)  	 			 //再向挡光区走指定步数,需要分档
        {
            for(i=0;i<mstep;i++)
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep--;
                if(rate>=100)
                    rate-=5;
            }
        }
        else 
        {
            for(i=0;i<mstep-30;i++)                //加速阶段
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep--;
                if(rate>=100)
                    rate-=5;
            }
            for(i=0;i<30;i++)                          //减速阶段
            {
                PORTB &=~0x02;
                delay1(rate);
                PORTB |=0x02;
                delay1(rate);
                Ystep--;
                if(rate<=180)
                    rate+=5;
            }
        }
    }
    delay(100,100);
	ClrCurrent();
    M_LOWMOVE;
    return 1;
}
  
 void main()
 {
  uint i;
  uint ZBstep;
  uchar  num=0;
  uchar Addr;
  
  init_devices();
  Addr=(PINC & 0x07) + 1;
	if(Addr==0x01)
	{
	  ClrCurrent();
	}
	if(Addr==0x02)
	{
	  ClrCurrent();
	}
	if(Addr==0x03)
	{
	  ClrCurrent();
	}
	if(Addr==0x04)
	{
	  ClrCurrent();
	}
	if(Addr==0x05)
	{
	  ClrCurrent();
	}
  M_LOWMOVE;
  M_ENABLE;
  LED_ON;
  //Addr=(PINC & 0x07)+1;
  delay(70,1000);
  TWCR = (1<<TWEA)|(1<<TWEN);

  while(1)
  {
   if(twi_Sreceive())
    {
	  BUSY_ON;
	  switch(I2cRbuf[0])
	  {
	     case 1: JBmove(1000);                       //进杯动作
	             break;
	     case 2: if(I2cRbuf[1]==0x01)                //勾杯复位
	               Reset1(0);
			     else if(I2cRbuf[1]==0x00)           //勾杯动作
			      {GTmove(0);}
			     break;
	     case 3: if(I2cRbuf[1]==0x01)                //推杯1复位
	              {Reset1(1);}
			     else if(I2cRbuf[1]==0x00)           //推杯1动作
			      {GTmove(1);}
			     break;
	     case 4: if(I2cRbuf[1]==0x01)                //推杯2复位
	              {Reset1(1);}
			     else if(I2cRbuf[1]==0x00)           //推杯2动作
			      {GTmove(1);}
			     break;
	     case 5: Reset2();                           //预温电机复位
	             break;
	     case 6: if(I2cRbuf[1]==0x01)                //预温区走到指定绝对步数
	             {
			      ZBstep=I2cRbuf[2];
				  ZBstep<<=8;
				  ZBstep |=I2cRbuf[3];
				  YW3move(ZBstep);	  	//不跨区走
			     }
	             else if(I2cRbuf[1]==0x00)
			     {
			      ZBstep=I2cRbuf[2];
				  ZBstep<<=8;
				  ZBstep |=I2cRbuf[3];
				  YW1move(ZBstep);	  	//跨区走
			     }
				 else
				    delay(500, 500);
			     break;
	     case 8: if(I2cRbuf[2]==0x01)                //预温区沿指定方向走指定步数
	             {
			      ZBstep=I2cRbuf[3];
				  ZBstep<<=8;
				  ZBstep |=I2cRbuf[4];
				  YW2move(I2cRbuf[1],ZBstep);		 //不跨区走
			     }
	             else if(I2cRbuf[2]==0x00)
			     {
			      ZBstep=I2cRbuf[3];
				  ZBstep<<=8;
				  ZBstep |=I2cRbuf[4];
				  YW1move(ZBstep);	 		 		 //跨区走
			     }
				 else
				    delay(500, 500);
			     break;
	     case 9: i=0;
		 	  	 while(!(TWCR & (1<<TWINT))&& i<=60000)i++;
		         if(i>60000)
			     {
                   //TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);////////////////
	               //twi_init();
	               TWCR = (1<<TWEA)|(1<<TWEN);
				   break;
			     }
			     i=0;
		         if((TWSR & 0xF8) == 0xA8)
		         {
			       I2cSbuf[0]=9;
			       I2cSbuf[1]=Addr;                   //查询报警
    		       I2cSbuf[2]=Eflag;
    			   TWDR = I2cRbuf[0];             //13
			       TWCR |= (1<<TWINT) | (1<<TWEA);
		         }
                 for(num=1; num<3; num++)
                 {
                    while(!(TWCR & (1<<TWINT))&& i<=10000)i++;
                    if(i>10000)
				    {
               		   //TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);////////////////
                       //twi_init();
                       TWCR = (1<<TWEA)|(1<<TWEN);
    	   			   break;
                    }
				    if((TWSR & 0xF8) == 0xB8)
                    {
                     TWDR = I2cSbuf[num];
                     TWCR |= (1<<TWINT) | (1<<TWEA);
                    }
                 }
               	 if(i>10000)
 		   		 {
                     //TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);////////////////
                     //twi_init();
                  	 TWCR = (1<<TWEA)|(1<<TWEN);
 				 	 break;
                 }
			   	 i=0;
               	 while(!(TWCR & (1<<TWINT))&& i<=10000)i++;
               	 if(i>10000)
			   	 {
                	 //TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);////////////////
	                 //twi_init();
	                 TWCR = (1<<TWEA)|(1<<TWEN);
					 break;
                 }
			   	 if((TWSR & 0xF8) == 0xC0)
               	 TWCR |= (1<<TWINT) | (1<<TWEA);
			   	 Eflag=0;
			   	 break;
	   case 127: M_ENABLE;                        //上电
	             break;
	   case 128: M_UNENABLE;                      //掉电
	             break;
	   default:;
	  }
	 
	 if(Eflag==0)
	    NORMAL;
	  else
	    UNNORMAL;
	 BUSY_OFF;
	}
  }
  
  /*i=(PINC &0x07);
  if(i==0x00)
   {
    while(1)
    {
	 JBmove(1000);
	 delay(100,100);
	}
   }
  if(i==0x01)
   {
    Reset1(0);
	delay(100,1000);
	while(1)
	GTmove(0);
   }
  if(i==0x02)
  {
   Reset1(1);
   delay(100,1000);
   while(1)
   GTmove(1);
  }
  if(i==0x03)
  {
   Reset1(1);
   delay(100,1000);
   while(1)
   GTmove(1);
  }
  if(i==0x04)
  {
   YW1move(0x01,0x00,50);
  }*/
 }

⌨️ 快捷键说明

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