Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] how to turn on a stepper motor using gsm modem

Status
Not open for further replies.

atoivan

Newbie level 3
Newbie level 3
Joined
May 20, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,331
Hi to all...
I am doing a project in which i will be interfacing a gsm modem , a keypad , and a stepper motor with PIC microcontroller(16f877a)
i should be able to send a predefined sms to the gsm modem so that the stepper motor will turn.

i am using MikroC IDE
my cell phone as the modem
and Proteus 8 Professional

i do not have any problem with the keypad i have the code and it working fine.

my problem is which AT command am i to use and how do i save the predefined string in the modem
so the stepper motor can turn NO

NB:
i keep having this error any time i compile my project
diagnostics: 0 127 All files Compiled in 156 ms
error: 4 355 Redefinition of 'ATdis [GSM.c] '. 'ATdis' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 7 355 Redefinition of 'atm1 [GSM.c] '. 'atm1' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 8 355 Redefinition of 'atm2 [GSM.c] '. 'atm2' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 9 355 Redefinition of 'atm3 [GSM.c] '. 'atm3' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 10 355 Redefinition of 'smsNun [GSM.c] '. 'smsNun' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 13 355 Redefinition of 'ok_gsm [GSM.c] '. 'ok_gsm' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 14 355 Redefinition of 'RELAY_OK [GSM.c] '. 'RELAY_OK' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 16 355 Redefinition of 'parse_type [GSM.c] '. 'parse_type' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 17 355 Redefinition of 'relay_no [GSM.c] '. 'relay_no' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 18 355 Redefinition of 'relay [GSM.c] '. 'relay' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 20 355 Redefinition of 'phone_number [GSM.c] '. 'phone_number' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 22 355 Redefinition of 'gsm_state [GSM.c] '. 'gsm_state' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 23 355 Redefinition of 'response_rcvd [GSM.c] '. 'response_rcvd' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 24 355 Redefinition of 'responseID [GSM.c] '. 'responseID' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 24 355 Redefinition of 'response [GSM.c] '. 'response' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 355 Redefinition of '?ICS_smsNun [GSM.c] '. '?ICS_smsNun' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 355 Redefinition of '?ICS_gsm_state [GSM.c] '. '?ICS_gsm_state' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 355 Redefinition of '?ICS_response_rcvd [GSM.c] '. '?ICS_response_rcvd' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 355 Redefinition of '?ICS_responseID [GSM.c] '. '?ICS_responseID' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 355 Redefinition of '?ICS_response [GSM.c] '. '?ICS_response' already defined in 'GSM_and_Keypad_lock.c' GSM.c
error: 0 102 Finished (with errors): 29 May 2014, 03:38:51 GSM_and_Keypad_lock.mcppi

and this are my codes so far can any one help me with this .
code for the main project
Code:
# include "GSM.h"

unsigned short kp, cnt, oldstate = 0;

     void code_enter();
     void code_read();
     void code_write();
     void change_code();
     void read_chose();
char code1[15] ,user1[4];
char chose[2];


int i = 0, j;
  int ii=0;
int w1;

//keypad module connections
char keypadPort at PORTD;
//end keypad module connections

//lcd module connections
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RA2_bit;
sbit LCD_D5 at RA3_bit;
sbit LCD_D6 at RA4_bit;
sbit LCD_D7 at RA5_bit;

// Pin direction
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISA2_bit;
sbit LCD_D5_Direction at TRISA3_bit;
sbit LCD_D6_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA5_bit;
                                                     /*

extern unsigned char keypad_read(void);  //Read from the keypad.
extern unsigned char ASCII_value (unsigned char key_number); //convert the numeric value of key scan to proper ASCII code. Apply only for TB40A onboard 4x4 keypad
extern unsigned char keypad_wait(void); //Wait until the key is pressed and released.

                                                */

/**************************************************************************************************************************************** */


   //hint LAT for out ,PORT for Inputs

