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:
El firmware realizado para este proyecto se divide en varios archivos, los cuales contienen las funciones que se utilizan en el main principal.
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 |
#include "ADC.h" |
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 |
#define TPM_INPUT_CLK 15000 //1.5MHz |
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 |
/***************************************************************************** |
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.
















