全国大学生机械创新设计大赛(步进电机控制,含电路图)

电路原理图:

程序源代码:

  
  
  
  
  1. #include <iom128v.h>  
  2.  
  3. #include <macros.h>  
  4.  
  5.                                      
  6.  
  7.    
  8.  
  9.    
  10.  
  11. void delay(int z)  
  12.  
  13. {  
  14.  
  15.        int i,j;  
  16.  
  17.        for(i=0;i<100;i++)  
  18.  
  19.           for(j=0;j<z;j++);  
  20.  
  21. }  
  22.  
  23.    
  24.  
  25.    
  26.  
  27. void Io_init(void)  
  28.  
  29. {  
  30.  
  31.         DDRA = 0X00;            //限位开关方向寄存器  
  32.  
  33.         DDRC = 0xff;            //控制电机方向寄存器  
  34.  
  35.         DDRD = 0X00;                  //控制按钮方向寄存器  
  36.  
  37.    
  38.  
  39.         PORTD = 0xff;             //控制按钮数据寄存器 拉高  
  40.  
  41.         PORTA = 0xff;           //限位开关数据寄存器 拉高  
  42.  
  43.         PORTC&=~(1<<PC0);       //停止所有有关电机控制的IO口  
  44.  
  45.         PORTC&=~(1<<PC1);  
  46.  
  47.         PORTC&=~(1<<PC2);  
  48.  
  49.         PORTC&=~(1<<PC3);  
  50.  
  51.         PORTC&=~(1<<PC4);  
  52.  
  53.         PORTC&=~(1<<PC5);  
  54.  
  55. }  
  56.  
  57.    
  58.  
  59.    
  60.  
  61. void Pwm_init(void)  
  62.  
  63. {  
  64.  
  65.        DDRB = 0Xff;   
  66.  
  67.         TCCR1A =0Xfd;                // TCCR1A =0Xfd; 30KHZ 速度最快  TCCR1A =0Xff; 8KHZ   
  68.  
  69.         TCCR1B =0X09;  
  70.  
  71.         TCNT1 =0X0000;  
  72.  
  73.         TIMSK = 0X00;  
  74.  
  75.         ETIMSK =0X00;  
  76.  
  77.         OCR1A =100;  
  78.  
  79.         OCR1B =100;  
  80.  
  81.         OCR1C =100;  
  82.  
  83. }  
  84.  
  85.    
  86.  
  87.    
  88.  
  89. void Pwm_init1(void)  
  90.  
  91. {  
  92.  
  93.        DDRE = 0Xff;   
  94.  
  95.         TCCR3A =0Xff;                // TCCR1A =0Xfd; 30KHZ 速度最快  TCCR1A =0Xff; 8KHZ   
  96.  
  97.         TCCR3B =0X0A;  
  98.  
  99.         TCNT3 =0X0000;  
  100.  
  101.         OCR3A =100;  
  102.  
  103.         OCR3B =100;  
  104.  
  105.         OCR3C =100;  
  106.  
  107. }  
  108.  
  109.    
  110.  
  111.    
  112.  
  113. void main(void)  
  114.  
  115. {  
  116.  
  117.      int a1,a2,a3,a4,a5,a6;  
  118.  
  119.        Io_init();  
  120.  
  121.         Pwm_init();  
  122.  
  123.         Pwm_init1();  
  124.  
  125.         while(1)  
  126.  
  127.         {  
  128.  
  129.             if((PIND&0x3F)!=0x3F)  
  130.  
  131.                {  
  132.  
  133.                delay(1);  
  134.  
  135.             if((PIND&0x3F)!=0x3F)  
  136.  
  137.             {  
  138.  
  139.                if(((PIND&0x01)==0&&(PINA&0x01)!=0x00))                 //显示器上升 按键是否按下  
  140.  
  141.                      {  
  142.  
  143.                          a1=1;  
  144.  
  145.                          while(a1)  
  146.  
  147.                          {  
  148.  
  149.                                PORTC |=(1<<PC0);           //电机使能  
  150.  
  151.                                 PORTC |=(1<<PC2);           //控制电机方向  
  152.  
  153.                          if((PIND&0x01)!=0x00)       //限位开关是否到达  
  154.  
  155.                                 {  
  156.  
  157.                                     a1=0;  
  158.  
  159.                                  PORTC&=~(1<<PC0);  
  160.  
  161.                                 }  
  162.  
  163.                                    if((PINA&0x01)==0x00)       //限位开关是否到达  
  164.  
  165.                                 {  
  166.  
  167.                                           PORTC&=~(1<<PC0);  
  168.  
  169.                                           a1=0;                     
  170.  
  171.                                 }  
  172.  
  173.                          }  
  174.  
  175.                      }  
  176.  
  177.                      if(((PIND&0x02)==0)&&((PINA&0x02)!=0x00))                   //显示器下降 按键是否按下  
  178.  
  179.                      {  
  180.  
  181.                          a2=1;  
  182.  
  183.                          while(a2)  
  184.  
  185.                          {  
  186.  
  187.                                PORTC |=(1<<PC0);          //电机使能     
  188.  
  189.                                 PORTC&=~(1<<PC2);          //控制电机方向  
  190.  
  191.                                 if((PIND&0x02)!=0x00)     //显示器下降 按键是否松开  
  192.  
  193.                                 {  
  194.  
  195.                                     a2=0;  
  196.  
  197.                                  PORTC&=~(1<<PC0);  
  198.  
  199.                                 }  
  200.  
  201.                                    if((PINA&0x02)==0x00)     //限位开关是否到达  
  202.  
  203.                                 {  
  204.  
  205.                                           PORTC&=~(1<<PC0);  
  206.  
  207.                                           a2=0;  
  208.  
  209.                                 }  
  210.  
  211.                          }      
  212.  
  213.                      }  
  214.  
  215.                   if(((PIND&0x04)==0)&&((PINA&0x08)!=0x00))  
  216.  
  217.                      {  
  218.  
  219.                          a3=1;  
  220.  
  221.                          while(a3)  
  222.  
  223.                         {  
  224.  
  225.                                PORTC |=(1<<PC4);  
  226.  
  227.                                 PORTC |=(1<<PC6);  
  228.  
  229.                                 if((PIND&0x04)!=0x00)  
  230.  
  231.                                 {  
  232.  
  233.                                    a3=0;  
  234.  
  235.                                 PORTC&=~(1<<PC4);  
  236.  
  237.                                 }                           
  238.  
  239.                                    if((PINA&0x08)==0x00)  
  240.  
  241.                                 {  
  242.  
  243.                                           PORTC&=~(1<<PC4);  
  244.  
  245.                                           a3=0;  
  246.  
  247.                                 }  
  248.  
  249.                          }  
  250.  
  251.                      }  
  252.  
  253.                      if(((PIND&0x08)==0)&&((PINA&0x04)!=0x00))  
  254.  
  255.                      {  
  256.  
  257.                          a4=1;  
  258.  
  259.                          while(a4)  
  260.  
  261.                         {  
  262.  
  263.                                PORTC |=(1<<PC4);  
  264.  
  265.                                 PORTC&=~(1<<PC6);  
  266.  
  267.                                 if((PIND&0x08)!=0x00)  
  268.  
  269.                                 {  
  270.  
  271.                                    a4=0;  
  272.  
  273.                                 PORTC&=~(1<<PC4);  
  274.  
  275.                                 }  
  276.  
  277.                                    if((PINA&0x04)==0x00)  
  278.  
  279.                                 {  
  280.  
  281.                                           PORTC&=~(1<<PC4);  
  282.  
  283.                                           a4=0;  
  284.  
  285.                                 }  
  286.  
  287.                          }  
  288.  
  289.                      }  
  290.  
  291.                      if(((PIND&0x10)==0)&&((PINA&0x20)!=0x00))  
  292.  
  293.                      {  
  294.  
  295.                          a5=1;  
  296.  
  297.                          while(a5)  
  298.  
  299.                          {  
  300.  
  301.                               PORTC |=(1<<PC1);  
  302.  
  303.                                PORTC |=(1<<PC3);  
  304.  
  305.                                if((PIND&0x10)!=0x00)  
  306.  
  307.                                {  
  308.  
  309.                                  a5=0;  
  310.  
  311.                               PORTC&=~(1<<PC1);  
  312.  
  313.                                }  
  314.  
  315.                                if((PINA&0x20)==0x00)  
  316.  
  317.                                {  
  318.  
  319.                                           PORTC&=~(1<<PC1);  
  320.  
  321.                                           a5=0;  
  322.  
  323.                                 }  
  324.  
  325.                          }  
  326.  
  327.                      }  
  328.  
  329.                      if(((PIND&0x20)==0)&&((PINA&0x10)!=0x00))  
  330.  
  331.                      {  
  332.  
  333.                          a6=1;  
  334.  
  335.                          while(a6)  
  336.  
  337.                          {  
  338.  
  339.                               PORTC |=(1<<PC1);  
  340.  
  341.                                PORTC&=~(1<<PC3);  
  342.  
  343.                                if((PIND&0x20)!=0x00)  
  344.  
  345.                                {  
  346.  
  347.                                  a6=0;  
  348.  
  349.                               PORTC&=~(1<<PC1);  
  350.  
  351.                                }  
  352.  
  353.                                if((PINA&0x10)==0x00)  
  354.  
  355.                                {  
  356.  
  357.                                           PORTC&=~(1<<PC1);  
  358.  
  359.                                           a6=0;  
  360.  
  361.                                }  
  362.  
  363.                          }      
  364.  
  365.                       }     
  366.  
  367.                 }   
  368.  
  369.                }     
  370.  
  371.         }  
  372.  
  373.    
  374.  
  375. }  

 

 

你可能感兴趣的:(机械创新设计竞赛,步进电机控制电路与程序)