Proyectos Microcontroladores

Menu Principal

Joomla Slide Menu by DART Creations

ADC DEMOQE

Valoración de los usuarios: / 0
PobreEl mejor 

 

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
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
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
 
 
 
unsigned int valor_adc=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 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=0x10| 0x04;
APCTL1=0x01;
ADCSC1=0x20;
 
for(;;) {
 
 
if(ADCSC1_COCO){
valor_adc=ADCR;
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 */
}
 

 

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
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
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
 
 
 
unsigned int valor_adc=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(){
 
valor_adc=ADCR;
}
 
 
 
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;
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 */
}
 

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 */
}
 

 

 

 

 

 

Escribir un comentario

No agredir a los demás compañeros
Evitar comentarios racistas o insultantes


Código de seguridad
Refescar

Buscador ThinkChip

Ingresar

¿Deseas apoyarme?

Enter Amount:

Distribuidores

Banner

Más allá de la ciencia

23:1 Jehová es mi pastor; nada me faltará.23:2 En lugares de delicados pastos me hará descansar;Junto a aguas de reposo me pastoreará.
23:3 Confortará mi alma;Me guiará por sendas de justicia por amor de su nombre.23:4 Aunque ande en valle de sombra de muerte,No temeré mal alguno, porque tú estarás conmigo;Tu vara y tu cayado me infundirán aliento.23:5 Aderezas mesa delante de mí en presencia de mis angustiadores;Unges mi cabeza con aceite; mi copa está rebosando.23:6 Ciertamente el bien y la misericordia me seguirán todos los días de mi vida,Y en la casa de Jehová moraré por largos días.
mod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_counter
mod_vvisit_counterToday39
mod_vvisit_counterYesterday161
mod_vvisit_counterThis week376
mod_vvisit_counterLast week579
mod_vvisit_counterThis month716
mod_vvisit_counterLast month1863
mod_vvisit_counterAll days2579

We have: 1 guests, 1 bots online
Your IP: 38.107.191.102
 , 
Today: Sep 08, 2010