PIC to MAX232

bwack

Joined Nov 15, 2011
113
@R!f@@ Have you looked in the Other folder ?
C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\Examples\Other

C_string.c
C:
/*
* Project name:
     C_string
* Copyright:
     (c) Mikroelektronika, 2008.
* Revision History:
     20080930:
       - initial release;
* Description:
     This program demonstrates usage of standard C string library routines.
     Compile and run the code through software simulator.
* Test configuration:
     MCU:             PIC16F887
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
     Oscillator:      HS, 08.0000 MHz
     Ext. Modules:    -
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
* NOTES:
     - None.
*/

char *res;
int result;

void main(){
  char txt[] = "mikroElektronika";
  char txt_sub[10] = "mikr";

  /* memchr */
  res =  memchr(txt, 'e', 16);
  res =  memchr(txt, 'e', 5);

  /* memcmp */
  result =  memcmp(txt, txt_sub, 16);      // 111
  result =  memcmp(txt, txt_sub, 4);       // 0

  /* memcpy */
  res = memcpy(txt+4, txt_sub, 4);
  res = memcpy(txt+10, txt_sub, 2);

  /* memmove */
  res = memmove(txt+5, txt_sub, 4);
  res = memmove(txt+11, txt_sub, 2);

  /* memset */
  memset(txt_sub+5,'a',2);

  /* strcat */
  txt_sub[3] = 0;
  strcat(txt_sub,"_test");

  /* strchr */
  res =  strchr(txt_sub, 't');

  /* strcmp */
  result = strcmp(txt,txt_sub);    //  19

  /* strcpy */
  res = strcpy(txt, txt_sub);

  /* strlen - Returns length of given (null-terminated) string */
  result = strlen(txt);          // 8

  /* strncat */
  txt[3] = 0;
  txt_sub[4] = 0;
  res = strncat(txt,txt_sub,8);
  txt[3] = 0;
  res = strncat(txt,txt_sub,2);

  /* strncpy */
  txt_sub[4] = 0;
  res = strncpy(txt,txt_sub,8);

  /* strspn */
  result = strspn(txt,txt_sub);    // 4

  /* strcspn */
  result = strcspn(txt_sub,txt);   // 0
  result = strcspn(txt_sub,"dxc"); // 4
  result = strcspn(txt_sub,"dxk"); // 2

  /* strncmp */
  txt[4] = 'r';
  result =  strncmp(txt_sub,txt,3);  // 0
  result =  strncmp(txt_sub,txt,6);  // -114

  /* strpbrk */
  res =  strpbrk(txt_sub,txt);

  /* strrchr */
  txt_sub[3] = 'k';
  res =  strrchr(txt_sub,'p'); // 0
  res =  strrchr(txt_sub,'k');

  /* strstr */
  txt_sub[3] = 0;
  res =  strstr(txt,txt_sub);
  txt[2] = 'P';
  res =  strstr(txt_sub,txt); // 0

}
C_type is also interesting. It has tests for upper, lowercase, numeric etc.I always thought C_type meant c variable type, but it means character type ..
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@bwack I did not check all of 'em mikroc lib.
I believe now will be a good time to go thru those.

@jayanthd..Sorry ...I had to go to a site...to check a diving scooter.
Plus to rewire a oxygen tank compressor.
Darn tired !.....just got back..I will head home soon and meddle with the code.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
C:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

#define ON   1
#define OFF  0

#define HIGH 1
#define LOW  0

#define TRUE 1
#define FALSE 0

#define SEND_AT 0
#define SEND_AT_CPIN_QUERY  1
#define AT_COPS_QUERY 2
#define AT_CSQ_QUERY 3

// GSM module connections
sbit gsmReset at RD7_bit;
sbit gsmReset_Direction at TRISD7_bit;
// End of GSM module connections

sbit LED at RD0_bit;

char gsmBuffer[40];
char buffer1[50];
char sms[30];
char smsIndex[4];
char signalStrength[5];

