Rev 1817 Rev 1818
1 #include "main.h" 1 #include "main.h"
2   2  
3 #define VERSION 0.1 3 #define VERSION 0.1
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 #include "GP2.h" 8 #include "GP2.h"
9   9  
10 #define ONE_WIRE_PIN PIN_E2 10 #define ONE_WIRE_PIN PIN_E2
11 #include "ds1820.c" 11 #include "ds1820.c"
12   12  
13 void main() 13 void main()
14 { 14 {
15 float temperature; 15 float temperature;
16   16  
17 setup_adc_ports(NO_ANALOGS|VSS_VDD); 17 setup_adc_ports(NO_ANALOGS|VSS_VDD);
18 setup_adc(ADC_CLOCK_DIV_2); 18 setup_adc(ADC_CLOCK_DIV_2);
19 setup_psp(PSP_DISABLED); 19 setup_psp(PSP_DISABLED);
20 setup_spi(SPI_SS_DISABLED); 20 setup_spi(SPI_SS_DISABLED);
21 setup_wdt(WDT_OFF); 21 setup_wdt(WDT_OFF);
22 setup_timer_0(RTCC_INTERNAL); 22 setup_timer_0(RTCC_INTERNAL);
23 setup_timer_1(T1_DISABLED); 23 setup_timer_1(T1_DISABLED);
24 setup_timer_2(T2_DISABLED,0,1); 24 setup_timer_2(T2_DISABLED,0,1);
25 setup_ccp1(CCP_OFF); 25 setup_ccp1(CCP_OFF);
26 setup_comparator(NC_NC_NC_NC); 26 setup_comparator(NC_NC_NC_NC);
27 setup_vref(FALSE); 27 setup_vref(FALSE);
28   28  
29 TDC_reset(); 29 TDC_reset();
30 30
31 MRange=TDC_MRANGE2; 31 MRange=TDC_MRANGE2;
32 hitin1=TDC_HITIN1_1; 32 hitin1=TDC_HITIN1_1;
33 hitin2=TDC_HITIN2_0; 33 hitin2=TDC_HITIN2_0;
34 hit1=TDC_MRANGE2_HIT1_START; 34 hit1=TDC_MRANGE2_HIT1_START;
35 hit2=TDC_MRANGE2_HIT2_1CH1; 35 hit2=TDC_MRANGE2_HIT2_1CH1;
36 en_int=(TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT); 36 en_int=(TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT);
37 en_err_val=TDC_ERRVAL_EN; 37 en_err_val=TDC_ERRVAL_EN;
38 delval1=0x0; 38 delval1=0x0;
39 delval2=0x0; 39 delval2=0x0;
40 delval3=0x0; 40 delval3=0x0;
41 calibrate=TDC_CALIBRATE_DIS; 41 calibrate=TDC_CALIBRATE_DIS;
42 disautocal=TDC_AUTOCAL_EN; 42 disautocal=TDC_AUTOCAL_EN;
43 43
44 firenum=TDC_FIRENUM_0; 44 firenum=TDC_FIRENUM_1;
45 div_fire=TDC_DIV_FIRE_2; 45 div_fire=TDC_DIV_FIRE_2;
46 46
47 Tcycle=TDC_TCYSLE_LONG; 47 Tcycle=TDC_TCYSLE_LONG;
48 portnum=TDC_TPORTNUM_2; 48 portnum=TDC_TPORTNUM_2;
49 fakenum=TDC_TFAKENUM_2; 49 fakenum=TDC_TFAKENUM_2;
50 50
51 TDC_update_registers(); 51 TDC_update_registers();
52 52
53 output_low(START); 53 output_low(START);
54 output_low(STOP1); 54 output_low(STOP1);
55 output_low(STOP2); 55 output_low(STOP2);
56   56  
57 delay_ms(50); 57 delay_ms(50);
58   58  
59 //write raw register values 59 //write raw register values
60 /* output_low(TDC_ENABLE); 60 /* output_low(TDC_ENABLE);
61 spi_xfer(TDC_stream,0x80338AE8,32); 61 spi_xfer(TDC_stream,0x80338AE8,32);
62 output_high(TDC_ENABLE); 62 output_high(TDC_ENABLE);
63   63  
64 output_low(TDC_ENABLE); 64 output_low(TDC_ENABLE);
65 spi_xfer(TDC_stream,0x81211400,32); 65 spi_xfer(TDC_stream,0x81211400,32);
66 output_high(TDC_ENABLE); 66 output_high(TDC_ENABLE);
67 67
68 output_low(TDC_ENABLE); 68 output_low(TDC_ENABLE);
69 spi_xfer(TDC_stream,0x82E03200,32); 69 spi_xfer(TDC_stream,0x82E03200,32);
70 output_high(TDC_ENABLE); 70 output_high(TDC_ENABLE);
71   71  
72 output_low(TDC_ENABLE); 72 output_low(TDC_ENABLE);
73 spi_xfer(TDC_stream,0x83083300,32); 73 spi_xfer(TDC_stream,0x83083300,32);
74 output_high(TDC_ENABLE); 74 output_high(TDC_ENABLE);
75   75  
76 output_low(TDC_ENABLE); 76 output_low(TDC_ENABLE);
77 spi_xfer(TDC_stream,0x84203400,32); 77 spi_xfer(TDC_stream,0x84203400,32);
78 output_high(TDC_ENABLE); 78 output_high(TDC_ENABLE);
79   79  
80 output_low(TDC_ENABLE); 80 output_low(TDC_ENABLE);
81 spi_xfer(TDC_stream,0x85080000,32); 81 spi_xfer(TDC_stream,0x85080000,32);
82 output_high(TDC_ENABLE); 82 output_high(TDC_ENABLE);
83 */ 83 */
84 TDC_start_cal_resonator(); 84 TDC_start_cal_resonator();
85 delay_ms(50); 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)); 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(); 87 // TDC_start_cal();
88 // delay_ms(50); 88 // delay_ms(50);
89 89
90 while(true) 90 while(true)
91 { 91 {
92 temperature = ds1820_read(); 92 temperature = ds1820_read();
93 93
94 TDC_init(); 94 // TDC_init();
95 95
96 delay_ms(50); 96 delay_ms(50);
97 printf("Temp: %f \r\n", temperature); 97 printf("Temp: %f \r\n", temperature);
98 delay_ms(50); 98 delay_ms(50);
99 printf("status: %LX \r\n", TDC_get_status()); 99 printf("status: %LX \r\n", TDC_get_status());
100   100  
101 TDC_start_cycle(); 101 // TDC_start_cycle();
102   102  
103 // delay_us(10); 103 // delay_us(10);
104 output_high(START); 104 output_high(START);
105 // delay_us(1); 105 // delay_us(1);
106 106
107 delay_us(100); 107 delay_us(100);
108 // output_low(START); 108 // output_low(START);
109 109
110 output_high(STOP1); 110 output_high(STOP1);
111 // delay_us(10); 111 // delay_us(10);
112 // delay_us(500); 112 // delay_us(500);
113 // output_high(STOP1); 113 // output_high(STOP1);
114 // delay_us(10); 114 // delay_us(10);
115 output_low(STOP1); 115 output_low(STOP1);
116 // delay_us(500); 116 // delay_us(500);
117 // output_high(STOP1); 117 // output_high(STOP1);
118 delay_us(10); 118 delay_us(10);
119 // output_low(STOP1); 119 // output_low(STOP1);
120 output_low(START); 120 output_low(START);
121   121  
122 // output_high(STOP2); 122 // output_high(STOP2);
123 // delay_us(10); 123 // delay_us(10);
124 // output_low(START); 124 // output_low(START);
125 output_high(STOP1); 125 output_high(STOP1);
126 // output_low(STOP2); 126 // output_low(STOP2);
127 // delay_us(500); 127 // delay_us(500);
128 // output_high(STOP2); 128 // output_high(STOP2);
129 // delay_us(10); 129 // delay_us(10);
130 output_low(STOP1); 130 output_low(STOP1);
131 // delay_us(500); 131 // delay_us(500);
132 // output_high(STOP2); 132 // output_high(STOP2);
133 // delay_us(10); 133 // delay_us(10);
134 // output_low(STOP2); 134 // output_low(STOP2);
135   135  
136   136  
137 // delay_ms(10); 137 // delay_ms(10);
138 // TDC_start_temp(); 138 TDC_start_temp();
139   139  
140 delay_ms(10); 140 delay_ms(10);
141 printf("status: %LX \r\n", TDC_get_status()); 141 printf("status: %LX \r\n", TDC_get_status());
142 delay_ms(50); 142 delay_ms(50);
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)); 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); 144 delay_ms(500);
145 145
146 }; 146 };
147 } 147 }