Line -... |
Line 1... |
- |
|
1 |
#include "main.h" |
- |
|
2 |
|
1 |
#include "C:\Cizelu\Documents\MLAB\projekty\cizelu\Maturitni_prace\Program\PIC\main.h" |
3 |
// NEPOUZIVAT PINY B6 A B7, JSOU VYHRAZENY PRO SERIOVOU KOMUNIKACI |
- |
|
4 |
// BAUD RATE = 4800 |
2 |
|
5 |
|
3 |
//univerzalni LED diody |
6 |
//univerzalni LED diody |
4 |
#define LED1 PIN_D0 |
7 |
#define LED1 PIN_A4 |
5 |
#define LED2 PIN_D1 |
8 |
#define LED2 PIN_A5 |
6 |
|
9 |
|
7 |
//piezo pipak |
10 |
//piezo pipak |
8 |
#DEFINE SOUND_HI PIN_B1 |
11 |
#DEFINE SOUND_HI PIN_B1 |
9 |
#DEFINE SOUND_LO PIN_B2 |
12 |
#DEFINE SOUND_LO PIN_B2 |
10 |
|
13 |
|
11 |
//naraznik |
14 |
//naraznik |
12 |
#define BUMPL !input(PIN_D2) |
15 |
#define BUMPL input(PIN_D6) |
13 |
#define BUMPR !input(PIN_D3) |
16 |
#define BUMPR input(PIN_D7) |
14 |
|
17 |
|
15 |
//nouzove senzory |
18 |
//nouzove senzory |
16 |
#define LINEL !input(PIN_A1) |
19 |
#define LINEL 0 |
17 |
#define LINER !input(PIN_A2) |
20 |
#define LINER 1 |
- |
|
21 |
#define TRESHOLD 200 // rozhodovaci uroven (zmereno 0 - cerna, 255 cerna) |
18 |
int line_l; |
22 |
int8 line_l; |
19 |
int line_r; |
23 |
int8 line_r; |
20 |
|
24 |
|
21 |
// motory |
25 |
// motory |
- |
|
26 |
#define LMF PIN_D0 |
- |
|
27 |
#define LMB PIN_D1 |
- |
|
28 |
#define RMF PIN_D2 |
- |
|
29 |
#define RMB PIN_D3 |
22 |
|
30 |
|
23 |
|
31 |
int16 bl; |
24 |
//PODPROGRAMY |
32 |
//PODPROGRAMY |
25 |
//SENZORY |
33 |
//SENZORY |
26 |
void cti_bocni_senzory() |
34 |
void read_blue_sensors() // cteni nouzovych senzoru |
27 |
{ |
35 |
{ |
28 |
set_adc_channel(LINEL); //cti levy nouzovy senzor |
36 |
set_adc_channel(LINEL); // cti levy nouzovy senzor |
29 |
delay_us(10); |
37 |
delay_us(10); |
30 |
line_l=read_adc(); |
38 |
line_l=read_adc(); |
- |
|
39 |
|
31 |
set_adc_channel(LINER); //cti pravy nouzovy senzor |
40 |
set_adc_channel(LINER); // cti pravy nouzovy senzor |
32 |
delay_us(10); |
41 |
delay_us(10); |
33 |
line_r=read_adc(); |
42 |
line_r=read_adc(); |
34 |
} |
43 |
} |
35 |
|
44 |
|
36 |
//PIPAK |
45 |
//PIPAK |
37 |
void beep(unsigned int16 period, unsigned int16 length) |
46 |
void beep(unsigned int16 period, unsigned int16 length) |
38 |
{ |
47 |
{ |
39 |
unsigned int16 bp; //promenna pro nastaveni delky |
48 |
unsigned int16 bp; //promenna pro nastaveni delky |
40 |
|
49 |
|
41 |
for(bp=length;bp>0;bp--) |
50 |
for(bp=length;bp>0;bp--) |
42 |
{ |
51 |
{ |
43 |
output_high(SOUND_HI);output_low(SOUND_LO); |
52 |
output_high(SOUND_HI);output_low(SOUND_LO); |
44 |
delay_us(period); |
53 |
delay_us(period); |
45 |
output_high(SOUND_LO);output_low(SOUND_HI); |
54 |
output_high(SOUND_LO);output_low(SOUND_HI); |
46 |
delay_us(period); |
55 |
delay_us(period); |
- |
|
56 |
} |
- |
|
57 |
} |
- |
|
58 |
//MOTORY |
- |
|
59 |
void l_motor_fwd(int8 speedl) // levy motor dopredu |
- |
|
60 |
{ |
- |
|
61 |
output_high(LMF); |
- |
|
62 |
output_low(LMB); |
- |
|
63 |
set_pwm2_duty(speedl); |
- |
|
64 |
} |
- |
|
65 |
|
- |
|
66 |
void l_motor_bwd(int8 speedl) // levy motor dozadu |
- |
|
67 |
{ |
- |
|
68 |
output_high(LMB); |
- |
|
69 |
output_low(LMF); |
- |
|
70 |
set_pwm2_duty(speedl); |
- |
|
71 |
} |
- |
|
72 |
|
- |
|
73 |
void r_motor_fwd(int8 speedr) // pravy motor dopredu |
- |
|
74 |
{ |
- |
|
75 |
output_high(RMF); |
- |
|
76 |
output_low(RMB); |
- |
|
77 |
set_pwm1_duty(speedr); |
- |
|
78 |
} |
- |
|
79 |
|
- |
|
80 |
void r_motor_bwd(int8 speedr) // pravy motor dozadu |
- |
|
81 |
{ |
- |
|
82 |
output_high(RMB); |
- |
|
83 |
output_low(RMF); |
- |
|
84 |
set_pwm1_duty(speedr); |
- |
|
85 |
} |
- |
|
86 |
|
- |
|
87 |
void l_motor_off() // levy motor vypnut |
- |
|
88 |
{ |
- |
|
89 |
output_low(LMF); |
- |
|
90 |
output_low(LMB); |
- |
|
91 |
set_pwm2_duty(0); |
- |
|
92 |
} |
- |
|
93 |
|
- |
|
94 |
void r_motor_off() // pravy motor vypnut |
- |
|
95 |
{ |
- |
|
96 |
output_low(RMF); |
- |
|
97 |
output_low(RMB); |
- |
|
98 |
set_pwm1_duty(0); |
- |
|
99 |
} |
- |
|
100 |
|
- |
|
101 |
void motor_test() // test motoru |
- |
|
102 |
{ |
- |
|
103 |
int8 i; |
- |
|
104 |
printf("TEST MOTORU\n"); |
- |
|
105 |
delay_ms(1000); |
- |
|
106 |
for(i=0;i<255;i++) |
- |
|
107 |
{ |
- |
|
108 |
l_motor_fwd(i); |
- |
|
109 |
printf("RYCHLOST: %u\n",i); |
- |
|
110 |
delay_ms(10); |
- |
|
111 |
} |
- |
|
112 |
for(i=255;i>0;i--) |
- |
|
113 |
{ |
- |
|
114 |
l_motor_fwd(i); |
- |
|
115 |
printf("RYCHLOST: %u\n",i); |
- |
|
116 |
delay_ms(10); |
- |
|
117 |
} |
- |
|
118 |
for(i=0;i<255;i++) |
- |
|
119 |
{ |
- |
|
120 |
l_motor_bwd(i); |
- |
|
121 |
printf("RYCHLOST: %u\n",i); |
- |
|
122 |
delay_ms(10); |
- |
|
123 |
} |
- |
|
124 |
|
- |
|
125 |
for(i=255;i>0;i--) |
- |
|
126 |
{ |
- |
|
127 |
l_motor_bwd(i); |
- |
|
128 |
printf("RYCHLOST: %u\n",i); |
- |
|
129 |
delay_ms(10); |
- |
|
130 |
} |
- |
|
131 |
for(i=0;i<255;i++) |
- |
|
132 |
{ |
- |
|
133 |
r_motor_fwd(i); |
- |
|
134 |
delay_ms(10); |
- |
|
135 |
} |
- |
|
136 |
|
- |
|
137 |
for(i=255;i>0;i--) |
- |
|
138 |
{ |
- |
|
139 |
r_motor_fwd(i); |
- |
|
140 |
printf("RYCHLOST: %u\n",i); |
- |
|
141 |
delay_ms(10); |
- |
|
142 |
} |
- |
|
143 |
for(i=0;i<255;i++) |
- |
|
144 |
{ |
- |
|
145 |
r_motor_bwd(i); |
- |
|
146 |
printf("RYCHLOST: %u\n",i); |
- |
|
147 |
delay_ms(10); |
- |
|
148 |
} |
- |
|
149 |
|
- |
|
150 |
for(i=255;i>0;i--) |
- |
|
151 |
{ |
- |
|
152 |
r_motor_bwd(i); |
- |
|
153 |
printf("RYCHLOST: %u\n",i); |
- |
|
154 |
delay_ms(10); |
- |
|
155 |
} |
- |
|
156 |
l_motor_off(); |
- |
|
157 |
r_motor_off(); |
- |
|
158 |
} |
- |
|
159 |
|
- |
|
160 |
void diagnostika() |
- |
|
161 |
{ |
- |
|
162 |
read_blue_sensors(); |
- |
|
163 |
printf("LEVA: %u \t",line_l); |
- |
|
164 |
delay_ms(20); |
- |
|
165 |
printf("PRAVA: %u \t",line_r); |
- |
|
166 |
delay_ms(20); |
- |
|
167 |
printf("L_NARAZ: %u \t",BUMPL); |
- |
|
168 |
delay_ms(20); |
- |
|
169 |
printf("P_NARAZ: %u \n",BUMPR); |
- |
|
170 |
delay_ms(20); |
- |
|
171 |
if(BUMPL&&BUMPR) |
- |
|
172 |
{ |
- |
|
173 |
motor_test(); |
47 |
} |
174 |
} |
48 |
} |
175 |
} |
49 |
|
176 |
|
50 |
// HLAVNI SMYCKA |
177 |
// HLAVNI SMYCKA |
51 |
void main() |
178 |
void main() |
52 |
{ |
179 |
{ |
- |
|
180 |
printf("POWER ON \n"); |
53 |
// NASTAVENI > provede se pouze pri zapnuti |
181 |
// NASTAVENI > provede se pouze pri zapnuti |
54 |
setup_adc_ports(ALL_ANALOG); // vsechny ADC porty > analogove vstupy |
182 |
setup_adc_ports(sAN0-sAN1-sAN2); |
55 |
setup_adc(ADC_CLOCK_INTERNAL); |
183 |
setup_adc(ADC_CLOCK_INTERNAL); |
56 |
setup_spi(SPI_SS_DISABLED); |
184 |
setup_spi(SPI_SS_DISABLED); |
57 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
185 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
58 |
setup_timer_1(T1_DISABLED); |
186 |
setup_timer_1(T1_DISABLED); |
59 |
setup_timer_2(T2_DIV_BY_16,255,1); //casovac pro PWM |
187 |
setup_timer_2(T2_DIV_BY_16,255,1); //casovac pro PWM |
60 |
setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2 |
188 |
setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2 |
61 |
setup_ccp2(CCP_PWM); // povolĂ PWM na pinu RC1 |
189 |
setup_ccp2(CCP_PWM); // povolĂ PWM na pinu RC1 |
62 |
setup_comparator(NC_NC_NC_NC); |
190 |
setup_comparator(NC_NC_NC_NC); |
- |
|
191 |
setup_vref(FALSE); |
63 |
output_high(LED1); // zhasne LED1 |
192 |
output_high(LED1); // zhasne LED1 |
64 |
output_high(LED2); // zhasne LED2 |
193 |
output_high(LED2); // zhasne LED2 |
- |
|
194 |
l_motor_off(); // vypne oba motory |
- |
|
195 |
r_motor_off(); // vypne oba motory |
65 |
|
196 |
printf("OK! \n"); |
- |
|
197 |
printf("VYBRAT MOD... \n"); |
66 |
while(true) |
198 |
while(true) |
67 |
{ |
199 |
{ |
- |
|
200 |
bl++; // primitivni blikani - oznacuje vypber modu |
68 |
if(BUMPL==1) |
201 |
if(bl>4096) |
69 |
{ |
202 |
{ |
70 |
output_low(LED1); |
203 |
output_low(LED1); |
- |
|
204 |
output_high(LED2); |
71 |
} |
205 |
} |
72 |
if(BUMPR==1) |
206 |
else |
73 |
{ |
207 |
{ |
- |
|
208 |
output_high(LED1); |
74 |
output_low(LED2); |
209 |
output_low(LED2); |
- |
|
210 |
} |
- |
|
211 |
if(bl>8192) |
- |
|
212 |
{ |
- |
|
213 |
bl=0; |
- |
|
214 |
} |
- |
|
215 |
if(BUMPL) |
- |
|
216 |
{ |
- |
|
217 |
printf("MOD: DIAGNOSTIKA\n"); |
- |
|
218 |
output_low(LED1); |
- |
|
219 |
output_high(LED2); |
- |
|
220 |
delay_ms(1000); |
- |
|
221 |
while(true) |
- |
|
222 |
{ |
- |
|
223 |
diagnostika(); |
75 |
} |
224 |
} |
76 |
} |
225 |
} |
- |
|
226 |
if(BUMPR) |
- |
|
227 |
{ |
- |
|
228 |
printf("MOD: STOPOVANI\n"); |
- |
|
229 |
output_low(LED2); |
- |
|
230 |
output_high(LED1); |
- |
|
231 |
delay_ms(1000); |
- |
|
232 |
while(true) |
- |
|
233 |
{ |
77 |
|
234 |
|
- |
|
235 |
} |
- |
|
236 |
} |
- |
|
237 |
} |
78 |
} |
238 |
} |