unsigned char myFlags = 0;

char gsmAttempt = 0, gsmState = 0;
unsigned int gsmBufferIndex = 0, signal_strength = 0;

sbit sendSmsFlag at myFlags.B0;

//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS=\"0000000000\"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
const char atCommandB[] = "AT+COPS?\r";
const char atCommandC[] = "AT+CPIN?\r";
const char atCommandD[] = "AT+CSQ?\r";
const char atCommandE[] = "ATH\r";

const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: \"SM\",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING\r\n\r\n";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
const char atResponseA[] = "+COPS: 0";
const char atResponseB[] = "+COPS: 0,2,47201";
const char atResponseC[] = "+COPS: 0,2,47202";
const char atResponseD[] = "+CSQ: ";
const char atResponseE[] = "AT\r\n\r\nOK\r\n";
const char atResponseF[] = "AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n";
const char atResponseG[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n";
const char atResponseH[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n";
const char atResponseI[] = "AT+CPIN?\r\n\r\nERROR\r\n";
const char atResponseJ[] = "AT+CPIN?\r\n\r\n+CPIN: READY\r\n";
const char atResponseK[] = "AT+CSQ\r\n\r\n+CSQ: ";
const char atResponseL[] = "AT+CSQ\r\n\r\n+CSQ: 99,99\r\nOK\r\n";
const char atResponseM[] = "AT+CMGF=1\r\n\r\nOK\r\n";
const char atResponseN[] = "AT+CMGD=1,4\r\n\r\nOK\r\n";
const char atResponseO[] = "ATH\r\n\r\nOK\r\n";
const char atResponseP[] = "AT+COPS?\r\n\r\n+COPS: ";
const char atResponseQ[] = "ATH\r\n\r\n";
const char atResponseR[] = "AT\r\n\r\n";
const char atResponseS[] = "AT+CPIN?\r\n\r\n";

const char msg1[] = "No Network ";
const char msg2[] = "No SIM     ";
const char msg3[] = "No Network And No SIM";
const char msg4[] = "Network and SIM Ready";
const char msg5[] = "Signal Strength = ";
const char msg6[] = "Not Registered On Network";
const char msg7[] = "SIM Removed";
const char msg8[] = "OK         ";
const char msg9[] = "ERROR      ";
const char msgA[] = "SIM Ready  ";
const char msgB[] = "Dhi Mobile ";
const char msgC[] = "Ooredoo    ";
const char msgD[] = "Incoming Call";
const char msgE[] = "Call Hanged-Up";

const char sms1[] = "LED is ON";
const char sms2[] = "LED is OFF";
const char sms3[] = "Invalid Command Received";

//Function Prototypes
void DelayXSec(unsigned long int sec);
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);

void interrupt() {

    if((RCIE_bit) && (RCIF_bit)) {
        if(OERR_bit) {
              OERR_bit = 0;
              CREN_bit = 0;
              CREN_bit = 1;
        }

        gsmBuffer[gsmBufferIndex++] = UART1_Read();
        gsmBuffer[gsmBufferIndex] = '\0';

        RCIF_bit = 0;
    }
}

char *CopyConst2Ram(char *dest, const char *src){
    char *d;

    d = dest;

    for(;*dest++ = *src++;)asm clrwdt;

    return d;
}

void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}

/*
void GSM_RESET() {
    asm clrwdt
    gsmReset = 1;
    Delay_ms(20);
    gsmReset = 0;
}
*/

void Extract_Signal_Strength(char *s1, char *s2) {
    unsigned int i = 0;

    asm clrwdt

    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }

    asm clrwdt

    while(*s1 != ' ') {
           *s1++;
           asm clrwdt
    }

    *s1++;

    while(*s1 != ',') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}

