Menu Principal

Joomla Slide Menu by DART Creations

Tarjeta Adquisicion de Datos ITLERMA parte 2

Valoración de los usuarios: / 0
PobreEl mejor 

 

Esta versión del firmware, tiene las siguientes caracteristicas:

  • Comunicación USB tipo HID
  • Comunicación Serial RS232
  • Lectura de 4 canales ADC en 12 bits y 8 bits
  • Señal PWM de salida
  • Control de Led para alerta visual.

La logica del programa es el siguiente:

Free Image Hosting at www.ImageShack.us

 

El firmware realizado para este proyecto se divide en varios archivos, los cuales contienen las funciones que se utilizan en el main principal.

 

Free Image Hosting at www.ImageShack.us

Como podemos ver, esta la carpeta donde se encuentra el stack USB, llamada "Usb_Drv", una esta incluida en los archivos de cabecera, y la otra donde se encuentran las funciones en C, ademas de esta libreria es necesaria el USB_User_API.c y USB_User_Api.h, para poder llamar a las funciones que transfieren la informacion por los endpoints. Esta las librerias para poder controlar los perifericos ADC, TPM, SCI.

El archivo ADC.h y ADC.c contiene las funciones necesarias para configurar los canales ADC y obtener los datos de los canales ADC ya sea para 12 bits o 8 bits.

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
#include "ADC.h"
 
 
 
void ADC_Init(void)
{
ADCCFG = 0x00;//0x04 mode 12 bit resolution
ADCSC2 = 0x00;//software triggered mode, compare mode disable
ADCSC1 = 0x00;//interrupt disable mode
APCTL1 = APCTL1_ADPC3_MASK| //channel 3
APCTL1_ADPC2_MASK| //channel 2
APCTL1_ADPC1_MASK| //channel 1
APCTL1_ADPC0_MASK; //channel 0
}
 
void ADC_Init_12bit(void) {
 
ADCCFG = 0x04;//0x04 mode 12 bit resolution
ADCSC2 = 0x00;//software triggered mode, compare mode disable
ADCSC1 = 0x00;//interrupt disable mode
APCTL1 = APCTL1_ADPC3_MASK| //channel 3
APCTL1_ADPC2_MASK| //channel 2
APCTL1_ADPC1_MASK| //channel 1
APCTL1_ADPC0_MASK; //channel 0
 
 
}
 
 
UINT8 ReadADCH(UINT8 u8Channel)
{
ADCSC1 = u8Channel;
while(!(ADCSC1 & 0x80));
return ADCRL ; //8-bit value stored.
}
 
 
UINT16 ReadADCH_12bit(UINT8 u8Channel){
 
ADCSC1=u8Channel;
while(!(ADCSC1 & 0x80));
min= ADCRL;
max= ADCRH;
return ADCR;
 
}

El archivo sci.h y sci.c contiene las funciones necesarias para configurar nuestro puerto serial, a que velocidad, con interrupción o sin ella, ademas de poder leer y escribir un dato o cadena de datos.

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/***************************************************************************
**
**
**  sci.c
**  functions definitions
**
**
***************************************************************************/ 
 
 #include "derivative.h" 
 #include "sci.h"
 #include "string.h"
 
 
 
void SCI1_Init(long Br, char Stopb, char Parityb, char Datal,unsigned char interTX,unsigned char interRX);
void SCI2_Init(long Br, char Stopb, char Parityb, char Datal,unsigned char interTX,unsigned char interRX);
 
void SCI1_PutChar(char send); 
char SCI1_GetChar(void); 
 
void SCI2_PutChar(char send); 
char SCI2_GetChar(void); 
 
