Autor Tema: Mpodrian ayudar con el modulo usart del pic 18f2550  (Leído 2593 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado pachomil

  • PIC12
  • **
  • Mensajes: 70
Mpodrian ayudar con el modulo usart del pic 18f2550
« en: 21 de Abril de 2011, 16:58:07 »
hola buenas tardes



estoy tratando de recibir una cadena de caracteres de un modem gsm y lo hago con un programa tomado del foro y modificado a mi necesidad, el codigo que les voy a pasar quizas es un poco repetitivo pero como todavia estoy en la fase de pruebas no borro nada para luego depurar asi que quizas les paresca largo pero ya cuando solucione todo lo optimizo, bueno el problema es que cuando el modem me envia el string y al ir y mirar los datos en la memoria eeprom del pic no hay nada o los datos que hay son confusos o errados, he mirado muchos ejemplos de compañeros foristas los he probado y ninguno me da la respuesta, el modem funciona a 3.3v y el pic a 5v utilizo un transistor 3n3904 como driver y asi la señal serial que viene del modem pueda estar a 5 voltios les adjunto el diagrama del transistor

#include <18F2550.h>
#device adc=8




#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                       //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12                    //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4                  //System Clock by 4
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled



#use fast_io(B)
#use delay(crystal=20000000)
//#include <SDCard_hard(2).c>
//#include <FAT16_SDCard1.3.c>
//#include <lcd.c>
//#include <string.h>
//#include <atones.c>

//************************************************************************************

#define segundo                         delay_ms(1000)
//#define SIZE 12
#define on                              output_high
#define off                             output_low
//#define I2C_SDA   PIN_C1
//#define EEPROM_SDA   PIN_C1
//#define I2C_SCL   PIN_C2
//#define EEPROM_SCL   PIN_C2

//***********************************************************************************

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
//#use rs232(baud=9600,parity=N,xmit=PIN_D2,rcv=PIN_D3,bits=8,errors)
//#use i2c(Master,Fast,sda=PIN_C1,scl=PIN_C2)
//************************************************************************************



void modem(void);

void addcbuff(char c);                                                           // añade caracter recibido al buffer
void inicbuff(void);                                                             // Borra buffer
void envio(void);
void leermensaje(void);
void clave(void);
void funcion(void);
void cuenta(void);
void led(void);
void prueba(void);
void borrado(void);

//********************************************************************************
int const lenbuff=80;   

int  xbuff=0x00;                                                                // Índice: siguiente char en cbuff
char cbuff[lenbuff];                                                            // Buffer
char rcvchar=0x00;                                                              // último caracter recibido
int1 flagcommand=0;
int16 i,j;
int16 clave_1;
int16 clave_2;
int16 clave_3;
int16 clave_4;
int8 contador=0;
int prue;
int32 tiempo=0;
int timer=0;
int16 subclave;
int32 loquelleva;

//**********************************INTERRUPCIONES SERIAL Y POR RB0 **********************************************


#int_rda
void serial_isr() {                                                                          // Interrupción recepción serie USART
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
   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 ...
                                                     
   }
   }




#INT_EXT
void IntRB0()
{
delay_ms(300);
contador=1;
//led();

}

//********************************************************************************
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
}
//**********************************************************************************
void addcbuff(char c)
{                 
   switch(c)
   {
   case 10:       
      flagcommand=1;             
      disable_interrupts(int_rda);
   break;
   default:
      cbuff[xbuff++]=c;
   if(xbuff>lenbuff)
      xbuff=lenbuff;
   }
}
//*************************************************************************************************
void cuenta(){
tiempo= read_eeprom(20);
delay_ms(600);
tiempo=tiempo-1;
write_eeprom(20,tiempo);

}

