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  
15 void get_string(char* s, unsigned int8 max)
1744 kakl 16 {
2164 kaklik 17 unsigned int8 len;
18 char c;
1744 kakl 19  
2164 kaklik 20 --max;
21 len=0;
22 do {
23 c=getc();
24 if(c==8) { // Backspace
25 if(len>0) {
26 len--;
27 putc(c);
28 putc(' ');
29 putc(c);
30 }
31 } else if ((c>=' ')&&(c<='~'))
32 if(len<=max) {
33 s[len++]=c;
34 putc(c);
35 }
36 } while(c!=13);
37 s[len]=0;
38 }
39  
40 signed int16 get_int() {
41 char s[5];
42 signed int16 i;
43  
44 get_string(s, 7);
45  
46 i=atoi(s);
47 return(i);
48 }
49  
50 void temperature_measurement() /// Temperature masurement by TDC and dallas sensor
51  
52 {
53 //For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!!
1745 kakl 54 TDC_reset();
2164 kaklik 55 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
56 en_err_val=TDC_ERRVAL_EN; // enable of error value output
57 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2015 kaklik 58  
2164 kaklik 59 portnum=TDC_TPORTNUM_4;
60 Tcycle=TDC_TCYCLE_SHORT;
61 fakenum=TDC_TFAKENUM_2;
62 selclkT=TDC_TSELCLK_128HS;
2163 kaklik 63  
2164 kaklik 64 TDC_update_registers();
65 delay_ms(10);
66  
67 TDC_init();
68 delay_ms(50);
69  
70 TDC_start_temp();
71 While(input(INTN_PIN)); // waiting for interrupt flag
72  
73 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));
74 printf("%f \r\n",ds1820_read()+273.15);
75  
76 }
77  
78 void measurementM2()
79 {
2163 kaklik 80 TDC_reset();
81 delay_ms(50);
1980 kaklik 82 MRange=TDC_MRANGE2; // sets measurement mode
1965 kaklik 83 hit1=TDC_MRANGE2_HIT1_START;
1980 kaklik 84 hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
85 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
86 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
87 en_err_val=TDC_ERRVAL_EN; // enable of error value output
88 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2162 kaklik 89 firenum=TDC_FIRENUM_1;
1980 kaklik 90  
91 delval1=0x0; // windowing disabled
1965 kaklik 92 delval2=0x0;
93 delval3=0x0;
94  
95 TDC_update_registers();
1931 kaklik 96  
2161 kaklik 97 delay_ms(10);
1980 kaklik 98  
2161 kaklik 99 //----------------------------------------------- Measuring mode 2
1886 kakl 100  
101 TDC_init();
2161 kaklik 102 delay_ms(50);
103 TDC_start_cycle();
104 While(!input(INTN_PIN)); // waiting for interrupt flag
2163 kaklik 105  
106 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses!
2160 kaklik 107  
1886 kakl 108 //----------------------------------------------- Pocitani
1964 kaklik 109  
2163 kaklik 110 printf("$TDC%s M2 ", VERSION);
111 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 112  
2164 kaklik 113 }
1967 kaklik 114  
2012 kaklik 115  
2164 kaklik 116 void main()
117 {
118  
119 char command[20];
120 char tmp[5];
121  
122 setup_adc_ports(NO_ANALOGS|VSS_VDD);
123 setup_adc(ADC_CLOCK_DIV_2);
124 setup_spi(SPI_SS_DISABLED);
125 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
126 setup_timer_1(T1_DISABLED);
127 setup_timer_2(T2_DISABLED,0,1);
128 setup_ccp1(CCP_OFF);
129 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
130  
131 TDC_reset();
132 delay_ms(50);
133  
134 while(TRUE)
135 {
136 get_string(command, 20);
1980 kaklik 137  
2164 kaklik 138 strcpy(tmp,"TM");
139 if (stricmp(command, tmp)) temperature_measurement();
140 strcpy(tmp,"M2");
141 if (stricmp(command, tmp)) measurementM2();
142  
2161 kaklik 143 /* MRange=TDC_MRANGE1;
1980 kaklik 144 hit1=TDC_MRANGE1_HIT1_NOAC;
145 hit2=TDC_MRANGE1_HIT2_NOAC;
146 hitin1=TDC_HITIN1_1;
147 hitin2=TDC_HITIN2_1;
148 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
149 en_err_val=TDC_ERRVAL_EN;
150 clkhsdiv=TDC_CLKHSDIV_4;
151 delval1=0x0;
152 delval2=0x0;
153 delval3=0x0;
154  
155 TDC_update_registers();
156  
157 delay_ms(100);
158  
159  
160 //----------------------------------------------- Mereni 1
161  
162 TDC_init();
163  
164 //----------------------------------------------- Pocitani
165  
2013 kaklik 166 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 167  
1980 kaklik 168 output_low(TDC_ENABLE); //status register
169 ret8=0;
170 ret8=(0b1011<<4)|4;
171 spi_xfer(TDC_stream,ret8,8);
172 ret16=spi_xfer(TDC_stream,0,16);
173 output_high(TDC_ENABLE);
2012 kaklik 174  
1980 kaklik 175 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());
176  
2012 kaklik 177 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));
1980 kaklik 178  
179  
2161 kaklik 180 */
2163 kaklik 181  
1886 kakl 182 }
1744 kakl 183 }