/*============================================================================== stepper_motor.h (tested on MINIANGLE STEPPER MOTOR 5V/1A and L298 IC) Bidirectional Stepper Motor Driver for Small Device C Compiler Written by Ervin Jung (2010) (more informations: <a href="http://www.electojects.com/motors" title="http://www.electojects.com/motors">http://www.electojects.com/motors</a>) -------------------------------------------------------------------------------- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve what you give them. Help stamp out software-hoarding! As a special exception, if you link this library with other files, some of which are compiled with SDCC, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. -------------------------------------------------------------------------------- defaults: L298 PIC & Motor 1 O----- R1 -----------> GND for Full Current (1 Ohm) 2 O--- OUTPUTA1 -----------> Motor (Green) 3 O----- OUTPUTA2 -----------> Motor (Blue) 4 O--- Vs -----------> Power Supply 5 O----- INPUTA1 -----------> PORTB7 6 O--- ENABLEA -----------> Vss for High Level 7 O----- INPUTA2 ----------> PORTB6 8 O--- GND -----------> GND 9 O----- Vss -----------> Vss (5V) 10 O--- INPUTB1 -----------> PORTB5 11 O----- ENABLEB -----------> Vss for High Level 12 O--- INPUTB2 -----------> PORTB4 13 O----- OUTPUTB1 -----------> Motor (Yellow) 14 O--- OUTPUTB2 -----------> Motor (Orange) 15 O----- R2 -----------> GND for Full Current (1 Ohm) Modes: WAVEFOM, FULL STEP, HALF STEP default mode FULL_STEP -------------------------------------------------------------------------------- an example: #include <pic18fregs.h> #define FOSC 48000000 #include <stdlib.h> #include <stepper_motor.h> #include <simple_delay.h> void main(void) { int i = 0; long end = 0; stepper_init(); while (1) { end = rand() % 5000; for(i = 0; i < end; i++) { stepper_step(STEP_RIGHT); delay_us(STEPPER_WAIT); } delay_ms(10); end = rand() % 5000 ; for(i=0; i<end; i++) { stepper_step(STEP_LEFT); delay_us(STEPPER_WAIT); } delay_ms(10); } } ==============================================================================*/ #ifndef __STEPPER_MOTOR_H__ #define __STEPPER_MOTOR_H__ static signed char stepper_phase = 0; #define FULL_STEP #define STEP_RIGHT 1 #define STEP_LEFT 0 #define CoilA_PIN1 LATBbits.LATB7 #define CoilA_PIN2 LATBbits.LATB6 #define CoilB_PIN1 LATBbits.LATB5 #define CoilB_PIN2 LATBbits.LATB4 #define CoilA_TRIS1 TRISBbits.TRISB7 #define CoilA_TRIS2 TRISBbits.TRISB6 #define CoilB_TRIS1 TRISBbits.TRISB5 #define CoilB_TRIS2 TRISBbits.TRISB4 #ifdef WAVEFORM #define STEPPER_LENGTH 4 #define STEPPER_WAIT 2500 //us #define CoilA1 0x01 // 0001 #define CoilA2 0x04 // 0100 #define CoilB1 0x02 // 0010 #define CoilB2 0x08 // 1000 #else #ifdef FULL_STEP #define STEPPER_LENGTH 8 #define STEPPER_WAIT 2500 //us #define CoilA1 0xCC // 11001100 #define CoilA2 0x33 // 00110011 #define CoilB1 0x66 // 01100110 #define CoilB2 0x99 // 10011001 #else // HALF_STEP #define STEPPER_LENGTH 8 #define STEPPER_WAIT 2500 // us #define CoilA1 0x83 // 10000011 #define CoilA2 0x38 // 00111000 #define CoilB1 0x0e // 00001110 #define CoilB2 0xe0 // 11100000 #endif #endif static char stepper_sign(char pattern) { return (pattern & (1 << stepper_phase)) > 0 ? 1 : 0; } void stepper_step(char direction) { if(direction) stepper_phase++; else stepper_phase--; if(stepper_phase > STEPPER_LENGTH) stepper_phase = 0; if(stepper_phase < 0) stepper_phase = STEPPER_LENGTH; CoilA_PIN1 = stepper_sign(CoilA1); CoilA_PIN2 = stepper_sign(CoilA2); CoilB_PIN1 = stepper_sign(CoilB1); CoilB_PIN2 = stepper_sign(CoilB2); } void stepper_init() { CoilA_TRIS1 = 0; CoilA_TRIS2 = 0; CoilB_TRIS1 = 0; CoilB_TRIS2 = 0; } #endif
More informations: http://electojects.com/motors/