3458 |
kaklik |
1 |
#include "main.h" |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
#define LED PIN_E1 |
|
|
5 |
#define CE PIN_E2 |
|
|
6 |
|
|
|
7 |
int16 count; |
|
|
8 |
|
|
|
9 |
int8 rcv_buf[0x10]; // I2C receive buffer |
|
|
10 |
int8 snd_buf[0x10]; // I2C send buffer |
|
|
11 |
|
|
|
12 |
int8 buffer[0x10]; // I2C buffer |
|
|
13 |
int8 address; |
|
|
14 |
|
|
|
15 |
const char cmd[40]={0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x80, 0x84, 0x1E, 0x00, 0xE0, 0xC8, 0x10, 0x00, 0x40, 0x42, 0x0F, 0x00, 0xA0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x00, 0x00, 0x00, 0x12, 0x03}; |
|
|
16 |
|
|
|
17 |
#INT_SSP |
|
|
18 |
void ssp_interupt () |
|
|
19 |
{ |
|
|
20 |
BYTE incoming, state; |
|
|
21 |
|
|
|
22 |
state = i2c_isr_state(); |
|
|
23 |
|
|
|
24 |
if(state < 0x80) //Master is sending data |
|
|
25 |
{ |
|
|
26 |
incoming = i2c_read(); |
|
|
27 |
if(state == 1) //First received byte is address |
|
|
28 |
{ |
|
|
29 |
address = incoming; |
|
|
30 |
if (incoming == 2) |
|
|
31 |
{ |
|
|
32 |
buffer[0]=make8(count,0); |
|
|
33 |
buffer[1]=make8(count,1); |
|
|
34 |
} |
|
|
35 |
} |
|
|
36 |
if(state == 2) //Second received byte is data |
|
|
37 |
buffer[address] = incoming; |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
} |
|
|
41 |
if(state == 0x80) //Master is requesting data |
|
|
42 |
{ |
|
|
43 |
i2c_write(buffer[address]); |
|
|
44 |
} |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
#int_TIMER2 // every 10 ms |
|
|
49 |
void TIMER2_isr(void) |
|
|
50 |
{ |
|
|
51 |
output_low(CE); |
|
|
52 |
count=get_timer1(); |
|
|
53 |
set_timer1(0); |
|
|
54 |
output_high(CE); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
void main() |
|
|
59 |
{ |
|
|
60 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
61 |
setup_adc(ADC_OFF); |
|
|
62 |
// setup_spi(SPI_SS_DISABLED); |
|
|
63 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
64 |
// setup_wdt(WDT_144MS); |
|
|
65 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
|
|
66 |
setup_timer_2(T2_DIV_BY_16,196,16); |
|
|
67 |
setup_ccp1(CCP_OFF); |
|
|
68 |
setup_comparator(NC_NC_NC_NC); |
|
|
69 |
setup_vref(FALSE); |
|
|
70 |
|
|
|
71 |
delay_ms(1000); |
|
|
72 |
int n; |
|
|
73 |
for (n=0;n<40;n++) |
|
|
74 |
{ |
|
|
75 |
putc(cmd[n]); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
printf("cvak...\r\n"); |
|
|
79 |
// snd_buf[2]=0x55; |
|
|
80 |
// snd_buf[3]=0xAA; |
|
|
81 |
|
|
|
82 |
set_timer1(0); |
|
|
83 |
enable_interrupts(INT_SSP); |
|
|
84 |
// enable_interrupts(INT_TIMER2); |
|
|
85 |
enable_interrupts(GLOBAL); |
|
|
86 |
|
|
|
87 |
while(true) |
|
|
88 |
{ |
|
|
89 |
|
|
|
90 |
output_high(LED); |
|
|
91 |
output_low(LED); |
|
|
92 |
set_timer1(0); |
|
|
93 |
output_high(CE); |
|
|
94 |
delay_ms(999); |
|
|
95 |
delay_us(966); |
|
|
96 |
output_low(CE); |
|
|
97 |
count=get_timer1(); |
|
|
98 |
|
|
|
99 |
printf("count: %Lu %X %X %X %X\r\n",count, buffer[0],buffer[1],buffer[2],buffer[3]); |
|
|
100 |
} |
|
|
101 |
} |