//************************************CLAVE DE PROGRAMACION*****************************************
//
//                                    CLAVE DE PROGRAMACION:
//
//**************************************************************************************************//
void clave()
{
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

delay_ms(100);
subclave = read_eeprom(14);


if (subclave==1){
  write_eeprom(10,clave_1);                                                   //guarda el nuevo dato en la memoria
  write_eeprom(11,clave_2);
  write_eeprom(12,clave_3);
  write_eeprom(13,clave_4);
  write_eeprom(14,subclave);
}   

       else
  {         clave_1=0x31;
            clave_2=0x32;
            clave_3=0x33;
            clave_4=0x34;
            subclave=0;
            write_eeprom(10,clave_1);                                                   //guarda el nuevo dato en la memoria
            write_eeprom(11,clave_2);
            write_eeprom(12,clave_3);
            write_eeprom(13,clave_4);
            write_eeprom(14,subclave);
}


//puts ("clave guardada en la memoria del pic");

}
//****************************************************************************************************
void envio(){
disable_interrupts(int_rda);
segundo;
printf("AT+CMGS=");
printf("%c" cbuff[25]);
delay_ms(100);
printf("%c" cbuff[26]);
delay_ms(100);
printf("%c" cbuff[27]);
delay_ms(100);
printf("%c" cbuff[28]);
delay_ms(100);
printf("%c" cbuff[29]);
delay_ms(100);
printf("%c" cbuff[30]);
delay_ms(100);
printf("%c" cbuff[31]);
delay_ms(100);
printf("%c" cbuff[32]);
delay_ms(100);
printf("%c" cbuff[33]);
delay_ms(100);
printf("%c" cbuff[34]);
delay_ms(100);
printf("%c" cbuff[35]);
delay_ms(100);
printf("%c" cbuff[36]);
delay_ms(100);
printf ("\r\n""");
puts("COMANDO ACEPTADO");
printf("%c" 0x1A);
delay_ms(100);
enable_interrupts(INT_RDA);
}

//****************************************************************************************************
void led()
{
on (PIN_C2);
delay_ms(500);
off (PIN_C2);
delay_ms(500);
on (PIN_C2);
delay_ms(500);
off (PIN_C2);
delay_ms(500);
}

//******************************************************************************************************

