-1

I'm writing code to make a module that turn on a microcontroller port via a sms. I'm using a Pic16F88 and a Sim900 module, i'm programming in mplab X and XC8. It works in Proteus but no in the circuit i've resolved the power consumption problems with some capacitors, so i think the problem is in the code. Maybe the module don't recognize the '\r' character as Carriage return??

    /*
 * File:   ContGsmMain.c
 * Author: camil
 *
 * Created on 28 de enero de 2020, 22:08
 */

// CONFIG1
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB0      // CCP1 Pin Selection bit (CCP1 function on RB0)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// CONFIG2
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode disabled)

#define _XTAL_FREQ 8000000
#include <xc.h>
#include "pic16f88.h"
#include "uart.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

//Funcion retorna la posición donde comienza la cadena seleccionada
int existe(char linea[], char busq[]);

void main(void) {
    TRISA=0;                // Program Init
    TRISB=0x04;
    PORTA=0;
    PORTB=0;
    OSCCON = 0b01111100;
    UART_Init(9600);
    __delay_ms(200);

    int porta[8]={0,0,0,0,0,0,0,0};     //Variable declaration
    char buffer[80]="";
    char *buff;
    char textbuf[10]="";
    int i=0,e=0;
    buff=buffer;

    RA3=1;                          //Module Init
     __delay_ms(3000);
     RA3=0;
    UART_Write_Text("AT+CMGF=1\r");
    __delay_ms(100);
    UART_Write_Text("AT+IPR=9600\r");
    __delay_ms(100);
    UART_Write_Text("AT+CNMI=2,2,0,0,0\r");   
    __delay_ms(100);     

    while(1){
            if(RCIF==1){                // Try if message is received
             for(i=0;i<80;i++){
                while(!RCIF);
                buff[i]=UART_Read();
                if(buff[i]=='\r') 
                     break; 
                }

     /*          itoa(textbuf,existe(buff, "L"),10 );      //Debugging segment
                  UART_Write_Text(textbuf);  */

                if(existe(buffer, "L")==1) {
                    porta[2]=!porta[2];
                    RA2=porta[2];

                /*  itoa(textbuf,porta[3],10 );     //Debugging segment
                UART_Write_Text("Existe ");
                UART_Write_Text(textbuf);
                UART_Write_Text("\r");*/
                }

                for(i=0;i<80;i++) buff[i]=0;             
            }
        }
}

int existe(char linea[], char busq[]){
    int e=0,a=0;
    char *carpal, *carbus;
    carpal=linea;
    carbus=busq;
    for(e=0;e<strlen(linea);e++){
        if(carpal[e]==carbus[a]){
            a++;
            if(a==strlen(busq)){
                return 1;
                }
            continue;
        }
        else a=0;
    }
    return 0;
   }
  • 2
    You put in some debug code to print out the received bytes, but forgot to tell us what you found out.... – Martin James Feb 21 '20 at 18:13
  • It works in a simulator but not on the real board, which you had other problems with. Do you have an appropriate resistor bias on control and port lines? Does a supplied example program work correctly? – Weather Vane Feb 21 '20 at 18:42
  • Have you got a pull up on the MCLR pin? – Mike Feb 21 '20 at 19:47
  • Yes i put a pull up con the MCRL, and comunicating the circuit with the computer instead with the GSM module worked. – Camilov87 Mar 04 '20 at 21:41

1 Answers1

0

At last i did it worked resolving 2 issues:

-Using a command "UART1_Write_Text("AT+ICF=3,0\r");" this make the GSM module to use software flowcontrol instead of hardware control. With this solution a had response to small inputs like "L", but when i changed the input to something like "Luz" it dont worked.

-Then i did use a control character in the inputs to break the loop in the if(buff[i]=='\r') in this case '_'.

I think the problem was the module sends a '\r' character before send the "Luz" input. Then it exit the loop and started to process before the UART stop receiving, then recevinig 3 characters give a error flag while the micro executes the rest of the code. Using the "Luz_" input and stop reading when the '_' is received worked for now.

This because the RCREG register allows 2 characters before giving a Overrun error.