/*
void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;

    asm clrwdt

    while(*s1) {
        if(*s1 == '\n')
            ++i;
        asm clrwdt
        *s1++;

        if(i == 2)break;
    }

    asm clrwdt

    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}

void Get_SMS_Index(char *s1, char *s2) {

    while(*s1 != 'C') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'M') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'T') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != 'I') {
           *s1++;
           asm clrwdt
    }

    while(*s1 != ',') {
           *s1++;
           asm clrwdt
    }

    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        asm clrwdt
    }

    *s2 = '\0';
    asm clrwdt
}
*/

void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt

    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));
        DelayXSec(2);
        (*idx) = 0;

        /*
        if((*gsmAttempt)++ == 2) {
            //GSM_RESET();
            //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;
        }
        */
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*gsmAttempt) = 0;
    (*idx) = 0;
}

/*
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    asm clrwdt

    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        DelayXSec(2);

        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }

    (*idx) = 0;

    Extract_SMS(s1, s6);

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    asm clrwdt
}

void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
    asm clrwdt

    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        asm clrwdt
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);

        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);

        DelayXSec(6);
    }

    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    asm clrwdt
}
*/

void main() {
OSCCON = 0x77;  // 8MHz Internal Osc.
ANSEL = 0x00;   // D I/O
ANSELH = 0x00;  // Rest as Digital IO.
CM1CON0 = 0x07; // Disable Comparator 1.
CM2CON0 = 0x07; // Disable Comparator 2.
PORTA = 0x00;      // Clear PORT A.
PORTB = 0x00;      // Clear PORT B.
PORTC = 0x00;      // Clear PORT C.
PORTD = 0x00;     // Clear PORT D.
PORTE = 0x00;      // Clear PORT E.

TRISA = 0xFF;   // PORT A as Input.
TRISB = 0x00;   // PORT B as Output.
TRISC = 0x38;   // RC <0-2> As Output, Rest as Input
// PORTDimg = 0x2F; // PORT D Shadow, Identical to TRISD.
TRISD = 0xFF;   //
TRISE = 0xFF;   // PORT E as Input.
//------- Initialte ADC & LCD & UART -------------------------------------------
//ADC_Init();              // Innitiate ADC.
VCFG0_bit = 1;           // Externel Vref +.
VCFG1_bit = 0;           // Internal Vref - as GND.
Lcd_Init();              // Innitiate LCD.
Lcd_Cmd(_LCD_CLEAR);     // Clear LCD.
Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor OFF.
Delay_ms(100);              // Wait to Stabilize.
UART1_Init(9600);           // Initialize UART module at 9600 baud.
Delay_ms(100);              // Wait for UART module to stabilize.
//------- Clear IRQ and Fire up the Timers -------------------------------------
OPTION_REG = 0x81;          // No pullups, Timer 0 at Prescaler 4.
INTCON = 0;                 // Disable All Interrupts First.
PIE1 = 0;                   // Disable All Peripheral Interrupts.
PIE2 = 0;                   // Disable All Peripheral Interrupts.
INTCON.T0IE = 1;            // Enable the Timer0 Interrupt.
INTCON.T0IF = 0;            // Clear Timer 0 Register Overflow Flag.
// TMR0 = TMR0set;           // Load Timer 0 with Preset.
T1CON = 0x30;               // No gate, Int Tcyc/8, TMR1 Stopped.
TMR1H = TMR1L = 0;          // Timer counts up to CCP value.
CCPR1 = 625;                // 1usTcyc * 8 Prescale * 625 = 5msec.
CCP1CON = 0x0B;             // NO Hardware PWM.
TMR1IF_bit = 0;             // PIR1 reg, Clear the TMR1 Overflow IRQ.
CCP1IF_bit = 0;             // Clear TMR1 IRQ flag bit.
RCIF_bit = 0;               // Clear Rx Interrupt Flag .................
T1CON.TMR1ON = 1;           // Start TIMER 1.
TMR1IE_bit = 1;             // Enable the Timer1 overflow Interrupt.
PIE1.RCIE = 1;              // Enable Rx Interrupt .................
CCP1IE_bit = 1;             // Enable the CCP1 Interrupt.
INTCON.GIE = 1;             // Global Interrupt Enabled.

    while(1) {

        asm clrwdt

        switch(gsmState) {
            case SEND_AT:
                  //Send AT and check for AT\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);

                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse1))) {   //OK received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg8));  //Print OK on LCD
                      gsmState = 1;    //Set state to SEND_AT_CPIN_QUERY
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print Error on LCD
                      gsmState = 0;     //Set state to SEND_AT
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {    //RING received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);

                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {    //ATH_OK received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));    //Print Call Hanged-Up on LCD
                          gsmState = 0;   //Set state to SEND_AT
                      }
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));    //Print ERROR on LCD
                          gsmState = 0;   //Set state to SEND_AT
                          break;
                      }
                  }

                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case SEND_AT_CPIN_QUERY:
                  //Send AT+CPIN? and check for response AT+CPIN?\r\n\r\n
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseS, atCommandC, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);

                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseJ))) {   //OK received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));   //SIM Ready on LCD
                      gsmState = 2;   //Set state AT_COPS_QUERY
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseI))) { //ERROR received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg7));  //Print SIM Removed on LCD
                      gsmState = 1;     //Set state to SEND_AT_CPIN_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);

                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE)); //Print OK on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY
                      }
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {  //ERROR received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 1; //Set state to SEND_AT_CPIN_QUERY
                          break;
                      }
                  }

                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case AT_COPS_QUERY:
                  //Send AT+COPS? and check for "AT+COPS?\r\n\r\n+COPS: "
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseP, atCommandB, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);

                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseF))) {  //"AT+COPS?\r\n\r\n+COPS: 0\r\n\r\nOK\r\n"; received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                      gsmState = 2;   //Set state to AT_COPS_QUERY
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseG))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47201\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgB));  //Print "Dhi Mobile" on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseH))) { //"AT+COPS?\r\n\r\n+COPS: 0,2,47202\r\n\r\nOK\r\n" received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgC));  //Print Oordeoo on LCD
                      gsmState = 3;   //Set state to AT_CSQ_QUERY
                      break;
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));    //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);

                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) {  //ATH OK received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY
                      }
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 2; //Set state to AT_COPS_QUERY
                          break;
                      }
                  }

                  gsmBufferIndex = 0;    //Clear uart buffer index
                  gsmAttempt = 0;        //Clear attempt counter
                  break;                 //break
            case AT_CSQ_QUERY:
                  //Send AT+CSQ? and check for response AT+CSQ\r\n\r\n+CSQ:
                  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseK, atCommandD, &gsmBufferIndex, &gsmAttempt, 0);
                  DelayXSec(1);

                  if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseK))) { //AT+CSQ\r\n\r\n+CSQ:  received
                      Extract_Signal_Strength(gsmBuffer, signalStrength); //Extract Singnal Strength

                      signal_strength = atoi(signalStrength);   //Convert Signal Strength Ascii to integer

                      if(signal_strength == 99) {       //Signal Strength is 99
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg1));   //Print No Network on LCD
                          gsmState = 3;   //Set state to AT_CSQ_QUERY
                      }
                      else {
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgA));
                          gsmState = 4;   //Set state to next state - (not coded)
                      }
                  }
                  else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse5))) {  //RING received
                      LCD_Out(2,1,CopyConst2Ram(buffer1, msgD));  //Print Incoming Call on LCD
                      //Send ATH to hang-up call and check for ATH
                      GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseQ, atCommandE, &gsmBufferIndex, &gsmAttempt, 0);
                      DelayXSec(1);

                      if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponseO))) { //ATH OK received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msgE));  //Print OK on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY
                      }
                      else if(strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse2))) { //ERROR received
                          LCD_Out(2,1,CopyConst2Ram(buffer1, msg9));  //Print ERROR on LCD
                          gsmState = 3;  //Set state to AT_CSQ_QUERY
                          break;
                      }
                  }

                  gsmBufferIndex = 0;
                  gsmAttempt = 0;
                  break;
            case 4:
                  break;
        };
    }
}
Untitled.png
Your last code (Above) and all it does is the "F"
No commands no LCD display..nothing NADA...!!!
*****************************************************************
Below is my version I tried so far
C:
Configuration Registers ;
CONFIG1 :$2007 : 0x23F4
CONFIG2 :$2008 : 0x0700
Oscillator : Internal 8MHz. RA6 & RA7 as Digital I/O.
Watchdog Timer : Disabled.
Power-up Timer : Disabled.
Master Clear : Enabled.
Code Protection : Disabled.
Data Protection : Disabled.
Brown Out Reset : Enabled at 4.0V.
Int. Ext. Switch : Disabled.
Failsafe CLK : Disabled.
LVP : Disabled.
ICD : Disabled.
Flash Program Self Write: Disabled.
********************************************************************************
Experimenting on how to send AT commands via UART1 using RX and TX.
checking the response to send commands through UART to a GSM Modem vis
OPEN AT commands.
Learning process to find whether I can command the modem to send SMS to
multiple hone numbers.
The Display is a 20x4 LCD in 4 bit mode used to show various functions
*/
// lcd module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// end lcd module connections
// lcd interface messages



