Codigo:// _232_buffered.c#include <16f876a.h> // Definiciones del PIC 16F876A#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT // Los Fuses de siempre#use delay(clock=4000000) // Oscilador a 4 Mhz#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar// CONSTANTES /////////////////////////////////////////////////////////////////int const lenbuff=32; // Longitud de buffer, Ajustar // a lo que desees (o te sea posible)// VARIABLES EN RAM ///////////////////////////////////////////////////////////int xbuff=0x00; // Índice: siguiente char en cbuffchar cbuff[lenbuff]; // Bufferchar rcvchar=0x00; // último caracter recibidoint1 flagcommand=0; // Flag para indicar comando disponible // Declaración de Funciones ///////////////////////////////////////////////////void inicbuff(void); // Borra buffer int addcbuff(char c); // añade caracter recibido al buffervoid echos(char c); // Eco selectivo sobre RS232void procesa_comando(void); // Procesa comando// INTERRUPCIONES /////////////////////////////////////////////////////////////#int_rdavoid serial_isr() { // Interrupción recepción serie USART rcvchar=0x00; // Inicializo caracter recibido if(kbhit()){ // Si hay algo pendiente de recibir ... rcvchar=getc(); // lo descargo y ... addcbuff(rcvchar); // lo añado al buffer y ... echos(rcvchar); // hago eco (si procede). }}// Desarrollo de Funciones ////////////////////////////////////////////////////void echos(char c){ // Echo selectivo ---------------------- switch(c){ case 0x0D: printf(" [Ent] " ); // Si he pulsado la tecla [Intro] break; case 0x08: printf(" [Del] " ); // Si he pulsado la tecla [Retroceso] break; case 0x1B: printf(" [Esc] " ); // Si he pulsado la tecla [Escape] break; default: putc(rcvchar); // Echo de cualquier otro caracter }}void inicbuff(void){ // Inicia a cbuff ------------------- int i; for(i=0;i<lenbuff;i++){ // Bucle que pone a 0 todos los cbuff[ i ]=0x00; // caracteres en el buffer } xbuff=0x00; // Inicializo el indice de siguiente // caracter}int addcbuff(char c){ // Añade a cbuff ----------------------- switch(c){ case 0x0D: // Enter -> Habilita Flag para procesar flagcommand=1; // Comando en Main break; case 0x08: // Del -> Borra último caracter del Buffer if(xbuff>0) cbuff[--xbuff]=0x00; break; case 0x01B: // Esc -> Borra el Buffer completamente inicbuff(); break; default: cbuff[xbuff++]=c; // Añade caracter recibido al Buffer }}// Programa Principal /////////////////////////////////////////////////////////void main() { inicbuff(); // Borra buffer al inicio printf("** RS232 Buffered **" ); // Presenta menú printf("[Enter] Procesa comando" ); printf("[Escape] Borra todo el buffer" ); printf("[Delete] Borra último carácter del buffer" ); printf("[\w] Comando Escribe" ); printf("[\r] Comando Lee" ); printf("" ); enable_interrupts(int_rda); // Habilita Interrupción RDA enable_interrupts(global); // Habilita interrupciones do { if(flagcommand) procesa_comando(); // Si hay comando pendiente // de procesar ... lo procesa. } while (TRUE);}// Procesador de Comandos /////////////////////////////////////////////////////void procesa_comando(void){ int i; char arg[lenbuff]; // Argumento de comando (si lo tiene) flagcommand=0; // Desactivo flag de comando pendiente. printf("Procesando ... " ); // Monitorizo procesando ... for(i=0;i<lenbuff;i++){ // Bucle que pone a 0 todos los arg[ i ]=0x00; // caracteres en el argumento } if(cbuff[0]=="\"&&cbuff[1]=="r"){ // Comparo inicio del buffer con comando "" printf("Leyendo ... " ); // Aqui lo que deseemos hacer con comando "" } if(cbuff[0]=="\"&&cbuff[ 1 ]=="w"){ // Comparo inicio del buffer con comando "w" i=2; do{ // Extraemos argumento del buffer arg[i-2]=cbuff; // a partir del 3er byte y hasta . }while(cbuff[++i ]!=0x00); printf("Escribiendo %s ... ",arg);// Aqui lo que deseemos hacer con comando "w" // Monitorizamos el argunmento. } inicbuff(); // Borro buffer. printf("Procesado." ); // Monitorizo procesado.}
Codigo:if(cbuff[0]=="\"&&cbuff[1]=="B"){ i=2; do{ // Extraemos argumento del buffer arg[i-2]=cbuff; // a partir del 3er byte y hasta . }while(cbuff[++i]!=0x00); valor=arg; // donde valor es respectivamente de tipo int temp_duty_cycle2=valor; printf("-duty cycle = %u", valor);}
Codigo:ATOI( )ATOL( )ATOI32()Syntax: ivalue = atoi(string) or lvalue = atol(string) or i32value = atoi32(string)Parameters: string is a pointer to a null terminated string of characters.Returns: ivalue is an 8 bit int.lvalue is a 16 bit int. i32value is a 32 bit int.Function: Converts the string pointed too by ptr to int representation. Accepts both decimal and hexadecimal argument. If the result cannot be represented, the behavior is undefined.Availability: All devices.Requires: #include <stdlib.h>Examples:char string[10];int x;strcpy(string,"123"x = atoi(string);// x is now 123
Codigo:...#include <stdlib.h>...void procesa_comando(void){ int i, xv;... if(cbuff[0]=="\"&&cbuff[1]=="w"){ // Comparo inicio del buffer con comando "w" i=2; do{ // Extraemos argumento del buffer arg[i-2]=cbuff[ i ]; // a partir del 3er byte y hasta . }while(cbuff[++i]!=0x00); xv = atoi(arg); printf("Escribiendo %s %u... ",arg,xv ) ; // Aqui lo que deseemos hacer con comando "w" // Monitorizamos el argunmento. }...