void main() {
     TRISB = 0xF0;
     ADCON1 = 0x0F;                                 // All AN pins as digitall

      // set ALL pinS to zero,

       TRISA = 0x00;
       TRISB = 0x00;
       TRISC = 0x00;
       TRISD = 0x00;
       
       PORTA = 0x00;
       PORTB = 0x00;
       PORTC = 0x00;
       PORTD = 0x00;
       
       Lcd_Init();                              // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);                     // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off

     code_read();
   
      Lcd_Cmd(_LCD_CLEAR);                            // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);                    // Cursor off
Lcd_Out(1, 3, "WELCOME TO");                 // Write message text on Lcd
Lcd_Out(2, 6, "USE");                         // Write message text on Lcd
delay_ms(2000) ;
Lcd_Cmd(_LCD_CLEAR);
                                           // Write message text on Lcd
Lcd_Out(2, 1, "KEYLESS DOORLOCK");                      // Write message text on Lcd
delay_ms(1000) ;
Lcd_Cmd(_LCD_CLEAR);

     cnt = 0;

 LCD_OUT(1 , 2,"Keypad --> 1");
 LCD_OUT(2 ,1, "SMS --> 2");
         ii =0;
          read_chose();
 if (chose[ii] == '1')
 {
 goto L1;
            }
 else if (chose[ii] == '2')
 {
    goto L2;
            }
/****************************************** KeyPad ******************************** */
  
L1: do
{
    Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2, 3, "Press *");
        i = 0;
        code_enter();
        if(code1[0] == 42){      // *
        Lcd_Cmd(_LCD_CLEAR);       // clear display
        Lcd_Out(1, 1, "Enter Code");
        //delay_ms(500);
        i = 0;
        code_enter();
        code_enter();
        code_enter();
        code_enter();
        code_enter();
        if(code1[0] == '1' && code1[1] == '1'  && code1[2] == '1' && code1[3] == '1' && code1[4] == '1' ){      // master code
        code_enter();
        if(code1[5] == 35){      // #
        change_code();

             }
                 }
else if(cnt<3 && code1[0] == user1[0] && code1[1] == user1[1] && code1[2] == user1[2] && code1[3] == user1[3] && code1[4] == 35){       // save password verification

Lcd_Cmd(_LCD_CLEAR);                      //Lcd clear
Lcd_out(1,4,"Door Open");                        //Door Open


EEPROM_Read(0x21);
w1 = 1;
Delay_ms(20);
EEPROM_Write(0X21, w1);
Delay_ms(20);
EEPROM_Read(0x21);

                       //move stepper motor to ON position
PORTC=0x80;
Delay_ms(500);
PORTB=0x40;
Delay_ms(500);
PORTB=0x20;
Delay_ms(500);
PORTB=0x10;
Delay_ms(500);

Delay_ms(9000);

LCD_Cmd(_LCD_CLEAR);
Lcd_out(1,4,"Door Close");

EEPROM_Read(0x21);
w1 = 0;
Delay_ms(20);
EEPROM_Write(0x21, w1);
Delay_ms(20);
EEPROM_Read(0x21);

//move stepper motor to OFF position
PORTB=0x10;
Delay_ms(500);
PORTB=0x20;
Delay_ms(500);
PORTB=0x40;
Delay_ms(500);
PORTB=0x80;
Delay_ms(500);

Delay_ms(2000);

                  }
else{
cnt++;
Lcd_Cmd(_LCD_CLEAR);     //clear display
Lcd_Out(1, 1, "Wrong Password");
Delay_ms(2000);

                                         }
if(cnt>=3)
goto L1;
                  }
} while(1);

/**************************** sms************************************************* */
L2:  do
{
  Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(2, 1, "Wait For SMS");
   
  parse_type = ok_gsm;
   
    // enable uart rx interrupt
  RCIE_bit = 1;
  PEIE_bit = 1;
  GIE_bit  = 1;
                  // turn on module

    UART1_init(9600);                             // initialize USART module

  Wait();

         while(1) {
    send_to_modem("AT");                              // send "AT" string until gsm sets up its baud rade
    Delay_ms(100);                               // and gets it correctly
    if (get_response() == ok_gsm)                // if gsm says "OK" on our baud rate we got it
      break;
  }
   // disable command echo
 send_to_modem(ATdis);
  wait_response(ok_gsm);
  
  // set text mode
 send_to_modem(atm1);
  wait_response(ok_gsm);
  
  

}while(1);


}



 /************************************************************************************ */

