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  
1886 kakl 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();
1886 kakl 24  
1815 kaklik 25 delay_ms(50);
26  
1886 kakl 27 int16 ret16;
28 int8 ret8;
1815 kaklik 29  
1967 kaklik 30 TDC_reset();
31 delay_ms(100);
1886 kakl 32  
1980 kaklik 33 while(TRUE)
34 {
2015 kaklik 35  
1886 kakl 36 //----------------------------------------------- Nastaveni registru
1980 kaklik 37  
38 MRange=TDC_MRANGE2; // sets measurement mode
1965 kaklik 39 hit1=TDC_MRANGE2_HIT1_START;
1980 kaklik 40 hitin1=TDC_HITIN1_4; // set nomber of hits on channel 1
41 hitin2=TDC_HITIN2_0; // disable channel 2 (normal state for this mode)
42 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT; // eneble all possible interrupt flags
43 en_err_val=TDC_ERRVAL_EN; // enable of error value output
44 clkhsdiv=TDC_CLKHSDIV_4; // divide clkHS by 4
2162 kaklik 45 firenum=TDC_FIRENUM_1;
1980 kaklik 46  
47 delval1=0x0; // windowing disabled
1965 kaklik 48 delval2=0x0;
49 delval3=0x0;
50  
51 TDC_update_registers();
1931 kaklik 52  
2161 kaklik 53 delay_ms(10);
1980 kaklik 54  
2161 kaklik 55 //----------------------------------------------- Measuring mode 2
1886 kakl 56  
57 TDC_init();
2161 kaklik 58 delay_ms(50);
59 TDC_start_cycle();
60 While(!input(INTN_PIN)); // waiting for interrupt flag
2160 kaklik 61  
1886 kakl 62 //----------------------------------------------- Pocitani
1964 kaklik 63  
2013 kaklik 64 // printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 65  
1886 kakl 66  
2161 kaklik 67 output_low(TDC_ENABLE); //status register read
1886 kakl 68 ret8=0;
69 ret8=(0b1011<<4)|4;
70 spi_xfer(TDC_stream,ret8,8);
71 ret16=spi_xfer(TDC_stream,0,16);
72 output_high(TDC_ENABLE);
2161 kaklik 73  
74 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()); //status register print
1967 kaklik 75  
2012 kaklik 76 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));
77  
78  
1980 kaklik 79 //----------------------------------------------- Nastaveni registru
80  
2161 kaklik 81 /* MRange=TDC_MRANGE1;
1980 kaklik 82 hit1=TDC_MRANGE1_HIT1_NOAC;
83 hit2=TDC_MRANGE1_HIT2_NOAC;
84 hitin1=TDC_HITIN1_1;
85 hitin2=TDC_HITIN2_1;
86 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
87 en_err_val=TDC_ERRVAL_EN;
88 clkhsdiv=TDC_CLKHSDIV_4;
89 delval1=0x0;
90 delval2=0x0;
91 delval3=0x0;
92  
93 TDC_update_registers();
94  
95 delay_ms(100);
96  
97  
98 //----------------------------------------------- Mereni 1
99  
100 TDC_init();
101  
102 //----------------------------------------------- Pocitani
103  
2013 kaklik 104 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 105  
1980 kaklik 106 output_low(TDC_ENABLE); //status register
107 ret8=0;
108 ret8=(0b1011<<4)|4;
109 spi_xfer(TDC_stream,ret8,8);
110 ret16=spi_xfer(TDC_stream,0,16);
111 output_high(TDC_ENABLE);
2012 kaklik 112  
1980 kaklik 113 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());
114  
2012 kaklik 115 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 116  
117 /// ----------------------------------------------- Temperature masurement
118  
2161 kaklik 119 */
2013 kaklik 120 TDC_reset();
121 portnum=TDC_TPORTNUM_4;
122 Tcycle=TDC_TCYCLE_SHORT;
123 fakenum=TDC_TFAKENUM_2;
124 selclkT=TDC_TSELCLK_128HS;
125  
126 TDC_update_registers();
127  
128 TDC_init();
1967 kaklik 129 TDC_start_temp();
2013 kaklik 130  
131  
1967 kaklik 132 output_low(TDC_ENABLE); //status register
133 ret8=0;
134 ret8=(0b1011<<4)|4;
135 spi_xfer(TDC_stream,ret8,8);
136 ret16=spi_xfer(TDC_stream,0,16);
137 output_high(TDC_ENABLE);
2011 kaklik 138 printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
139 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());
1886 kakl 140 }
1744 kakl 141 }