Rev 3734 Rev 3735
1 // Atomic counter with I2C and RS232 output 1 // Atomic counter with I2C and RS232 output
2   2  
3 // Usage conditions: 3 // Usage conditions:
4 // 1. The first I2C or RS232 readout can be performed minimally 20 s after power up. 4 // 1. The first I2C or RS232 readout can be performed minimally 20 s after power up.
5 // 2. The I2C internal address 0 has to be read first. 5 // 2. The I2C internal address 0 has to be read first.
6 // 3. An I2C readout can be performed at 15-th, 35-th and 55-th second of UTC. 6 // 3. An I2C readout can be performed at 15-th, 35-th and 55-th second of UTC.
7 // 7 //
8 // Counter gives 32 bit value: 8 // Counter gives 32 bit value:
9 // I2C register address 0 = LSB 9 // I2C register address 0 = LSB
10 // I2C register address 3 = MSB 10 // I2C register address 3 = MSB
11   11  
12 #define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" 12 #define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
13 #include "main.h" 13 #include "main.h"
14 #use i2c(SLAVE, Fast, sda=PIN_C4, scl=PIN_C3, force_hw, address=0xA2) 14 #use i2c(SLAVE, Fast, sda=PIN_C4, scl=PIN_C3, force_hw, address=0xA2)
15   15  
16 #include <string.h> 16 #include <string.h>
17   17  
18 #define SEL0 PIN_E0 // external counter division ratio 18 #define SEL0 PIN_E0 // external counter division ratio
19 #define SEL1 PIN_E1 // external counter division ratio 19 #define SEL1 PIN_E1 // external counter division ratio
20 #define MR PIN_E2 // external counter master reset 20 #define MR PIN_E2 // external counter master reset
21 #define CLKI PIN_C0 // internal counter input 21 #define CLKI PIN_C0 // internal counter input
22   22  
23 unsigned int32 count; // count per second 23 unsigned int32 count; // count per second
24   24  
-   25 int1 fire_setup; // flag for sending setup to GPS
-   26  
25 #define BUF_LEN 4 27 #define BUF_LEN 4
26 int8 buffer[BUF_LEN]; // I2C buffer 28 int8 buffer[BUF_LEN]; // I2C buffer
27 int8 address=0; 29 int8 address=0;
28   30  
29 unsigned int16 of=0; // count of overflow 31 unsigned int16 of=0; // count of overflow
-   32  
-   33 // 1x 100 us per 10 s UTC synchronised; 40 configuration bytes
30 34 char cmd[50]={40, 0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x80, 0x96, 0x98, 0x00, 0xE0, 0xC8, 0x10, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0xC6, 0x51};
-   35  
31 // configure GPS 36 // configure GPS
32 void setup_GPS() 37 void setup_GPS()
33 { 38 {
34 // 1x 100 us per 10 s UTC synchronised -  
35 const char cmd[40]={0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x80, 0x96, 0x98, 0x00, 0xE0, 0xC8, 0x10, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0xC6, 0x51}; -  
36 int n; 39 int n;
-   40 int len;
-   41 len=cmd[0];
37 for (n=0;n<40;n++) putc(cmd[n]); 42 for (n=1;n<=len;n++) putc(cmd[n]);
38 } 43 }
39   44  
40 #INT_SSP 45 #INT_SSP
41 void ssp_interupt () 46 void ssp_interupt ()
42 { 47 {
43 int8 incoming, state; 48 int8 incoming, state;
44   49  
45 state = i2c_isr_state(); 50 state = i2c_isr_state();
46   51  
47 if(state < 0x80) //Master is sending data 52 if(state < 0x80) //Master is sending data
48 { 53 {
49 incoming = i2c_read(); // Read byte 54 incoming = i2c_read(); // Read byte
50   55  
51 if(state == 1) //Second received byte is address of register 56 if(state == 1) //Second received byte is address of register
52 { 57 {
53 address = incoming; 58 address = incoming;
54 } 59 }
-   60  
-   61 if(state == 2) //Thid received byte are configuration data
-   62 {
-   63 if ((address==0)&&(incoming==0))
-   64 {
-   65 fire_setup = 1; // Write configuration to the GPS if configuration data length is 0
-   66 }
-   67 else
-   68 {
-   69 cmd[address] = incoming; // Store byte to configuration sentence
-   70 }
-   71 }
55 } 72 }
56 if(state == 0x80) //Master is requesting data 73 if(state == 0x80) //Master is requesting data
57 { 74 {
58 //i2c_read(); // Dummy read of I2C device address 75 //i2c_read(); // Dummy read of I2C device address
59 76
60 if(address == 0) // Change buffer atomically at reading of the first byte 77 if(address == 0) // Change buffer atomically at reading of the first byte
61 { 78 {
62 buffer[0]=make8(count,0); 79 buffer[0]=make8(count,0);
63 buffer[1]=make8(count,1); 80 buffer[1]=make8(count,1);
64 buffer[2]=make8(count,2); 81 buffer[2]=make8(count,2);
65 buffer[3]=make8(count,3); 82 buffer[3]=make8(count,3);
66 } 83 }
-   84 if(address <= BUF_LEN)
-   85 {
67 if(address <= BUF_LEN) i2c_write(buffer[address]); // Prepare one byte to SSP buffer 86 i2c_write(buffer[address]); // Prepare one byte to SSP buffer
-   87 }
68 else 88 else
69 { 89 {
70 i2c_write(0x00); // There is nothing to prepare, so zero 90 i2c_write(0x00); // There is nothing to prepare, so zero
71 } 91 }
72 } 92 }
73   93  
74 if(state == 0x81) //Master is requesting data 94 if(state == 0x81) //Master is requesting data
75 { 95 {
76 i2c_write(buffer[1]); // Prepare next byte to SSP buffer 96 i2c_write(buffer[1]); // Prepare next byte to SSP buffer
77 } 97 }
78 if(state == 0x82) //Master is requesting data 98 if(state == 0x82) //Master is requesting data
79 { 99 {
80 i2c_write(buffer[2]); // Prepare next byte to SSP buffer 100 i2c_write(buffer[2]); // Prepare next byte to SSP buffer
81 } 101 }
82 if(state == 0x83) //Master is requesting data 102 if(state == 0x83) //Master is requesting data
83 { 103 {
84 i2c_write(buffer[3]); // Prepare next byte to SSP buffer 104 i2c_write(buffer[3]); // Prepare next byte to SSP buffer
85 -  
86 setup_GPS(); -  
87 } 105 }
88   106  
89 if(state > 0x83) //Master is requesting data 107 if(state > 0x83) //Master is requesting data
90 { 108 {
91 i2c_write(0x00); // There is nothing to prepare, so zero 109 i2c_write(0x00); // There is nothing to prepare, so zero
92 } 110 }
93 } 111 }
94   112  
95   113  
96   114  
97 #int_EXT // Interrupt from 1PPS (RB0) 115 #int_EXT // Interrupt from 1PPS (RB0)
98 void EXT_isr(void) 116 void EXT_isr(void)
99 { 117 {
100 unsigned int16 countH; 118 unsigned int16 countH;
101 unsigned int8 countL; 119 unsigned int8 countL;
102 int16 of2; 120 int16 of2;
103 121
104 of2=of; // read overflow counter 122 of2=of; // read overflow counter
105 countH=get_timer1(); // read internal counter 123 countH=get_timer1(); // read internal counter
106 countL=0; 124 countL=0;
107 output_low(SEL0); 125 output_low(SEL0);
108 output_low(SEL1); 126 output_low(SEL1);
109 countL=input(CLKI); // read bit 0 of external counter 127 countL=input(CLKI); // read bit 0 of external counter
110 output_high(SEL0); 128 output_high(SEL0);
111 // output_low(SEL1); 129 // output_low(SEL1);
112 countL|=input(CLKI)<<1; // read bit 1 of external counter 130 countL|=input(CLKI)<<1; // read bit 1 of external counter
113 output_low(SEL0); 131 output_low(SEL0);
114 output_high(SEL1); 132 output_high(SEL1);
115 countL|=input(CLKI)<<2; // read bit 2 of external counter 133 countL|=input(CLKI)<<2; // read bit 2 of external counter
116 output_high(SEL0); 134 output_high(SEL0);
117 // output_high(SEL1); 135 // output_high(SEL1);
118 countL|=input(CLKI)<<3; // read bit 3 of external counter 136 countL|=input(CLKI)<<3; // read bit 3 of external counter
119   137  
120 output_low(MR); // External counter Master Reset 138 output_low(MR); // External counter Master Reset
121 output_high(MR); 139 output_high(MR);
122 set_timer1(0); // Internal counter reset 140 set_timer1(0); // Internal counter reset
123 of=0; // Overflow counter reset 141 of=0; // Overflow counter reset
124 142
125 count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate 143 count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate
126   144  
127 // printf("%010Lu\r\n", count); 145 // printf("%010Lu\r\n", count);
128 } 146 }
129   147  
130 #int_TIMER1 // Interrupf from overflow 148 #int_TIMER1 // Interrupf from overflow
131 void TIMER1_isr(void) 149 void TIMER1_isr(void)
132 { 150 {
133 of++; 151 of++;
134 } 152 }
135   153  
136 void main() 154 void main()
137 { 155 {
138 setup_adc_ports(NO_ANALOGS|VSS_VDD); 156 setup_adc_ports(NO_ANALOGS|VSS_VDD);
139 setup_adc(ADC_OFF); 157 setup_adc(ADC_OFF);
140 // setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use! 158 // setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
141 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 159 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
142 setup_wdt(WDT_2304MS); 160 setup_wdt(WDT_2304MS);
143 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); 161 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
144 setup_timer_2(T2_DISABLED,0,1); 162 setup_timer_2(T2_DISABLED,0,1);
145 setup_comparator(NC_NC_NC_NC); 163 setup_comparator(NC_NC_NC_NC);
146 setup_vref(FALSE); 164 setup_vref(FALSE);
147   165  
148 restart_wdt(); 166 restart_wdt();
149 delay_ms(1000); 167 delay_ms(1000);
150 restart_wdt(); 168 restart_wdt();
151 169
152 // setup GPS 170 // setup GPS
153 setup_GPS(); 171 setup_GPS();
154   172  
155 ext_int_edge( L_TO_H ); // set 1PPS active edge 173 ext_int_edge( L_TO_H ); // set 1PPS active edge
156 enable_interrupts(INT_TIMER1); 174 enable_interrupts(INT_TIMER1);
157 enable_interrupts(INT_EXT); 175 enable_interrupts(INT_EXT);
158 enable_interrupts(INT_SSP); 176 enable_interrupts(INT_SSP);
159 enable_interrupts(GLOBAL); 177 enable_interrupts(GLOBAL);
160 178
161 buffer[0]=0x0; // Clear I2C output buffer 179 buffer[0]=0x0; // Clear I2C output buffer
162 buffer[1]=0x0; 180 buffer[1]=0x0;
163 buffer[2]=0x0; 181 buffer[2]=0x0;
164 buffer[3]=0x0; 182 buffer[3]=0x0;
165   183  
166 printf("\r\ncvak...\r\n"); 184 //printf("\r\ncvak...\r\n");
-   185
-   186 fire_setup = 0;
167   187  
168 while(true) 188 while(true)
169 { 189 {
170 restart_wdt(); 190 restart_wdt();
171 delay_ms(1500); 191 delay_ms(1000);
-   192 if (fire_setup)
-   193 {
-   194 setup_GPS(); // Write configuration to the GPS
-   195 fire_setup = 0;
-   196 }
172 // printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); 197 //printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
173 printf("%010Lu\r\n", count); 198 //printf("%010Lu\r\n", count);
174 } 199 }
175 } 200 }