En estas practicas, veremos como utilizar el periferico ADC del microcontrolador MC9S08QE128, este microcontrolador tiene una resolución de 12 bits, por lo tanto tenemos una precisión de 4096 bits.
El primer ejercicio, nos enseñar la configuración más sencilla, con la tarjeta DEMOQE moveremos el potenciometro, y veremos como se encienden y apagan los leds, dependiendo del valor ADC en el canal PTA0:
1 |
#include <hidef.h> /* for EnableInterrupts macro */ |
Para la siguiente practica, se realiza la misma acción, pero ahora lo hacemos mediante la interrupció ADC, la cual vemos configurada en el registro ADCSC1 habilitando el bit 0x04, o mejor dicho AIEN, para mejor referencia, leer el datasheet del MC9S08QE128.
1 |
#include <hidef.h> /* for EnableInterrupts macro */ |
Ahora veremos como realizar la lectura de más de un canal ADC, para ello, podremos leer dos canales, el primero es el canal AD0 conectado al potenciometro de la tarjeta, el segundo es AD1 conectado al eje X del acelerometro de la tarjeta (MMA7260QT), por lo que veremos como cambian las variables, cuando en la ventana de debug pongamos en la opción mode-->Periodical--> tiempo de muestreo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */ unsigned int valor_adc=0; unsigned int valor_adc2=0; void Init_Config_Clock(void){ /* Common initialization of the write once registers */ /* SOPT1: COPE=0,COPT=1,STOPE=0,RSTOPE=0,BKGDPE=1,RSTPE=0 */ SOPT1 = 0x42; /* SPMSC1: LVDF=0,LVDACK=0,LVDIE=0,LVDRE=1,LVDSE=1,LVDE=1,BGBE=0 */ SPMSC1 = 0x1C; /* SPMSC2: LPR=0,LPRS=0,LPWUI=0,PPDF=0,PPDACK=0,PPDE=1,PPDC=0 */ SPMSC2 = 0x02; /* SPMSC3: LVDV=0,LVWV=0,LVWIE=0 */ SPMSC3 &= (unsigned char)~0x38; /* System clock initialization */ if (*(unsigned char*far)0xFFAF != 0xFF) { /* Test if the device trim value is stored on the specified address */ ICSTRM = *(unsigned char*far)0xFFAF; /* Initialize ICSTRM register from a non volatile memory */ ICSSC = (unsigned char)((*(unsigned char*far)0xFFAE) & (unsigned char)0x01); /* Initialize ICSSC register from a non volatile memory */ } /* ICSC1: CLKS=0,RDIV=0,IREFS=0,IRCLKEN=0,IREFSTEN=0 */ ICSC1 = 0x00; /* Initialization of the ICS control register 1 */ /* ICSC2: BDIV=0,RANGE=0,HGO=0,LP=0,EREFS=1,ERCLKEN=0,EREFSTEN=0 */ ICSC2 = 0x04; /* Initialization of the ICS control register 2 */ while(!ICSSC_OSCINIT) { /* Wait until the initialization of the external crystal oscillator is completed */ } /* ICSSC: DRST_DRS=0,DMX32=0 */ ICSSC &= (unsigned char)~0xE0; /* Initialization of the ICS status and control */ while((ICSSC & 0xC0) != 0x00) { /* Wait until the FLL switches to Low range DCO mode */ } /* SCGC1: TPM3=1,TPM2=1,TPM1=1,ADC=1,IIC2=1,IIC1=1,SCI2=1,SCI1=1 */ SCGC1 = 0xFF; /* SCGC2: DBG=1,FLS=1,IRQ=1,KBI=1,ACMP=1,RTC=1,SPI2=1,SPI1=1 */ SCGC2 = 0xFF; } void interrupt VectorNumber_Vadc ADC_isr(){ if(ADCSC1 & 0x1F){ valor_adc2=ADCR; ADCSC1= 0x40; //siguiente canal cambiamos a AD0 }else{ valor_adc=ADCR; ADCSC1 = 0x041; //siguiente canal cambiamos a AD1 } } void main(void) { EnableInterrupts; /* enable interrupts */ /* include your code here */ Init_Config_Clock(); //configuramos el reloj para que trabaje a 8 MHz con un cristal de 32.768KHz PTCDD= 0x3F; //configuramos como salida el puerto C los pines C0 a C5, para salida son 1, para entrada 0 PTCD=0xFF; ADCCFG=0x40|0x10| 0x04|0x01; APCTL1=0x01|0x02; ADCSC1=0x40|0x20; for(;;) { if(valor_adc>1000){ PTCD_PTCD0=0; } else{ PTCD_PTCD0=1; } if(valor_adc>4000){ PTCD_PTCD1=0; } else{ PTCD_PTCD1=1; } } /* loop forever */ /* please make sure that you never leave main */ } |















