-
Notifications
You must be signed in to change notification settings - Fork 5
/
controlling-stepper-motor-pic16f877a.c
70 lines (59 loc) · 1.68 KB
/
controlling-stepper-motor-pic16f877a.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
#define SW1 RB0_bit
#define SW2 RB1_bit
#define SW3 RB2_bit
#define SW4 RB3_bit
#define ENABLE RC5_bit
#define DIR RC6_bit
#define PULSE RC2_bit
void dynamic_delay_ms(unsigned int delay);
void main()
{
unsigned int frequency=0;
TRISB0_bit=1;
TRISB1_bit=1;
TRISB2_bit=1;
TRISB3_bit=1;
TRISC5_bit=0;
TRISC6_bit=0;
TRISC2_bit=0;
ENABLE=1; //disable motor
while(1)
{
if(SW1==0)
{
frequency=250; //set motor speed to 250Hz
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW2==0)
{
frequency=250; //set motor speed to 250Hz
DIR=0; //set motor direction to CCW
ENABLE=0; //enable motor
}
else if(SW3==0)
{
frequency=500; //set motor speed to 500Hz
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW4==0)
{
frequency=500; //set motor speed to 500Hz
DIR=0; //set motor direction to CCW
ENABLE=0; //enable motor
}
else
{
frequency=0; //set motor speed to 800Hz
ENABLE=1; //disable motor
}
PULSE=1;
PULSE=0;
if(frequency>0)dynamic_delay_ms(1000/frequency);
}
}
void dynamic_delay_ms(unsigned int delay)
{
for( ;delay>0;delay-=1)Delay_Ms(1);
}