3463 |
kaklik |
1 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
3458 |
kaklik |
2 |
#include "main.h" |
3463 |
kaklik |
3 |
#use i2c(SLAVE, Fast, sda=PIN_C4, scl=PIN_C3, force_hw, address=0xA2) |
3458 |
kaklik |
4 |
|
3463 |
kaklik |
5 |
//#include <string.h> |
3458 |
kaklik |
6 |
|
|
|
7 |
#define LED PIN_E1 |
|
|
8 |
#define CE PIN_E2 |
|
|
9 |
|
3462 |
kaklik |
10 |
#define SEL0 PIN_E0 // external counter division ratio |
|
|
11 |
#define SEL1 PIN_E1 // external counter division ratio |
|
|
12 |
#define MR PIN_E2 // external counter master reset |
|
|
13 |
#define CLKI PIN_C0 // internal counter input |
|
|
14 |
#define BEEP PIN_C3 // buzzer |
3458 |
kaklik |
15 |
|
3462 |
kaklik |
16 |
unsigned int32 count; |
3458 |
kaklik |
17 |
|
3463 |
kaklik |
18 |
const int8 buf_len=8; |
|
|
19 |
int8 buffer[buf_len]; // I2C buffer |
|
|
20 |
int8 address=0; |
3462 |
kaklik |
21 |
|
|
|
22 |
unsigned int16 of=0; // count of overflow |
|
|
23 |
|
3458 |
kaklik |
24 |
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}; |
|
|
25 |
|
3463 |
kaklik |
26 |
|
3458 |
kaklik |
27 |
#INT_SSP |
|
|
28 |
void ssp_interupt () |
|
|
29 |
{ |
3463 |
kaklik |
30 |
BYTE incoming, state; |
3458 |
kaklik |
31 |
|
|
|
32 |
state = i2c_isr_state(); |
|
|
33 |
if(state < 0x80) //Master is sending data |
|
|
34 |
{ |
|
|
35 |
incoming = i2c_read(); |
|
|
36 |
if(state == 1) //First received byte is address |
|
|
37 |
{ |
|
|
38 |
address = incoming; |
|
|
39 |
if (incoming == 2) |
|
|
40 |
{ |
3463 |
kaklik |
41 |
/* buffer[0]=make8(anemo,0); |
|
|
42 |
buffer[1]=make8(anemo,1); |
|
|
43 |
buffer[2]=make8(rain,0); |
|
|
44 |
buffer[3]=make8(rain,1); |
|
|
45 |
*/ } |
3458 |
kaklik |
46 |
} |
|
|
47 |
if(state == 2) //Second received byte is data |
|
|
48 |
buffer[address] = incoming; |
|
|
49 |
} |
|
|
50 |
if(state == 0x80) //Master is requesting data |
|
|
51 |
{ |
3463 |
kaklik |
52 |
if(address <= buf_len) i2c_write(buffer[address]); |
|
|
53 |
else i2c_write(ID[address - buf_len]); |
3458 |
kaklik |
54 |
} |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
|
3463 |
kaklik |
58 |
|
|
|
59 |
/*#int_EXT // Interrupt from 1PPS |
3462 |
kaklik |
60 |
void EXT_isr(void) |
|
|
61 |
{ |
|
|
62 |
unsigned int16 countH; |
|
|
63 |
unsigned int8 countL; |
|
|
64 |
char countS[10], a[4], b[4], c[4]; // strings for printing results |
|
|
65 |
|
|
|
66 |
countL=0; |
|
|
67 |
countH=get_timer1(); // read internal counter |
|
|
68 |
output_low(SEL0); |
|
|
69 |
output_low(SEL1); |
|
|
70 |
countL=input(CLKI); // read bit 0 of external counter |
|
|
71 |
output_high(SEL0); |
|
|
72 |
output_low(SEL1); |
|
|
73 |
countL|=input(CLKI)<<1; // read bit 1 of external counter |
|
|
74 |
output_low(SEL0); |
|
|
75 |
output_high(SEL1); |
|
|
76 |
countL|=input(CLKI)<<2; // read bit 2 of external counter |
|
|
77 |
output_high(SEL0); |
|
|
78 |
output_high(SEL1); |
|
|
79 |
countL|=input(CLKI)<<3; // read bit 3 of external counter |
|
|
80 |
|
|
|
81 |
output_low(MR); // External counter Master Reset |
|
|
82 |
output_high(MR); |
|
|
83 |
|
|
|
84 |
set_timer1(0); // Internal counter reset |
|
|
85 |
|
|
|
86 |
count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate |
|
|
87 |
|
|
|
88 |
sprintf(countS,"%09Lu", count); // engeneering values conversion |
|
|
89 |
strncpy(a, countS, 3); a[3]='\0'; |
|
|
90 |
strncpy(b, &countS[3], 3); b[3]='\0'; |
|
|
91 |
strncpy(c, &countS[6], 3); c[3]='\0'; |
|
|
92 |
|
|
|
93 |
printf("%s\r\n", countS); // output to RS232 |
|
|
94 |
|
|
|
95 |
output_toggle(BEEP); // cvak... |
|
|
96 |
|
|
|
97 |
of=0; // reset overflow counter |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
#int_TIMER1 // Interrupf from overflow |
|
|
101 |
void TIMER1_isr(void) |
|
|
102 |
{ |
|
|
103 |
of++; |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
/*#int_TIMER2 // every 10 ms |
3458 |
kaklik |
108 |
void TIMER2_isr(void) |
|
|
109 |
{ |
|
|
110 |
output_low(CE); |
|
|
111 |
count=get_timer1(); |
|
|
112 |
set_timer1(0); |
|
|
113 |
output_high(CE); |
3462 |
kaklik |
114 |
}*/ |
3458 |
kaklik |
115 |
|
|
|
116 |
|
|
|
117 |
void main() |
|
|
118 |
{ |
3463 |
kaklik |
119 |
/* setup_adc_ports(NO_ANALOGS|VSS_VDD); |
3458 |
kaklik |
120 |
setup_adc(ADC_OFF); |
3463 |
kaklik |
121 |
// setup_spi(SPI_SS_DISABLED); |
3458 |
kaklik |
122 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
123 |
// setup_wdt(WDT_144MS); |
|
|
124 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
|
|
125 |
setup_timer_2(T2_DIV_BY_16,196,16); |
|
|
126 |
setup_ccp1(CCP_OFF); |
|
|
127 |
setup_comparator(NC_NC_NC_NC); |
|
|
128 |
setup_vref(FALSE); |
3463 |
kaklik |
129 |
|
|
|
130 |
*/ |
|
|
131 |
|
|
|
132 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
133 |
// setup_adc(ADC_CLOCK_DIV_2); |
|
|
134 |
setup_adc(ADC_OFF); |
|
|
135 |
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use! |
|
|
136 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
|
|
137 |
// setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS); |
|
|
138 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
|
|
139 |
// setup_timer_2(T2_DISABLED,0,1); |
|
|
140 |
setup_comparator(NC_NC_NC_NC); |
|
|
141 |
setup_vref(FALSE); |
|
|
142 |
// setup_oscillator(OSC_8MHZ|OSC_INTRC); |
|
|
143 |
|
|
|
144 |
// ext_int_edge( L_TO_H ); // set 1PPS active edge |
|
|
145 |
// enable_interrupts(INT_TIMER1); |
|
|
146 |
// enable_interrupts(INT_EXT); |
|
|
147 |
enable_interrupts(INT_SSP); |
|
|
148 |
// enable_interrupts(INT_TIMER2); |
|
|
149 |
enable_interrupts(GLOBAL); |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
/* delay_ms(1000); |
3458 |
kaklik |
153 |
int n; |
3462 |
kaklik |
154 |
for (n=0;n<40;n++) putc(cmd[n]); // setup GPS |
3458 |
kaklik |
155 |
|
3463 |
kaklik |
156 |
*/ |
|
|
157 |
|
3458 |
kaklik |
158 |
printf("cvak...\r\n"); |
|
|
159 |
|
|
|
160 |
|
3463 |
kaklik |
161 |
buffer[2]=0; |
|
|
162 |
buffer[3]=0; |
|
|
163 |
buffer[4]=0; |
|
|
164 |
buffer[5]=0; |
|
|
165 |
|
3458 |
kaklik |
166 |
while(true) |
3462 |
kaklik |
167 |
{ |
3463 |
kaklik |
168 |
// output_high(LED); |
|
|
169 |
delay_ms(1000); |
|
|
170 |
// output_low(LED); |
|
|
171 |
// delay_ms(999); |
3462 |
kaklik |
172 |
printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
3463 |
kaklik |
173 |
|
3458 |
kaklik |
174 |
} |
|
|
175 |
} |