// AT Commands with CR "\r"
const char ATC00[] = "AT\r";          // Checking Response (Just OK)
const char ATC01[] = "ATE1\r";        // Enable Echo   (Just OK)
const char ATC02[] = "AT+IPR=9600\r"; // Set 9600 Baud
const char ATC03[] = "AT+CMGF=1\r";   // Change to Text mode (Just OK)
const char ATC04[] = "AT+CSQ\r";      // Read the RX strength (+CSQ: xx,xx & OK)
const char ATC05[] = "AT+COPS?\r";    // SIM Registeration  (+COPS: x,x,xxxxx & OK)
const char ATC06[] = "AT+CPIN?\r";    // SIM Status  (+CPIN: READY)
const char ATC07[] = "AT+CMGD=1,4\r"; // Delete All SMS (Just OK after 10 sec)

const char ATC10[] = "ATH\r";         // Hang Up Incoming
// Numbers to send SMS

// Modem Response Commands to Parse
const char ATR01[] = "\r\nOK\r\n";
const char ATR02[] = "ERROR";
const char ATR03[] = "> ";
const char ATR04[] = "RING\r\n\r\n";
const char ATR05[] = "AT+CPIN?\r\n\r\n+CPIN: READY";
const char ATR06[] = "AT+COPS?\r\n\r\n+COPS: 0";
const char ATR07[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47201";
const char ATR08[] = "AT+COPS?\r\n\r\n+COPS: 0,2,47202";
const char ATR09[] = "AT\r\n\r\nOK\r\n";
const char ATR10[] = "ATH\r\n\r\nOK\r\n";
const char ATR11[] = "ATH\r\n\r\n";
//const char ATR12[] = "";
//const char ATR13[] = "";

// ******* LCD Message (ROM Storage, Saves RAM) ********************************
char String[21];//Declare array set to max size required plus 1 \
(for terminator]for copying into, 20 characters + 1 = 21. (20 for 20X4 LCD)
// Copy CONST to RAM string
char * Const2Ram(char * Rifaa, const char * Shi){
char * R;
R = Rifaa;
for(;*Rifaa++ = *Shi++;);
return R;
}
// Modem Status LCD Display
const char MSLDCL[] = "            ";
const char MSLD01[] = "Signal :";
const char MSLD02[] = "-";
const char MSLD03[] = "dBm.";
const char MSLD04[] = "SIM    :";     //  AT+CMGS=\"0000000000\"\r";
const char MSLD05[] = " Ready.   ";
const char MSLD06[] = "ERROR!";
const char MSLD07[] = "Network:";
const char MSLD08[] = " DhiMobile. ";
const char MSLD09[] = " Ooredoo.   ";
const char MSLD10[] = "Detecting!";
const char MSLD11[] = "Registering!";
//--------------
const char msg8[] = "OK         ";

#define Send_AT 0
#define CPIN_Query 1
#define COPS_Query 2
#define CSQ_Query 3

char GSM_Buffer[40];
char buffer1[50];
char SMS[30];
char SMS_Index[4];
char SignalStrength[5];
char GSM_Attempt = 0, GSM_State = 0;
unsigned int gsmBufferIndex = 0, Signal_Strength = 0;

void DelayXSec(unsigned long int sec){
  while(sec != 0){
    Delay_ms(1000);
    --sec;
  }
}

void Extract_Signal_Strength(char *s1, char *s2){
  unsigned int i = 0;
  while(*s1) {
    if(*s1 == '\n')
      ++i;
      *s1++;
    if(i == 2)break;
  }
  while(*s1 != ' '){
    *s1++;
  }
  *s1++;
  while(*s1 != ','){
    *s2++ = *s1++;
  }
  *s2 = '\0';
}
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, \
                            const char *s4, unsigned int *idx, \
                            char *GSM_Attempt, char clr){
  while(strstr(s1, Const2Ram(s2, s3)) == 0) {
    UART1_Write_Text(Const2Ram(s2, s4));
    DelayXSec(2);
    (*idx) = 0;
    if(clr)memset(GSM_Buffer, '\0', sizeof(GSM_Buffer));
      (*GSM_Attempt) = 0;
    (*idx) = 0;
  }
}
void main(){
OSCCON = 0x77;  // 8MHz Internal Osc.
ANSEL = 0x00;   // D I/O
ANSELH = 0x00;  // Rest as Digital IO.
CM1CON0 = 0x07; // Disable Comparator 1.
CM2CON0 = 0x07; // Disable Comparator 2.
PORTA = 0x00;      // Clear PORT A.
PORTB = 0x00;      // Clear PORT B.
PORTC = 0x00;      // Clear PORT C.
PORTD = 0x00;     // Clear PORT D.
PORTE = 0x00;      // Clear PORT E.

TRISA = 0xFF;   // PORT A as Input.
TRISB = 0x00;   // PORT B as Output.
TRISC = 0x38;   // RC <0-2> As Output, Rest as Input
// PORTDimg = 0x2F; // PORT D Shadow, Identical to TRISD.
TRISD = 0xFF;   //
TRISE = 0xFF;   // PORT E as Input.
//------- Initialte ADC & LCD & UART -------------------------------------------
ADC_Init();              // Innitiate ADC.
VCFG0_bit = 1;           // Externel Vref +.
VCFG1_bit = 0;           // Internal Vref - as GND.
Lcd_Init();              // Innitiate LCD.
Lcd_Cmd(_LCD_CLEAR);     // Clear LCD.
Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor OFF.
Delay_ms(100);              // Wait to Stabilize.
UART1_Init(9600);           // Initialize UART module at 9600 baud.
Delay_ms(100);              // Wait for UART module to stabilize.
//------- Clear IRQ and Fire up the Timers -------------------------------------
OPTION_REG = 0x81;          // No pullups, Timer 0 at Prescaler 4.
INTCON = 0;                 // Disable All Interrupts First.
PIE1 = 0;                   // Disable All Peripheral Interrupts.
PIE2 = 0;                   // Disable All Peripheral Interrupts.
INTCON.T0IE = 1;            // Enable the Timer0 Interrupt.
INTCON.T0IF = 0;            // Clear Timer 0 Register Overflow Flag.
// TMR0 = TMR0set;           // Load Timer 0 with Preset.
T1CON = 0x30;               // No gate, Int Tcyc/8, TMR1 Stopped.
TMR1H = TMR1L = 0;          // Timer counts up to CCP value.
CCPR1 = 625;                // 1usTcyc * 8 Prescale * 625 = 5msec.
CCP1CON = 0x0B;             // NO Hardware PWM.
TMR1IF_bit = 0;             // PIR1 reg, Clear the TMR1 Overflow IRQ.
CCP1IF_bit = 0;             // Clear TMR1 IRQ flag bit.
RCIF_bit = 0;               // Clear Rx Interrupt Flag .................
T1CON.TMR1ON = 1;           // Start TIMER 1.
TMR1IE_bit = 1;             // Enable the Timer1 overflow Interrupt.
PIE1.RCIE = 1;              // Enable Rx Interrupt .................
CCP1IE_bit = 1;             // Enable the CCP1 Interrupt.
INTCON.GIE = 1;             // Global Interrupt Enabled.

while(1){
   switch(GSM_State){
     case Send_AT:  // Send AT and check for AT\r\n\r\n
       GSM_Send_Const_Command(GSM_Buffer, buffer1, ATR11, ATC00, \
                              &gsmBufferIndex, &GSM_Attempt, 0);
       DelayXSec(1);
         if(strstr(GSM_Buffer, Const2Ram(buffer1, ATR09))) { //OK received
         LCD_Out(2,1,Const2Ram(buffer1, msg8)); //Print OK on LCD
         GSM_State = Send_AT; //Set state to CPIN_QUERY
     }
   }
}
}
/*
#define Send_AT 0
#define CPIN_Query 1
#define COPS_Query 2
#define CSQ_Query 3
*/


