Rev Author Line No. Line
1744 kakl 1 #include "main.h"
2012 kaklik 2 #include <math.h>
2161 kaklik 3  
4 #define INTN_PIN PIN_D7
2160 kaklik 5 #include "GP2.h"
1744 kakl 6  
2163 kaklik 7 #define VERSION "0.2"
1757 kakl 8  
1774 kaklik 9 #define ONE_WIRE_PIN PIN_E2
10 #include "ds1820.c"
11  
1744 kakl 12 void main()
13 {
2012 kaklik 14 setup_adc_ports(NO_ANALOGS|VSS_VDD);
15 setup_adc(ADC_CLOCK_DIV_2);
16 setup_spi(SPI_SS_DISABLED);
17 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
18 setup_timer_1(T1_DISABLED);
19 setup_timer_2(T2_DISABLED,0,1);
20 setup_ccp1(CCP_OFF);
21 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
1744 kakl 22  
1745 kakl 23 TDC_reset();
1815 kaklik 24 delay_ms(50);
25  
1980 kaklik 26 while(TRUE)
27 {
2015 kaklik 28  
1886 kakl 29 //----------------------------------------------- Nastaveni registru
2163 kaklik 30  
31 TDC_reset();
32 delay_ms(50);
1980 kaklik 33 MRange=TDC_MRANGE2; // sets measurement mode
1965 kaklik 34 hit1=TDC_MRANGE2_HIT1_START;
1980 kaklik 35 hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
36 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
37 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
38 en_err_val=TDC_ERRVAL_EN; // enable of error value output
39 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2162 kaklik 40 firenum=TDC_FIRENUM_1;
1980 kaklik 41  
42 delval1=0x0; // windowing disabled
1965 kaklik 43 delval2=0x0;
44 delval3=0x0;
45  
46 TDC_update_registers();
1931 kaklik 47  
2161 kaklik 48 delay_ms(10);
1980 kaklik 49  
2161 kaklik 50 //----------------------------------------------- Measuring mode 2
1886 kakl 51  
52 TDC_init();
2161 kaklik 53 delay_ms(50);
54 TDC_start_cycle();
55 While(!input(INTN_PIN)); // waiting for interrupt flag
2163 kaklik 56  
57 // STOP2 INPUT MUST BE PULLED HIGH - else GP2 does not respond to stop pulses!
2160 kaklik 58  
1886 kakl 59 //----------------------------------------------- Pocitani
1964 kaklik 60  
2163 kaklik 61 printf("$TDC%s M2 ", VERSION);
62 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 63  
2163 kaklik 64 TDC_init();
1967 kaklik 65  
2012 kaklik 66  
1980 kaklik 67 //----------------------------------------------- Nastaveni registru
68  
2161 kaklik 69 /* MRange=TDC_MRANGE1;
1980 kaklik 70 hit1=TDC_MRANGE1_HIT1_NOAC;
71 hit2=TDC_MRANGE1_HIT2_NOAC;
72 hitin1=TDC_HITIN1_1;
73 hitin2=TDC_HITIN2_1;
74 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
75 en_err_val=TDC_ERRVAL_EN;
76 clkhsdiv=TDC_CLKHSDIV_4;
77 delval1=0x0;
78 delval2=0x0;
79 delval3=0x0;
80  
81 TDC_update_registers();
82  
83 delay_ms(100);
84  
85  
86 //----------------------------------------------- Mereni 1
87  
88 TDC_init();
89  
90 //----------------------------------------------- Pocitani
91  
2013 kaklik 92 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 93  
1980 kaklik 94 output_low(TDC_ENABLE); //status register
95 ret8=0;
96 ret8=(0b1011<<4)|4;
97 spi_xfer(TDC_stream,ret8,8);
98 ret16=spi_xfer(TDC_stream,0,16);
99 output_high(TDC_ENABLE);
2012 kaklik 100  
1980 kaklik 101 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());
102  
2012 kaklik 103 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 104  
105  
2161 kaklik 106 */
2163 kaklik 107 /// ----------------------------------------------- Temperature masurement
2013 kaklik 108 TDC_reset();
2163 kaklik 109 delay_ms(50);
110  
111 //For temperature measurement TDC unit must be initialised in measurement mode2 this is not destribed in datasheet!!
112  
113 hit1=TDC_MRANGE2_HIT1_START;
114 hitin1=TDC_HITIN1_4; // set number of hits on channel 1
115 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
116 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
117 en_err_val=TDC_ERRVAL_EN; // enable of error value output
118 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
119  
2013 kaklik 120 portnum=TDC_TPORTNUM_4;
121 Tcycle=TDC_TCYCLE_SHORT;
122 fakenum=TDC_TFAKENUM_2;
123 selclkT=TDC_TSELCLK_128HS;
124  
125 TDC_update_registers();
2163 kaklik 126 delay_ms(10);
2013 kaklik 127  
128 TDC_init();
2163 kaklik 129 delay_ms(50);
130  
1967 kaklik 131 TDC_start_temp();
2013 kaklik 132  
2163 kaklik 133 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));
134 printf("%f \r\n",ds1820_read()+273.15);
1886 kakl 135 }
1744 kakl 136 }