-
Notifications
You must be signed in to change notification settings - Fork 5
/
controlling-dc-brush-motor-pic16f877a.c
56 lines (50 loc) · 1.46 KB
/
controlling-dc-brush-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
#define SW1 RB0_bit
#define SW2 RB1_bit
#define SW3 RB2_bit
#define SW4 RB3_bit
#define ENABLE RC5_bit
#define DIR RC6_bit
void main()
{
TRISB0_bit=1;
TRISB1_bit=1;
TRISB2_bit=1;
TRISB3_bit=1;
TRISC5_bit=0;
TRISC6_bit=0;
ENABLE=1; //disable motor
PWM1_Init(5000); //initialize PWM1 module at 5KHz
PWM1_Set_Duty(0); //initialize PWM1 dut cycle as 0
PWM1_Start(); //start PWM1
while(1)
{
if(SW1==0)
{
PWM1_Set_Duty(192); //set motor speed to 75%
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW2==0)
{
PWM1_Set_Duty(192); //set motor speed to 75%
DIR=0; //set motor direction to CCW
ENABLE=0; //enable motor
}
else if(SW3==0)
{
PWM1_Set_Duty(255); //set motor speed to 100%
DIR=1; //set motor direction to CW
ENABLE=0; //enable motor
}
else if(SW4==0)
{
PWM1_Set_Duty(255); //set motor speed to 100%
DIR=0; //set motor direction to CCW
ENABLE=0; //enable motor
}
else
{
ENABLE=1; //disable motor
}
}
}