void interrupt(){
  if(INTCON.T0IF){
    INTCON.T0IF = 0;
  //  BLAAAAAAAAH:
  }
  if(CCP1IE_bit){
    if(CCP1IF_bit){
      CCP1IF_bit = 0;

  // More BLAAAAAAH;
    }
  }
  if((RCIE_bit) && (RCIF_bit)) {
    if(OERR_bit) {
      OERR_bit = 0;
      CREN_bit = 0;
      CREN_bit = 1;
    }
     GSM_Buffer[gsmBufferIndex++] = UART1_Read();
     GSM_Buffer[gsmBufferIndex] = '\0';

      RCIF_bit = 0;
  }
}
Hey...I see OK on LCD
PIC responding to AT command
One thing I noticed is when I reset it using the MCLR, display comes back with "OK'
and if I cycle power to the PIC it is working normally...The MCLR is not resetting the program..that should not happen....MCLR should reset the complete program.!
 
Last edited:

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Yes I compiled ur version and Realterm is giving just a "NULL" char and Fe sorta looking thing.
I posted the exact code you gave..I compiled to a F887 and it programed and gave me nothing. o_O

I tried my version ( it is also posted )
and it is issuing AT every second...I replied through realterm and OK came on LCD

Funny thing is I wrote the code by looking at yours. :confused:
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
One thing for sure is that the IO and all setting's before while(1) begins is same in both codes.
C:
 OSCCON = 0x77;  // 8MHz Internal Osc.
