0

我的项目有问题,我需要你的帮助。
我的项目是通过 gui 使用 PIC16F877A 控制带有 4 个直流电机的机械臂,并“教”它们一些动作。我已经编写了 gui 中带有 8 个按钮的动作代码,上、下、左、右等。
我可以从模拟引脚读取值并将它们从串行发送到 pc,现在我通过读取模拟值驱动特定位置的电机遇到问题。
我用while循环完成了它,但它只运行1个电机,然后它停止了,我什么也做不了。一开始我可以使用所有其他按钮,但如果我按下“b”或“e”,它就会堆叠起来,我什么也做不了。
你能帮忙吗??
PS对不起我的英语。

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

  }
}
4

0 回答 0