用模拟值驱动直流电机

时间:2015-12-22 11:43:33

标签: microcontroller microc

我的项目有问题,我需要你的肝脏 我的项目是用一台带有PIC16F877A的4个直流电机控制一个机器人手臂,并教授"教授"他们有些动作。我已经用gui,up,down,left,right等8个按钮编写了动作的代码 我可以从模拟引脚读取值并将它们从串口发送到pc,现在我通过读取模拟值来驱动特定位置的电机,从而出现问题。
我已经使用while循环但是它只运行了1个电机然后它停止了我无法做任何事情。一开始我可以使用所有其他按钮但是如果我按下' b'或者' e'它堆叠,我什么都做不了。
能帮忙吗? 附:抱歉我的英文。

unsigned int y0, y1, y2, y3;
char x;
char buffer[15];
void GetDelay(){
      Delay_ms(80);
      }
void main() {

      CMCON = 0x07;
      ADCON1 = 0x80;

      UART1_Init(9600);
      Delay_ms(100);
      ADC_Init();
      Delay_ms(100);
      TRISA0_bit = 1;
      PORTA.F0 = 1;
      TRISA1_bit = 1;
      PORTA.F1 = 1;
      TRISA2_bit = 1;
      PORTA.F2 = 1;
      TRISA3_bit = 1;
      PORTA.F3 = 1;
      TRISB = 0;
      PORTB = 0;
      TRISB0_bit = 1;
      PORTB.F0 = 1;
      TRISB1_bit = 1;
      PORTB.F1 = 1;
      TRISB4_bit = 1;
      PORTB.F4 = 0;
      TRISB5_bit = 1;
      PORTB.F5 = 0;
      TRISC1_bit = 0;
      PORTC.F1 = 0;
      TRISC2_bit = 0;
      PORTC.F2 = 0;
      TRISC3_bit = 0;
      PORTC.F3 = 0;
      TRISC4_bit = 0;
      PORTC.F4 = 0;
      TRISC5_bit = 0;
      PORTC.F5 = 0;
      TRISD0_bit = 1;
      PORTD.F0 = 1;
      TRISD1_bit = 1;
      PORTD.F1 = 1;
      TRISD2_bit = 1;
      PORTD.F2 = 1;
      TRISD3_bit = 1;
      PORTD.F3 = 1;
      TRISD4_bit = 1;
      PORTD.F4 = 1;
      TRISD5_bit = 1;
      PORTD.F5 = 1;
      TRISD6_bit = 0;
      PORTD.F6 = 0;
      TRISD7_bit = 0;
      PORTD.F7 = 0;

PWM2_Init(5000);  //Initialize PWM1
PWM2_Start();  //start PWM1
PWM2_Set_Duty(127); //Set current duty for PWM1

      while(1){
        if(RB4_bit==0){

               if (UART1_Data_Ready() == 1){
                  x = UART1_Read();
               }
               //ARPAGH OPEN
               if (x=='1' ){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;

               }
               //ARPAGH CLOSE
               else if (x=='2'){
                  RC4_bit=1;
                  RC5_bit=0;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
               }
               //2h ARTHRWSH DOWN
               else if (x=='3'){
                  RB3_bit=1;
                  RB2_bit=0;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
               }
               //2h ARTHRWSH UP
               else if (x=='4'){
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
               }
               //1h ARTHRWSH DOWN
               else if (x=='5'){
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
               }
               //1h ARTHRWSH UP
               else if (x=='6'){
                  RD6_bit=1;
                  RD7_bit=0;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
               }
               //PERISTROFH RIGHT
               else if (x=='7'){
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //PERISTROFH LEFT
               else if (x=='8'){
                  RC2_bit=0;
                  RC3_bit=1;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //ANALOG
               else if (x=='9'){

                   y0 = ADC_Get_Sample(0);
                   sprintl(buffer, "%d", y0);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y2 = ADC_Get_Sample(2);
                   sprintl(buffer, "%d", y2);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y1 = ADC_Get_Sample(1);
                   sprintl(buffer, "%d", y1);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y3 = ADC_Get_Sample(3);
                   sprintl(buffer, "%d", y3);
                   UART1_Write_Text(buffer);
                   //UART1_Write_Text("\n");
                   UART1_Write(13);
               }
               //START POSITION
               else if (x=='b'){
               //ARPAGH OPEN
               while (RD2_Bit==0){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //2h ARTHRWSH UP
               while (y2>440){
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                }
               //1h ARTHRWSH DOWN
               while (y1>470){
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  }
               //PERISTROFH RIGHT
               while (y3>120){
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  }
               }
              //FINAL POSITION
               else if (x=='e'){
               //ARPAGH OPEN
               while (RD2_Bit==0){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //2h ARTHRWSH DOWN
               while (y2<440){
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                  RB3_bit=1;
                  RB2_bit=0;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;

                }
                //2h ARTHRWSH UP
               while (y2>440){
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;

                }
               //1h ARTHRWSH UP
               while (y1<470){
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  RD6_bit=1;
                  RD7_bit=0;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;

                  }
               //1h ARTHRWSH DOWN
               while (y1>470){
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;

                  }
               //ARPAGH CLOSE
               while (RD3_Bit==0){
                  RC4_bit=1;
                  RC5_bit=0;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //PERISTROFH LEFT
               while (y3<120){
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  RC2_bit=0;
                  RC3_bit=1;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //PERISTROFH RIGHT
               while (y3>120){
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }

               }

               x = '\0';
   }

  }
}

0 个答案:

没有答案