Rev Author Line No. Line
1744 kakl 1 #include "main.h"
2012 kaklik 2 #include <math.h>
2164 kaklik 3 #include <stdlib.h>
2161 kaklik 4  
2164 kaklik 5  
2161 kaklik 6 #define INTN_PIN PIN_D7
2160 kaklik 7 #include "GP2.h"
1744 kakl 8  
2163 kaklik 9 #define VERSION "0.2"
1757 kakl 10  
1774 kaklik 11 #define ONE_WIRE_PIN PIN_E2
12 #include "ds1820.c"
13  
2164 kaklik 14 void temperature_measurement() /// Temperature masurement by TDC and dallas sensor
15  
16 {
17 //For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!!
1745 kakl 18 TDC_reset();
2164 kaklik 19 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
20 en_err_val=TDC_ERRVAL_EN; // enable of error value output
21 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2015 kaklik 22  
2164 kaklik 23 portnum=TDC_TPORTNUM_4;
24 Tcycle=TDC_TCYCLE_SHORT;
25 fakenum=TDC_TFAKENUM_2;
26 selclkT=TDC_TSELCLK_128HS;
2163 kaklik 27  
2164 kaklik 28 TDC_update_registers();
29 delay_ms(10);
30  
31 TDC_init();
32 delay_ms(50);
33  
34 TDC_start_temp();
35 While(input(INTN_PIN)); // waiting for interrupt flag
36  
37 printf("$TDC%s TMP %10LU %10LU %10LU %10LU ", VERSION, TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
38 printf("%f \r\n",ds1820_read()+273.15);
39  
40 }
41  
2178 kaklik 42 void measurementM1()
2164 kaklik 43 {
2178 kaklik 44 MRange=TDC_MRANGE1;
45 hit1=TDC_MRANGE1_HIT1_NOAC;
46 hit2=TDC_MRANGE1_HIT2_NOAC;
47 hitin1=TDC_HITIN1_1;
48 hitin2=TDC_HITIN2_1;
49 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
50 en_err_val=TDC_ERRVAL_EN;
51 clkhsdiv=TDC_CLKHSDIV_4;
52 delval1=0x0;
53 delval2=0x0;
54 delval3=0x0;
55  
56 TDC_update_registers();
57  
58 delay_ms(100);
59  
60  
61 //----------------------------------------------- Mereni 1
62  
63 TDC_init();
64  
65 //----------------------------------------------- Pocitani
66  
67 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
68  
69 /* output_low(TDC_ENABLE); //status register
70 ret8=0;
71 ret8=(0b1011<<4)|4;
72 spi_xfer(TDC_stream,ret8,8);
73 ret16=spi_xfer(TDC_stream,0,16);
74 output_high(TDC_ENABLE);
75  
76 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());
77 */
78 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));
79  
80  
81 }
82  
83  
84 void measurementM2(unsigned int hits)
85 {
2163 kaklik 86 TDC_reset();
87 delay_ms(50);
1980 kaklik 88 MRange=TDC_MRANGE2; // sets measurement mode
1965 kaklik 89 hit1=TDC_MRANGE2_HIT1_START;
2178 kaklik 90 // hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
1980 kaklik 91 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
92 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
93 en_err_val=TDC_ERRVAL_EN; // enable of error value output
94 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2162 kaklik 95 firenum=TDC_FIRENUM_1;
1980 kaklik 96  
2178 kaklik 97 switch(hits)
98 {
99 case 1:
100 hitin1=TDC_HITIN1_2;
101 break;
102  
103 case 2:
104 hitin1=TDC_HITIN1_3;
105 break;
106  
107 case 3:
108 hitin1=TDC_HITIN1_4;
109 break;
110  
111 default: return;
112 }
113  
1980 kaklik 114 delval1=0x0; // windowing disabled
1965 kaklik 115 delval2=0x0;
116 delval3=0x0;
117  
118 TDC_update_registers();
1931 kaklik 119  
2161 kaklik 120 delay_ms(10);
1980 kaklik 121  
2161 kaklik 122 //----------------------------------------------- Measuring mode 2
1886 kakl 123  
124 TDC_init();
2161 kaklik 125 delay_ms(50);
126 TDC_start_cycle();
127 While(!input(INTN_PIN)); // waiting for interrupt flag
2163 kaklik 128  
129 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses!
2160 kaklik 130  
1886 kakl 131 //----------------------------------------------- Pocitani
1964 kaklik 132  
2163 kaklik 133 printf("$TDC%s M2 ", VERSION);
134 printf("%3.7f %3.7f %3.7f \r\n", TDC_mrange2_get_time(1), TDC_mrange2_get_time(2), TDC_mrange2_get_time(3));
2012 kaklik 135  
2164 kaklik 136 }
1967 kaklik 137  
2179 kaklik 138 void get_command(char *ptr, unsigned int max) // gets string of defined maximum lenght
139 {
140 char c=0;
141 unsigned int len=0;
2012 kaklik 142  
2179 kaklik 143 while ((c=getc()) != 13)
144 {
145 ptr[len]=c;
146  
147 if (len == max-2)
148 {
149 ptr[len+1]=0;
150 return;
151 }
152 len++;
153 }
154  
155 return;
156 }
157  
158  
2164 kaklik 159 void main()
160 {
161  
162 char command[20];
163 char tmp[5];
2179 kaklik 164 char *ptr;
165 unsigned long parameter;
2164 kaklik 166 setup_adc_ports(NO_ANALOGS|VSS_VDD);
167 setup_adc(ADC_CLOCK_DIV_2);
168 setup_spi(SPI_SS_DISABLED);
169 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
170 setup_timer_1(T1_DISABLED);
171 setup_timer_2(T2_DISABLED,0,1);
172 setup_ccp1(CCP_OFF);
173 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
174  
175 TDC_reset();
176 delay_ms(50);
177  
2178 kaklik 178 printf("\r\n\r\n# TDC%s (C) 2011 Jakub Kakona\r\n",VERSION); // Welcome message
179 // printf("#%s\r\n",&REV[4]);
180  
2164 kaklik 181 while(TRUE)
182 {
2179 kaklik 183 printf("$TDC%s->", VERSION); // print prompt
184 get_command(command, 20); // receive command from terminal
185 printf("%s\r\n", command); // echo received command
2178 kaklik 186  
2164 kaklik 187 strcpy(tmp,"TM");
2178 kaklik 188 if (!strncmp(command, tmp, 2)) temperature_measurement();
1980 kaklik 189  
2178 kaklik 190 strcpy(tmp,"M2 ");
191 if (!strncmp(command, tmp, 3))
192 {
2179 kaklik 193 parameter=strtol(command+3,&ptr,10);
194  
195 printf("%s\r\n", command+3);
196 printf("%lu\r\n",parameter);
2178 kaklik 197 measurementM2(parameter);
198 }
1980 kaklik 199  
2178 kaklik 200 strcpy(tmp,"M1 ");
201 if (!strncmp(command, tmp, 3))
202 {
203 measurementM1();
204 }
1980 kaklik 205  
1886 kakl 206 }
1744 kakl 207 }