yogi
Joined: 25 Jun 2006 Posts: 185
|
Posted: Wed Jun 28, 2006 4:49 pm Post subject: stepper motor interfacing with AVR ( C)(bipolar stepper) |
|
|
This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibin
http://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.com
BIPOLAR STEPPER MOTOR DRIVING USING AVR
see the connections
| Code: |
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN L293D AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define L293D_DDR DDRC
#define L293D_PORT PORTC
#define Pin_1 0
#define Pin_2 1
#define Pin_3 2
#define Pin_4 3
#define L293D_Mask 0x0f
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void Init_Ports(void);
void delay_ms(unsigned char time_ms);
void main()
{
int i;
int steps[]={15,13,12,14};
Init_Ports();
i=0;
do
{
L293D_PORT=steps[i];
i++;
if(i>4) i=0;
delay_ms(50);
}
while(1);
}
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
L293D_DDR|=(_BV(Pin_1 )|_BV(Pin_2)|_BV(Pin_3 )|_BV(Pin_4)); //setting pins for output
L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2)|_BV(Pin_3 )|_BV(Pin_4));//starting motor is Off
}
/*----------------------------------------------------------------
--------FUNCTION TO GENERATE DELAY IN MILLISECONDS----------------
-----------------------------------------------------------------*/
void delay_ms(unsigned char time_ms)
{
unsigned short delay_count = F_CPU / 400;
unsigned short cnt;
asm volatile ("n"
"L_dl1%=:nt"
"mov %A0, %A2nt"
"mov %B0, %B2n"
"L_dl2%=:nt"
"sbiw %A0, 1nt"
"brne L_dl2%=nt"
"dec %1nt" "brne L_dl1%=nt":"=&w" (cnt)
:"r"(time_ms), "r"((unsigned short) (delay_count))
);
}
|
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN L293D AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define L293D_DDR DDRC
#define L293D_PORT PORTC
#define Pin_1 0
#define Pin_2 1
#define Pin_3 2
#define Pin_4 3
#define L293D_Mask 0x0f
Here are the connections used
int steps[]={15,13,12,14};
the steps sequence used for the bipolar stepper motors are shown in arrya. This will make 4 steps. Repeat this sequence again for continous motion.
FOLDER:bipolarstepper(yahoo group-booksbybibin, file-avrbook.rar)
OUTPUT
The motor will continue the rotation
IMPORTANT NOTE
See the ratings of the stepper motor before using. The maximum current should be less than 600mA, otherwise the L293D won't be able to drive it. Pin8 of L293D is connected to the Vcc of the stepper motor. Apply proper Vcc to Pin8 |
|