📄 da0_da3.lst
字号:
261 1
262 1 }
263
264
265 //-----------------------------------------------------------------------------
266 // PORT_Init
267 //-----------------------------------------------------------------------------
268 //
269 // Return Value : None
270 // Parameters : None
271 //
272 // This function configures the crossbar and GPIO ports.
273 //
274 // P0.0 digital push-pull UART TX
275 // P0.1 digital open-drain UART RX
276 // P1.6 digital push-pull LED
277 // AIN0.1 analog Analog input (no configuration necessary)
278 //-----------------------------------------------------------------------------
279 void PORT_Init (void)
280 {
281 1 XBR0 = 0x04; // Route UART0 to crossbar
282 1 XBR2 |= 0x40; // Enable crossbar, weak pull-ups
283 1 P0MDOUT |= 0x01; // enable TX0 as a push-pull output
284 1 P1MDOUT |= 0x40; // enable LED as push-pull output
285 1
286 1
287 1 P0MDOUT |= 0x01; // Set TX1 pin to push-pull
288 1 P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
289 1
290 1 }
291
292 //-----------------------------------------------------------------------------
293 // UART0_Init
294 //-----------------------------------------------------------------------------
295 //
296 // Return Value : None
297 // Parameters : None
298 //
299 // Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
300 //
301 //-----------------------------------------------------------------------------
302 void UART0_Init (void)
303 {
C51 COMPILER V8.02 DA0_DA3 07/14/2008 16:53:08 PAGE 6
304 1 SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
305 1 TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
306 1 TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
307 1 TR1 = 1; // start Timer1
308 1 CKCON |= 0x10; // Timer1 uses SYSCLK as time base
309 1 PCON |= 0x80; // SMOD00 = 1
310 1 TI0 = 1; // Indicate TX0 ready
311 1 }
312
313 //-----------------------------------------------------------------------------
314 // ADC0_Init
315 //-----------------------------------------------------------------------------
316 //
317 // Return Value : None
318 // Parameters : None
319 //
320 // Configure ADC0 to use Timer3 overflows as conversion source, to
321 // generate an interrupt on conversion complete, and to use right-justified
322 // output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
323 //
324 //-----------------------------------------------------------------------------
325 void ADC0_Init (void)
326 {
327 1
328 1 ADC0CN = 0x04; // ADC0 disabled; normal tracking
329 1 // mode; ADC0 conversions are initiated
330 1 // on overflow of Timer3; ADC0 data is
331 1 // right-justified
332 1
333 1 REF0CN = 0x07; // Enable temp sensor, on-chip VREF,
334 1 // and VREF output buffer
335 1
336 1 AMX0CF = 0x00; // AIN inputs are single-ended (default)
337 1
338 1 AMX0SL = 0x00; // Select AIN0.0 pin as ADC mux input
339 1 // ISR will change this to step through
340 1 // inputs
341 1
342 1 ADC0CF = (SYSCLK/SAR_CLK) << 3; // ADC conversion clock = 2.5MHz
343 1 ADC0CF |= 0x00; // PGA gain = 1 (default)
344 1
345 1 EIE2 |= 0x02; // enable ADC interrupts
346 1
347 1 }
348
349 //-----------------------------------------------------------------------------
350 // TIMER3_Init
351 //-----------------------------------------------------------------------------
352 //
353 // Return Value : None
354 // Parameters :
355 // 1) none
356 //
357 // Configure Timer3 to auto-reload at interval specified by <counts> (no
358 // interrupt generated) using SYSCLK as its time base.
359 //
360 //-----------------------------------------------------------------------------
361 void TIMER3_Init (void)
362 {
363 1
364 1 TMR3CN = 0x02; // Stop Timer3; Clear TF3; set sysclk
365 1 // as timebase
C51 COMPILER V8.02 DA0_DA3 07/14/2008 16:53:08 PAGE 7
366 1
367 1 RCAP3 = 65535 -(SYSCLK / 50000); // Init reload values for 20uS
368 1 TMR3 = RCAP3; // Set to reload immediately
369 1 EIE2 |= 0x01; // Disable Timer3 interrupts
370 1 TMR3CN |= 0x04; // start Timer3
371 1
372 1 }
373
374
375 //-----------------------------------------------------------------------------
376 // Interrupt Service Routines
377 //-----------------------------------------------------------------------------
378
379 //-----------------------------------------------------------------------------
380 // ADC0_ISR
381 //-----------------------------------------------------------------------------
382 //
383 // This ISR is called when the ADC0 completes a conversion. Each value is
384 // added to a running total <accumulator>, and the local decimation counter
385 // <int_dec> decremented. When <int_dec> reaches zero, we post the decimated
386 // result in the global variable <Result[]>.
387 //
388 // The analog input is sampled, held, and converted on a Timer2 overflow. To
389 // maximize input settling time, the analog mux is also advanced to the next
390 // input on the Timer2 overflow. Two different indices are held globally:
391 // amux_convert: index of the analog input undergoing conversion
392 // amux_input: index of the analog input selected in the analog
393 // multiplexer
394 //
395 //
396 //-----------------------------------------------------------------------------
397 void ADC0_ISR (void) interrupt 15
398 {
399 1
400 1 static unsigned int_dec=INT_DEC; // Integrate/decimate counter
401 1 // we post a new result when
402 1 // int_dec = 0
403 1
404 1 static long accumulator[ANALOG_INPUTS] ={0L};
405 1 // Here's where we integrate the
406 1 // ADC samples from input AIN0.0
407 1 unsigned char i;
408 1
409 1 AD0INT = 0; //clear ADC conversion complete overflow
410 1
411 1 accumulator[amux_convert] += ADC0; // Read ADC value and add to running
412 1 // total
413 1
414 1 if(amux_convert == (ANALOG_INPUTS-1))// reset input index if the last input
415 1 //was just read
416 1 {
417 2 int_dec--; // Update decimation counter
418 2 // when last of the analog inputs
419 2 // sampled
420 2 }
421 1
422 1 if (int_dec == 0) // If zero, then post result
423 1 {
424 2 int_dec = INT_DEC; // Reset counter
425 2
426 2 for(i=0; i<ANALOG_INPUTS; i++)
427 2 {
C51 COMPILER V8.02 DA0_DA3 07/14/2008 16:53:08 PAGE 8
428 3 Result[i] = accumulator[i] >> 8; //Copy decimated values into Result
429 3 accumulator[i] = 0L; // Reset accumulators
430 3 }
431 2 }
432 1
433 1 amux_convert = amux_input; // now that conversion results are
434 1 // stored, advance index to the analog
435 1 // input currently selected on the mux
436 1
437 1 LED = 1;
438 1
439 1 }
440
441 //-----------------------------------------------------------------------------
442 // TIMER4_ISR
443 //-----------------------------------------------------------------------------
444 //
445 // The timer4 overflow triggers the ADC0 conversion on the analog MUX input
446 // previously selected. It is permissable to change the analog MUX
447 // input once conversion has started, as the ADC has an internal sample
448 // and hold.
449 //
450 // This ISR routine will then select the next analog MUX input so as to
451 // maximize the settling time.
452 //
453 //-----------------------------------------------------------------------------
454
455 void TIMER3_ISR(void) interrupt 14
456 {
457 1
458 1 TMR3CN &= ~0x80; // acknowledge interrupt
459 1
460 1 amux_input ++; // step to the next analog mux input
461 1
462 1 if(amux_input == ANALOG_INPUTS) // reset input index if the last input
463 1 { // was just read
464 2 amux_input=0; // reset input index back to AIN0.0
465 2 }
466 1
467 1 AMX0SL = amux_input; // select the next input on the analog
468 1 // multiplexer
469 1
470 1 LED = 0;
471 1 }
472
473 //-----------------------------------------------------------------------------
474 // Support Subroutines
475 //-----------------------------------------------------------------------------
476 //-----------------------------------------------------------------------------
477 // Wait_MS
478 //-----------------------------------------------------------------------------
479 //
480 // Return Value : None
481 // Parameters:
482 // 1) unsigned int ms - number of milliseconds of delay
483 // range is full range of integer: 0 to 65335
484 //
485 // This routine inserts a delay of <ms> milliseconds.
486 //
487 //-----------------------------------------------------------------------------
488 void Wait_MS(unsigned int ms)
489 {
C51 COMPILER V8.02 DA0_DA3 07/14/2008 16:53:08 PAGE 9
490 1
491 1 CKCON &= ~0x20; // use SYSCLK/12 as timebase
492 1
493 1 RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
494 1 TMR2 = RCAP2;
495 1
496 1 ET2 = 0; // Disable Timer 2 interrupts
497 1
498 1 TR2 = 1; // Start Timer 2
499 1
500 1 while(ms)
501 1 {
502 2 TF2 = 0; // Clear flag to initialize
503 2 while(!TF2); // Wait until timer overflows
504 2 ms--; // Decrement ms
505 2 }
506 1
507 1 TR2 = 0; // Stop Timer 2
508 1
509 1 }
510
511 //-----------------------------------------------------------------------------
512 // End Of File
513 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 463 ----
CONSTANT SIZE = 28 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 68 5
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 + -