-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTD2(PWM).c
162 lines (144 loc) · 3 KB
/
TD2(PWM).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
#include "xc_config_settings.h"
#include "adc.h"
#include "timers.h"
#include "delays.h"
#include "math.h"
#include "pwm.h"
void move(int angle, int forward);
void config(void);
void Rmotor(int power);
void Lmotor(int power);
int x = 0;
int time360 = 2000;
int main(void)
{
config();
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(0, 1); //forward
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(0, -1); //backward
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(0, -1); //backward
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(0, 2);//fast forward
Delay10KTCYx(20);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(90, 0); //turn right
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(-90, 0); //turn left
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
move(0, 3); //curve
ClosePWM4();
ClosePWM5();
while (1);
}
void move(int angle, int forward)
{
int exec_time_ms = ((double)sqrt(angle*angle)/360.0)*time360;
if(forward == 3)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(800);
Delay10KTCYx(225);
PORTHbits.RH3 = 0;
}
if(forward == 2)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(700);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
if(forward == 1)
{
PORTHbits.RH3 = 1;
Rmotor(625);
Lmotor(625);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
else if(forward == -1)
{
PORTHbits.RH3 = 1;
Rmotor(375);
Lmotor(375);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
else if(forward == 0)
{
if(angle > 0)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(300);
for(int i = 0; i < exec_time_ms; i++)
{
Delay10TCYx(200);
Delay10TCYx(200);
}
PORTHbits.RH3 = 0;
}
else
{
PORTHbits.RH3 = 1;
Rmotor(300);
Lmotor(700);
for(int i = 0; i < exec_time_ms; i++)
{
Delay10TCYx(200);
Delay10TCYx(200);
}
PORTHbits.RH3 = 0;
}
}
}
void config(void)
{
//pwm output
TRISGbits.RG3 = 0;
TRISGbits.RG4 = 0;
TRISH = 0b10010100;
//enable bit
//PORTHbits.RH3 = 1;
//unipolar setting
PORTHbits.RH0 = 1;
PORTHbits.RH1 = 1;
//direction bits
PORTHbits.RH5 = 0;
PORTHbits.RH6 = 0;
//timer configuration
OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
//OpenPWM2
OpenPWM4(252);
OpenPWM5(252);
}
void Rmotor(int power)
{
SetDCPWM4(power);
}
void Lmotor(int power)
{
SetDCPWM5(power);
}