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