-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm_driver.c
171 lines (152 loc) · 4.52 KB
/
sm_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief Stepper motor driver.
*
* Stepper motor driver, increment/decrement the position and outputs the
* correct signals to stepper motor.
*
* - File: sm_driver.c
* - Compiler: IAR EWAAVR 4.11A
* - Supported devices: All devices with a 16 bit timer can be used.
* The example is written for ATmega48
* - AppNote: AVR446 - Linear speed control of stepper motor
*
* \author Atmel Corporation: http://www.atmel.com \n
* Support email: [email protected]
*
* $Name: RELEASE_1_0 $
* $Revision: 1.2 $
* $RCSfile: sm_driver.c,v $
* $Date: 2006/05/08 12:25:58 $
*****************************************************************************/
#ifdef __GNUC__
#include <avr/io.h>
#else
#include <ioavr.h>
#endif
#include "global.h"
#include "sm_driver.h"
// Bit position for data in step table
#define BIT_A1 3
#define BIT_A2 2
#define BIT_B1 1
#define BIT_B2 0
//! Table with control signals for stepper motor
#ifdef __GNUC__
#else
__flash
#endif
unsigned char steptab[] = {((1<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (0<<BIT_B2)),
((1<<BIT_A1) | (0<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)),
((0<<BIT_A1) | (0<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)),
((0<<BIT_A1) | (1<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)),
((0<<BIT_A1) | (1<<BIT_A2) | (0<<BIT_B1) | (0<<BIT_B2)),
((0<<BIT_A1) | (1<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2)),
((0<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2)),
((1<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2))};
//! Position of stepper motor (relative to starting position as zero)
int stepPosition = 0;
/*! \brief Init of io-pins for stepper motor.
*/
void sm_driver_Init_IO(void)
{
// Init of IO pins
SM_PORT &= ~((1<<A1) | (1<<A2) | (1<<B1) | (1<<B2)); // Set output pin registers to zero
SM_DRIVE |= ((1<<A1) | (1<<A2) | (1<<B1) | (1<<B2)); // Set output pin direction registers to output
SM_DRIVE |= (1<<PD2) | (1<<PD3); // direction and clock
}
#define ENABLE (PC1)
void sm_driver_enable() {
DDRC |= (1<<ENABLE);
PORTC |= (1<<ENABLE);
}
void sm_driver_disable() {
DDRC |= (1<<ENABLE);
PORTC &= ~(1<<ENABLE);
}
void sm_direction_cw() {
SM_PORT &= ~(1<<PD2);
}
void sm_direction_ccw() {
SM_PORT |= (1<<PD2);
}
/*! \brief Move the stepper motor one step.
*
* Makes the stepcounter inc/dec one value and outputs this to the
* steppermotor.
* This function works like a stepper motor controller, a call to the function
* is the stepping pulse, and parameter 'inc' is the direction signal.
*
* \param inc Direction to move.
* \return Stepcounter value.
*/
unsigned char sm_driver_StepCounter(signed char inc)
{
// Counts 0-1-...-6-7 in halfstep, 0-2-4-6 in fullstep
static unsigned char counter = 0;
SM_PORT |= (1<<PD3);
// Update
if(inc == CCW){
stepPosition--;
}
else{
stepPosition++;
}
#ifdef HALFSTEPS
if(inc){
counter++;
}
else{
counter--;
}
#else
if(inc){
counter += 2;
}
else{
counter -= 2;
}
#endif
// Stay within the steptab
counter &= 0x07;
sm_driver_StepOutput(counter);
// SM_PORT = (inc == CCW)?0xc:8;
// SM_PORT = (inc == CCW)?0x4:0;
SM_PORT &= ~(1<<PD3);
return(counter);
}
/*! \brief Convert the stepcounter value to signals for the stepper motor.
*
* Uses the stepcounter value as index in steptab to get correct
* steppermotor control signals.
* Converts these signals to work with the stepper driver hardware.
*
* \param pos Stepcounter value.
*/
void sm_driver_StepOutput(unsigned char pos)
{
unsigned char temp = steptab[pos];
/*
// Output bit by bit
if(temp&(1<<BIT_A1))
SM_PORT |= (1<<A1);
else
SM_PORT &= ~(1<<A1);
if(temp&(1<<BIT_A2))
SM_PORT |= (1<<A2);
else
SM_PORT &= ~(1<<A2);
if(temp&(1<<BIT_B1))
SM_PORT |= (1<<B1);
else
SM_PORT &= ~(1<<B1);
if(temp&(1<<BIT_B2))
SM_PORT |= (1<<B2);
else
SM_PORT &= ~(1<<B2);
*/
// Output the fast way
SM_PORT |= ((temp<<4)&0xF0);
SM_PORT &= ((temp<<4)|0x0F);
}