Rev Author Line No. Line
1744 kakl 1 #include "main.h"
2  
1886 kakl 3 #define VERSION 0.2
1757 kakl 4  
1759 kakl 5 #define START PIN_D4
6 #define STOP1 PIN_D5
7 #define STOP2 PIN_D6
1886 kakl 8  
1774 kaklik 9 #include "GP2.h"
1759 kakl 10  
1774 kaklik 11 #define ONE_WIRE_PIN PIN_E2
12 #include "ds1820.c"
13  
1744 kakl 14 void main()
15 {
16 setup_adc_ports(NO_ANALOGS|VSS_VDD);
17 setup_adc(ADC_CLOCK_DIV_2);
18 setup_psp(PSP_DISABLED);
19 setup_spi(SPI_SS_DISABLED);
20 setup_wdt(WDT_OFF);
21 setup_timer_0(RTCC_INTERNAL);
22 setup_timer_1(T1_DISABLED);
23 setup_timer_2(T2_DISABLED,0,1);
24 setup_ccp1(CCP_OFF);
25 setup_comparator(NC_NC_NC_NC);
26 setup_vref(FALSE);
27  
1745 kakl 28 TDC_reset();
1886 kakl 29  
1759 kakl 30 output_low(START);
31 output_low(STOP1);
32 output_low(STOP2);
1815 kaklik 33  
34 delay_ms(50);
35  
1886 kakl 36 /*
37 1 0 0 0 0 ADR2 ADR1 ADR0 Write into address ADR
38 1 0 1 1 0 ADR2 ADR1 ADR0 Read from address ADR
39  
40  
41  
42  
43  
44  
45 */
1815 kaklik 46  
1964 kaklik 47 int32 ble;
1886 kakl 48 int16 ret16;
49 int8 ret8;
1815 kaklik 50  
1931 kaklik 51  
1886 kakl 52 while(TRUE)
1744 kakl 53 {
1931 kaklik 54 delay_ms(100);
55  
56  
1886 kakl 57 TDC_reset();
58 delay_ms(100);
59  
60 //----------------------------------------------- Nastaveni registru
61 output_low(TDC_ENABLE);
62 ble=0;
1964 kaklik 63 ble=(8<<28)|(0<<24); // write addres
1886 kakl 64 ble|=(0<<20)|(0<<16)|(0<<14)|(3<<12)|(1<<10)|(0<<9)|(0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(0<<1)|0;
65 spi_xfer(TDC_stream,ble,32);
66 output_high(TDC_ENABLE);
67  
68 output_low(TDC_ENABLE);
69 ble=0;
70 ble=(8<<28)|(1<<24);
71 ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0;
72 spi_xfer(TDC_stream,ble,32);
73 output_high(TDC_ENABLE);
1815 kaklik 74  
1886 kakl 75 output_low(TDC_ENABLE);
76 ble=0;
77 ble=(8<<28)|(2<<24);
78 ble|=(1<<21)|(1<<20)|(1<<19)|0;
79 spi_xfer(TDC_stream,ble,32);
80 output_high(TDC_ENABLE);
81  
82 output_low(TDC_ENABLE);
83 ble=0;
84 ble=(8<<28)|(3<<24);
85 ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0;
86 spi_xfer(TDC_stream,ble,32);
87 output_high(TDC_ENABLE);
88  
89 output_low(TDC_ENABLE);
90 ble=0;
91 ble=(8<<28)|(4<<24);
92 ble|=(4<<19)|0;
93 spi_xfer(TDC_stream,ble,32);
94 output_high(TDC_ENABLE);
95  
96 output_low(TDC_ENABLE);
97 ble=0;
98 ble=(8<<28)|(5<<24);
99 ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0;
100 spi_xfer(TDC_stream,ble,32);
101 output_high(TDC_ENABLE);
1931 kaklik 102  
103  
1886 kakl 104 //----------------------------------------------- Vypis registru
1964 kaklik 105  
106 printf("- %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
1886 kakl 107  
108 output_low(TDC_ENABLE);
109 ret8=0;
110 ret8=(0b1011<<4)|4;
111 spi_xfer(TDC_stream,ret8,8);
112 ret16=spi_xfer(TDC_stream,0,16);
113 output_high(TDC_ENABLE);
114 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu] ", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&ret16);
115  
1964 kaklik 116 printf("%X\r\n",TDC_get_reg1());
1886 kakl 117  
118 //----------------------------------------------- Mereni
119  
120 TDC_init();
121  
1759 kakl 122 delay_ms(50);
1886 kakl 123  
124 TDC_start_cycle();
125  
126 delay_ms(200);
127  
128 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H)
129  
1759 kakl 130 output_high(START);
1886 kakl 131 output_low(START);
132 delay_us(1);
1798 kaklik 133  
1886 kakl 134 output_high(STOP1);
135 output_low(STOP1);
136 delay_us(1);
1759 kakl 137  
1886 kakl 138  
1815 kaklik 139 output_high(STOP1);
1886 kakl 140 output_low(STOP1);
141 delay_us(1);
142  
143 output_high(STOP1);
144 output_low(STOP1);
145 delay_us(1);
146  
147  
148  
149 //----------------------------------------------- Pocitani
150 int32 nn;
151 for(nn=3;nn<=5;nn++)
152 {
153 delay_ms(500);
1964 kaklik 154  
155 printf("* %LX %LX %LX %LX ", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
1886 kakl 156  
1964 kaklik 157 output_low(TDC_ENABLE); //status register
1886 kakl 158 ret8=0;
159 ret8=(0b1011<<4)|4;
160 spi_xfer(TDC_stream,ret8,8);
161 ret16=spi_xfer(TDC_stream,0,16);
162 output_high(TDC_ENABLE);
163 printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu] ", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&ret16);
1964 kaklik 164  
165 printf("%X\r\n",TDC_get_reg1());
1886 kakl 166  
167 // Next calculation
168 output_low(TDC_ENABLE);
169 ble=0;
1964 kaklik 170 ble=(8<<28)|(1<<24); // write to reg1
1886 kakl 171 ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00;
172 spi_xfer(TDC_stream,ble,32);
173 output_high(TDC_ENABLE);
174  
175 }
176  
177 }
1759 kakl 178  
1744 kakl 179 }