void code_enter()
{
kp =0;    //Reset key code variable

     do              //wait for key to be pressed and released
     //kp = Keypad_Key_Press();     // store key code in kp variable
     kp = Keypad_Key_Click();       // store key code in kp variable
     while (!kp);
     
     //prepare value for output, transform key to it's ASCII value
     switch (kp)
     {
     case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
     case  2: kp = 50; break; // 2
     case  3: kp = 51; break; // 3
     case  5: kp = 52; break; // 4
     case  6: kp = 53; break; // 5
     case  7: kp = 54; break; // 6
     case  8: kp = 55; break; // 7
     case  9: kp = 56; break; // 9
     case 10: kp = 42; break; // *
     case 11: kp = 48; break; // 0
     case 12: kp = 35; break; // #
}

if (kp!=oldstate)
{
i =1;
}
else
{
i++;
}
if (i==255)
{
i=0;
LCD_OUT (2,  10, " ");
}
code1[i] = kp;
     Lcd_Chr(2, i+1, code1[i]);        //Print key ASCII value on Lcd
     i++;
     }
void code_read(){      //read data from eeprom
     Delay_ms(20);
     user1[0] = EEPROM_Read(0x00);            // Read data from address 0
     Delay_ms(20);
     user1[1] = EEPROM_Read(0x01);            // Read data from address 1
     Delay_ms(20);
     user1[2] = EEPROM_Read(0x02);            // Read data from address 2
     Delay_ms(20);
     user1[3] = EEPROM_Read(0x03);            // Read data from address 3
     Delay_ms(20);
     }
     
     void code_write()
     {       //write data to eeprom
     Delay_ms(20);
     EEPROM_Write(0x00,code1[0]);           // Write data to address 0
     Delay_ms(20);
     EEPROM_Write(0x01,code1[1]);           // Write data to address 1
     Delay_ms(20);
     EEPROM_Write(0x02,code1[2]);           // Write data to address 2
     Delay_ms(20);
     EEPROM_Write(0x03,code1[3]);           // Write data to address 3
     }

void change_code()
{
     Lcd_Cmd(_LCD_CLEAR);           // clear display
     Lcd_Out(1,1, "Enter New Code:");

     i = 0;

      code_enter();
      code_enter();
      code_enter();
      code_enter();


     code_write();
     delay_ms(20);
     code_read();
     delay_ms(20);

     Lcd_Cmd(_LCD_CLEAR);           // clear display
     Lcd_Out(1,1, "New Code Set");
     Delay_ms(5000);
     }
void read_chose()
{
kp =0;    //Reset key code variable

     do              //wait for key to be pressed and released
     //kp = Keypad_Key_Press();     // store key code in kp variable
     kp = Keypad_Key_Click();       // store key code in kp variable
     while (!kp);

     //prepare value for output, transform key to it's ASCII value
     switch (kp)
     {
     case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
     case  2: kp = 50; break; // 2
     case  3: kp = 51; break; // 3
     case  5: kp = 52; break; // 4
     case  6: kp = 53; break; // 5
     case  7: kp = 54; break; // 6
     case  8: kp = 55; break; // 7
     case  9: kp = 56; break; // 9
     case 10: kp = 42; break; // *
     case 11: kp = 48; break; // 0
     case 12: kp = 35; break; // #
}

if (kp!=oldstate)
{
ii =1;
}
else
{
ii++;
}
if (ii==255)
{
ii=0;
LCD_OUT (2,  10, " ");
}
chose[ii] = kp;
     Lcd_Chr(2, ii+1, chose[ii]);        //Print key ASCII value on Lcd
     ii++;
     }

GSM header file

Code:
/#ifndef GSM_H

// set of AT commands
const char ATdis[] = "ATE0";                      // disable command echo

// sms AT commands
const char atm1[] = "AT+CMGF=1";                 // Command for setting SMS text mode
const char atm2[] = "AT+CMGR=1";                 // Command for reading message from location 1 from inbox
const char atm3[] = "AT+CMGD=1,4";               // Erasing all messages from inbox
   char smsNun[] = "AT+CMGS=\"";                // sends SMS to desired number

// responses to parse
const ok_gsm    = 0;
const RELAY_OK  = 1;

char parse_type;                                 // Determins parsing type, OK response or SMS message
char relay_no[3];                                // Message that contains relay number
char relay;                                      // Relay number
// phone number string
char phone_number[20];

char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1;

void interrupt();
void send_to_modem(const char *s);
short get_response();
void wait_response(char rspns);
void wait();
char Get_Relay_Number();

