Rev 3462 Rev 3463
-   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
9 #define SEL1 PIN_E1 // external counter division ratio 11 #define SEL1 PIN_E1 // external counter division ratio
10 #define MR PIN_E2 // external counter master reset 12 #define MR PIN_E2 // external counter master reset
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
63 65
64 countL=0; 66 countL=0;
65 countH=get_timer1(); // read internal counter 67 countH=get_timer1(); // read internal counter
66 output_low(SEL0); 68 output_low(SEL0);
67 output_low(SEL1); 69 output_low(SEL1);
68 countL=input(CLKI); // read bit 0 of external counter 70 countL=input(CLKI); // read bit 0 of external counter
69 output_high(SEL0); 71 output_high(SEL0);
70 output_low(SEL1); 72 output_low(SEL1);
71 countL|=input(CLKI)<<1; // read bit 1 of external counter 73 countL|=input(CLKI)<<1; // read bit 1 of external counter
72 output_low(SEL0); 74 output_low(SEL0);
73 output_high(SEL1); 75 output_high(SEL1);
74 countL|=input(CLKI)<<2; // read bit 2 of external counter 76 countL|=input(CLKI)<<2; // read bit 2 of external counter
75 output_high(SEL0); 77 output_high(SEL0);
76 output_high(SEL1); 78 output_high(SEL1);
77 countL|=input(CLKI)<<3; // read bit 3 of external counter 79 countL|=input(CLKI)<<3; // read bit 3 of external counter
78   80  
79 output_low(MR); // External counter Master Reset 81 output_low(MR); // External counter Master Reset
80 output_high(MR); 82 output_high(MR);
81 83
82 set_timer1(0); // Internal counter reset 84 set_timer1(0); // Internal counter reset
83 85
84 count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate 86 count=((unsigned int32)of<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate
85   87  
86 sprintf(countS,"%09Lu", count); // engeneering values conversion 88 sprintf(countS,"%09Lu", count); // engeneering values conversion
87 strncpy(a, countS, 3); a[3]='\0'; 89 strncpy(a, countS, 3); a[3]='\0';
88 strncpy(b, &countS[3], 3); b[3]='\0'; 90 strncpy(b, &countS[3], 3); b[3]='\0';
89 strncpy(c, &countS[6], 3); c[3]='\0'; 91 strncpy(c, &countS[6], 3); c[3]='\0';
90 92
91 printf("%s\r\n", countS); // output to RS232 93 printf("%s\r\n", countS); // output to RS232
92   94  
93 output_toggle(BEEP); // cvak... 95 output_toggle(BEEP); // cvak...
94   96  
95 of=0; // reset overflow counter 97 of=0; // reset overflow counter
96 } 98 }
97   99  
98 #int_TIMER1 // Interrupf from overflow 100 #int_TIMER1 // Interrupf from overflow
99 void TIMER1_isr(void) 101 void TIMER1_isr(void)
100 { 102 {
101 of++; 103 of++;
102 } 104 }
103   105  
104   106  
105 /*#int_TIMER2 // every 10 ms 107 /*#int_TIMER2 // every 10 ms
106 void TIMER2_isr(void) 108 void TIMER2_isr(void)
107 { 109 {
108 output_low(CE); 110 output_low(CE);
109 count=get_timer1(); 111 count=get_timer1();
110 set_timer1(0); 112 set_timer1(0);
111 output_high(CE); 113 output_high(CE);
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 }