Thông báo

Collapse
No announcement yet.

Điều khiển 3 động cơ bước

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Điều khiển 3 động cơ bước

    Em đang làm mạch điều khiển 3 động cơ bước sử giao tiếp máy tính. Các bác xem dùm em, nó bị sai gì mà khi nhấn nút thì lúc quay đủ vòng lúc không, còn cắm dây cổng com vào thì mấy con led giật giật không chạy dc
    Sơ đồ nguyên lý dc-rs232.DSN
    Code:
    #include <16f877a.h>
    #include <def_877a.h>
    #fuses   NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT
    #use delay(clock=20000000)
    #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7) 
    
    int led[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //1,2,3,4,5,6,7,8,9
    int8 a,b,c,d,byte_cao,byte_thap,hoatdongx,hoatdongy,hoatdongz,malenh;
    int8 chieuy,tocdoy,chieux,tocdox,chieuz,tocdoz;
    int16 i,x,sobuocx,sobuocy,sobuocz;
    int8 t1[8]={9,8,12,4,6,2,3,1};   //mang dieu khien dong co quay thuan
    int8 k1[8]={1,3,2,6,4,12,8,9};   //mang dieu khien dong co quay nghich
    int8 t2[8]={144,128,192,64,96,32,48,16};
    int8 k2[8]={16,48,32,96,64,192,128,144};
    void chayx(int8,int16,int8);
    void chayy(int8,int16,int8);
    void chayz(int8,int16,int8);
    void quetphim();
    void hienthi();
    ////////////////////////
    
    #INT_RDA
    void ngatrs232()
    {
    malenh=getc();
    switch(malenh)
    {
       case 0:
             chieux=getc();
             byte_cao=getc();
             byte_thap=getc();
             tocdox=getc();
             sobuocx=byte_cao*256+byte_thap;
             hoatdongx=1;
             break;
       case 1:
             chieuy=getc();
             byte_cao=getc();
             byte_thap=getc();
             tocdoy=getc();
             sobuocy=byte_cao*256+byte_thap;
             hoatdongy=1;
             break;
       case 2:
             chieuz=getc();
             byte_cao=getc();
             byte_thap=getc();
             tocdoz=getc();
             sobuocz=byte_cao*256+byte_thap;
             hoatdongz=1;
             break;         
       case 3:
             hoatdongx=0;
             hoatdongy=0;
             hoatdongz=0;
             break;
       case 4:
             tocdox=getc();
             break;
       case 5:
             tocdoy=getc();
             break;
       case 6:
             tocdoz=getc();
             break;
       case 7:
             break;
    
    }
    }
    
    
    ////////////////\\\\\\\\\\\\\\\
    
    void hienthi() //quet led 
    {
          portd=led[a]; RC4=0; delay_us(300); RC4=1;portd=0xff;
          portd=led[b]; RC5=0; delay_us(300); RC5=1;portd=0xff;
          portd=led[c]; RD7=0; delay_us(300); RD7=1;portd=0xff;
    }
    
    ////////////////\\\\\\\\\\\\\\\
    ///////////////  \\\\\\\\\\\\\\\
    
    void quetphim()
    {
       x=1080/0.9;
       if(ra0==0)
       {
          delay_ms(1000); //chong rung phim
    
          for(i=0;i<x;i++)
          {
          PORTB=t1[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
          }
       a++;
       if(a==10) a=0;
       portb=0;
    
       }
       
         
       else if(ra1==0)
       {
          delay_ms(1000); //chong rung phim
    
         for(i=0;i<x;i++)
          {
          PORTB=k1[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
          }
       a--;
       if(a==-1) a=9;
       portb=0;
       }
    
       else if(ra2==0)
       {
          delay_ms(1000); //chong rung phim  
          for(i=0;i<x;i++)
       {         
          PORTB=t2[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
       }
       b++;
       if(b==10) b=0;
       portb=0;
       }
    
    
       else if(ra3==0)
       {
          delay_ms(1000); //chong rung phim  
          for(i=0;i<x;i++)
       {         
          PORTB=k2[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
       }
       b--;
       if(b==-1) b=9;
       portb=0;
      } 
    
    
       else if(ra4==0)
       {
          delay_ms(1000); //chong rung phim
          for(i=0;i<x;i++) 
       {         
          PORTC=t1[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
       }
       c++;
       if(c==10) c=0;
       rc0=0;
       rc1=0;
       rc2=0;
       rc3=0;  
       }
    
    
       else if(ra5==0)
       {
          delay_ms(1000); //chong rung phim
       for(i=0;i<x;i++)
       {         
          PORTC=k1[d];
          delay_ms(1);
          d++;if(d>7)d=0;
          hienthi();
       }
       c--;
       if(c==-1) c=9;
       rc0=0;
       rc1=0;
       rc2=0;
       rc3=0;
       }
      hienthi(); 
    }
    ///////////////////////////////
    void chayx(int8 chieu,int16 sobuoc,int8 tocdo)
    {
    switch(chieu)
    {
    case 0:
          for(i=0;i<sobuoc;i++)
       {         
          PORTB=k1[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    case 1:
          for(i=0;i<sobuoc;i++)
       {         
          PORTB=t1[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    }
    }   
    
    
    //////////////////////////////
    void chayy(int8 chieu,int16 sobuoc,int8 tocdo)
    {
    switch(chieu)
    {
    case 0:
          for(i=0;i<sobuoc;i++)
       {         
          PORTB=k2[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    case 1:
          for(i=0;i<sobuoc;i++)
       {         
          PORTB=t2[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    }
    }
    //////////////////////////////
    void chayz(int8 chieu,int16 sobuoc,int8 tocdo)
    {
    switch(chieu)
    {
    case 0:
       for(i=0;i<sobuoc;i++)
       {         
          PORTC=k1[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    case 1:
       for(i=0;i<sobuoc;i++) 
       {         
          PORTC=t1[d];
          delay_ms(tocdo);
          d++;if(d>7)d=0;
       }
       break;
    }
    }
    
    ////////////////\\\\\\\\\\\\\\\
    ///////////////  \\\\\\\\\\\\\\\
    void main()
    {
       enable_interrupts(GLOBAL);
       enable_interrupts(INT_RDA);
       trisd = 0x00;
       portd = 0x00;
       trisb = 0x00;
       portb=0x00;
       OUTPUT_LOW(PIN_c0);
       OUTPUT_LOW(PIN_c1);
       OUTPUT_LOW(PIN_c2);
       OUTPUT_LOW(PIN_c3);
       OUTPUT_LOW(PIN_c4);
       OUTPUT_LOW(PIN_c5);
       hoatdongx=0;
       hoatdongy=0;
       hoatdongz=0;
    while(1)
    {
    quetphim();
    if(hoatdongx==1)
    {
       for(i=0;i<sobuocx;i++)
        {
           if(hoatdongx==1)
           {chayx(chieux,sobuocx,tocdox);}
           else break; 
        }
          hoatdongx =0;   
    }
    
    if(hoatdongy==1)
    {
       for(i=0;i<sobuocy;i++)
        {
           if(hoatdongy==1)
           {chayy(chieuy,sobuocy,tocdoy);}
           else break; 
        }
          hoatdongy =0;   
    }
    
    if(hoatdongz==1)
    {
       for(i=0;i<sobuocz;i++)
        {
           if(hoatdongz==1)
           {chayz(chieuz,sobuocz,tocdoz);}
           else break; 
        }
          hoatdongz =0;   
    }
    }
    }

Về tác giả

Collapse

mvuongvn Tìm hiểu thêm về mvuongvn

Bài viết mới nhất

Collapse

Đang tải...
X