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