Rev 3462 Rev 3463
Line -... Line 1...
-   1 #define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
1 #include "main.h" 2 #include "main.h"
-   3 #use i2c(SLAVE, Fast, sda=PIN_C4, scl=PIN_C3, force_hw, address=0xA2)
2   4  
3 #include <string.h> 5 //#include <string.h>
4   6  
5 #define LED PIN_E1 7 #define LED PIN_E1
6 #define CE PIN_E2 8 #define CE PIN_E2
7   9  
8 #define SEL0 PIN_E0 // external counter division ratio 10 #define SEL0 PIN_E0 // external counter division ratio
Line 11... Line 13...
11 #define CLKI PIN_C0 // internal counter input 13 #define CLKI PIN_C0 // internal counter input
12 #define BEEP PIN_C3 // buzzer 14 #define BEEP PIN_C3 // buzzer
13   15  
14 unsigned int32 count; 16 unsigned int32 count;
15   17  
16   -  
-   18 const int8 buf_len=8;
17 int8 buffer[0x10]; // I2C buffer 19 int8 buffer[buf_len]; // I2C buffer
18 int8 address; 20 int8 address=0;
19   21  
20 unsigned int16 of=0; // count of overflow 22 unsigned int16 of=0; // count of overflow
21   23  
22 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}; 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};
23   25  
-   26  
24 #INT_SSP 27 #INT_SSP
25 void ssp_interupt () 28 void ssp_interupt ()
26 { 29 {
27 BYTE incoming, state; 30 BYTE incoming, state;
28   31  
29 state = i2c_isr_state(); 32 state = i2c_isr_state();
30 -  
31 if(state < 0x80) //Master is sending data 33 if(state < 0x80) //Master is sending data
32 { 34 {
33 incoming = i2c_read(); 35 incoming = i2c_read();
34 if(state == 1) //First received byte is address 36 if(state == 1) //First received byte is address
35 { 37 {
36 address = incoming; 38 address = incoming;
37 if (incoming == 2) 39 if (incoming == 2)
38 { 40 {
39 buffer[0]=make8(count,0); 41 /* buffer[0]=make8(anemo,0);
40 buffer[1]=make8(count,1); 42 buffer[1]=make8(anemo,1);
41 buffer[2]=make8(count,2); 43 buffer[2]=make8(rain,0);
42 buffer[3]=make8(count,3); 44 buffer[3]=make8(rain,1);
43 } 45 */ }
44 } 46 }
45 if(state == 2) //Second received byte is data 47 if(state == 2) //Second received byte is data
46 buffer[address] = incoming; 48 buffer[address] = incoming;
47 -  
48 -  
49 } 49 }
50 if(state == 0x80) //Master is requesting data 50 if(state == 0x80) //Master is requesting data
51 { 51 {
52 i2c_write(buffer[address]); 52 if(address <= buf_len) i2c_write(buffer[address]);
-   53 else i2c_write(ID[address - buf_len]);
53 } 54 }
54 } 55 }
55   56  
56   57  
-   58  
57 #int_EXT // Interrupt from 1PPS 59 /*#int_EXT // Interrupt from 1PPS
58 void EXT_isr(void) 60 void EXT_isr(void)
59 { 61 {
60 unsigned int16 countH; 62 unsigned int16 countH;
61 unsigned int8 countL; 63 unsigned int8 countL;
62 char countS[10], a[4], b[4], c[4]; // strings for printing results 64 char countS[10], a[4], b[4], c[4]; // strings for printing results
Line 112... Line 114...
112 }*/ 114 }*/
113   115  
114 116
115 void main() 117 void main()
116 { 118 {
117 setup_adc_ports(NO_ANALOGS|VSS_VDD); 119 /* setup_adc_ports(NO_ANALOGS|VSS_VDD);
118 setup_adc(ADC_OFF); 120 setup_adc(ADC_OFF);
119 setup_spi(SPI_SS_DISABLED); 121 // setup_spi(SPI_SS_DISABLED);
120 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 122 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
121 // setup_wdt(WDT_144MS); 123 // setup_wdt(WDT_144MS);
122 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); 124 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
123 setup_timer_2(T2_DIV_BY_16,196,16); 125 setup_timer_2(T2_DIV_BY_16,196,16);
124 setup_ccp1(CCP_OFF); 126 setup_ccp1(CCP_OFF);
125 setup_comparator(NC_NC_NC_NC); 127 setup_comparator(NC_NC_NC_NC);
126 setup_vref(FALSE); 128 setup_vref(FALSE);
-   129  
127 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  
128 delay_ms(1000); 152 /* delay_ms(1000);
129 int n; 153 int n;
130 for (n=0;n<40;n++) putc(cmd[n]); // setup GPS 154 for (n=0;n<40;n++) putc(cmd[n]); // setup GPS
131   155  
-   156 */
-   157  
132 printf("cvak...\r\n"); 158 printf("cvak...\r\n");
133 159
134 ext_int_edge( L_TO_H ); // set 1PPS active edge -  
-   160  
135 enable_interrupts(INT_TIMER1); 161 buffer[2]=0;
136 enable_interrupts(INT_EXT); 162 buffer[3]=0;
137 enable_interrupts(INT_SSP); 163 buffer[4]=0;
138 // enable_interrupts(INT_TIMER2); -  
139 enable_interrupts(GLOBAL); 164 buffer[5]=0;
140   165  
141 while(true) 166 while(true)
142 { 167 {
143 /* output_high(LED); 168 // output_high(LED);
144 delay_ms(999); 169 delay_ms(1000);
145 output_low(LED); 170 // output_low(LED);
146 delay_ms(999); 171 // delay_ms(999);
147 printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); 172 printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
148 */ 173  
149 } 174 }
150 } 175 }