0,0 → 1,96 |
#include "laser.h" |
#include "16F88_Reg.H" |
|
#define SERVO PIN_B5 // Vystup pro rizeni serva |
#define LASER PIN_B4 // Vstup pro cteni laseru |
|
// kroutitka |
#define CERVENA 0 // AN0 |
#define CERNA 1 // AN1 |
//#define ZELENA 3 // AN3 |
//#define MODRA 4 // AN4 |
|
//#define OFFSET 100 // Predni kolecko vprostred pri care vprostred |
//#define HYSTERESE 3 // Rozdil mezi behem tam a zpet |
|
boolean edge; |
int8 uhel; |
|
#int_EXT |
EXT_isr() |
{ |
int n, t1, t2, t; |
|
set_timer0(0); // Vynulovani casovace pro 2ms |
output_high(SERVO); |
while(get_timer0()<(1000/256)); // Ceka 1ms |
for(n=uhel; n>0; n--) delay_us(3); // Sirka impulzu podle uhlu |
output_low(SERVO); |
while(get_timer0()<(3000/256)); // Ceka do 3ms, nez zacne scanovat |
|
set_timer0(0); // Vynulovani casovace pro zjisteni polohy cary |
if(edge) // Zrcatko prejizdelo tam nebo zpet? |
{ |
edge=false; |
ext_int_edge(H_TO_L); // Pristi inerrupt bude od opacne hrany |
INT0IF_1=0; // Povoleni dalsiho preruseni |
enable_interrupts(INT_EXT); |
enable_interrupts(GLOBAL); |
// do |
{ |
while(0==input(LASER)); // Ceka na detekci cary |
// output_high(SERVO); |
t1=get_timer0(); // Poznamena cas nabezne hrany |
while(1==input(LASER)); // Ceka az zkonci cara |
// output_low(SERVO); |
t2=get_timer0(); // Poznamena cas sestupne hrany |
t=t2-t1; |
} //while((t<5) || (t>7)); // Cara je detekovana, kdyz trva mezi xx ms |
if ((t>3) && (t<7)) uhel=(160-(read_adc()>>2))-t1; |
set_adc_channel(1); |
} |
else |
{ |
edge=true; |
ext_int_edge(L_TO_H); |
INT0IF_1=0; // Povoleni dalsiho preruseni |
enable_interrupts(INT_EXT); |
enable_interrupts(GLOBAL); |
// do |
{ |
while(0==input(LASER)); // Ceka na detekci cary |
t1=get_timer0(); // Poznamena cas nabezne hrany |
while(1==input(LASER)); // Ceka az zkonci cara |
t2=get_timer0(); // Poznamena cas sestupne hrany |
t=t2-t1; |
} //while((t<5) || (t>7)); // Cara je detekovana, kdyz trva mezi xx ms |
if ((t>3) && (t<7)) uhel=((read_adc()>>2)+32)+t1; |
set_adc_channel(0); |
} |
while(true); |
} |
|
|
|
void main() |
{ |
setup_adc_ports(sAN0|sAN1|VSS_VDD); // AD pro kroutitka |
setup_adc(ADC_CLOCK_INTERNAL); |
setup_spi(FALSE); |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); |
setup_timer_1(T1_DISABLED); |
setup_timer_2(T2_DISABLED,0,1); |
setup_comparator(NC_NC_NC_NC); |
setup_vref(FALSE); |
setup_oscillator(False); |
|
delay_ms(100); |
set_adc_channel(0); |
Delay_ms(1); |
|
ext_int_edge(L_TO_H); |
edge=true; |
uhel=((read_adc()>>2)+32)+30;; |
enable_interrupts(INT_EXT); |
enable_interrupts(GLOBAL); |
} |