Maker Pro
Maker Pro

pic programming help - controlling GSM to send message from PIC16F873A

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
Hi guys,
I need a help. Need to know how to program a pic to send a sms through a GSM module/ I have purchased this module, but it does not have and datashhets. it has only 3 RMC connector pins (gnd, tx., Rx.). Am using PIC16f873a and Mikro C pro for programming. Need to know how to program from the controller kit to a mobile/ number when one of the ports of the pic gets excited. please give me this information and the workflow....
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
I don't know how you think anyone will be able to help you. We don't know any more about your GSM module than you do. You need to find documentation for it.

You didn't even tell us the manufacturer or part number of the GSM module!
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
that's my problem Kris. one of my senior bought this module without spec sheets,but he is out of contact now. the only info on the module is www.sife.com. but here was no response from their side when I contacted them. And now I have to build a system with this GSM module. I cannot waste it. my only doubt is how to program this pic to send a message from my module to a mobile phone. I have seen modules sending sms by connecting to a pc by using AT instructions. but I don't know how to program in a pic directly to do it...!
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
Surely there are labels on the module? Something that looks like a part number, type or similar. Try these numbers in Google. Show us a clear photo of the module, someone here may have the information you need.

I have seen modules sending sms by connecting to a pc by using AT instructions. but I don't know how to program in a pic directly to do it...!
In the same way as on a PC: you set up the UART within the PIC for the correct data format (baudrate, parity etc.) and connect the module's Rx to the PIC's TX and vice versa. You are then ready to send AT commands byte by byte to the module and receive information in the same way.
However, you need to know which AT commands the module understands and which status messages you can read from the module.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
I get "server not found" for sife.com. All I can suggest is that you get documentation for similar units made by different companies and see whether it responds to AT commands. You should be able to test it with a PC running a terminal emulator, talking through an RS-232 port to an RS-232-to-logic-level converter IC such as a MAX232 connected to the device's TXD and RXD signals. Once you've figured out what commands to use, you use the PIC's UART to transmit and receive characters to the device.

Good luck!
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
I have attached the programming that i did from reference to the Data sheet of PIC16F873A.
need to send the msg "HAI" to a mobile number. I have programmed this so as to send a message "HAI" when PIC is powered. I don't know if this is correct please correct if I am wrong, from which I could build up a program.
 

Attachments

  • proto2.txt
    867 bytes · Views: 207
Last edited:

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
There's not much point trying new, untested code on a PIC. If it doesn't work, there are dozens of possible reasons, and it's hard to debug. Test your GSM modem first using a PC and typing the AT commands on the keyboard. That will allow you to try different baud rates and find out what baud rate the module uses.

Once you can send AT commands to it, you can test whether it supports the exact commands you want to use, by sending them manually and seeing how it responds. Once you have everything working using a PC and a terminal emulator, you can change to the PIC.
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
the GSM modem works well with the PC for At commands

////////////////////////////////////////
CODE:

AT // press enter
AT+CMGF=1 // enter
AT+CMGS="mobile number"
// enter the message
// press ctrl+Z
/////////////////////////////////////////
but I don't know how to feed this 'AT' instructions to PIC.
*) how to feed the enter key operation?,
*) entering the mobile number?
*) feeding operation of 'ctrl+Z'
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
I have not used a key board with PIC just want to send a message from PIC+GSM module when a input pin on pic goes HIGH
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
I have attached the programming that i did from reference to the Data sheet of PIC16F873A.
I'm not familiar with the PIC's serial port architecture so I don't know whether your other initialisation code is correct or not, but there is an obvious bug here:
Code:
  for (i=0;i<2;i++)
  {
  TXREG=a[i];
  }
  txreg=0x0D;
Each time you place a character into the USART's transmit register (TXREG), you have to wait until the USART has transferred it to the transmit shift register (TSR) before you place the next character into the transmit register. If you don't wait, it will overwrite the previous value in TXREG and characters will be lost.

Also your code is poorly structured. You should use separate functions to send characters and strings to the serial port, not a whole lot of for() loops and extra code in the mainline. The string transmit function can detect the null ('\0') character at the end of the string, so you don't need to pass an explicit string length to it; it just needs a pointer to the start of the string.

Here's what I suggest:
Code:
void usart_tx_char(char c) // Transmit a single character, c, through the USART.
//** warning: this function could get stuck forever,
//** waiting for TXREG to become empty if the USART
//** transmitter is disabled or the baud rate generator
//** is not working.
{
  while (TXIF == 0)
    ;                // Wait while TXREG is full (TXIF is 0)
  TXREG = c;         // Output character to USART
}//usart_tx_char


void usart_tx_string(char * cp) // Transmit a character string through the USART.
{
  auto char c;
  while ((c = *cp++) != '\0') // For every character until the final null
    usart_tx_char(c);         // Transmit the character from the string
}//usart_tx_string


