-
Notifications
You must be signed in to change notification settings - Fork 5
/
controlling-industrial-servo-motor-pic16f877a.c
75 lines (62 loc) · 1.81 KB
/
controlling-industrial-servo-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
71
72
73
74
75
#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 ALARM RC7_bit
#define PULSE RC2_bit
void dynamic_delay_100us(unsigned char 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;
TRISC7_bit=1;
TRISC2_bit=0;
ENABLE=1; //disable motor
while(1)
{
if(SW1==0)
{
frequency=5000; //set motor speed to 5000Hz
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW2==0)
{
frequency=5000; //set motor speed to 5000Hz
DIR=0; //set motor direction to CCW
ENABLE=0; //enable motor
}
else if(SW3==0)
{
frequency=10000; //set motor speed to 10000Hz
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW4==0)
{
frequency=10000; //set motor speed to 10000Hz
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;
Delay_us(1);
PULSE=0;
if(frequency>0)dynamic_delay_100us(10000/frequency);
}
}
void dynamic_delay_100us(unsigned char delay)
{
for( ;delay>0;delay-=1)Delay_Us(100);
}