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