1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
#use i2c(Slave,Fast,sda=PIN_B1,scl=PIN_B4,force_hw,address=0xA2) // Motor 2 |
2 |
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2 |
3 |
|
3 |
|
4 |
#define H1 PIN_A1 |
4 |
#define H1 PIN_A1 |
5 |
#define L1 PIN_A2 |
5 |
#define L1 PIN_A2 |
6 |
#define H2 PIN_A3 |
6 |
#define H2 PIN_A3 |
7 |
#define L2 PIN_A4 |
7 |
#define L2 PIN_A4 |
8 |
|
8 |
|
9 |
signed int8 command; // rozsah +-127 |
9 |
signed int8 command; // rozsah +-127 |
10 |
|
10 |
|
11 |
#INT_SSP |
11 |
#INT_SSP |
12 |
void ssp_interupt () |
12 |
void ssp_interupt () |
13 |
{ |
13 |
{ |
14 |
BYTE incoming, state; |
14 |
BYTE incoming, state; |
15 |
|
15 |
|
16 |
output_a(0); // vypnuti vsech budicu |
16 |
output_a(0); // vypnuti vsech budicu |
17 |
|
17 |
|
18 |
state = i2c_isr_state(); |
18 |
state = i2c_isr_state(); |
19 |
|
19 |
|
20 |
if(state < 0x80) //Master is sending data |
20 |
if(state < 0x80) //Master is sending data |
21 |
{ |
21 |
{ |
22 |
command = i2c_read(); |
22 |
command = i2c_read(); |
23 |
} |
23 |
} |
24 |
|
24 |
|
25 |
if(state == 0x80) //Master is requesting data |
25 |
if(state == 0x80) //Master is requesting data |
26 |
{ |
26 |
{ |
27 |
i2c_write(command); |
27 |
i2c_write(command); |
28 |
} |
28 |
} |
29 |
} |
29 |
} |
30 |
|
30 |
|
31 |
|
31 |
|
32 |
void main() |
32 |
void main() |
33 |
{ |
33 |
{ |
34 |
int8 speed; |
34 |
int8 speed; |
35 |
|
35 |
|
36 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
36 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
37 |
setup_adc(ADC_OFF); |
37 |
setup_adc(ADC_OFF); |
38 |
setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS); |
38 |
setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS); |
39 |
setup_timer_1(T1_DISABLED); |
39 |
setup_timer_1(T1_DISABLED); |
40 |
setup_timer_2(T2_DISABLED,0,1); |
40 |
setup_timer_2(T2_DISABLED,0,1); |
41 |
setup_comparator(NC_NC_NC_NC); |
41 |
setup_comparator(NC_NC_NC_NC); |
42 |
setup_vref(FALSE); |
42 |
setup_vref(FALSE); |
43 |
setup_oscillator(OSC_8MHZ|OSC_INTRC); |
43 |
setup_oscillator(OSC_8MHZ|OSC_INTRC); |
44 |
|
44 |
|
45 |
enable_interrupts(GLOBAL); |
45 |
enable_interrupts(GLOBAL); |
46 |
enable_interrupts(INT_SSP); |
46 |
enable_interrupts(INT_SSP); |
47 |
|
47 |
|
48 |
command=-128; // zastaveni po resetu |
48 |
command=-128; // zastaveni po resetu |
49 |
|
49 |
|
50 |
while(true) |
50 |
while(true) |
51 |
{ |
51 |
{ |
52 |
|
52 |
|
53 |
if (command==-128) // prikaz na odpojeni mustku nebo chybna hodnota |
53 |
if (command==-128) // prikaz na odpojeni mustku nebo chybna hodnota |
54 |
{ |
54 |
{ |
55 |
output_a(0); // volnobeh |
55 |
output_a(0); // volnobeh |
56 |
continue; |
56 |
continue; |
57 |
}; |
57 |
}; |
58 |
|
58 |
|
59 |
speed=command+127; // posunuti 0 pro zaporna cisla |
59 |
speed=command+127; // posunuti 0 pro zaporna cisla |
60 |
|
60 |
|
61 |
output_a(0b10010); // vpred |
61 |
output_a(0b10010); // vpred |
62 |
delay_us(speed); |
62 |
delay_us(speed); |
63 |
output_a(0); // vypnuti vsech budicu |
63 |
output_a(0); // vypnuti vsech budicu |
64 |
delay_us(1); |
64 |
delay_us(1); |
65 |
restart_wdt(); |
65 |
restart_wdt(); |
66 |
output_a(0b01100); // vzad |
66 |
output_a(0b01100); // vzad |
67 |
delay_us(254-speed); |
67 |
delay_us(254-speed); |
68 |
output_a(0); // vypnuti vsech budicu |
68 |
output_a(0); // vypnuti vsech budicu |
69 |
delay_us(1); |
69 |
delay_us(1); |
70 |
} |
70 |
} |
71 |
} |
71 |
} |
72 |
|
72 |
|
73 |
|
73 |
|
74 |
|
74 |
|