0

Hello Friends i dont know what happen neither switch-case nor if,else statement works for me i want to give some data to both PORTB & PORTD when some specific data are come to the PORTA register in my "switch block" previously i used PINA instead of PORTA but it still not works but when i start debugging and giving some data by giving PORTA=0b00001110 it easily gives values PORTB=0b00000010.... please help..

/*
 * 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 回答
1

请注意,要测试的寄存器不是PORTA但是PINAPORTx在提及硬件单元和访问特殊功能寄存器时,您必须区分“端口 x” 。读取“端口 A”意味着读取PINA寄存器。

  • PORTx是您设置输出(或者如果配置为输入)的寄存器
  • PINx是在引脚上保持当前输入电平的寄存器。PORTx(如果配置为输出,它们当然会得到与(但有一些延迟)相同的值

PIN在现代 AVR 上,如果您想切换输出,也可以写入寄存器。PORT与读取、异或和写回它的正常方式相比,这节省了几个周期。

于 2015-02-23T16:39:00.700 回答