📄 f32x_adc0_externalinput_mux.lst
字号:
257 // P0.7 - analog (VREF)
258 //
259 // P1.0 - analog input (ADC0)
260 // P1.1 - analog input (ADC0)
261 // P1.2 - analog input (ADC0)
262 // P1.3 - analog input (ADC0)
263 // P1.4 - analog input (ADC0)
264 // P1.5 - analog input (ADC0)
265 //
266 //-----------------------------------------------------------------------------
267 void Port_Init (void)
268 {
269 1 P1SKIP = 0x3F; // Skip all analog pins
270 1
271 1 XBR0 = 0x01; // UART0 TX and RX pins enabled
272 1 XBR1 = 0x40; // Enable crossbar and weak pull-ups
273 1
274 1 P0MDOUT |= 0x10; // Enable TX0 as a push-pull output
275 1
276 1 P0MDIN &= ~0x80; // P0.7 is analog (VREF)
277 1
278 1 P1MDIN &= ~0x3F; // Set desired pins as analog inputs
279 1 }
280
281 //-----------------------------------------------------------------------------
282 // Timer2_Init
283 //-----------------------------------------------------------------------------
284 //
285 // Return Value : None
286 // Parameters : None
287 //
288 // Configure Timer2 to 16-bit auto-reload and generate an interrupt at 10 us
289 // intervals. Timer2 overflows automatically triggers ADC0 conversion.
290 //
291 //-----------------------------------------------------------------------------
292 void Timer2_Init (void)
293 {
294 1 TMR2CN = 0x00; // Stop Timer2; Clear TF2;
295 1 // use SYSCLK as timebase, 16-bit
296 1 // auto-reload
297 1 CKCON |= 0x10; // Select SYSCLK for timer 2 source
298 1 TMR2RL = 65535 - (SYSCLK / 10000); // Init reload value for 10 us
299 1 TMR2 = 0xffff; // Set to reload immediately
300 1 ET2 = 1; // Enable Timer2 interrupts
301 1 TR2 = 1; // Start Timer2
302 1 }
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 6
303
304 //-----------------------------------------------------------------------------
305 // ADC0_Init
306 //-----------------------------------------------------------------------------
307 //
308 // Return Value : None
309 // Parameters : None
310 //
311 // Configures ADC0 to make single-ended analog measurements on Port 1 according
312 // to the values of <ANALOG_INPUTS> and <PIN_TABLE>.
313 //
314 //-----------------------------------------------------------------------------
315 void ADC0_Init (void)
316 {
317 1 ADC0CN = 0x02; // ADC0 disabled, normal tracking,
318 1 // conversion triggered on TMR2 overflow
319 1
320 1 REF0CN = 0x03; // Enable internal VREF
321 1
322 1 AMX0P = PIN_TABLE[0]; // ADC0 initial positive input = P1.0
323 1 AMX0N = 0x1F; // ADC0 negative input = GND
324 1 // i.e., single ended mode
325 1
326 1 ADC0CF = ((SYSCLK/3000000)-1)<<3; // Set SAR clock to 3MHz
327 1
328 1 ADC0CF |= 0x00; // Right-justify results
329 1
330 1 EIE1 |= 0x08; // Enable ADC0 EOC interrupt
331 1
332 1 AD0EN = 1; // Enable ADC0
333 1 }
334
335 //-----------------------------------------------------------------------------
336 // UART0_Init
337 //-----------------------------------------------------------------------------
338 //
339 // Return Value : None
340 // Parameters : None
341 //
342 // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
343 //
344 //-----------------------------------------------------------------------------
345 void UART0_Init (void)
346 {
347 1 SCON0 = 0x10; // SCON0: 8-bit variable bit rate
348 1 // level of STOP bit is ignored
349 1 // RX enabled
350 1 // ninth bits are zeros
351 1 // clear RI0 and TI0 bits
352 1 if (SYSCLK/BAUDRATE/2/256 < 1) {
353 2 TH1 = -(SYSCLK/BAUDRATE/2);
354 2 CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
355 2 } else if (SYSCLK/BAUDRATE/2/256 < 4) {
356 2 TH1 = -(SYSCLK/BAUDRATE/2/4);
357 2 CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
358 2 CKCON |= 0x01;
359 2 } else if (SYSCLK/BAUDRATE/2/256 < 12) {
360 2 TH1 = -(SYSCLK/BAUDRATE/2/12);
361 2 CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
362 2 } else if (SYSCLK/BAUDRATE/2/256 < 48) {
363 2 TH1 = -(SYSCLK/BAUDRATE/2/48);
364 2 CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 7
365 2 CKCON |= 0x02;
366 2 } else {
367 2 while (1); // Error. Unsupported baud rate
368 2 }
369 1
370 1 TL1 = TH1; // Init Timer1
371 1 TMOD &= ~0xF0; // TMOD: timer 1 in 8-bit autoreload
372 1 TMOD |= 0x20;
373 1 TR1 = 1; // START Timer1
374 1 TI0 = 1; // Indicate TX0 ready
375 1 }
376
377 //-----------------------------------------------------------------------------
378 // Interrupt Service Routines
379 //-----------------------------------------------------------------------------
380
381 //-----------------------------------------------------------------------------
382 // Timer2_ISR
383 //-----------------------------------------------------------------------------
384 //
385 // This routine changes to the next Analog MUX input whenever Timer2 overflows
386 // for the next ADC sample. This allows the ADC to begin setting on the new
387 // input while converting the old input.
388 //
389 void Timer2_ISR (void) interrupt 5
390 {
391 1 TF2H = 0; // Clear Timer2 interrupt flag
392 1
393 1 // Set up the AMUX for the next ADC input
394 1 // ADC0 positive input = P1.<PIN_TABLE[AMUX_INPUT+1]>
395 1 // ADC0 negative input = GND
396 1 // i.e., single ended mode
397 1 if (AMUX_INPUT == (ANALOG_INPUTS - 1))
398 1 {
399 2 AMX0P = PIN_TABLE[0];
400 2 }
401 1 else
402 1 {
403 2 AMX0P = PIN_TABLE[AMUX_INPUT+1];
404 2 }
405 1 }
406
407 //-----------------------------------------------------------------------------
408 // ADC0_ISR
409 //-----------------------------------------------------------------------------
410 //
411 // This ISR averages <INT_DEC> samples for each analog MUX input then prints
412 // the results to the terminal. The ISR is called after each ADC conversion,
413 // which is triggered by Timer2.
414 //
415 //-----------------------------------------------------------------------------
416 void ADC0_ISR (void) interrupt 10
417 {
418 1 static unsigned int_dec = INT_DEC; // Integrate/decimate counter
419 1 // A new result is posted when
420 1 // int_dec is 0
421 1
422 1 // Integrate accumulator for the ADC samples from input pins
423 1 static long accumulator[ANALOG_INPUTS] = 0x00000000;
424 1
425 1 unsigned char i; // Loop counter
426 1
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 8
427 1
428 1 AD0INT = 0; // Clear ADC conversion complete
429 1 // overflow
430 1
431 1
432 1 accumulator[AMUX_INPUT] += ADC0; // Read the ADC value and add it to the
433 1 // running total
434 1
435 1 // Reset sample counter <int_dec> and <AMUX_INPUT> if the final input was
436 1 // just read
437 1 if(AMUX_INPUT == (ANALOG_INPUTS - 1))
438 1 {
439 2 int_dec--; // Update decimation counter
440 2 // when the last of the analog inputs
441 2 // is sampled
442 2
443 2 if (int_dec == 0) // If zero, then post the averaged
444 2 { // results
445 3 int_dec = INT_DEC; // Reset counter
446 3
447 3 // Copy each averaged ADC0 value into the RESULT array
448 3 for(i = 0; i < ANALOG_INPUTS; i++)
449 3 {
450 4 // Copy averaged values into RESULT
451 4 RESULT[i] = accumulator[i] / int_dec;
452 4
453 4 // Reset accumulators
454 4 accumulator[i] = 0x00000000;
455 4 }
456 3 }
457 2
458 2 AMUX_INPUT = 0; // Reset input index back to P1.0
459 2 }
460 1 // Otherwise, increment the AMUX channel counter
461 1 else
462 1 {
463 2 AMUX_INPUT++; // Step to the next analog mux input
464 2 }
465 1 }
466
467 //-----------------------------------------------------------------------------
468 // Support Subroutines
469 //-----------------------------------------------------------------------------
470
471 //-----------------------------------------------------------------------------
472 // Timer0_Init
473 //-----------------------------------------------------------------------------
474 //
475 // Return Value : None
476 // Parameters :
477 // 1) int ms - number of milliseconds to wait
478 // range is positive range of an int: 0 to 32767
479 //
480 // This function configures the Timer0 as a 16-bit timer, interrupt enabled.
481 // Using the internal osc. at 12MHz with a prescaler of 1:8 and reloading the
482 // T0 registers with TIMER0_RELOAD_HIGH/LOW, it will wait for <ms>
483 // milliseconds.
484 // Note: The Timer0 uses a 1:12 prescaler
485 //-----------------------------------------------------------------------------
486 void Timer0_wait(int ms)
487 {
488 1 TH0 = TIMER0_RELOAD_HIGH; // Init Timer0 High register
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 9
489 1 TL0 = TIMER0_RELOAD_LOW ; // Init Timer0 Low register
490 1 TMOD |= 0x01; // Timer0 in 16-bit mode
491 1 CKCON &= 0xFC; // Timer0 uses a 1:12 prescaler
492 1 TR0 = 1; // Timer0 ON
493 1
494 1 while(ms)
495 1 {
496 2 TF0 = 0; // Clear flag to initialize
497 2 while(!TF0); // Wait until timer overflows
498 2 ms--; // Decrement ms
499 2 }
500 1
501 1 TR0 = 0; // Timer0 OFF
502 1 }
503
504 //-----------------------------------------------------------------------------
505 // End Of File
506 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 485 ----
CONSTANT SIZE = 27 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 51 6
IDATA SIZE = 6 ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -