CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Interfacing RTC and GSM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

Interfacing RTC and GSM
PostPosted: Tue Apr 03, 2012 12:13 am     Reply with quote

hi, I am tying to interface RTC DS1307 and GSM modem to control electrical devices. Both are working independently but not when integrated.
Here is my code.
Code:

#include <16F877A.h>
#define max 65534
#device *=16
#device adc=10

#include"pic.h"
#include"lcd1.h"
#include<string.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3)
#include "ds1307.c"

#define h_sw rc0
#define m_sw rc1
#define s_sw rc2
#define set_sw
#define pir rd3
#define ir rd2

char num[15];
char msg[25];
char no3[12]="xxxxx";
char noofc[12]="xxxx";
int hon=0,mon=0,s,l;
int sethron();
int setmnon();
int sethrof();
int setmnof();

void mode100();
void mode0();
void mode50();

int hof=0,mof=0;
int setflg=0;
int lflg=0;
char ctr1[]="**D1ON";
char ctr2[]="**D2ON";
char ctr3[]="**D1OF";
char ctr4[]="**D2OF";
 
 //////////////////////////////////////////////
 
 msg_read(char *num,char *msg)
{   
   char ch;
   int i,j,m;
   int1 flg=0;
   unsigned long  time;
   
   while(kbhit())
{
      ch=getch();
   if(ch=='"')
   {
   while(ch!= '1')         ///Loop to reach to No
   {
      
   if(kbhit())
      {
         ch=getch();
         time=0;
      }
      else
      time++;
      if(time > max)
      return(0);

   
   
               //Extracting NO
         
         for(j=0;j<=12;)
            {
               if(kbhit())
                  {      
                     num[j]=getch();
                     j++;
                  }
            }      

            num[j]='\0';
      //   prints(num);
   //Extacting Message
         j=0;
   
         while(1)
      {   
            
            if(kbhit())
                  {   
                     ch=getch();
                     if(ch==10)
                     flg=1;
                     time=0;
                        
                        if(flg)
                           {
                              if(j>0)
                                 {
                                    if(ch==10)
                                       {   j-=2;
                                          msg[j]='\0';

         go(64);
         //prints(msg);
         
                                          return(1);
                                       }
                                 msg[j-1]=ch;
                                 }
                        
                              j++;
                            }   
                  }//kbhit
         
         }
      }
}   
}
}
sms_cntrl()
 {
    msg_read(num,msg);
    if(msg_read(num,msg)==1)
      {
   
                 if(!strcmp(ctr1,msg))
                 {
                    //mode50();
                    delay_ms(800);
                    clrscr();
                    delay_ms(1000);
                 //   sms1_on();
                    lflg=1;   
                 }
                 else if(!strcmp(ctr3,msg))
                 {
                   // mode0();
                    delay_ms(800);
                    clrscr();
                    delay_ms(1000);
                //    sms1_of();
                    lflg=0;
                   }
                   else
                   {
                   delay_ms(1300);
                   clrscr();
                //   sms2_wrong();
                   }
               
}
}

 
void sms_start(char*ch)
     {
   printf("AT+CMGS=");
   printf("\"%s\"\r",ch);
   }
    void sms_send(void)
    {
    putchar(26);
    delay_ms(200);
     }
    void sms1_on()
    {
    sms_start(num);
    printf("Device1 is ON");
    sms_send();
    }
   
   void sms2_on()
    {
    sms_start(num);
    printf("Device2 is ON");
    sms_send();
   
   }
   void sms2_wrong()
    {
    sms_start(num);
    printf("Wrong Format,Try **D_ON/OFF");
    sms_send();
   
   }
   void sms2_me()
    {
    sms_start(no3);
    printf("PAACET PJT IS ON");
    sms_send();
   
   }   
   void sms2_ofc()
   {
      sms_start(noofc);
       printf("Street Light fault");
       sms_send();
}
   void sms1_of()
    {
    sms_start(num);
    printf("Device1 is OFF");
    sms_send();
    }
   void sms2_of()
    {
    sms_start(num);
    printf("Device2 is OFF");
    sms_send();
    }
   
    float ldr()
{
   set_adc_channel(0);
   delay_us(20);
   return(read_adc());
}      
/////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////

void main()
{
      byte sec;
         byte min;
        byte hrs;
       byte day;
       byte month;
        byte year;
         byte dow;
         char col[]=":";
         int m=35;
         int msgflg=1;

      trisb=0x00;
      trisc0=trisc1=trisc2=trisd0=1;
      trisd4=trisd5=trisd6=trisd7=0;
      trisd2=trisd3=1;

      enable_interrupts(INT_RDA);
        enable_interrupts(global);
      lcdinit();
      go(4);
      printc("xxxxx");
      go(64);
      printc("xxxxxxxxx");
      delay_ms(1000);
      clrscr();
      mode0();
      sms2_me();
      setup_adc(ADC_CLOCK_INTERNAL);
       setup_adc_ports(ALL_ANALOG);
//initialize 1307
 //ds1307_init();
 //ds1307_set_date_time(26,3,12,3,4,56,55); //day, month,year,dow,hr,min.sec

lflg=0;

        while(1)
       
        {
           if(kbhit())
          {
          
           sms_cntrl();
          
          }
          
           mode50();
            delay_ms(1000);
          
          
          
          
                sms_cntrl();
               
         
                      if(lflg)
                        {
                           if(ldr()>700)
                           {
                              if(msgflg)
                              {
                              sms2_ofc();
                               msgflg=0;
                             }
                          }
                       }
                    else
                      {
                ds1307_get_date(day,month,year,dow);
                    ds1307_get_time(hrs,min,sec);
                       if(msg_read(num,msg)==1)
                 sms_cntrl();
                   go(0);
                     print(hrs);prints(col);print(min);prints(col);print(sec);
                 delay_ms(1000);
          
                   go(76);
                  print(ldr());
                 }       
              if(ir||pir)
           {
              if(ir)
             
           mode100();
           delay_ms(2000);     
           }
              
          
///////////////////////////////////////////////////////////////////////
         
         if(setflg)
         {
            go(64);
            print(hon);prints(col);print(mon);printc("   ");
             print(hof);prints(col);print(mof);printc("   ");
              delay_ms(500);
           clrscr();
          }
       if(rd0)
          {
           printc("Press to set Time");
           sethron();
              setmnon();
              sethrof();
              setmnof();
           setflg=1;
          }
       if(hon==hrs&&mon==min)
          {
          mode50();
          delay_ms(500);
          hon=mon=0;
          setflg=0;
          lflg=1;
          if(pir||ir)
          mode100();
          else
          mode50();
          }
        if(hof==hrs&&mof==min)   
          {
          mode0();
          lflg=0;
          delay_ms(500);
          hof=mof=0;
          setflg=0;
           }
     }
}        
   
      
   
/////////////////////////////////////////////////////////////////////
      
         int sethron()
         
         {   do
            {
   
             clrscr();
            printc(" Set ON HR");
            delay_ms(500);
            
            if(rc0)
            {
            
               
               hon=hon+1;
               go(64);
               print(hon);
               delay_ms(300);
               if(hon==24)
               {
                  hon=0;
               }
            }
            
         
            else if(rc1)
            {
               hon=hon-1;
               go(64);
               print(hon);
               delay_ms(300);
               if(hon==-1)
               {
                  hon=23;
               }
            }      
         }       
    
     while(!rd0);
     return(hon);
     }
         
         
         int setmnon()
         
         {
            do
            {
         
            clrscr();
            printc(" Set ON min");
            delay_ms(500);
            
            if(rc0)
            {
            
               
               mon=mon+1;
               go(64);
               print(mon);
               delay_ms(200);
               if(mon==60)
               {
                  mon=0;
               }
               }
            
         
            else if(rc1)
            {
               mon=mon-1;
                  go(64);
                  print(mon);
               delay_ms(200);
               if(mon==-1)
               {
                  mon=59;
               }
            }      
            }       
    
     while(!rd0);
     return(mon);
     }   
    
     int sethrof()
           
         {   do
            {
   
             clrscr();
            printc(" Set OFF HR");
            delay_ms(500);
            
            if(rc0)
            {
            
               
               hof=hof+1;
               go(64);
               print(hof);
               delay_ms(200);
               if(hof==24)
               {
                  hof=0;
               }
            }
            
         
            else if(rc1)
            {
               hof=hof-1;
               go(64);
               print(hof);
               delay_ms(200);
               if(hof==-1)
               {
                  hof=23;
               }
            }      
         }       
    
     while(!rd0);
     return(hof);
     }
        int setmnof()
         
         {
            do
            {
         
            clrscr();
            printc(" Set OFF mn");
            delay_ms(500);
            
            if(rc0)
            {
            
               
               mof=mof+1;
               go(64);
               print(mof);
               delay_ms(200);
               if(mof==60)
               {
                  mof=0;
               }
               }
            
         
            else if(rc1)
            {
               mof=mof-1;
                  go(64);
                  print(mof);
               delay_ms(200);
               if(mof==-1)
               {
                  mof=59;
               }
            }      
            }       
    
     while(!rd0);
     return(mof);
     }         
           
   void mode50()
   {
      rd4=1;rd5=rd6=rd7=0;
      go(12);
      printc("m50");
   }
   void mode0()
   {
      rd4=rd5=rd6=rd7=1;
      go(12);
      printc("m0");
   }
   void mode100()
   {
      rd4=rd5=rd6=0;
      rd7=1;
      go(12);
      printc("m100");
   }
               
   
   

I am using the compiler CCS 3.36

Anyone please check these?
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 8:51 am     Reply with quote

so, what is your problem exactly?

expected results?
symptoms?

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 5:55 pm     Reply with quote

Both RTC and GSM works independently.
But GSM is not working, means messages are not received by the controller. I tried to use interrupt but, not enough space.
temtronic



Joined: 01 Jul 2010
Posts: 9161
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 04, 2012 5:47 am     Reply with quote

couple of comments...

1) whenever you use the hardware UART, be sure to add 'errors' to the uses rs232(....) code !

2) NEVER enable any interrupt without having an ISR for it ! You enable INT_RDA but I can't se the 'handler' ( ISR) for it. Program will crash...

3) your timesetting code is very similar,so codespace could be saved by creating a common function and calling it instead of all the inline code you have.

4) you've coded the ldf() function to return a float,but the ADC will only return a 10bit integer.

5) go(xx) means what ???? either I can't see it or it's missing ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group