Rev 3470 Rev 3501
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 #define BUF_LEN 4 25 #define BUF_LEN 4
26 int8 buffer[BUF_LEN]; // I2C buffer 26 int8 buffer[BUF_LEN]; // I2C buffer
27 int8 address=0; 27 int8 address=0;
28   28  
29 unsigned int16 of=0; // count of overflow 29 unsigned int16 of=0; // count of overflow
30   30  
31 // 1x 100 us per 10 s UTC synchronised 31 // 1x 100 us per 10 s UTC synchronised
32 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}; 32 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};
33 33
34 #INT_SSP 34 #INT_SSP
35 void ssp_interupt () 35 void ssp_interupt ()
36 { 36 {
37 int8 incoming, state; 37 int8 incoming, state;
38   38  
39 state = i2c_isr_state(); 39 state = i2c_isr_state();
40   40  
41 if(state < 0x80) //Master is sending data 41 if(state < 0x80) //Master is sending data
42 { 42 {
43 incoming = i2c_read(); // Read byte 43 incoming = i2c_read(); // Read byte
44   44  
45 if(state == 1) //Second received byte is address of register 45 if(state == 1) //Second received byte is address of register
46 { 46 {
47 address = incoming; 47 address = incoming;
48 } 48 }
49 } 49 }
50 if(state == 0x80) //Master is requesting data 50 if(state == 0x80) //Master is requesting data
51 { 51 {
52 //i2c_read(); // Dummy read of I2C device address 52 //i2c_read(); // Dummy read of I2C device address
53 53
54 if(address == 0) // Change buffer atomically at reading of the first byte 54 if(address == 0) // Change buffer atomically at reading of the first byte
55 { 55 {
56 buffer[0]=make8(count,0); 56 buffer[0]=make8(count,0);
57 buffer[1]=make8(count,1); 57 buffer[1]=make8(count,1);
58 buffer[2]=make8(count,2); 58 buffer[2]=make8(count,2);
59 buffer[3]=make8(count,3); 59 buffer[3]=make8(count,3);
60 } 60 }
61 if(address <= BUF_LEN) i2c_write(buffer[address]); // Prepare one byte to SSP buffer 61 if(address <= BUF_LEN) i2c_write(buffer[address]); // Prepare one byte to SSP buffer
62 else 62 else
63 { 63 {
64 i2c_write(0x00); // There is nothing to prepare, so zero 64 i2c_write(0x00); // There is nothing to prepare, so zero
65 } 65 }
66 } 66 }
67   67  
68 if(state == 0x81) //Master is requesting data 68 if(state == 0x81) //Master is requesting data
69 { 69 {
70 i2c_write(buffer[1]); // Prepare next byte to SSP buffer 70 i2c_write(buffer[1]); // Prepare next byte to SSP buffer
71 } 71 }
72 if(state == 0x82) //Master is requesting data 72 if(state == 0x82) //Master is requesting data
73 { 73 {
74 i2c_write(buffer[2]); // Prepare next byte to SSP buffer 74 i2c_write(buffer[2]); // Prepare next byte to SSP buffer
75 } 75 }
76 if(state == 0x83) //Master is requesting data 76 if(state == 0x83) //Master is requesting data
77 { 77 {
78 i2c_write(buffer[3]); // Prepare next byte to SSP buffer 78 i2c_write(buffer[3]); // Prepare next byte to SSP buffer
79 } 79 }
80   80  
81 if(state > 0x83) //Master is requesting data 81 if(state > 0x83) //Master is requesting data
82 { 82 {
83 i2c_write(0x00); // There is nothing to prepare, so zero 83 i2c_write(0x00); // There is nothing to prepare, so zero
84 } 84 }
85 } 85 }
86   86  
87   87  
88   88  
89 #int_EXT // Interrupt from 1PPS (RB0) 89 #int_EXT // Interrupt from 1PPS (RB0)
90 void EXT_isr(void) 90 void EXT_isr(void)
91 { 91 {
92 unsigned int16 countH; 92 unsigned int16 countH;
93 unsigned int8 countL; 93 unsigned int8 countL;
94 int16 of2; 94 int16 of2;
95 95
96 of2=of; // read overflow counter 96 of2=of; // read overflow counter
97 countH=get_timer1(); // read internal counter 97 countH=get_timer1(); // read internal counter
98 countL=0; 98 countL=0;
99 output_low(SEL0); 99 output_low(SEL0);
100 output_low(SEL1); 100 output_low(SEL1);
101 countL=input(CLKI); // read bit 0 of external counter 101 countL=input(CLKI); // read bit 0 of external counter
102 output_high(SEL0); 102 output_high(SEL0);
103 // output_low(SEL1); 103 // output_low(SEL1);
104 countL|=input(CLKI)<<1; // read bit 1 of external counter 104 countL|=input(CLKI)<<1; // read bit 1 of external counter
105 output_low(SEL0); 105 output_low(SEL0);
106 output_high(SEL1); 106 output_high(SEL1);
107 countL|=input(CLKI)<<2; // read bit 2 of external counter 107 countL|=input(CLKI)<<2; // read bit 2 of external counter
108 output_high(SEL0); 108 output_high(SEL0);
109 // output_high(SEL1); 109 // output_high(SEL1);
110 countL|=input(CLKI)<<3; // read bit 3 of external counter 110 countL|=input(CLKI)<<3; // read bit 3 of external counter
111   111  
112 output_low(MR); // External counter Master Reset 112 output_low(MR); // External counter Master Reset
113 output_high(MR); 113 output_high(MR);
114 set_timer1(0); // Internal counter reset 114 set_timer1(0); // Internal counter reset
115 of=0; // Overflow counter reset 115 of=0; // Overflow counter reset
116 116
117 count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate 117 count=((unsigned int32)of2<<20)+((unsigned int32)countH<<4)+(unsigned int32)countL; // concatenate
118   118  
119 printf("%010Lu\r\n", count); 119 // printf("%010Lu\r\n", count);
120 } 120 }
121   121  
122 #int_TIMER1 // Interrupf from overflow 122 #int_TIMER1 // Interrupf from overflow
123 void TIMER1_isr(void) 123 void TIMER1_isr(void)
124 { 124 {
125 of++; 125 of++;
126 } 126 }
127   127  
128   128  
129 void main() 129 void main()
130 { 130 {
131 setup_adc_ports(NO_ANALOGS|VSS_VDD); 131 setup_adc_ports(NO_ANALOGS|VSS_VDD);
132 setup_adc(ADC_OFF); 132 setup_adc(ADC_OFF);
133 // setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use! 133 // setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
134 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 134 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
135 setup_wdt(WDT_2304MS); 135 setup_wdt(WDT_2304MS);
136 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); 136 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
137 setup_timer_2(T2_DISABLED,0,1); 137 setup_timer_2(T2_DISABLED,0,1);
138 setup_comparator(NC_NC_NC_NC); 138 setup_comparator(NC_NC_NC_NC);
139 setup_vref(FALSE); 139 setup_vref(FALSE);
140   140  
141 restart_wdt(); 141 restart_wdt();
142 delay_ms(1000); 142 delay_ms(1000);
143 restart_wdt(); 143 restart_wdt();
144 144
145 // setup GPS 145 // setup GPS
146 { 146 {
147 int n; 147 int n;
148 for (n=0;n<40;n++) putc(cmd[n]); 148 for (n=0;n<40;n++) putc(cmd[n]);
149 } 149 }
150   150  
151 ext_int_edge( L_TO_H ); // set 1PPS active edge 151 ext_int_edge( L_TO_H ); // set 1PPS active edge
152 enable_interrupts(INT_TIMER1); 152 enable_interrupts(INT_TIMER1);
153 enable_interrupts(INT_EXT); 153 enable_interrupts(INT_EXT);
154 enable_interrupts(INT_SSP); 154 enable_interrupts(INT_SSP);
155 enable_interrupts(GLOBAL); 155 enable_interrupts(GLOBAL);
156 156
157 buffer[0]=0x0; // Clear I2C output buffer 157 buffer[0]=0x0; // Clear I2C output buffer
158 buffer[1]=0x0; 158 buffer[1]=0x0;
159 buffer[2]=0x0; 159 buffer[2]=0x0;
160 buffer[3]=0x0; 160 buffer[3]=0x0;
161   161  
162 printf("\r\ncvak...\r\n"); 162 printf("\r\ncvak...\r\n");
163   163  
164 while(true) 164 while(true)
165 { 165 {
166 restart_wdt(); 166 restart_wdt();
167 delay_ms(1000); 167 delay_ms(1500);
168 // printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]); 168 // printf("%X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
-   169 printf("%010Lu\r\n", count);
169 } 170 }
170 } 171 }