Error in Embedded C code for Motor Shield

There is no issue while compiling or running the code except that the motors do not rotate. I have tried to debug it , but the only conclusion i have come to is that there is some error while handling the data with the shift register.
I’m using an Arduino Uno.

/* This is Embed c code for using a adafruit motor shield. The shift Register used is 74HC595N 
 * Pin 4 or PD4 is connected to Master Reset(MR) and Shift Register Clock Input (SHcp) 
 * Pin 7 or PD7 is connected to Output Enable (OE) 
 * Pin 12 or PB4 is connected to Storage Register Clock Input (STcp)
 * Pin 8 or PB0 is connected to Serial data input(Ds)
 */
 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600
  uint8_t Stop = 0b00000000;
  uint8_t Forward =0b00100111;
  uint8_t Backward = 0b11011000;
  uint8_t Left = 0b00111100;
  uint8_t Right = 0b11000011; 
void setup() 
{
  Init_Controller(); 
}
void ShiftOut(uint8_t data)
{
  PORTD &= ~0x80;//Enable, low
  PORTB &= ~0x01;//Data, low
  PORTB &= ~0x10;//Latch, low
  for(int i = 0;i<8;i++)
  {
    PORTD &= ~0x10;//Clock, low
    if( (!!(data & (1<<i))) == 0 )
    {
    PORTB &= ~0x01;//Data, low 
    }
    else
    {
     PORTB |= 0x01;//Data, high
    }
 PORTD |= 0x10;//Clock, high
  }
  PORTB |= 0x10;//Latch, high
  PORTD |= 0x80;//Enable, high
}
void Init_Controller()
{
  DDRB = 0b00010001;
  DDRD = 0b10010000;
}
void loop() 
{  
  
  ShiftOut(Left);
  delay(1000);
  ShiftOut(Right);
  delay(1000);
  ShiftOut(Forward);
  delay(1000);
  ShiftOut(Backward);
  delay(1000);
  ShiftOut(Stop);
  delay(1000);
}

There is no issue while compiling or running the code except that the motors do not rotate.

Such a small issue. Can't you just overlook that?

Clearly, a schematic is essential. Without the shift register, can you make the motor(s) move? Why are you using a shift register, anyway?

The Motor Shield is made of a 8 bit shift register and 2 motor drivers. I need the Shield to run the motors or i would fry the board.

Plenty of people use the Adafruit motor shield library with the Adafruit motor shield. Why aren't you?

Even if, in the end, you can't use the Adafruit library, there is NO excuse for not using it while determining if your hardware is connected correctly, and functioning.

PaulS:
Such a small issue. Can't you just overlook that?

Clearly, a schematic is essential. Without the shift register, can you make the motor(s) move? Why are you using a shift register, anyway?

I've mounted the Motor Shield directly onto the board. i've also the mentioned the pin connections. If you still want the schematics let me know. I'll upload them.

Are your motor constants and your shift direction consistent? Adafruit looks like they do msb first, while you’re doing lsb first...

westfw:
Are your motor constants and your shift direction consistent? Adafruit looks like they do msb first, while you’re doing lsb first…

I’ve tried the Adafruit library and the system works. The motor constants and shift direction are constant . The motors should rotate irrespective if msb or lsb first.

Ah. Your bit clears are wrong…

You have:

  PORTD |= (0<<PD7);   //Enable, low
  PORTB |= (0<<PB0);   //Data, low
  PORTB |= (0<<PB4);   //Latch, low

Whereas you need

  PORTD &= ~(1<<PD7);   //Enable, low

And similar.
See [TUT] [C] Bit manipulation (AKA "Programming 101") | AVR Freaks

Thanks for the link , it was really useful. I made the appropriate changes but the code still won't work. I can't seem to understand how to debug it anymore. Any pointers on how to approach this . As in the approach i should take to debug it. I even went through the adafruit library

In the absence of tools like logic analyzers, I’d put LEDs on the relevant signals and insert big delays in your code, so that you can more-or-less single-step through your logic.

void debug(char *s) {
  Serial.println(s);  // print info
  while (Serial.read() < 0)
    ;  // wait for next step
}

  :

  for(int i = 0;i<8;i++)  {
    PORTD &= ~0x10;//Clock, low
    debug("Clock Low");
    if( (!!(data & (1<<i))) == 0 ) {
      PORTB &= ~0x01;//Data, low
      debug("data Low");
    } else {
      PORTB |= 0x01;//Data, high
      debug("data high");
    }
    PORTD |= 0x10;//Clock, high
    debug("Clock high");
  }

So… You have the ancient and long-discontinued V1 Adafruit Motor Shield, right? Not the newer version that uses a completely different IO mechanism? The lack of “stackable headers” will make attaching LEDs more difficult, I guess.

I tried as you suggested. When i put the Serial.Println commands the code started working. Then i realized that maybe i my code and the shift register were simply no in sync and that i needed to put a few delays to keep them in sync. The code works now . Thanks for all the help.

This is my final code

/* This is Embed c code for using a adafruit motor shield. The shift Register used is 74HC595N 
 * Pin 4 or PD4 is connected to Master Reset(MR) and Shift Register Clock Input (SHcp) 
 * Pin 7 or PD7 is connected to Output Enable (OE) 
 * Pin 12 or PB4 is connected to Storage Register Clock Input (STcp)
 * Pin 8 or PB0 is connected to Serial data input(Ds)
 */
 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600
  uint8_t Stop = 0b00000000;
  uint8_t Forward =0b11100100;
  uint8_t Backward = 0b00011011;
  uint8_t Left = 0b00111100;
  uint8_t Right = 0b11000011; 
  int t = 10;
void setup() 
{
  Serial.begin(9600);
  Init_Controller(); 
}
void ShiftOut(uint8_t data)
{
  PORTD &= ~0x80;//Enable, low
  delay(10);
  PORTB &= ~0x01;//Data, low
  delay(10);
  PORTB &= ~0x10;//Latch, low
  delay(10);
  for(int i = 0;i<8;i++)
  {
    PORTD &= ~0x10;//Clock, low
    delay(10);
    if( (!!(data & (1<<i))) == 0 )
    {
    PORTB &= ~0x01;//Data, low
    delay(10);
    }
    else
    {
     PORTB |= 0x01;//Data, high
    delay(10);
    }
 PORTD |= 0x10;//Clock, high
  delay(10);
  }
  PORTB |= 0x10;//Latch, high
  delay(10);
  PORTD |= 0x80;//Enable, high
  delay(10);
}
void Init_Controller()
{
  DDRB = 0b00010001;
  DDRD = 0b10010000;
}
void loop() 
{  
  for(int i = 0;i<t;i++)
  ShiftOut(Left);
  
  for(int j = 0;j<t;j++)
  ShiftOut(Right);

  for(int k = 0;k<t;k++)
  ShiftOut(Forward);

  for(int l = 0;l<t;l++)
  ShiftOut(Backward);
  
  ShiftOut(Stop);
  delay(1000);
}