📄 msteps.c
字号:
/* C program for National Semi LMD18245/PIC microstepping driver board.
IRQ version for Step input.
****PIC16f84A version****
Embedded Acquistion Systems
www.embeddedtronics.com
copyright 2003-2004
Compiled with PCW PIC Compiler Version 3.021
May 29, 2003
Note:
If you see any errors, please contact me. jimf@embeddedtronics.com
Use this code at your own risk, no guarantee that it will work for you.
This source code is for Non-commercial personal use only.
If you want to use this code for any other purpose,
please contact sales@embeddedtronics.com for a commercial license.
If you modify/enhance this program, please send me a copy. Thanks.
*/
/* PIC F84 Pin definition.
pin17 RA0 Motor Driver A, DAC M1 Output
pin18 RA1 Motor Driver A, DAC M2 Output
pin1 RA2 Motor Driver A, DAC M3 Output
pin2 RA3 Motor Driver A, DAC M4 Output
pin3 RA4 unused
pin6 RB0 STEP Input
pin7 RB1 DIR Input
pin8 RB2 Motor Driver A Direction Output
pin9 RB3 Motor Driver B Direction Output
pin10 RB4 Motor Driver B, DAC M1 Output
pin11 RB5 Motor Driver B, DAC M2 Output
pin12 RB6 Motor Driver B, DAC M3 Output
pin13 RB7 Motor Driver B, DAC M4 Output
*/
/*
CS OUT current sense resister
(Vref x D/16) / (250e-6 x R) = A
R=20K 1amp
R=10K 2amp
R=8K 2.5amp
R=6.6K 3amp
1/4 watt
Maximum driver rating 3amps @ 55volts
*/
#include <16F84A.h>
#use delay(clock=20000000)
#fuses HS,NOWDT
#byte PORT_A = 5
#byte PORT_B = 6
static char pos = 0;
#define step_input PIN_B0
#define dir_input PIN_B1
//Choose step input configuration
//#define fullstep
//#define halfstep
//#define halfstep_torque
//#define microstep4
#define microstep8
//#define microstep10
//Comment out if step multiplier is NOT needed
//#define step_multiply
//#define step_x 2 //Number of steps to multiply
//----------------------------------------------------
#ifdef fullstep
#define STEPS 4
//full step drive
//200 steps rev
byte const PB[4] = {0b11111000,
0b00000000,
0b11110100,
0b00001100};
byte const PA[4] = {0b0000,
0b1111,
0b0000,
0b1111};
#endif
//----------------------------------------------------
#ifdef halfstep
// half step drive without torque compensation
// 400 steps rev
#define STEPS 8
byte const PB[8] = {0b11111000,
0b11111000,
0b00000000,
0b11110000,
0b11110100,
0b11110100,
0b00001100,
0b11111100};
byte const PA[8] = {0b0000,
0b1111,
0b1111,
0b1111,
0b0000,
0b1111,
0b1111,
0b1111};
#endif
#ifdef halfstep_torque
// half step drive with torque compensation
// 400 steps rev
#define STEPS 8
byte const PB[8] = {0b00001100,
0b10111100,
0b11111000,
0b10111000,
0b00000000,
0b10110000,
0b11110100,
0b10110100};
byte const PA[8] = {0b1111,
0b1011,
0b0000,
0b1011,
0b1111,
0b1011,
0b0000,
0b1011};
#endif
//----------------------------------------------------
#ifdef microstep4
// quarter step drive
// 800 steps rev
#define STEPS 16
byte const PB[16] ={0b00001100,
0b01101100,
0b10111100,
0b11101100,
0b11111000,
0b11101000,
0b10111000,
0b01101000,
0b00000000,
0b01100000,
0b10110000,
0b11100000,
0b11110100,
0b11100100,
0b10110100,
0b01100100};
byte const PA[16] = {0b1111,
0b1110,
0b1011,
0b0110,
0b0000,
0b0110,
0b1011,
0b1110,
0b1111,
0b1110,
0b1011,
0b0110,
0b0000,
0b0110,
0b1011,
0b1110};
#endif
//----------------------------------------------------
#ifdef microstep8
// eight step drive
// 1600 steps rev
#define STEPS 32
byte const PB[32] ={0b00001100,
0b00111100,
0b01101100,
0b10001100,
0b10111100,
0b11001100,
0b11101100,
0b11111100,
0b11111000,
0b11111000,
0b11101000,
0b11011000,
0b10111000,
0b10001000,
0b01101000,
0b00111000,
0b00000000,
0b00110000,
0b01100000,
0b10000000,
0b10110000,
0b11000000,
0b11100000,
0b11110000,
0b11110100,
0b11110100,
0b11100100,
0b11010100,
0b10110100,
0b10000100,
0b01100100,
0b00110100};
byte const PA[32] ={0b1111,
0b1111,
0b1110,
0b1101,
0b1011,
0b1000,
0b0110,
0b0011,
0b0000,
0b0011,
0b0110,
0b1000,
0b1011,
0b1100,
0b1110,
0b1111,
0b1111,
0b1111,
0b1110,
0b1101,
0b1011,
0b1000,
0b0110,
0b0011,
0b0000,
0b0011,
0b0110,
0b1000,
0b1011,
0b1100,
0b1110,
0b1111};
#endif
//----------------------------------------------------
#ifdef microstep10
// ten step drive
// 2000 steps rev
#define STEPS 40
byte const PB[40] ={0b00001100,
0b00101100,
0b01011100,
0b01111100,
0b10011100,
0b10111100,
0b11001100,
0b11011100,
0b11101100,
0b11111100,
0b11111000,
0b11111000,
0b11101000,
0b11011000,
0b11001000,
0b10111000,
0b10011000,
0b01111000,
0b01011000,
0b00101000,
0b00000000,
0b00100000,
0b01010000,
0b01110000,
0b10010000,
0b10110000,
0b11000000,
0b11010000,
0b11100000,
0b11110000,
0b11110100,
0b11110100,
0b11100100,
0b11010100,
0b11000100,
0b10110100,
0b10010100,
0b01110100,
0b01010100,
0b00100100};
byte const PA[40] ={0b1111,
0b1111,
0b1110,
0b1101,
0b1100,
0b1011,
0b1001,
0b0111,
0b0101,
0b0010,
0b0000,
0b0010,
0b0101,
0b0111,
0b1001,
0b1011,
0b1100,
0b1101,
0b1110,
0b1111,
0b1111,
0b1111,
0b1110,
0b1101,
0b1100,
0b1011,
0b1001,
0b0111,
0b0101,
0b0010,
0b0000,
0b0010,
0b0101,
0b0111,
0b1001,
0b1011,
0b1100,
0b1101,
0b1110,
0b1111};
#endif
//----------------------------------------------------
void step(char dir)
{
pos = (pos + STEPS + dir) % STEPS;
/* output to drivers */
PORT_A = PA[pos];
PORT_B = PB[pos];
}
void stepper_off() //Turn off power to all windings.
{
PORT_A = 0;
PORT_B = 0;
}
void init_stepper() //Move a few to register correct step position.
{
int i;
for (i=0;i<8;i++){
step(-1);
delay_ms(100);
}
for (i=0;i<8;i++){
step(1);
delay_ms(100);
}
}
void test1(void){
int j,speed, accel;
int16 i;
speed = 3
; //max motor speed, lower is faster
accel = 1; //acceleration rate, higher is faster
for(;;){
j=5;
for (i=0;i<200;i++){
step(-1);
j=j-accel;
if (j<speed){
j=speed;
};
delay_ms(j);
}
delay_ms(300);
j=4;
for (i=0;i<200;i++){
step(1);
j=j-accel;
if (j<speed){
j=speed;
};
delay_ms(j);
}
delay_ms(300);
} //end for
} //end test1
void maxcurrent() {
//turn on both drivers with max current for testing resister value.
//Use this carefully, may destroy driver and/or motors.
//Make sure you have correct current limit resister installed.
PORT_A = 0b1111;
PORT_B = 0b11111000;
}
#INT_EXT // Pin B0 interrupt service routine for Step input Active low
void ext_isr() {
//read dir input and advance motor
signed int dir,i;
dir=input(dir_input);
if (dir==0){
dir=-1;}
//Step Multiply routine
#ifdef step_multiply
for (i=0;i<step_x;i++){
step(dir);
delay_us(5); //Note: May need to increase or decrease this delay depending on how slow your motor is.
}
#else
step(dir);
#endif
}
void main() {
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
ext_int_edge(H_TO_L); // init interrupt edge triggering for B0, Active Low
enable_interrupts(INT_EXT); // turn on external B0 interrupt
enable_interrupts(GLOBAL);
set_tris_a(0x10); //set port A&B for correct data i/o directions
set_tris_b(0x03);
#use fast_io (A)
#use fast_io (B)
stepper_off(); //No current to motor.
delay_ms(1000); //1 second turn on delay.
init_stepper();
//test1();
//maxcurrent();
for(;;); //keep looping, waiting for interrupt request
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -