Que tal, soy nuevo en este foro así que sepan disculpar mi falta de experiencia.
Quería comentarles que estoy teniendo un serio problema a la hora de recibir caracteres por la UART2 del pic 18f26j50. Éste está conectado a un módem GSM SIM900 y los problemas surgen a la hora de recibir la respuesta a los comandos AT que le envío. El tema es que recibo solo 2 caracteres y después, por algún motivo que desconozco, el pic súbitamente deja de funcionar. Ya he descartado cualquier problema de hard, de hecho, chequeo con un multímetro y veo que hay actividad en el pin correspondiente. Además de esto, poseo un archivo .hex el cual realiza esta actividad correctamente, demostrando que el problema está en la programación.
Físicamente los pines RX y TX del SIM900 están conectados a los pines A1/RP1 y A0/RP0, respectivamente. Tengo entendido que el remapeo necesario está bien hecho pero bué, ya no estoy seguro de nada.
Para observar los caracteres y la operatoria del pic, hago uso del puerto USB. En el hyperterminal veo lo siguiente:
Testeo
FFNNN-1NNN0
0DNNN13NNN1
Entenderán el formato cuando vean el archivo .c a continuación:
#include <18F26j50.h> // Definición de registros internos.
#FUSES NOWDT //No Watch Dog Timer
#FUSES HSPLL //High Speed Crystal/Resonator with PLL enabled
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOPROTECT //Code not protected from reading
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES IOL1WAY //Allows only one reconfiguration of peripheral pins
#FUSES PRIMARY //Primary clock is system clock when scs=00
#FUSES NOWPCFG
#FUSES WPEND
#FUSES WPDIS
#FUSES CPUDIV3 //System clock by 3
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES T1DIG //Secondary Oscillator Source may be select regardless of T1CON.3 state
#FUSES MSSPMSK7
#FUSES PLLDIV3
#FUSES NODSWDT
#use delay(clock=16000000)
#pin_select U2TX=PIN_A1
#pin_select U2RX=PIN_A0
#use rs232(UART2, BAUD=9600, PARITY=N, XMIT=PIN_A1, RCV=PIN_A0, BITS=8, STREAM=SERIAL_GSM)
#include "usb_cdc.h" // Descripción de funciones del USB.
#include "usb_desc_cdc.h" // Descriptores del dispositivo USB.
#byte OSCCON = 0xFD3
#byte UCFG = 0xF39
#byte OSCTUNE= 0xF9B
#byte RCSTA1 = 0xFAC
#byte RCSTA2 = 0xF9C
#define PIN_ON output_high
#define PIN_OFF output_low
#define LED PIN_B4
int e;
int const lenbuffGSM=20; // Tamaño del buffer de recepción
int8 cbuffGSM[lenbuffGSM]; // Buffer de recepcion de datos serie
void inicializacionGSM(void)
{
int cont=0;
delay_ms(1000);
puts("A",SERIAL_GSM);
delay_ms(300);
putchar(0x0d,SERIAL_GSM);
delay_ms(3000);
for(cont=0;cont<3;cont++)
{
puts("AT",SERIAL_GSM);
delay_ms(300);
putchar(0x0d,SERIAL_GSM);
delay_ms(200);
PIN_ON(LED);
delay_ms(200);
PIN_OFF(LED);
}
fprintf(SERIAL_GSM,"AT+CMGF=1\r");
delay_ms(300);
fprintf(SERIAL_GSM,"ATE1\r"); //echo habilitado
delay_ms(300);
return;
}
void recon_caracter(void)
{
int8 k=0;
delay_ms(3000);
fprintf(SERIAL_GSM,"ATI\r"); //debería devolver varios caracteres
while (TRUE)
{
if ( kbhit(SERIAL_GSM) )
{
e=0;
e=fgetc(SERIAL_GSM);
cbuffGSM[k]=e;
printf(usb_cdc_putc,"%XNNN%dNNN%d\n\r",cbuffGSM[k],cbuffGSM[k],k); //para ver el caracter en distintos formatos
k++;
#asm //por las dudas q se me reseteara por WDT, pero es redundante ya q está apagado
clrwdt;
#endasm
}
}
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD); //desactivo entradas analogicas
setup_adc(ADC_OFF|ADC_TAD_MUL_0); //desactivo entradas analogicas
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_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_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
disable_interrupts(INT_RDA);
disable_interrupts(GLOBAL);
OSCTUNE = 0xC0;
OSCCON = 0xF0;
UCFG |= 0x4;
int contador1=0;
set_tris_a(0b00101101); //configuro los puertos
set_tris_b(0b00000011);
set_tris_c(0b01000101);
PIN_OFF(led);
usb_cdc_init(); // Configuramos al puerto virtual.
usb_init(); // Inicializamos el stack USB.
while(!usb_cdc_connected()) {} // espera a detectar una transmisión de la PC (Set_Line_Coding).
do{
usb_task();
if (usb_enumerated()) // Espera a que el dispositivo sea enumerado por el host.
{
printf(usb_cdc_putc,"Testeo\n\r");
contador1++;
delay_ms(500);
}
}while (contador1==0);
PIN_ON(led);
inicializacionGSM();
recon_caracter();
}