void prueba()
{         write_eeprom(29,(cbuff[56]));
            write_eeprom(30,(cbuff[57]));                                                   //guarda el nuevo dato en la memoria
            write_eeprom(31,(cbuff[58]));
            write_eeprom(32,(cbuff[59]));
            write_eeprom(33,(cbuff[60]));
            write_eeprom(34,(cbuff[61]));
 
 
if((cbuff[56]==0x31)){
(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(2000);
 }
if((cbuff[57]==0x32)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(2000);
 }
  if((cbuff[58]==0x33)){
(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(1000),(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)),

  delay_ms(1000), (on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(1000);
 }
 if((cbuff[59]==0x34)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
 delay_ms(2000);
 }
  if((cbuff[61]==0x31)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(2000);
 }
//do
//if (timer==1){cuenta();
//}     


prue=0;
//inicbuff();

}

//******************************PROGRAMA PRINCIPAL  **************************************************


void modem()
{

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

  puts("AT+IPR=9600");
  segundo;
  puts("AT+IPR=9600");
  segundo;
  puts("AT+IFC=0,0");
  segundo;
  PUTS("AT+CMGF=1");
  segundo;
   PUTS("AT+CMGD=4");
  segundo;
   PUTS("AT+CMGD=2");
  segundo;
 //PUTS("AT+CSCS= ");
  //  segundo;

 /* PUTS("AT+CMGD=5");
    segundo;
  PUTS("AT+CMGD=6");
    segundo;
  PUTS("AT+CMGD=7");
    segundo;
  PUTS("AT+CMGD=8");
    segundo;
  PUTS("AT+CMGD=9");
    segundo;
  PUTS("AT+CMGD=10");
    segundo;
  PUTS("AT+CMGD=11");
    segundo;
  PUTS("AT+CMGD=12");
    segundo;
  PUTS("AT+CMGD=13");
  segundo;
  */
 
  puts("ATE0");
  segundo;

delay_ms(500);
}

//**********************************************************************************************

void leermensaje(void)
{
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
//enable_interrupts(GLOBAL);
//enable_interrupts(INT_RDA);
//inicbuff();
//delay_ms(1000);
clave_1= read_eeprom(10);
clave_2= read_eeprom(11);
clave_3= read_eeprom(12);
clave_4= read_eeprom(13);


delay_ms(1000);

PUTS("AT+CMGR=1,1");
 
     contador=0;
     prue=1;
//delay_ms(1000);

}
//************************************************************************************************
void funcion()
{

if((cbuff[68]==0x31)&& (cbuff[69]==0x31)){ led(),on(PIN_C0), segundo, envio();                   //funciona libre   
}


if((cbuff[66]==0x31)&& (cbuff[69]==0x32)){ led(), timer=1;                                     //arranque un periodo predeterminado
    if((cbuff[71]==0x31)){tiempo=5400;}
    if((cbuff[71]==0x32)){tiempo=9000;}
    if((cbuff[71]==0x33)){tiempo=12600;}
    if((cbuff[71]==0x34)){tiempo=16200;}
    if((cbuff[71]==0x35)){tiempo=19800;}
    if((cbuff[71]==0x36)){tiempo=23400;}
    if((cbuff[71]==0x37)){tiempo=27000;}
    if((cbuff[71]==0x38)){tiempo=30600;}
    if((cbuff[71]==0x39)){tiempo=34200;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x30)){tiempo=37800;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x31)){tiempo=41400;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x32)){tiempo=45000;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x33)){tiempo=48600;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x34)){tiempo=52200;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x35)){tiempo=55800;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x36)){tiempo=59400;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x37)){tiempo=63000;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x38)){tiempo=66600;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x39)){tiempo=70200;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x30)){tiempo=73800;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x31)){tiempo=71400;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x32)){tiempo=81000;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x33)){tiempo=84600;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x34)){tiempo=88200;};
    envio(), on (PIN_C0), write_eeprom(20,tiempo) ;
   
}


if((cbuff[68]==0x31)&& (cbuff[69]==0x33)){ off (PIN_C0), envio(),
tiempo=0, write_eeprom(20,tiempo);                                               // fin de periodo
}


if((cbuff[68]==0x31)&& (cbuff[69]==0x34)){
 (clave_1 = cbuff[71]);
 (clave_2 = cbuff[72]);
 (clave_3 = cbuff[73]);
 (clave_4 = cbuff[74]);
 subclave = 1;
 write_eeprom(14,subclave);
 clave();                                                                       // cambiar clave
}


if((cbuff[68]==0x31)&& (cbuff[69]==0x35)){
   tiempo= read_eeprom(20);
    if((cbuff[71]==0x31)){tiempo=tiempo+3600;}
    if((cbuff[71]==0x32)){tiempo=tiempo+7200;}
    if((cbuff[71]==0x33)){tiempo=tiempo+10800;}
    if((cbuff[71]==0x34)){tiempo=tiempo+14400;}
    if((cbuff[71]==0x35)){tiempo=tiempo+18000;}
    if((cbuff[71]==0x36)){tiempo=tiempo+21600;}
    if((cbuff[71]==0x37)){tiempo=tiempo+25200;}
    if((cbuff[71]==0x38)){tiempo=tiempo+28800;}
    if((cbuff[71]==0x39)){tiempo=tiempo+32400;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x30)){tiempo=tiempo+36000;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x31)){tiempo=tiempo+39600;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x32)){tiempo=tiempo+43200;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x33)){tiempo=tiempo+46800;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x34)){tiempo=tiempo+50400;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x35)){tiempo=tiempo+54000;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x36)){tiempo=tiempo+57600;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x37)){tiempo=tiempo+61200;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x38)){tiempo=tiempo+64800;}
    if((cbuff[71]==0x31)&& (cbuff[72]== 0x39)){tiempo=tiempo+68400;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x30)){tiempo=tiempo+72000;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x31)){tiempo=tiempo+75600;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x32)){tiempo=tiempo+79200;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x33)){tiempo=tiempo+82800;}
    if((cbuff[71]==0x32)&& (cbuff[72]== 0x34)){tiempo=tiempo+86400;};
    envio(), write_eeprom(20,tiempo) ;                                                                             //adicionar tiempo
}


