00001 /** \file firmware_irele485-628.c.cIr a la documentación de este archivo.00001 /** \file firmware_iRELE485-628.c 00002 * \brief Este fichero contiene el cuerpo principal del Firmware del\n 00003 * dispositivo auxiliar satélite de la iACD V1.0.4. denominado iRELE485-628 00004 * 00005 * Integra todos los subsistemas y funciones implementados.\n 00006 * Source para el compilador CCS C v.3.242 00007 * 00008 * Microprocesador : <b>16F628A</b> (18 pines PDIP) \n\n 00009 * Fuses utilizados: \n 00010 * \li <b>INTRC</b> Internal RC Osc 00011 * \li <b>MCLR</b> Master Clear pin enabled 00012 * \li <b>NOWDT</b> No Watch Dog Timer 00013 * \li <b>NOPROTECT</b> Code not protected from reading 00014 * \li <b>NOPUT</b> No Power Up Timer 00015 * \li <b>NOBROWNOUT</b> No brownout reset 00016 * \li <b>NOLVP</b> No low voltage prgming, B3(PIC16) 00017 * \li <b>NOCPD</b> No EE protection 00018 * 00019 * \author Diego Márquez García-Cuervo \n 00020 * <http://picmania.garcia-cuervo.net> 00021 * 00022 * \version 1.0.0.A 00023 */ 00024 00025 /////////////////////////////////////////////////////////////////////////////////////////////////// 00026 // 00027 // Definiciones estándar del 16F628A 00028 // 00029 /////////////////////////////////////////////////////////////////////////////////////////////////// 00030 00031 #include <16F628A.h> 00032 00033 /////////////////////////////////////////////////////////////////////////////////////////////////// 00034 // 00035 // Fuses y ajuste de Clock 00036 // 00037 /////////////////////////////////////////////////////////////////////////////////////////////////// 00038 00039 #fuses INTRC,MCLR,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NOCPD 00040 #use delay(clock=4000000) 00041 00042 /////////////////////////////////////////////////////////////////////////////////////////////////// 00043 // 00044 // Defines y Constantes 00045 // 00046 /////////////////////////////////////////////////////////////////////////////////////////////////// 00047 00048 /*! \def DRIVER_RELE 00049 * Pin accionador del Relé número 1 \li HIGH : Relé ON. 00050 */ 00051 #define DRIVER_RELE PIN_B4 00052 00053 /*! \def TX_485_ENABLE 00054 * Pin que habilita la transmisión por el canal RS485 00055 * \li LOW Habilita recepción RS485 00056 * \li HIGH Habilita transmisión RS485. 00057 */ 00058 #define TX_485_ENABLE PIN_B5 00059 00060 /*! \def bytes_for_USART_buffer 00061 * \brief Establece el número de bytes de longitud del buffer de la USART. 00062 */ 00063 #define bytes_for_USART_buffer 12 00064 00065 /*! \var const char VERSION 00066 * \brief Versión del Firmware. 00067 */ 00068 const char VERSION[]="1.0.0\0"; 00069 00070 /*! \var const char WhatAmI 00071 * \brief Identificador de tipo de dispositivo: 'R' Relé. 00072 */ 00073 const char WhatAmI = 'R'; 00074 00075 /*! \var const char WhoAmI 00076 * \brief Identificador de dispositivo. 00077 */ 00078 const char WhoAmI = '1'; 00079 00080 /*! \var const char Command_Execute 00081 * \brief Comando que indica ejecución de acción : Activar Relé 00082 */ 00083 const char Command_Execute = 'X'; 00084 00085 /*! \var const char end_of_transmit 00086 * \brief Carácter que indica fin de transmisión (no buffereable) 00087 */ 00088 const char end_of_transmit = '!'; 00089 00090 /////////////////////////////////////////////////////////////////////////////////////////////////// 00091 // 00092 // Canal de Comunicación 00093 // 00094 /////////////////////////////////////////////////////////////////////////////////////////////////// 00095 00096 #use rs232(baud=9600,xmit=PIN_B2,rcv=PIN_B1) 00097 00098 /////////////////////////////////////////////////////////////////////////////////////////////////// 00099 // 00100 // Prototipos de Funciones 00101 // 00102 /////////////////////////////////////////////////////////////////////////////////////////////////// 00103 00104 void USART_activa_tx(void); 00105 void USART_activa_rx(void); 00106 void USART_add_to_buffer(char c); 00107 00108 /////////////////////////////////////////////////////////////////////////////////////////////////// 00109 // 00110 // R A M : Variables Globales Estáticas 00111 // 00112 /////////////////////////////////////////////////////////////////////////////////////////////////// 00113 00114 /*! \var USART_buffer 00115 * \brief Buffer de la USART 00116 */ 00117 static char USART_buffer[bytes_for_USART_buffer]; 00118 00119 /*! \var USART_nextRec 00120 * \brief Indice del Buffer de la USART 00121 */ 00122 static int8 USART_nextRec; 00123 00124 /** \var Command 00125 * \brief Comando válido para ejecutar. 00126 */ 00127 int8 Command; 00128 00129 /////////////////////////////////////////////////////////////////////////////////////////////////// 00130 // 00131 // Funciones 00132 // 00133 /////////////////////////////////////////////////////////////////////////////////////////////////// 00134 00135 /** \brief Abre el canal RS485 para transmitir. 00136 * \return void 00137 */ 00138 void USART_activa_tx(void){ 00139 output_high(TX_485_ENABLE); 00140 delay_ms(5); 00141 } 00142 00143 /** \brief Abre el canal RS485 para recibir. 00144 * \return void 00145 */ 00146 void USART_activa_rx(void){ 00147 delay_ms(5); 00148 output_low(TX_485_ENABLE); 00149 delay_ms(1); 00150 } 00151 00152 /** \brief Inicializa el Buffer de la USART 00153 * 00154 * Recoge el último carácter recibido desde la USART sobre USART_buffer 00155 * e incrementa en 1 el índice USART_nextRec.\n\n 00156 * Si el carácter recibido es Retorno de carro '\\r' ó 0x0D comprueba identidad de dispositivo y comando recibido 00157 * 00158 * \return void 00159 */ 00160 void USART_add_to_buffer(char c){ 00161 00162 USART_buffer[USART_nextRec++]=c; 00163 if(USART_nextRec==bytes_for_USART_buffer){ 00164 USART_nextRec=0; 00165 } 00166 00167 if(c==end_of_transmit){ 00168 --USART_nextRec; 00169 if(USART_buffer[USART_nextRec-3]==WhatAmI){ 00170 if(USART_buffer[USART_nextRec-2]==WhoAmI){ 00171 if(USART_buffer[USART_nextRec-1]==Command_Execute){ 00172 Command=0x01; 00173 } 00174 } 00175 } 00176 USART_nextRec = 0; 00177 } 00178 } 00179 00180 /////////////////////////////////////////////////////////////////////////////////////////////////// 00181 // 00182 // Rutinas de Servicio de Interrupciones 00183 // 00184 /////////////////////////////////////////////////////////////////////////////////////////////////// 00185 00186 #int_rda 00187 /** \brief Interrupción por : Recepción del Canal Serie. 00188 * 00189 */ 00190 void interrupt_service_rutine_rda(void) { 00191 00192 char USART_nextChar; 00193 00194 USART_nextChar='\0'; 00195 if(kbhit()){ 00196 USART_nextChar=getc(); 00197 USART_add_to_buffer(USART_nextChar); 00198 } 00199 } 00200 00201 /////////////////////////////////////////////////////////////////////////////////////////////////// 00202 // 00203 // M A I N 00204 // 00205 /////////////////////////////////////////////////////////////////////////////////////////////////// 00206 00207 /** \brief Constituye el núcleo principal del sistema 00208 * 00209 * \return void. 00210 */ 00211 void main(void) { 00212 00213 setup_oscillator(OSC_4MHZ); 00214 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 00215 setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); 00216 setup_timer_2(T2_DISABLED,0,1); 00217 setup_comparator(NC_NC_NC_NC); 00218 setup_vref(FALSE); 00219 port_b_pullups(False); 00220 set_tris_b(0b00000010); 00221 output_low(DRIVER_RELE); 00222 delay_ms(100); 00223 00224 USART_nextRec = 0; 00225 Command = 0x00; 00226 00227 USART_activa_rx(); 00228 delay_ms(333); 00229 00230 enable_interrupts(global); 00231 enable_interrupts(int_rda); 00232 00233 USART_activa_tx(); 00234 printf("[iRELE485-628] v.%s\r\n",version); 00235 USART_activa_rx(); 00236 00237 do{ 00238 00239 if(Command==0x01){ 00240 Command =0x00; 00241 00242 USART_activa_tx(); 00243 printf("R%cA%c\r\n",WhoAmI,end_of_transmit); 00244 USART_activa_rx(); 00245 00246 output_high(DRIVER_RELE); 00247 delay_ms(2000); 00248 output_low(DRIVER_RELE); 00249 } 00250 00251 } while(true); 00252 } 00253 00254 /////////////////////////////////////////////////////////////////////////////////////////////////// 00255 // 00256 // End of firmware 00257 // 00258 ///////////////////////////////////////////////////////////////////////////////////////////////////
Generado el Thu Oct 25 19:37:53 2007 para Firmware para RELE485-628 por1.5.3