0

こんにちは、switch-case ステートメントも if,else ステートメントも機能しません。以前は PINA を使用していた「スイッチ ブロック」の PORTA レジスターに特定のデータが入ったときに、PORTB と PORTD の両方にデータを提供したいと考えています。 PORTAの代わりにまだ動作しませんが、デバッグを開始し、PORTA = 0b00001110を指定してデータを与えると、PORTB = 0b00000010の値が簡単に得られます....助けてください..

/*
 * robotic_arm.c
 *
 * Created: 2/3/2015 10:39:25 AM
 *  Author: Shrikant Vaishnav
 */ 

#define F_CPU 1600000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{ DDRA=0x00;//make PORTA as input
  DDRB=0xFF;//make PORTB as output
  DDRD=0XFF;//make PORTD as output

    while(1)
    {

        switch(PORTA)
        {

        //First Three conditions for Robotic ARMs

        case 0b00001110:
         {
            PORTB=0b00000010;
             _delay_ms(50);
             break;
         }

         case 0b00001101:
         {
            PORTB=0b00001000 ;
             _delay_ms(50);
            break; 
         }

         case 0b00001011:
         {
            PORTB=0b00100000 ;
             _delay_ms(50);
             break; 
         }

         //Condition for Direction Change of Motors of Robotic Arms
         case 0b00000110:
         {
             PORTB=0b00000001;
              _delay_ms(50);
             break;

         }

         case 0b00000101:
         {
             PORTB=0b00000100;
              _delay_ms(50);
             break;

         }

          case 0b00000011:
          {
              PORTB=0b00100000;
              _delay_ms(50);
              break;

          }

        //Now Driving Robotic Car
         case 0b00000010:
         {
             PORTD=0b00000010;
              _delay_ms(50);
             break;

         }

          case 0b00000001:
          {
              PORTD=0b00000001;
               _delay_ms(50);
              break;

          }

        default:
        { 
            PORTB=0b00000000; //0ff motors when no signal sent
            PORTD=0b00000000; //OFF DRIVING CAR
             _delay_ms(50);
            break;

         }


    }
}

return 0;

}
4

2 に答える 2

1

PORTAの上位ピンは全部0ですか?そうでない場合、どのケースも一致しません。

あなたが試すことができます

switch(PINA & 0x0F)

代わりは。これにより、より高いピンが原因でケースが失敗することがなくなります。

于 2015-02-22T20:04:36.843 に答える