if((cbuff[68]==0x33)&& (cbuff[69]==0x33)){                                      //programar abandono

}

if((cbuff[68]==0x34)&& (cbuff[69]==0x33)){                                      //leer imei y enviarlo
}
delay_ms(20);
prue=0;
//envio();
//inicbuff();
}
//*****************************************************************************************************************************
void borrado()
{
(cbuff[56])=0x00;
(cbuff[57])=0x00;
(cbuff[58])=0x00;
(cbuff[59])=0x00;
(cbuff[60])=0x00;
(cbuff[61])=0x00;
(cbuff[62])=0x00;
(cbuff[63])=0x00;
(cbuff[64])=0x00;
(cbuff[65])=0x00;


}
//***********************************PROGRAMA PRINCIPAL ************************************************************************

void main()
{
 int l;
 #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
 
 
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   //setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   //setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   ext_int_edge(H_TO_L);
   enable_interrupts(GLOBAL);
  // set_tris_c(0b10000000);
   
//inicbuff();
borrado();
off (PIN_C0);
on (PIN_A0);

led();

delay_ms(10000);

modem();

clave();
 
led();
 
enable_interrupts(int_ext);
enable_interrupts(INT_RDA);

aaa:
do
{

//if( contador==0)   enable_interrupts(int_ext);
if( contador!=0)  l=1, enable_interrupts(INT_RDA), leermensaje() ; 

//if( contador==1)  l=1, delay_ms(1000), enable_interrupts(INT_RDA),leermensaje() ;

//while(l!=1);



//************************DESPUES DE LEIDO EL MENSAJE LO ANALIZA*****************************************
;

if (prue==1){ prueba();
}


if(flagcommand==1)
        {   
     flagcommand=0;

 if((cbuff[63]==clave_1) && ( cbuff[64]==clave_2)&& ( cbuff[65]==clave_3)&& ( cbuff[66]==clave_4)){             //clave de programacion
 funcion(), j=1;
 }
 
 
        }
 delay_ms(10);       
     
//inicbuff();
}         
while(j!=1);
inicbuff();
l=0;
goto aaa;
}

Desconectado AngelGris

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 2480
Re: Mpodrian ayudar con el modulo usart del pic 18f2550
« Respuesta #1 en: 21 de Abril de 2011, 18:31:27 »
  No trabajo con CCS pero me da la impresión que no debería estar la sentencia "#use rs232............" dentro de la rutina de atención a la interrupción por puerto serial.

  Otra cosa, me parece que sería conveniente trabajar primeramente sólo la parte seríal para ver si recibe bien los datos y luego ir sumándole las distintas cosas que necesites.
De vez en cuando la vida
nos besa en la boca
y a colores se despliega
como un atlas

Desconectado pachomil

  • PIC12
  • **
  • Mensajes: 70
Re: Mpodrian ayudar con el modulo usart del pic 18f2550
« Respuesta #2 en: 22 de Abril de 2011, 11:13:24 »
buenos dias

gracias angelgris pero si tienes razon lo que pasa es que yo primero empece con la programacion del modem y probe y corregi todos los problemas y ya iva en la recepcion de datos pero hay me quede lo he hecho solo recibiendo y me pasa lo mismo por eso puse todo el programa completo y lo de usar la sentencia #use rs232 dentro de la interrupcion ya lo he probada y no da ningun cambio, mil gracias por la ayuda

Desconectado bmb

  • PIC18
  • ****
  • Mensajes: 423