ANSEL = 0x00;   // D I/O
ANSELH = 0x00;  // Rest as Digital IO.
CM1CON0 = 0x07; // Disable Comparator 1.
CM2CON0 = 0x07; // Disable Comparator 2.
PORTA = 0x00;      // Clear PORT A.
PORTB = 0x00;      // Clear PORT B.
PORTC = 0x00;      // Clear PORT C.
PORTD = 0x00;     // Clear PORT D.
PORTE = 0x00;      // Clear PORT E.

TRISA = 0xFF;   // PORT A as Input.
TRISB = 0x00;   // PORT B as Output.
TRISC = 0x38;   // RC <0-2> As Output, Rest as Input
// PORTDimg = 0x2F; // PORT D Shadow, Identical to TRISD.
TRISD = 0xFF;   //
TRISE = 0xFF;   // PORT E as Input.
//------- Initialte ADC & LCD & UART -------------------------------------------
//ADC_Init();              // Innitiate ADC.
VCFG0_bit = 1;           // Externel Vref +.
VCFG1_bit = 0;           // Internal Vref - as GND.
Lcd_Init();              // Innitiate LCD.
Lcd_Cmd(_LCD_CLEAR);     // Clear LCD.
Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor OFF.
Delay_ms(100);              // Wait to Stabilize.
UART1_Init(9600);           // Initialize UART module at 9600 baud.
Delay_ms(100);              // Wait for UART module to stabilize.
//------- Clear IRQ and Fire up the Timers -------------------------------------
OPTION_REG = 0x81;          // No pullups, Timer 0 at Prescaler 4.
INTCON = 0;                 // Disable All Interrupts First.
PIE1 = 0;                   // Disable All Peripheral Interrupts.
PIE2 = 0;                   // Disable All Peripheral Interrupts.
INTCON.T0IE = 1;            // Enable the Timer0 Interrupt.
INTCON.T0IF = 0;            // Clear Timer 0 Register Overflow Flag.
// TMR0 = TMR0set;           // Load Timer 0 with Preset.
T1CON = 0x30;               // No gate, Int Tcyc/8, TMR1 Stopped.
TMR1H = TMR1L = 0;          // Timer counts up to CCP value.
CCPR1 = 625;                // 1usTcyc * 8 Prescale * 625 = 5msec.
CCP1CON = 0x0B;             // NO Hardware PWM.
TMR1IF_bit = 0;             // PIR1 reg, Clear the TMR1 Overflow IRQ.
CCP1IF_bit = 0;             // Clear TMR1 IRQ flag bit.
RCIF_bit = 0;               // Clear Rx Interrupt Flag .................
T1CON.TMR1ON = 1;           // Start TIMER 1.
TMR1IE_bit = 1;             // Enable the Timer1 overflow Interrupt.
PIE1.RCIE = 1;              // Enable Rx Interrupt .................
CCP1IE_bit = 1;             // Enable the CCP1 Interrupt.
INTCON.GIE = 1;             // Global Interrupt Enabled.
This is same for your version and mine and it is the one I am using for my project and it needs to be that. The commented out ones are not in use because those code blocks are not in use for experiment the GSM coding.

I know for sure it is not the problem.
 

jayanthd

Joined Jul 4, 2015
945
Ok. Morning I will test the code on hardware with SIM900. As AT commands and responses are same I think it will work. I will post the realterm responses.
 

jayanthd

Joined Jul 4, 2015
945
In my project settings did you selected INTRC Oscillator type and clock frequency 8 MHz ? It was actually XT type 4 MHz.

Can you show the RealTerm Screenshot showing the responses for my code ?
 

jayanthd

Joined Jul 4, 2015
945
I found the culprit.

C:
void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        asm clrwdt
        --sec;
    }
}
Your WDT settings are different than mine. Mine was for 2.3 seconds. WDT is resetting and hence you see only AT.

Change to

Code:
OPTION_REG = 0x8F;
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
In your code

C:
[LIST=1]
[*]switch(GSM_State){
[*]     case Send_AT: // Send AT and check for AT\r\n\r\n
[*]       GSM_Send_Const_Command(GSM_Buffer, buffer1, ATR11, ATC00, \
[*]                             &gsmBufferIndex, &GSM_Attempt, 0);
[*]       DelayXSec(1);
[*]         if([URL='http://www.opengroup.org/onlinepubs/009695399/functions/strstr.html']strstr[/URL](GSM_Buffer, Const2Ram(buffer1, ATR09))) { //OK received
[*]         LCD_Out(2,1,Const2Ram(buffer1, msg8)); //Print OK on LCD
[*]         GSM_State = Send_AT; //Set state to CPIN_QUERY
[*]     }
[*]   }
[*]
[/LIST]
After LCD prints OK then GSM_State is again set to Send_AT and this is causing repeated sending of AT and repeated printing of OK.

After OK is received then you have to change the GSM_State to next state.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
What you do not understand is my settings needs to be that what I showed so my original code works, I have to use the GSM on those settings.

As for the test one I did it is to see if your coding is applicable.
I believe it is.
So I will finish my version looking at yours.
 
Top