//#endif
[code]
#include "GSM.h"

// uart rx interrupt handler
void interrupt(){
char tmp;
if (RCIF_bit == 1) { // do we have uart rx interrupt request?
tmp = UART1_Read(); // get received byte

if(parse_type!=RELAY_OK){ // If we are parsing OK message
// process reception through state machine
// we are parsing only "OK" response
switch (gsm_state) {
case 0: {
response = -1; // clear response
if (tmp == 'O') // we have 'O', it could be "OK"
gsm_state = 1; // expecting 'K'
break;
}
case 1: {
if (tmp == 'K') { // we have 'K' ->
response = ok_gsm; // we have "OK" response
gsm_state = 2; // expecting CR+LF
}
else
gsm_state = 0; // reset state machine
break;
}
case 2: {
if (tmp == 13) // we have 13, it could be CR+LF
gsm_state = 3; // expecting LF
else
gsm_state = 0; // reset state machine
break;
}
case 3: {
if (tmp == 10) { // we have LF, response is complete
response_rcvd = 1; // set reception flag
responseID = response; // set response ID
}
gsm_state = 0; // reset state machine
break;
}
default: { // unwanted character
gsm_state = 0; // reset state machine
break;
}
}
}
if(parse_type==RELAY_OK){ // Parsing received message
switch (gsm_state) {
case 0: {
response = -1; // clear response
if (tmp == 'R') // we have 'R'
gsm_state = 10; // expecting 'e'
break;
}
case 10: {
if (tmp == 'e') // we have 'e'
gsm_state = 11; // expecting 'l'
else
gsm_state = 0; // reset state machine
break;
}
case 11: {
if (tmp == 'l') // we have 'l'
gsm_state = 12; // expecting 'a'
else
gsm_state = 0; // reset state machine
break;
}
case 12: {
if (tmp == 'a') // we have 'a'
gsm_state = 13; // expecting 'y'
else
gsm_state = 0; // reset state machine
break;
}
case 13: {
if (tmp == 'y') { // we have 'y'
gsm_state = 14; // expecting first digit
}
else
gsm_state = 0; // reset state machine
break;
}
case 14: {
relay_no[0] = tmp; // setting first digit into array
gsm_state = 15; // expecting second digit
break;
}
case 15: {
relay_no[1] = tmp; // setting first digit into array
relay_no[2] = 0; // setting null
response = RELAY_OK; // we have relay response
response_rcvd = 1; // set reception flag
responseID = response; // set response ID
gsm_state = 0; // reset state machine
break;
}
default: { // unwanted character
gsm_state = 0; // reset state machine
break;
}
}
}
}
}

// send ATC command
void send_to_modem(const char *s)
{
while(*s) { // send command string
UART1_Write(*s++);
}
UART1_Write(0x0D); // terminate command with CR
}

// get GSM response, if there is any
short get_response() {
if (response_rcvd) {
response_rcvd = 0;
return responseID;
}
else
return -1;
}

// wait for GSM response
void wait_response(char rspns) {
while (get_response() != rspns)
;
}

// pause
void wait() {
Delay_ms(3000); // 3 seconds delay
}

[/code]

Have attach an image of the circuit

 

hello this is a good project,is it possible that you can help me to just demonstrate the steps for implementing such projects;me I want to light the lamps using GSM and AT Commands.

Please help.
Thanks!!
 

are you including the gsm.h file in a number of .c files in your project?

the following statement in gsm.h
Code:
const char atm1[] = "AT+CMGF=1";                 // Command for setting SMS text mode

defines atm1[], i.e. in effect allocates storage for it - including it in a number of .c files could give errors
 

Hello,thanks for your response,this is my first time I am going to implement such projects with AT Commands and PIC;can you please advise me on different steps to follow?
And those files to be used.

Thank you!!

- - - Updated - - -

Hello,can you help me to think far,I am just implementing the same project for turning bulbs On using as SMS via GSM;but I am having confusion,where may I insert those codes of GSM Header ?what about those of the first file yo created here in your project?

Thank you!!
 

try moving your variable definitions such as
Code:
const char atm1[] = "AT+CMGF=1";
into gsm.c
you probably don't need them in any other files - if you do declare them extern in gsm.h
Code:
extern const char atm1[] ;
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top