Re: Mpodrian ayudar con el modulo usart del pic 18f2550
« Respuesta #3 en: 22 de Abril de 2011, 12:50:52 »
Hola pachomil, revisar todo tu código tardaría mucho, pero le he dado una ojeada y esto es lo que logro ver:

- Al inicio tienes estos fuses:

Código: C
  1. #FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
  2. #FUSES PLL12                    //Divide By 12(48MHz oscillator input)
  3. #FUSES CPUDIV4                  //System Clock by 4
  4. #FUSES USBDIV                   //USB clock source comes from PLL divide by 2
  5. #FUSES VREGEN                   //USB voltage regulator enabled

Debes revisar que si quieres que los pines análogos del puerto B lo sean al iniciar el PIC y que la configuración del PLL esté de acuerdo al cristal que estás usando.

- Luego tienes #use fast_io(b) pero no veo que uses la instrucción set_tris_b() por ninguna parte.

- Más adelante declaras algunas variables para ser usadas con la eeprom como int16, int32 y luego las almacenas en la eeprom como si fueran variables de 8 bits.  Debes usar la librería que tiene CCS para almacenar y leer datos de más de 8 bits en la eeprom.

- Utilizas muchas veces #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors).  Con la primera que aparece en el código es suficiente.

- En INT_EXT() utilizas un delay_ms(300).  No es correcto poner demoras en las rutinas de interrupción.

- En la interrupción RDA almacenas al caracter recibido en la variable rcvchar, pero después no haces nada con ella.

- Ya a nivel de comentario, veo que utilizas una forma de hacer expresiones con los if() que te confieso :oops: que no había visto antes en C.

Código: C
  1. if((cbuff[57]==0x32)){
  2. (on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(2000);
  3. }

- También veo que utilizas etiquetas como en lenguaje ensamblador.  En C hay instrucciones para no tener que hacer uso de estas.

Finalmente como tienes un código tan extenso y con varios errores, te haría la misma sugerencia que AngelGris y empieces a descartar por partes haciendo códigos de prueba más pequeños, asegurarte que recibes bien los datos por el puerto serial y luego de estar seguro que funciona bien, le adicionaría los demás elementos.

Saludos!

Desconectado pachomil

  • PIC12
  • **
  • Mensajes: 70
Re: Mpodrian ayudar con el modulo usart del pic 18f2550
« Respuesta #4 en: 22 de Abril de 2011, 16:52:30 »
gracias bmb por tu ayuda

siguiendo las recomendaciones hice las modificaciones que me recomendaron y le quite al codigo todo lo que no se necesita probar por el momento, de todas maneras para poder leer el mensaje del modem lo debo programar y esas rutinas si las deje, y tengo una rutina que yo llamo prueba para ver si el pic si me recibe  los datos, con respecto a rcvchar, lo utilizo en en cada caracter que va llegando y lo paso al buffer,   he probado y sigo sin poder recibir nada, en la simulacion del proteus si me muestra que recibe los datos bien

#include <18F2550.h>
#device adc=8


#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                       //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
//#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
//#FUSES PLL12                    //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4                  //System Clock by 4
//#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
//#FUSES VREGEN                   //USB voltage regulator enabled
#use delay(crystal=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)


//******************************************************************************************************
#define segundo                         delay_ms(1000)
#define on                              output_high
#define off                             output_low


//******************************************************************************************************

void modem(void);
void addcbuff(char c);                                                           // añade caracter recibido al buffer
void inicbuff(void);                                                             // Borra buffer
void leermensaje(void);
void led(void);
void prueba(void);


//********************************************************************************
int const lenbuff=80;   
int  xbuff=0x00;                                                                // Índice: siguiente char en cbuff
char cbuff[lenbuff];                                                            // Buffer
char rcvchar=0x00;                                                              // último caracter recibido
int1 flagcommand=0;
int16 i,j;

int8 contador=0;
int prue;


      #int_rda

      void 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 ...
   
        }
 
      }
 


