Rev 2159 Rev 2160
1 #include "main.h" 1 #include "main.h"
2 #include <math.h> 2 #include <math.h>
3   -  
4 #use fast_io (D) 3 #include "GP2.h"
5   4  
6 #define VERSION 0.2 5 #define VERSION 0.2
7   6  
8 #define START PIN_D4 -  
9 #define STOP1 PIN_D5 -  
10 #define STOP2 PIN_D7 -  
11   -  
12 #include "GP2.h" -  
13   -  
14 #define ONE_WIRE_PIN PIN_E2 7 #define ONE_WIRE_PIN PIN_E2
15 #include "ds1820.c" 8 #include "ds1820.c"
16   9  
17 void main() 10 void main()
18 { 11 {
19   -  
20 /* setup_adc_ports(NO_ANALOGS|VSS_VDD); // for PIC18F4550 -  
21 setup_adc(ADC_CLOCK_DIV_2); -  
22 setup_psp(PSP_DISABLED); -  
23 setup_spi(SPI_SS_DISABLED); -  
24 setup_wdt(WDT_OFF); -  
25 setup_timer_0(RTCC_INTERNAL); -  
26 setup_timer_1(T1_DISABLED); -  
27 setup_timer_2(T2_DISABLED,0,1); -  
28 setup_ccp1(CCP_OFF); -  
29 setup_comparator(NC_NC_NC_NC); -  
30 setup_vref(FALSE); -  
31 */ -  
32 setup_adc_ports(NO_ANALOGS|VSS_VDD); 12 setup_adc_ports(NO_ANALOGS|VSS_VDD);
33 setup_adc(ADC_CLOCK_DIV_2); 13 setup_adc(ADC_CLOCK_DIV_2);
34 setup_spi(SPI_SS_DISABLED); 14 setup_spi(SPI_SS_DISABLED);
35 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 15 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
36 setup_timer_1(T1_DISABLED); 16 setup_timer_1(T1_DISABLED);
37 setup_timer_2(T2_DISABLED,0,1); 17 setup_timer_2(T2_DISABLED,0,1);
38 setup_ccp1(CCP_OFF); 18 setup_ccp1(CCP_OFF);
39 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 19 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
40   20  
41 set_tris_d(0x00); -  
42   -  
43 TDC_reset(); 21 TDC_reset();
44   22  
45 output_low(START); -  
46 output_low(STOP1); -  
47 output_low(STOP2); -  
48   -  
49 delay_ms(50); 23 delay_ms(50);
50   24  
51 int16 ret16; 25 int16 ret16;
52 int8 ret8; 26 int8 ret8;
53   27  
54 TDC_reset(); 28 TDC_reset();
55 delay_ms(100); 29 delay_ms(100);
56 30
57 while(TRUE) 31 while(TRUE)
58 { 32 {
59 33
60 //----------------------------------------------- Nastaveni registru 34 //----------------------------------------------- Nastaveni registru
61 35
62 MRange=TDC_MRANGE2; // sets measurement mode 36 MRange=TDC_MRANGE2; // sets measurement mode
63 hit1=TDC_MRANGE2_HIT1_START; 37 hit1=TDC_MRANGE2_HIT1_START;
64 hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1 38 hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
65 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode) 39 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
66 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags 40 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
67 en_err_val=TDC_ERRVAL_EN; // enable of error value output 41 en_err_val=TDC_ERRVAL_EN; // enable of error value output
68 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4 42 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
69 43
70 delval1=0x0; // windowing disabled 44 delval1=0x0; // windowing disabled
71 delval2=0x0; 45 delval2=0x0;
72 delval3=0x0; 46 delval3=0x0;
73 47
74 TDC_update_registers(); 48 TDC_update_registers();
75   49  
76 delay_ms(100); 50 delay_ms(100);
77   51  
78 //----------------------------------------------- Mereni 2 52 //----------------------------------------------- Mereni 2
79 53
80 TDC_init(); 54 TDC_init();
81 55
82 delay_ms(50); 56 delay_ms(50);
83 57
84 TDC_start_cycle(); 58 TDC_start_cycle();
85 59
86 delay_ms(200); 60 delay_ms(200);
87 -  
88 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme byt v H) -  
89 61
90 output_high(START); -  
91 output_low(START); -  
92 delay_us(150); -  
93 -  
94 output_high(STOP1); -  
95 output_low(STOP1); -  
96 delay_us(1); -  
97 -  
98 output_high(STOP1); -  
99 output_low(STOP1); -  
100 delay_us(10); -  
101 -  
102 output_high(STOP1); -  
103 output_low(STOP1); -  
104 delay_us(1); -  
105 -  
106 //----------------------------------------------- Pocitani 62 //----------------------------------------------- Pocitani
107   63  
108 // printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 64 // printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
109   65  
110 66
111 output_low(TDC_ENABLE); //status register 67 output_low(TDC_ENABLE); //status register
112 ret8=0; 68 ret8=0;
113 ret8=(0b1011<<4)|4; 69 ret8=(0b1011<<4)|4;
114 spi_xfer(TDC_stream,ret8,8); 70 spi_xfer(TDC_stream,ret8,8);
115 ret16=spi_xfer(TDC_stream,0,16); 71 ret16=spi_xfer(TDC_stream,0,16);
116 output_high(TDC_ENABLE); 72 output_high(TDC_ENABLE);
117 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status()); 73 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status());
118   74  
119 printf("Time2: %3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3)); 75 printf("Time2: %3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3));
120   76  
121   77  
122 //----------------------------------------------- Nastaveni registru 78 //----------------------------------------------- Nastaveni registru
123 79
124 MRange=TDC_MRANGE1; 80 MRange=TDC_MRANGE1;
125 hit1=TDC_MRANGE1_HIT1_NOAC; 81 hit1=TDC_MRANGE1_HIT1_NOAC;
126 hit2=TDC_MRANGE1_HIT2_NOAC; 82 hit2=TDC_MRANGE1_HIT2_NOAC;
127 hitin1=TDC_HITIN1_1; 83 hitin1=TDC_HITIN1_1;
128 hitin2=TDC_HITIN2_1; 84 hitin2=TDC_HITIN2_1;
129 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; 85 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
130 en_err_val=TDC_ERRVAL_EN; 86 en_err_val=TDC_ERRVAL_EN;
131 clkhsdiv=TDC_CLKHSDIV_4; 87 clkhsdiv=TDC_CLKHSDIV_4;
132 delval1=0x0; 88 delval1=0x0;
133 delval2=0x0; 89 delval2=0x0;
134 delval3=0x0; 90 delval3=0x0;
135 91
136 TDC_update_registers(); 92 TDC_update_registers();
137   93  
138 delay_ms(100); 94 delay_ms(100);
139   95  
140   96  
141 //----------------------------------------------- Mereni 1 97 //----------------------------------------------- Mereni 1
142 98
143 TDC_init(); 99 TDC_init();
144 -  
145 delay_ms(50); -  
146 output_low(START); -  
147 output_low(STOP1); -  
148 output_low(STOP2); -  
149 -  
150 output_high(START); // start of time measurement -  
151   -  
152 output_high(STOP2); -  
153 output_high(STOP1); -  
154 -  
155 output_low(STOP1); -  
156 output_low(STOP2); -  
157 output_low(START); -  
158   -  
159   -  
160   100  
161 //----------------------------------------------- Pocitani 101 //----------------------------------------------- Pocitani
162   102  
163 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 103 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
164   104  
165 output_low(TDC_ENABLE); //status register 105 output_low(TDC_ENABLE); //status register
166 ret8=0; 106 ret8=0;
167 ret8=(0b1011<<4)|4; 107 ret8=(0b1011<<4)|4;
168 spi_xfer(TDC_stream,ret8,8); 108 spi_xfer(TDC_stream,ret8,8);
169 ret16=spi_xfer(TDC_stream,0,16); 109 ret16=spi_xfer(TDC_stream,0,16);
170 output_high(TDC_ENABLE); 110 output_high(TDC_ENABLE);
171   111  
172 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status()); 112 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu]\r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status());
173   113  
174 printf("Time1: %3.7f %3.7f %3.7f \r\n", TDC_mrange1_get_time(1,0,1,1), TDC_mrange1_get_time(2,0,2,1), TDC_mrange1_get_time(1,1,2,1)); 114 printf("Time1: %3.7f %3.7f %3.7f \r\n", TDC_mrange1_get_time(1,0,1,1), TDC_mrange1_get_time(2,0,2,1), TDC_mrange1_get_time(1,1,2,1));
175   115  
176 /// ----------------------------------------------- Temperature masurement 116 /// ----------------------------------------------- Temperature masurement
177   117  
178   118  
179 TDC_reset(); 119 TDC_reset();
180 portnum=TDC_TPORTNUM_4; 120 portnum=TDC_TPORTNUM_4;
181 Tcycle=TDC_TCYCLE_SHORT; 121 Tcycle=TDC_TCYCLE_SHORT;
182 fakenum=TDC_TFAKENUM_2; 122 fakenum=TDC_TFAKENUM_2;
183 selclkT=TDC_TSELCLK_128HS; 123 selclkT=TDC_TSELCLK_128HS;
184   124  
185 TDC_update_registers(); 125 TDC_update_registers();
186   126  
187 TDC_init(); 127 TDC_init();
188 TDC_start_temp(); 128 TDC_start_temp();
189 129
190 130
191 output_low(TDC_ENABLE); //status register 131 output_low(TDC_ENABLE); //status register
192 ret8=0; 132 ret8=0;
193 ret8=(0b1011<<4)|4; 133 ret8=(0b1011<<4)|4;
194 spi_xfer(TDC_stream,ret8,8); 134 spi_xfer(TDC_stream,ret8,8);
195 ret16=spi_xfer(TDC_stream,0,16); 135 ret16=spi_xfer(TDC_stream,0,16);
196 output_high(TDC_ENABLE); 136 output_high(TDC_ENABLE);
197 printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); 137 printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
198 printf(" [%Lu %Lu %Lu %Lu %Lu %Lu %Lu] \r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status()); 138 printf(" [%Lu %Lu %Lu %Lu %Lu %Lu %Lu] \r\n", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&TDC_get_status());
199 } 139 }
200 } 140 }