Rev 1818 Rev 1886
Line 1... Line 1...
1 #include "main.h" 1 #include "main.h"
2   2  
3 #define VERSION 0.1 3 #define VERSION 0.2
4   4  
5 #define START PIN_D4 5 #define START PIN_D4
6 #define STOP1 PIN_D5 6 #define STOP1 PIN_D5
7 #define STOP2 PIN_D6 7 #define STOP2 PIN_D6
-   8  
8 #include "GP2.h" 9 #include "GP2.h"
9   10  
10 #define ONE_WIRE_PIN PIN_E2 11 #define ONE_WIRE_PIN PIN_E2
11 #include "ds1820.c" 12 #include "ds1820.c"
12   13  
13 void main() 14 void main()
14 { 15 {
15 float temperature; -  
16   -  
17 setup_adc_ports(NO_ANALOGS|VSS_VDD); 16 setup_adc_ports(NO_ANALOGS|VSS_VDD);
18 setup_adc(ADC_CLOCK_DIV_2); 17 setup_adc(ADC_CLOCK_DIV_2);
19 setup_psp(PSP_DISABLED); 18 setup_psp(PSP_DISABLED);
20 setup_spi(SPI_SS_DISABLED); 19 setup_spi(SPI_SS_DISABLED);
21 setup_wdt(WDT_OFF); 20 setup_wdt(WDT_OFF);
Line 26... Line 25...
26 setup_comparator(NC_NC_NC_NC); 25 setup_comparator(NC_NC_NC_NC);
27 setup_vref(FALSE); 26 setup_vref(FALSE);
28   27  
29 TDC_reset(); 28 TDC_reset();
30 29  
31 MRange=TDC_MRANGE2; -  
32 hitin1=TDC_HITIN1_1; -  
33 hitin2=TDC_HITIN2_0; -  
34 hit1=TDC_MRANGE2_HIT1_START; -  
35 hit2=TDC_MRANGE2_HIT2_1CH1; -  
36 en_int=(TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT); -  
37 en_err_val=TDC_ERRVAL_EN; -  
38 delval1=0x0; -  
39 delval2=0x0; -  
40 delval3=0x0; -  
41 calibrate=TDC_CALIBRATE_DIS; -  
42 disautocal=TDC_AUTOCAL_EN; -  
43 -  
44 firenum=TDC_FIRENUM_1; -  
45 div_fire=TDC_DIV_FIRE_2; -  
46 -  
47 Tcycle=TDC_TCYSLE_LONG; -  
48 portnum=TDC_TPORTNUM_2; -  
49 fakenum=TDC_TFAKENUM_2; -  
50 -  
51 TDC_update_registers(); -  
52 -  
53 output_low(START); 30 output_low(START);
54 output_low(STOP1); 31 output_low(STOP1);
55 output_low(STOP2); 32 output_low(STOP2);
56   33  
57 delay_ms(50); 34 delay_ms(50);
58   35  
-   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 0 1 1 1 0 0 0 0 Init
59 //write raw register values 40 0 1 0 1 0 0 0 0 Power On Reset
-   41 0 0 0 0 0 0 0 1 Start_Cycle
-   42 0 0 0 0 0 0 1 0 Start_Temp
-   43 0 0 0 0 0 0 1 1 Start_Cal_Resonator
-   44 0 0 0 0 0 1 0 0 Start_Cal_TDC
-   45 */
-   46  
-   47 int32 ble,ret32;
-   48 int16 ret16;
-   49 int8 ret8;
-   50  
-   51 while(TRUE)
-   52 {
-   53 TDC_reset();
-   54 delay_ms(100);
-   55
-   56 //----------------------------------------------- Nastaveni registru
60 /* output_low(TDC_ENABLE); 57 output_low(TDC_ENABLE);
-   58 ble=0;
-   59 ble=(8<<28)|(0<<24);
-   60 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;
61 spi_xfer(TDC_stream,0x80338AE8,32); 61 spi_xfer(TDC_stream,ble,32);
62 output_high(TDC_ENABLE); 62 output_high(TDC_ENABLE);
63   63
64 output_low(TDC_ENABLE); 64 output_low(TDC_ENABLE);
-   65 ble=0;
-   66 ble=(8<<28)|(1<<24);
-   67 ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0;
65 spi_xfer(TDC_stream,0x81211400,32); 68 spi_xfer(TDC_stream,ble,32);
66 output_high(TDC_ENABLE); 69 output_high(TDC_ENABLE);
67 70
68 output_low(TDC_ENABLE); 71 output_low(TDC_ENABLE);
-   72 ble=0;
-   73 ble=(8<<28)|(2<<24);
-   74 ble|=(1<<21)|(1<<20)|(1<<19)|0;
69 spi_xfer(TDC_stream,0x82E03200,32); 75 spi_xfer(TDC_stream,ble,32);
70 output_high(TDC_ENABLE); 76 output_high(TDC_ENABLE);
71   77
72 output_low(TDC_ENABLE); 78 output_low(TDC_ENABLE);
-   79 ble=0;
-   80 ble=(8<<28)|(3<<24);
-   81 ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0;
73 spi_xfer(TDC_stream,0x83083300,32); 82 spi_xfer(TDC_stream,ble,32);
74 output_high(TDC_ENABLE); 83 output_high(TDC_ENABLE);
75   84
76 output_low(TDC_ENABLE); 85 output_low(TDC_ENABLE);
-   86 ble=0;
-   87 ble=(8<<28)|(4<<24);
-   88 ble|=(4<<19)|0;
77 spi_xfer(TDC_stream,0x84203400,32); 89 spi_xfer(TDC_stream,ble,32);
78 output_high(TDC_ENABLE); 90 output_high(TDC_ENABLE);
79   91
80 output_low(TDC_ENABLE); 92 output_low(TDC_ENABLE);
-   93 ble=0;
-   94 ble=(8<<28)|(5<<24);
-   95 ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0;
81 spi_xfer(TDC_stream,0x85080000,32); 96 spi_xfer(TDC_stream,ble,32);
82 output_high(TDC_ENABLE); 97 output_high(TDC_ENABLE);
83 */ -  
84 TDC_start_cal_resonator(); -  
85 delay_ms(50); -  
86 printf("calibrate: %LX, %LX, %LX, %LX \r\n", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); -  
87 // TDC_start_cal(); -  
88 // delay_ms(50); -  
89 98
90 while(true) -  
91 { -  
92 temperature = ds1820_read(); -  
93 99
-   100
-   101 //----------------------------------------------- Vypis registru
-   102 output_low(TDC_ENABLE);
-   103 ret8=0;
-   104 ret8=(0b1011<<4)|0;
-   105 spi_xfer(TDC_stream,ret8,8);
-   106 ret32=spi_xfer(TDC_stream,0,32);
-   107 output_high(TDC_ENABLE);
-   108 printf("- %LX ", ret32);
-   109
-   110 output_low(TDC_ENABLE);
-   111 ret8=0;
-   112 ret8=(0b1011<<4)|1;
-   113 spi_xfer(TDC_stream,ret8,8);
-   114 ret32=spi_xfer(TDC_stream,0,32);
-   115 output_high(TDC_ENABLE);
-   116 printf("%LX ", ret32);
-   117
-   118 output_low(TDC_ENABLE);
-   119 ret8=0;
-   120 ret8=(0b1011<<4)|2;
-   121 spi_xfer(TDC_stream,ret8,8);
-   122 ret32=spi_xfer(TDC_stream,0,32);
-   123 output_high(TDC_ENABLE);
-   124 printf("%LX ", ret32);
-   125
-   126 output_low(TDC_ENABLE);
-   127 ret8=0;
-   128 ret8=(0b1011<<4)|3;
-   129 spi_xfer(TDC_stream,ret8,8);
-   130 ret32=spi_xfer(TDC_stream,0,32);
-   131 output_high(TDC_ENABLE);
-   132 printf("%LX ", ret32);
-   133
-   134 output_low(TDC_ENABLE);
-   135 ret8=0;
-   136 ret8=(0b1011<<4)|4;
-   137 spi_xfer(TDC_stream,ret8,8);
-   138 ret16=spi_xfer(TDC_stream,0,16);
-   139 output_high(TDC_ENABLE);
-   140 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);
-   141
-   142 output_low(TDC_ENABLE);
-   143 ret8=0;
-   144 ret8=(0b1011<<4)|5;
-   145 spi_xfer(TDC_stream,ret8,8);
-   146 ret8=spi_xfer(TDC_stream,0,8);
-   147 output_high(TDC_ENABLE);
-   148 printf("%X\r\n", ret8);
-   149
-   150 //----------------------------------------------- Mereni
-   151
94 // TDC_init(); 152 TDC_init();
95 153
96 delay_ms(50); 154 delay_ms(50);
97 printf("Temp: %f \r\n", temperature); -  
98 delay_ms(50); -  
99 printf("status: %LX \r\n", TDC_get_status()); -  
100   155
101 // TDC_start_cycle(); 156 TDC_start_cycle();
-   157
-   158 delay_ms(200);
-   159
-   160 output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H)
102   161
103 // delay_us(10); -  
104 output_high(START); 162 output_high(START);
-   163 output_low(START);
105 // delay_us(1); 164 delay_us(1);
-   165
-   166 output_high(STOP1);
-   167 output_low(STOP1);
-   168 delay_us(1);
106 169
107 delay_us(100); -  
108 // output_low(START); -  
109 170
110 output_high(STOP1); 171 output_high(STOP1);
111 // delay_us(10); -  
112 // delay_us(500); -  
113 // output_high(STOP1); -  
114 // delay_us(10); -  
115 output_low(STOP1); 172 output_low(STOP1);
116 // delay_us(500); -  
117 // output_high(STOP1); -  
118 delay_us(10); 173 delay_us(1);
119 // output_low(STOP1); -  
120 output_low(START); -  
121   174
122 // output_high(STOP2); -  
123 // delay_us(10); -  
124 // output_low(START); -  
125 output_high(STOP1); 175 output_high(STOP1);
126 // output_low(STOP2); -  
127 // delay_us(500); -  
128 // output_high(STOP2); -  
129 // delay_us(10); -  
130 output_low(STOP1); 176 output_low(STOP1);
131 // delay_us(500); -  
132 // output_high(STOP2); -  
133 // delay_us(10); 177 delay_us(1);
134 // output_low(STOP2); -  
135   178
136   179
137 // delay_ms(10); -  
138 TDC_start_temp(); -  
139   180
-   181 //----------------------------------------------- Pocitani
140 delay_ms(10); 182 int32 nn;
141 printf("status: %LX \r\n", TDC_get_status()); 183 for(nn=3;nn<=5;nn++)
142 delay_ms(50); 184 {
143 printf("measured: %LX, %LX, %LX, %LX \r\n", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4)); -  
144 delay_ms(500); 185 delay_ms(500);
145 186
-   187 output_low(TDC_ENABLE);
-   188 ret8=0;
-   189 ret8=(0b1011<<4)|0;
-   190 spi_xfer(TDC_stream,ret8,8);
-   191 ret32=spi_xfer(TDC_stream,0,32);
-   192 output_high(TDC_ENABLE);
-   193 printf("* %LX ", ret32);
-   194
-   195 output_low(TDC_ENABLE);
-   196 ret8=0;
-   197 ret8=(0b1011<<4)|1;
-   198 spi_xfer(TDC_stream,ret8,8);
-   199 ret32=spi_xfer(TDC_stream,0,32);
-   200 output_high(TDC_ENABLE);
-   201 printf("%LX ", ret32);
-   202
-   203 output_low(TDC_ENABLE);
-   204 ret8=0;
-   205 ret8=(0b1011<<4)|2;
-   206 spi_xfer(TDC_stream,ret8,8);
-   207 ret32=spi_xfer(TDC_stream,0,32);
-   208 output_high(TDC_ENABLE);
-   209 printf("%LX ", ret32);
-   210
-   211 output_low(TDC_ENABLE);
-   212 ret8=0;
-   213 ret8=(0b1011<<4)|3;
-   214 spi_xfer(TDC_stream,ret8,8);
-   215 ret32=spi_xfer(TDC_stream,0,32);
-   216 output_high(TDC_ENABLE);
-   217 printf("%LX ", ret32);
-   218
-   219 output_low(TDC_ENABLE);
-   220 ret8=0;
-   221 ret8=(0b1011<<4)|4;
-   222 spi_xfer(TDC_stream,ret8,8);
-   223 ret16=spi_xfer(TDC_stream,0,16);
-   224 output_high(TDC_ENABLE);
-   225 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);
-   226
-   227 output_low(TDC_ENABLE);
-   228 ret8=0;
-   229 ret8=(0b1011<<4)|5;
-   230 spi_xfer(TDC_stream,ret8,8);
-   231 ret8=spi_xfer(TDC_stream,0,8);
-   232 output_high(TDC_ENABLE);
-   233 printf("%X\r\n", ret8);
-   234
-   235 // Next calculation
-   236 output_low(TDC_ENABLE);
-   237 ble=0;
-   238 ble=(8<<28)|(1<<24);
-   239 ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00;
-   240 spi_xfer(TDC_stream,ble,32);
-   241 output_high(TDC_ENABLE);
-   242
-   243 }
-   244
146 }; 245 }
-   246  
147 } 247 }