#INT_EXT
void IntRB0()
{

contador=1;


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

//**********************************************************************************
void addcbuff(char c)
{                 
   switch(c)
   {
   case 10:       
      flagcommand=1;             
      disable_interrupts(int_rda);
   break;
   default:
      cbuff[xbuff++]=c;
   if(xbuff>lenbuff)
      xbuff=lenbuff;
   }
}

//******************************************************************************************************

void prueba()
{         write_eeprom(29,(cbuff[56]));
            write_eeprom(30,(cbuff[57]));                                                   //guarda el nuevo dato en la memoria
            write_eeprom(31,(cbuff[58]));
            write_eeprom(32,(cbuff[59]));
            write_eeprom(33,(cbuff[60]));
            write_eeprom(34,(cbuff[61]));
 
 
if((cbuff[56]==0x31)){
(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(2000);
 }
if((cbuff[57]==0x32)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(2000);
 }
  if((cbuff[58]==0x33)){
(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(1000),(on (PIN_C2)), delay_ms(1000), (off (PIN_C2)),

  delay_ms(1000), (on (PIN_C2)), delay_ms(1000), (off (PIN_C2)), delay_ms(1000);
 }
 if((cbuff[59]==0x34)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
 delay_ms(2000);
 }
  if((cbuff[61]==0x31)){
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(300),
(on (PIN_C2)), delay_ms(300), (off (PIN_C2)), delay_ms(2000);
 }
     


prue=0;
//inicbuff();

}

void led()
{
on (PIN_C2);
delay_ms(500);
off (PIN_C2);
delay_ms(500);
on (PIN_C2);
delay_ms(500);
off (PIN_C2);
delay_ms(500);
}


void modem()
{

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

  puts("AT+IPR=9600");
  segundo;
  puts("AT+IPR=9600");
  segundo;
  puts("AT+IFC=0,0");
  segundo;
  PUTS("AT+CMGF=1");
  segundo;
   PUTS("AT+CMGD=4");
  segundo;
   PUTS("AT+CMGD=2");
  segundo;
  puts("ATE0");
  segundo;

delay_ms(500);
}

void leermensaje(void)
{

delay_ms(1000);
PUTS("AT+CMGR=1,1");
     contador=0;
     prue=1;

}


void main()
{
 int l;
 #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
 
 
   setup_adc_ports(NO_ANALOGS|VSS_VDD);

   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   ext_int_edge(H_TO_L);
   enable_interrupts(GLOBAL);

   


off (PIN_C0);
on (PIN_A0);

led();

delay_ms(10000);

modem();


 
led();
 
enable_interrupts(int_ext);
enable_interrupts(INT_RDA);

aaa:
do
{

 
if( contador!=0)  l=1, enable_interrupts(INT_RDA), leermensaje() ; 


if (prue==1){ prueba();
}


if(flagcommand==1)
        {   
     flagcommand=0;
     j=1;
     

}
}
while(j!=1);

inicbuff();
l=0;
goto aaa;
}



ahh se me olvidaba con respecto al comentario de bmb de usar los if de esa forma depronto como programa en otros lenguajes como visual lo he heredado de alla, pero como me funciona por eso no me habia dado cuenta que el c no se utiliza casi

Desconectado bmb

  • PIC18
  • ****
  • Mensajes: 423
Re: Mpodrian ayudar con el modulo usart del pic 18f2550
« Respuesta #5 en: 23 de Abril de 2011, 18:42:30 »
Hola pachomil, me alegra que ya estés cerca de solucionarlo ya que dices que te trabaja por ahora en proteus.  Lo único que veo mal por el momento, sin revisar a fondo tu código, es que al comentar el fuse PBADEN, es lo mismo que si no lo hicieras, ya que esa es la configuración por defecto de los pines análogos del puerto B del PIC (ver hoja de datos).  Debes poner explicitamente el fuse NOPBADEN para indicarle al compilador que configure los pines del puerto B como E/S al iniciar el PIC.

Saludos!