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.

GSM sim900 to PIC16F877A

Status
Not open for further replies.

bmsngobe

Newbie level 3
Joined
Nov 6, 2014
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
36
Hi

I am looking for c code of sending an sms from PIC16F877A to GSM module sim900 via RS232.
GSM responds well to all commands when connected to laptop via usb-rs232 cable. I have connected my cable as follows (2-3, and 3-2, 5-5).
I think the problem is with my code.
 

Post your code so we can see if we can help.



Cheers
 

Maybe bank switching is causing the problem.
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Picc-Lite header file for processor special function registers and peripherals
#include <pic.h>
#include <htc.h>
#include <stdlib.h>
#include "uartUJ.h"   //Uart Header file invoked
#include "lcdUJ.h"
 
void WriteToUsart(void);
void ReadPortsValues(void);
void InitializePorts(void);
 
#define _XTAL_FREQ 4000000 //4MHz Oscillator
 
#define cr  0x0D    //cursor return to home position
#define ff  0x0C    //form feed
 
// Configuration bit settings
//__CONFIG(XT & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGDIS & UNPROTECT);
 
// Local Subroutine and Function declarations
void adc_init()
{
    ADCON1=0B10000000;         //Right just, Fosc/8 conversion clock, Vref=5V
    ADCON0=0B01000000;        //Fosc/8 conv_clock for proper sampling of 1.6uS.
    ADON=1;                  //Turning on the ADC module.
}
 
int adc_read()
{
    int x=0;
    GO=1;
    while(GO)continue;
    x=ADRESH<<8;    //Store 8-bit ADRESH and shift it 8 times so that it sits on bit8 to bit15
    x =x|ADRESL;   //Join ADRESL to ADRESH and store in Register x to form a 16-bit register
    return x;     //return the value of new register x to main loop for further processing and display.
}
 
/*const unsigned char mes5[]= "AT\r";
const unsigned char mes6[]= "AT+CMGF=1\r";
const unsigned char mes7[]= "AT+CMGS=\"+27797185004\"\r";
const unsigned char mes8[]="0x01A";*/
 
 
 
//variable declarations
unsigned char Vstr[5];
int result,voltage;
 
const unsigned char mes1[]= "AT";
const unsigned char mes2[]= "AT+CMGF=1";
const unsigned char mes3[]= "AT+CMGS=\"+27797185004\"";
const unsigned char mes4[]= "JRA has been";
const unsigned char mes5[]="0x1A"; //Send command (ctrl+z)
const unsigned char mes6[]="0x0d"; //Enter command
 
//Main Program
void main()
{
    while(!RA4)
        {
            InitializePorts();
            uart_init();
            ReadPortsValues();
        }
}
 
void InitializePorts(void)
{
  PORTA=0;            //Clear Porta
  TRISA=0xFF;         // Make Porta Digital
  adc_init();         // Convert Analog to Digital Converter
}
 
void ReadPortsValues(void)
{
    while(1)
        {
            result=adc_read();
            PORTD=result;
            voltage=result*5;
            itoa(Vstr,voltage,10);
            WriteToUsart();
        }
}
 
// Writting a string on to a screen
void WriteToUsart(void)
{
    //putch(ff);
    putch (cr);      //Take the cursor back to the first position
    uart_putrs(mes1);   //Write message1
    putch('\n');
uart_putrs(mes6);
   putch(cr);       //Take the cursor back to the first position
//  uart_putrs(mes6);
    __delay_ms(2000);
    uart_putrs(mes2);
putch('\n');
    uart_putrs(mes6);
    putch('\n');        //Take cursor to a new line
    putch(cr);      //Take the cursor back to the first position
    __delay_ms(2000);
    uart_putrs(mes3);
 putch('\n');
    uart_putrs(mes6);
    putch('\n');        //Take cursor to a new line
   putch(cr);       //Take the cursor back to the first position
    __delay_ms(2000);
    uart_putrs(mes4);
    __delay_ms(2000);
    putch('\n');
    putch(cr);
//  __delay_ms(2000);
    uart_putrs(mes5);
    putch('\n');
    putch(cr);
    __delay_ms(2000);
}



- - - Updated - - -

Hi Milan, even without bank switch, it does not send message to gsm from the pic
 
Last edited by a moderator:

Hi bmsngobe,

After you send your modem command strings you are only sending \n this should be \r\n.

Instead of sending as putch('\n'); just edit your string definitions as :

const unsigned char mes1[]= "AT\r\n";
const unsigned char mes2[]= "AT+CMGF=1\r\n";
const unsigned char mes3[]= "AT+CMGS=\"+27797185004\"\r\n";

or you can send strings as literals: uart_putrs("AT\r\n"); //Write message1 for example.

Another thing to remember is the modem needs to synchronize autobauding so I would suggest that you either send AT twice or have a delay after AT before you send another command.

You should really check for the modem response after sending AT (which is \r\nOK\r\n).

Also check the hardware i.e. you have correct signal levels on the RX/TX pins of the modem as the SIM900 levels if I recall correctly are about 2.8 volts. If your PIC has different levels then you will need level shifting.

Cheers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top