void SCI2_send_string(char *string);
void SCI1_send_string(char *string);
 
 
void bin_to_string(unsigned int value, char *str);
 
 
 
 void SCI1_Init(long Br, char Stopb, char Parityb, char Datal,unsigned char interTX,unsigned char interRX) 
 {
  int Baudrate;
 
  Baudrate =  (int)((long)BUS_CLK_SCI/16/Br);
 
  if(Baudrate < 1)
    Baudrate = 1;
 
  SCI1BD = Baudrate;
 
  SCI1C1 = 0x00;
 
  SCI1C2 = 0x0C;
 
 
  if(Parityb == 1) //odd
  {
    SCI1C1_PE = 1;
    SCI1C1_PT = 1;
    SCI1C1_M  = 1;
  } 
  else 
  {
    if(Parityb == 2) //even
    {
     SCI1C1_PE = 1;
     SCI1C1_PT = 0;
     SCI1C1_M  = 1;
    } 
    else                   //none
    {
      SCI1C1_PE = 0;
      SCI1C1_PT = 0; 
      SCI1C1_M  = 0;
    }
  }
 
  if((Datal != 8) || (Stopb != 1)) 
  {
    return;
  }
 
  if(interTX){
   TRANSMIT_INTERRUPT_ENABLE1=1;
  }
 
  if(interRX){
   RECEIVER_INTERRUPT_ENABLE1=1;    
  }
 
  return;
 }
 
 
void SCI1_PutChar(char send) 
{
  char dummy;
 
  while(!SCI1S1_TDRE);
  dummy = SCI1S1;
  SCI1D  = send;  
  return;  
}
 
char SCI1_GetChar(void) 
{
  char dummy;
 
  while(!SCI1S1_RDRF);
  dummy = SCI1S1;
  return SCI1D;  
}
 
 
void SCI2_Init(long Br, char Stopb, char Parityb, char Datal,unsigned char interTX,unsigned char interRX) 
 {
  int Baudrate;
 
  Baudrate =  (int)((long)BUS_CLK_SCI/16/Br);
 
  if(Baudrate < 1)
    Baudrate = 1;
 
  SCI2BD = Baudrate;
 
  SCI2C1 = 0x00;
 
  SCI2C2 = 0x0C;
 
 
  if(Parityb == 1) //odd
  {
    SCI2C1_PE = 1;
    SCI2C1_PT = 1;
    SCI2C1_M  = 1;
  } 
  else 
  {
    if(Parityb == 2) //even
    {
     SCI2C1_PE = 1;
     SCI2C1_PT = 0;
     SCI2C1_M  = 1;
    } 
    else                   //none
    {
      SCI2C1_PE = 0;
      SCI2C1_PT = 0; 
      SCI2C1_M  = 0;
    }
  }
 
  if((Datal != 8) || (Stopb != 1)) 
  {
    return;
  }
 
  if(interTX){
   TRANSMIT_INTERRUPT_ENABLE2=1;
  }
 
  if(interRX){
   RECEIVER_INTERRUPT_ENABLE2=1;    
  }
 
  return;
 }
 
 
void SCI2_PutChar(char send) 
{
  char dummy;
 
  while(!SCI2S1_TDRE);
  dummy = SCI2S1;
  SCI2D  = send;  
  return;  
}
 
char SCI2_GetChar(void) 
{
  char dummy;
 
  while(!SCI2S1_RDRF);
  dummy = SCI2S1;
  return SCI2D;  
}
 
 
void SCI1_send_string(char *string){
 while (*string){
 
 while (!FLAG_TRANSMIT_DATA1);
 SCI1D=*string;
 string++; 
 }
}
 
void SCI2_send_string(char *string){
while(*string){
 
 while (!FLAG_TRANSMIT_DATA2);
 SCI2D=*string;
 string++; 
}
}
 
 
 
 
// Converts a 16-bit value into a ASCII string
// "value" is the 16-bit value
// "*str" is a pointer to the string
// This is a very simple way to convert a number to decimal format
void bin_to_string(unsigned int value, char *str)
{
  char aux;
  unsigned int unit;
  aux = 0;
  str[0] = str[1] = str[2] = str[3] = str[4] = '0';
  str[5] = 0;
  unit = 10000;
  while (value)
  {
    if (value>=unit)
    {
      value -= unit;
      str[aux]++;
    } else
    {
      aux++;
      unit /= 10;
    }    
  }
  while (str[0]=='0')
  {
    for (aux=0; aux<=4; aux++) str[aux]=str[aux+1];
  }
}
 

 


El archivo tpm.h y tpm.c contiene las funciones necesarias para manejar la salida de señal PWM, iniciarla o pararla, asi como configurar la frecuencia y señal de ancho de pulso.

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
#define TPM_INPUT_CLK   15000     //1.5MHz
 
