Plateau
Junior Member level 3
- Joined
- Jan 4, 2012
- Messages
- 30
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,288
- Activity points
- 1,553
lks, is everybody ok?
I'm making a Track System using a GSM and GPS module, both controlled by a PIC18F4680. At the moment, I'm facing a big problem to handle the data that come through USART using the interrupt approach. I don't know why, but after enabling the RS232 receive data available (#INT_RDA) interrupt, the LCD stops working (lcd_putc or printf through LCD don't work) and the microcontroller seems to stuck in some part.
My code is below. If someone can help me with these problems, feel free for making changes in my code.
Thanks in advance,
Pedro Rosa.
P.S: The compiler used was CCS 4.130
main.c
defines.h
function.c
I'm making a Track System using a GSM and GPS module, both controlled by a PIC18F4680. At the moment, I'm facing a big problem to handle the data that come through USART using the interrupt approach. I don't know why, but after enabling the RS232 receive data available (#INT_RDA) interrupt, the LCD stops working (lcd_putc or printf through LCD don't work) and the microcontroller seems to stuck in some part.
My code is below. If someone can help me with these problems, feel free for making changes in my code.
Thanks in advance,
Pedro Rosa.
P.S: The compiler used was CCS 4.130
main.c
Code:#include <18f4680.H> #include <defines.h> #include <global.c> #use delay(clock=16610000) #fuses HS,NOWDT,NOPROTECT,NOLVP #use rs232 ( baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=SIM900D, bits=8, ERRORS ) #use rs232 ( baud=9600, xmit=PIN_C5, stream=PK2, bits=8, ERRORS ) #include <flex_lcd.c> #include <functions.c> void main(){ setup_timer_0(RTCC_OFF); // Configuração do Timer0 setup_timer_1(T1_DISABLED); // Configuração do Timer1 setup_timer_2(T2_DISABLED,0,1); // Configuração do Timer2 setup_ccp1(CCP_OFF); // Configuração do CCP1 setup_comparator(NC_NC_NC_NC); // Configuração dos Comparadores setup_vref(FALSE); // Configuração da Tensão de referência lcd_init(); delay_ms(500); fputs("ABCD",PK2); enable_interrupts(GLOBAL); inicializaPIC(); while(1){ output_toggle(LED_VERMELHO); delay_ms(500); } }
defines.h
Code:/* Header destinado a DEFINES */ //#define LED_USART_GSM PIN_D2 //#define LED_BUTTON PIN_D1 /*LEDs para diagnóstico*/ #define LED_VERMELHO PIN_D0 #define LED_VERDE PIN_D2 #define LED_AMARELO PIN_D1 /*SIM900D*/ #define PPWR_SIM900D PIN_D3 #define PRX_SIM900D PIN_C7 #define PTX_SIM900D PIN_C6 #define SIM900_OK 1 /*UP501*/ #define PRX_UP501 PIN_C4 //Software USART /*Celular*/ #define tamNumCelular 13 //Comprimento máximo do número do celular #define posStatusCadastro 0 //Posi;áo que indica se há ou náo celular cadastrado #define posInicialCelular 1 //Posição inicial para armazenar o celular #define posFinalCelular (posInicialCelular + tamNumCelular - 1) //Posição final para armazenar o celular /*EEPROM*/ #define OFFSET 0x0000
function.c
Code:void inicializaPIC(){ /*Alterna Leds para indicar inicialização*/ output_high(LED_VERMELHO); delay_ms(1000); output_high(LED_VERDE); delay_ms(1000); output_high(LED_AMARELO); delay_ms(1000); output_low(LED_AMARELO); delay_ms(1000); output_low(LED_VERDE); delay_ms(1000); output_low(LED_VERMELHO); /*Carregar variáveis globais*/ temCelularCadastrado = getTemCelCadastrado(); if(temCelularCadastrado == 'S'){ numCel = getCelCadastrado(); } else{ output_high(LED_AMARELO); /*Checar se a inicialização foi bem sucedida*/ if(inicializaGSM() != SIM900_OK ){ int16 stuckTime = 0; for(;;){ /*LEDs piscando a cada 1s */ output_toggle(LED_VERMELHO); output_toggle(LED_VERDE); output_toggle(LED_AMARELO); delay_ms(500); stuckTime++; /*Resetar o PIC após 60s*/ if(stuckTime == 120){ /*Desligar o SIM900D através do pino*/ output_high(PPWR_SIM900D); delay_ms(2000); output_low(PPWR_SIM900D); reset_cpu(); } } }//Ao receber o SMS //+CMTI: "SM",2 //posSMSRecebido = chegouSMS(); } } int8 inicializaGSM(){ int8 status = 0; output_high(LED_VERMELHO); delay_ms(2000); /*Ligar o SIM900D através do pino*/ output_high(PPWR_SIM900D); delay_ms(2000); output_low(PPWR_SIM900D); /*--------------------------------*/ /*Delay de 25s para sincronismo com a rede GSM*/ delay_ms(25000); /*Checar se o SIM900D está conectado a rede*/ status = isGSMOk(); output_low(LED_VERMELHO); return status; } int8 isGSMOk(){ int8 USARTCarCounter=0;//Contador de caracteres recebidos na USART int8 msgCounter=0;//Contador de caracteres da mensagem de interesse char msg[9];//Tamanho máximo da mensagem de status (1\r\n\r\nOK\r\n) msg = "";//Garantir que não há lixo na variável output_high (LED_VERMELHO); /*Desabilitar o Echo Mode: ATE0*/ fputs("ATE0\r\n", SIM900D); delay_ms(1000); output_low(LED_VERMELHO); enable_interrupts(INT_RDA); /*AT+CCALR?: 0 = NOT READY; 1 = READY*/ fputs("AT+CCALR?\r\n", SIM900D); delay_ms(500); for(;;){ if(interrupt == 1){ if(USARTCarCounter > 9){ msg[msgCounter] = caracterRecebido; msgCounter++; if(caracterRecebido == '\r'){ disable_interrupts(INT_RDA); output_high(LED_AMARELO); return msg; } } else if(caracterRecebido == headerStatusGSMVec[USARTCarCounter]){ USARTCarCounter++; putc(USARTCarCounter, PK2); } interrupt=0; caracterRecebido = ' '; } } } #INT_RDA void RDA_isr(void){ caracterRecebido = getc(SIM900D); interrupt=1; //putc(caracterRecebido, PK2); }
Last edited: