📄 temp_3.lst
字号:
164
165 //-----------------------------------------------------------------------------
166 // PORT_Init
167 //-----------------------------------------------------------------------------
168 //
169 // This routine configures the crossbar and GPIO ports.
170 //
171 void PORT_Init (void)
172 {
173 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
174 1
175 1 SFRPAGE = CONFIG_PAGE; // set SFR page
176 1
177 1 XBR0 = 0x00;
178 1 XBR1 = 0x00;
179 1 XBR2 = 0x44; // Enable crossbar and weak pull-up
C51 COMPILER V8.02 TEMP_3 03/14/2008 17:50:21 PAGE 4
180 1 // Enable UART1
181 1
182 1 P0MDOUT |= 0x01; // Set TX1 pin to push-pull
183 1 P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
184 1
185 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
186 1 }
187
188 //-----------------------------------------------------------------------------
189 // UART1_Init
190 //-----------------------------------------------------------------------------
191 //
192 // Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
193 //
194 void UART1_Init (void)
195 {
196 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
197 1
198 1 SFRPAGE = UART1_PAGE;
199 1 SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
200 1
201 1 SFRPAGE = TIMER01_PAGE;
202 1 TMOD &= ~0xF0;
203 1 TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
204 1
205 1
206 1 if (SYSCLK/BAUDRATE/2/256 < 1) {
207 2 TH1 = -(SYSCLK/BAUDRATE/2);
208 2 CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
209 2 } else if (SYSCLK/BAUDRATE/2/256 < 4) {
210 2 TH1 = -(SYSCLK/BAUDRATE/2/4);
211 2 CKCON &= ~0x13; // Clear all T1 related bits
212 2 CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
213 2 } else if (SYSCLK/BAUDRATE/2/256 < 12) {
214 2 TH1 = -(SYSCLK/BAUDRATE/2/12);
215 2 CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
216 2 } else {
217 2 TH1 = -(SYSCLK/BAUDRATE/2/48);
218 2 CKCON &= ~0x13; // Clear all T1 related bits
219 2 CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
220 2 }
221 1
222 1 TL1 = TH1; // initialize Timer1
223 1 TR1 = 1; // start Timer1
224 1
225 1 SFRPAGE = UART1_PAGE;
226 1 TI1 = 1; // Indicate TX1 ready
227 1
228 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
229 1
230 1 }
231
232 //-----------------------------------------------------------------------------
233 // ADC0_Init
234 //-----------------------------------------------------------------------------
235 //
236 // Configure ADC0 to use Timer3 overflows as conversion source, to
237 // generate an interrupt on conversion complete, and to use left-justified
238 // output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
239 //
240 void ADC0_Init (void)
241 {
C51 COMPILER V8.02 TEMP_3 03/14/2008 17:50:21 PAGE 5
242 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
243 1
244 1 SFRPAGE = ADC0_PAGE;
245 1
246 1 ADC0CN = 0x05; // ADC0 disabled; normal tracking
247 1 // mode; ADC0 conversions are initiated
248 1 // on overflow of Timer3; ADC0 data is
249 1 // left-justified
250 1 REF0CN = 0x07; // enable temp sensor, on-chip VREF,
251 1 // and VREF output buffer
252 1 AMX0SL = 0x0f; // Select TEMP sens as ADC mux output
253 1 ADC0CF = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz
254 1 ADC0CF |= 0x01; // PGA gain = 2
255 1
256 1 EIE2 |= 0x02; // enable ADC interrupts
257 1
258 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
259 1 }
260
261 //-----------------------------------------------------------------------------
262 // Timer3_Init
263 //-----------------------------------------------------------------------------
264 //
265 // Configure Timer3 to auto-reload at interval specified by <counts> (no
266 // interrupt generated) using SYSCLK as its time base.
267 //
268 void Timer3_Init (int counts)
269 {
270 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
271 1
272 1 SFRPAGE = TMR3_PAGE;
273 1
274 1 TMR3CN = 0x00; // Stop Timer3; Clear TF3;
275 1 TMR3CF = 0x08; // use SYSCLK as timebase
276 1
277 1 RCAP3 = -counts; // Init reload values
278 1 TMR3 = RCAP3; // set to reload immediately
279 1 EIE2 &= ~0x01; // disable Timer3 interrupts
280 1 TR3 = 1; // start Timer3
281 1
282 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
283 1 }
284
285 //-----------------------------------------------------------------------------
286 // Interrupt Service Routines
287 //-----------------------------------------------------------------------------
288
289 //-----------------------------------------------------------------------------
290 // ADC0_ISR
291 //-----------------------------------------------------------------------------
292 //
293 // ADC0 end-of-conversion ISR
294 // Here we take the ADC0 sample, add it to a running total <accumulator>, and
295 // decrement our local decimation counter <int_dec>. When <int_dec> reaches
296 // zero, we post the decimated result in the global variable <result>.
297 //
298 void ADC0_ISR (void) interrupt 15
299 {
300 1 static unsigned int_dec=INT_DEC; // integrate/decimate counter
301 1 // we post a new result when
302 1 // int_dec = 0
303 1 static long accumulator=0L; // here's where we integrate the
C51 COMPILER V8.02 TEMP_3 03/14/2008 17:50:21 PAGE 6
304 1 // ADC samples
305 1
306 1 AD0INT = 0; // clear ADC conversion complete
307 1 // indicator
308 1
309 1 accumulator += ADC0; // read ADC value and add to running
310 1 // total
311 1 int_dec--; // update decimation counter
312 1
313 1 if (int_dec == 0) { // if zero, then post result
314 2 int_dec = INT_DEC; // reset counter
315 2 result = accumulator >> 8;
316 2 accumulator = 0L; // reset accumulator
317 2 }
318 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 470 ----
CONSTANT SIZE = 27 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 10 8
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -