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
45  
46 delval1=0x0; // windowing disabled
1965 kaklik 47 delval2=0x0;
48 delval3=0x0;
49  
50 TDC_update_registers();
1931 kaklik 51  
2161 kaklik 52 delay_ms(10);
1980 kaklik 53  
2161 kaklik 54 //----------------------------------------------- Measuring mode 2
1886 kakl 55  
56 TDC_init();
2161 kaklik 57 delay_ms(50);
58 TDC_start_cycle();
59 While(!input(INTN_PIN)); // waiting for interrupt flag
2160 kaklik 60  
1886 kakl 61 //----------------------------------------------- Pocitani
1964 kaklik 62  
2013 kaklik 63 // printf("Time2: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 64  
1886 kakl 65  
2161 kaklik 66 output_low(TDC_ENABLE); //status register read
1886 kakl 67 ret8=0;
68 ret8=(0b1011<<4)|4;
69 spi_xfer(TDC_stream,ret8,8);
70 ret16=spi_xfer(TDC_stream,0,16);
71 output_high(TDC_ENABLE);
2161 kaklik 72  
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()); //status register print
1967 kaklik 74  
2012 kaklik 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));
76  
77  
1980 kaklik 78 //----------------------------------------------- Nastaveni registru
79  
2161 kaklik 80 /* MRange=TDC_MRANGE1;
1980 kaklik 81 hit1=TDC_MRANGE1_HIT1_NOAC;
82 hit2=TDC_MRANGE1_HIT2_NOAC;
83 hitin1=TDC_HITIN1_1;
84 hitin2=TDC_HITIN2_1;
85 en_int= TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT;
86 en_err_val=TDC_ERRVAL_EN;
87 clkhsdiv=TDC_CLKHSDIV_4;
88 delval1=0x0;
89 delval2=0x0;
90 delval3=0x0;
91  
92 TDC_update_registers();
93  
94 delay_ms(100);
95  
96  
97 //----------------------------------------------- Mereni 1
98  
99 TDC_init();
100  
101 //----------------------------------------------- Pocitani
102  
2013 kaklik 103 // printf("Time1: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
2012 kaklik 104  
1980 kaklik 105 output_low(TDC_ENABLE); //status register
106 ret8=0;
107 ret8=(0b1011<<4)|4;
108 spi_xfer(TDC_stream,ret8,8);
109 ret16=spi_xfer(TDC_stream,0,16);
110 output_high(TDC_ENABLE);
2012 kaklik 111  
1980 kaklik 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());
113  
2012 kaklik 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));
1980 kaklik 115  
116 /// ----------------------------------------------- Temperature masurement
117  
2161 kaklik 118 */
2013 kaklik 119 TDC_reset();
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();
126  
127 TDC_init();
1967 kaklik 128 TDC_start_temp();
2013 kaklik 129  
130  
1967 kaklik 131 output_low(TDC_ENABLE); //status register
132 ret8=0;
133 ret8=(0b1011<<4)|4;
134 spi_xfer(TDC_stream,ret8,8);
135 ret16=spi_xfer(TDC_stream,0,16);
136 output_high(TDC_ENABLE);
2011 kaklik 137 printf("Temp: %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
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());
1886 kakl 139 }
1744 kakl 140 }