void TPM_Init(void);
char TPM_Config(char Freq, char Duty);
void TPM_Stop(void);
void TPM_Start(void);
 
 
/**********************************************************************************************
 * TPM_Init: This function initilizes the TPM module
 *
 * Parameters: none
 *
 *
 * Return: void
 *********************************************************************************************/
void TPM_Init(void)
{
TPM1SC = 0x00;
TPM2SC = 0x00;
 
return;
}
 
 
/**********************************************************************************************
 * TPM_Init: This function initilizes the TPM module
 *
 * Parameters: Freq: (Hundred Hz) Range: 1-1000
 * Duty: 0-100%
 *
 * Note: Frequency = Freq * 100 (100Hz-100KHz)
 *
 * Return: 0: failed 1: success
 *********************************************************************************************/
char TPM_Config(char Freq, char Duty)
{
unsigned int Mod = TPM_INPUT_CLK;
unsigned long Cnt = 0x00;
 
if(Freq > 255)
return 0;
else
Mod /= Freq;
 
TPM1MOD = Mod;
 
TPM1C2SC = 0x24;
 
Cnt = (unsigned long)(Mod * Duty);
Cnt /= 100;
 
TPM1C2V = (int)Cnt;
 
return 1;
}
 
 
/**********************************************************************************************
 * TPM_Init: This function initilizes the TPM module
 *
 * Parameters: Num : 0 TPM1; 1: TPM2
 *
 *
 * Return: void
 *********************************************************************************************/
void TPM_Start(void)
{
TPM1CNT = 0;
TPM1SC |= 0x0C; //bus clock, divided by 8
 
return;
}
 
 
 
/**********************************************************************************************
 * TPM_Init: This function initilizes the TPM module
 *
 * Parameters: none
 *
 *
 * Return: void
 *********************************************************************************************/
void TPM_Stop(void)
{
char Dummy;
 
Dummy = TPM1SC;
TPM1SC &= 0x67; //bus clock, divided by 8
 
return;
}
 

El archivo MCU.h y MCU.c contiene las funciones necesarias para configurar correctamente el modulo de la señal del reloj, la cual esta en 48MHz para el cpu, y con un BUS CLOCK de 24 MHz, configuramos el pin como salida, para el led de la tarjeta.

 

En el archivo main podemos ver el siguiente codigo:

main.c
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
/*****************************************************************************
 
  ing braulio elias chi salavarria
  date 24/12/09
  firmware v1.0
  datalogger itl think chip
 
******************************************************************************/
 
 
 
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
 
 
 
////table of includes of my program//////
#include "MCU.h"
#include "ADC.h"
#include "sci.h"
#include "tpm.h"
 
 
//includes USB driver//
#include "Usb_Drv.h" /* USB Main Driver */
#include "Usb_Config.h" /* USB Configuration header */
#include "FSLTypes_File.h" /* FSL Standar Definitions */
#include "USB_User_API.h" /* USB API for USB Module */
//These are user defined commands.
#define PWM_ON 0x20
#define PWM_OFF 0x30
#define PWM_F 0x40
#define PWM_D 0x50
#define LED_ON 0x60
#define LED_OFF 0x70
 
#define ADC_12BITS 0xC0
#define ADC_8BITS 0xC1
#define READ_ANALOG_DATA_8 0xD0
#define READ_ANALOG_DATA_12_CH0 0xE0
#define READ_ANALOG_DATA_12_CH1 0xE1
#define READ_ANALOG_DATA_12_CH2 0xE2
#define READ_ANALOG_DATA_12_CH3 0xE3
 

podemos ver como se incluyen los archivos, y tambien las definiciones, estas definiciones, son las que enviaremos para controlar que hacer, por ejemplo, si queremos que nos lea los datos del ADC en 8 bits, mandaremos el codigo 0xC1 o tambien podemos hacer la definición de ese numero como ADC_8BITS.

Atrás...                                                                                                                                                                                                                                  Siguiente...

 

 

 

 

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

16Porque de tal manera amó Dios al mundo, que ha dado a su Hijo unigénito, para que todo aquel que en él cree, no se pierda, mas tenga vida eterna.

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.101
 , 
Today: Sep 08, 2010