void usart_tx_string_cr(char * cp) // Transmit a character string plus a final carriage return through the USART.
{
  usart_tx_string(cp);   // Transmit the character from the string
  usart_tx_char('\x0D'); // Transmit a carriage return
}//usart_tx_string_cr


void main(void)
{
  ///// (put your initialisation code here).
  usart_tx_string_cr("AT");
  usart_tx_string_cr("AT+CMGF=1");
  usart_tx_string_cr("AT+CMGF=999999999");
  usart_tx_string_cr("HI"); // "Hai" is not a word
  usart_tx_string("CMGC=\x1A");
}//main
You can avoid the usart_tx_string_cr() function and just use usart_tx_string() if you put the carriage return into each string you want to transmit. For example, replace usart_tx_string_cr("AT"); with usart_tx_string("AT\x0D");
The character combination \x0D means a character with a hex value of 0x0D (13 decimal), which is a carriage return character in the ASCII character set.

The character '\x1A' is a Ctrl-Z. ASCII number 26. That last string is transmitted without a final carriage return; I'm not sure whether it should have one or not.

You don't need to specify the number of characters in explicitly defined strings inside the square brackets after the variable name; the compiler can figure that out for itself! Each string will be stored as the sequence of characters you specify, plus a final null character ('\0' or '\x00'). This null terminator is used by usart_tx_string() to detect the end of the string.

These simplifications shorten your program by about 30% and make it much more readable.

Edit: You can also use the \r instead of \x0D. Don't use \n (newline); it's actually a line feed (0x0A).
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
tried your code but its not working

Anroop, I imagine that you are right now working on a more complete answer because you appreciate the effort Kris has put in and you realise that you need to tell him exactly what's not working.

Perhaps you'll tell us how you've tested pieces of the code in isolation and which one doesn't appear to be working how you expect it should.

Or maybe you're figuring out just how to tell us that you're a bit beyond your depth right now and how much you'd appreciate it if one of us might be able to point out how you can test the code piece by piece.

I only mention all of this because there are people out there who might misinterpret your response and think that you're not grateful for the effort Kris has put in. And I just know that can't be right.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
LOL!

Thanks Steve. I wasn't sure how to respond!

I was thinking that perhaps I should offer to fly over to his place and get it working for him.
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
Hi kris and steve,
I have currently started working with a PIC16F873A and GSM (SIM-300) module. the GSM modem does not have a data sheet, but it works fine on PC with a application provided in with the GSM modem using AT commands. so I decided to do it the same way I found the controller and GSM working fine separately. I have interfaced GSM to USART port of the controller and filled in the code. for identification I coded the initial part of code to make a led glow and final code to stop it glow so that i can understand that the in-between code (AT commands) are executed and the leds glow but i don't recieve the sms.
....

need to know syntax!!

my task:
1) read input and send message when i/p is high (or) send message once, when the controller is switched ON.
2) use only PIC and GSM modem (no keyboard. mobile number and message filled in the PIC program)

*****************
the main thing is I don't know how to send these AT commands to PIC (coding to send AT commands) and I was asking for help. for instance sent the AT commands to GSM as char (one by one) and gave delay to send them as u suggested kris. filled in the ascii codes for enter function and ctrl+z and so on..... now do you understand me???!!!!


above this I don't know how to explain this....
 
Last edited:

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
OK. There are lots of things you need to get right in hardware and software to make your PIC circuit work.

Let's start with the hardware. You said that the GSM module has three signals: TXD, RXD and GND. And you said that you can send an SMS if you connect it to a PC and use the software they supplied, right?

How does the module connect to the PC? What port on the PC does it connect to? Can you open up the plug(s) and draw a diagram of how the connecting cable is wired?

Does the software provided give you any information about the communication with the GSM modem? Does it show the commands as they are sent, and the responses from the modem? Does it tell you anything about the data format? The baud rate, for example?

Also, what board design are you using with the PIC? You say you have an input that needs to trigger the SMS, and you have an LED. What else is on the board? Do you have a schematic diagram of it?

What other information do you have about this project? Remember, I'm not there. I can't see what you have there. So I don't know what questions to ask, to get the details. You have to pretend that you're coming to this project from outside, for the first time. What questions would you ask? Then, tell me the answers!
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
I have Attached block diagram for connection. the software don't show the code flow.
 

Attachments

  • block diagram.png
    block diagram.png
    15.8 KB · Views: 174
  • connectionblock diagram.png
    connectionblock diagram.png
    13.2 KB · Views: 160

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Can you answer my other questions?
 

anroop

Dec 11, 2013
65
Joined
Dec 11, 2013
Messages
65
It connects to COM-3 port, with baud rate 9600. that's all the info I have got!
 
Top