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;

}
Shrikant Vaishnav
  • 80
  • 1
  • 5
  • 13
  • Perhaps your external switches are incorrect, or give a brief transient value. In the latter case, the `default` statement might be invoked. As a suggestion, why don't you try just writing PORTA input to PORTB output continually and look at what happens. And BTW in the statement `case 0b00001101: { PORTA=0b00001000; ...}` did you really mean to write to `PORTA`? – Weather Vane Feb 22 '15 at 18:48
  • You probably did not intend to define a clock frequency of 1.6GHz? The _delay_ms() requires a properly set F_CPU. – Rev Feb 23 '15 at 07:42

2 Answers2

1

Are you sure the higher pins of PORTA are all 0? If not, then none of the cases will match.

You can try

switch(PINA & 0x0F)

instead. This will ensure that the higher pins will not cause the cases to fail.

UncleO
  • 8,299
  • 21
  • 29
1

Please note the the register to test is not PORTA but PINA. You have to distinguish between 'port x' when referring the hardware unit and PORTx when talking about accessing the special function registers. Reading 'port A' means reading the PINA register.

  • PORTx are the registers where you set the output (or if configured as input) the pull ups
  • PINx are the registers that hold the current input level at the pins. (if configured to output they got of course the same value as the PORTx (but with some delay)

On modern AVRs you also can write to the PIN registers if you want to toggle the output. This saves a few cycles compared to the normal way of reading the PORT, XORing it and writing it back.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80