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.

Urgent sample code sending sms when press p1^3 in AT COMMAND by using c language???

Status
Not open for further replies.

Ni yanfang

Member level 3
Member level 3
Joined
Jul 30, 2013
Messages
59
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
432
Hi I really confuse that how to write coding on sending sms when press p1^3 button in AT COMMAND by using c language???
I connect Gsm modem with AT89C51 (MCU), all set up already .
 

Code:
#include <reg51.h>              
#include <stdio.h>              
#define dataport P1;
sbit irout=P2^1;
sbit rs=P2^2;
sbit rw=P2^3;
sbit e=P2^4;

void serial_init(void);
void delay(int);
void lcdinit();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void convert(unsigned char *);
void serial_init(void)
{
   SCON = 0x50;                 
   TMOD = 0x20;             
   TH1   = 0xFD;         
   TR1   = 1;               
   TI   = 1;                
}
void serial(void) interrupt 4
{
unsigned int y[25];
if(RI==1)
{
y[25]=SBUF;
RI=0;
lcdinit();
convert(y[25]);
}
}
void lcdinit()
{
lcdcmd(0x38);  //2 Line, 5X7 Matrix
lcdcmd(0x0c);  //Display On, Cursor Blink
delay(1);
lcdcmd(0x01);  // Clear Screen
delay(1);
lcdcmd(0x81);  // Setting cursor to first position of first line
delay(1);
}
void convert(unsigned char *strs)
{
unsigned char m;
while(strs[m]!='\0')
{
lcddata(strs[m]);
m++;
}
}
void lcddata(unsigned char dat)
{
dataport = dat;
rs=1;
rw=0;
e=1;
delay(1);
e=0;
return;
}
void lcdcmd(unsigned char dat)
{
dataport = dat;
rs=0;
rw=0;
e=1;
delay(1) ;
e=0;
return;
}
void delay(int n)
{
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<1000;j++);
}

void main(void)
{
IE=0x90;
irout=1;
irout=0;           
while(1)
{
       
	   if(irout==1)
	   {  
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"#######8520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
		while(irout==1);
		}
        
}}


just take it as example then proceed with your code
 

Code:
#include <reg51.h>              
#include <stdio.h>              
#define dataport P1;
sbit irout=P2^1;
sbit rs=P2^2;
sbit rw=P2^3;
sbit e=P2^4;

void serial_init(void);
void delay(int);
void lcdinit();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void convert(unsigned char *);
void serial_init(void)
{
   SCON = 0x50;                 
   TMOD = 0x20;             
   TH1   = 0xFD;         
   TR1   = 1;               
   TI   = 1;                
}
void serial(void) interrupt 4
{
unsigned int y[25];
if(RI==1)
{
y[25]=SBUF;
RI=0;
lcdinit();
convert(y[25]);
}
}
void lcdinit()
{
lcdcmd(0x38);  //2 Line, 5X7 Matrix
lcdcmd(0x0c);  //Display On, Cursor Blink
delay(1);
lcdcmd(0x01);  // Clear Screen
delay(1);
lcdcmd(0x81);  // Setting cursor to first position of first line
delay(1);
}
void convert(unsigned char *strs)
{
unsigned char m;
while(strs[m]!='\0')
{
lcddata(strs[m]);
m++;
}
}
void lcddata(unsigned char dat)
{
dataport = dat;
rs=1;
rw=0;
e=1;
delay(1);
e=0;
return;
}
void lcdcmd(unsigned char dat)
{
dataport = dat;
rs=0;
rw=0;
e=1;
delay(1) ;
e=0;
return;
}
void delay(int n)
{
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<1000;j++);
}

void main(void)
{
IE=0x90;
irout=1;
irout=0;           
while(1)
{
       
	   if(irout==1)
	   {  
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"#######8520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
		while(irout==1);
		}
        
}}


just take it as example then proceed with your code
sorry I forgot to mention I just want to send sms . and I did not connect lcd.
 

Code:
#include <reg51.h>              
#include <stdio.h>              
sbit irout=P1^3;
sbit rs=P2^2;
sbit rw=P2^3;
sbit e=P2^4;
void serial_init(void);
void delay(int);
void serial_init(void)
{
   SCON = 0x50;                 
   TMOD = 0x20;             
   TH1   = 0xFD;         
   TR1   = 1;               
   TI   = 1;                
}
void serial(void) interrupt 4
{
unsigned int y[25];
if(RI==1)
{
y[25]=SBUF;
RI=0;
}
}
void delay(int n)
{
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<1000;j++);
}
void main(void)
{
IE=0x90;
irout=1;
irout=0;           
while(1)
{ 		if(irout==1)
{
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"9747358520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
		while(irout==1);
 } 
}}

i think this is the program that you want... try it and update me....

All d best....
 

Code:
#include <reg51.h>              
#include <stdio.h>              
sbit irout=P1^3;
sbit rs=P2^2;
sbit rw=P2^3;
sbit e=P2^4;
void serial_init(void);
void delay(int);
void serial_init(void)
{
   SCON = 0x50;                 
   TMOD = 0x20;             
   TH1   = 0xFD;         
   TR1   = 1;               
   TI   = 1;                
}
void serial(void) interrupt 4
{
unsigned int y[25];
if(RI==1)
{
y[25]=SBUF;
RI=0;
}
}
void delay(int n)
{
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<1000;j++);
}
void main(void)
{
IE=0x90;
irout=1;
irout=0;           
while(1)
{ 		if(irout==1)
{
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"9747358520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
		while(irout==1);
 } 
}}

i think this is the program that you want... try it and update me....

All d best....

Thank you so much for your reply. But Actually i want to write program so that elderly can press button and auto send sms to family member when they fall down or in danger.


Ok Do you know how to add in ledis. led on when sending sms, led ^is in off mode when device do nothing. And led connect p3^6.


Or can you explain some coding, I am not really understand. thanks.

Code:
sbit irout=P1^3; (irout???)
printf("AT+CMGS=\"9747358520\"%c",13);(13??)


And do you know how to write baud rate 115200?

I run this program but i can not send sms. Can you help me again.
 
Last edited by a moderator:

Code:
  printf("AT+CMGS=\"9747358520\"%c",13);   // after enter the phone number we have to give ALT+ENTER to send message 13 is ASCII Value for ALT+ENTER
irout is the name assigned for pin P1^3

it is the simple code for sending message when press P1^3 pin in microcontroller. for more u have to change as u required...

http://www.testech-elect.com/enfora/tutorial_send_receive_sms_using_gsm_modem.htm
for more detail about GSM refer this link... then u can understand... if anymore doubt post here...
 
Code:
  printf("AT+CMGS=\"9747358520\"%c",13);   // after enter the phone number we have to give ALT+ENTER to send message 13 is ASCII Value for ALT+ENTER
irout is the name assigned for pin P1^3

it is the simple code for sending message when press P1^3 pin in microcontroller. for more u have to change as u required...

http://www.testech-elect.com/enfora/tutorial_send_receive_sms_using_gsm_modem.htm
for more detail about GSM refer this link... then u can understand... if anymore doubt post here...
I read through AT command code already. But can you help me add in LED p3^6 in your code. led on when device is sending sms and led off when device is not sending sms or did nothing. Thank you so much.
 

Code:
sbit status P3^6; // name assigned for P3^6
void main(void)
{
IE=0x90;
irout=1;
status=0; 
irout=0;           
while(1)
{ 		if(irout==1)
{
status=1;
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"9747358520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
status=0;
		while(irout==1);

 }

Change the main function like this
 

Code:
sbit status P3^6; // name assigned for P3^6
void main(void)
{
IE=0x90;
irout=1;
status=0; 
irout=0;           
while(1)
{ 		if(irout==1)
{
status=1;
	    delay(200); 
        serial_init();                                                                                            
        printf("AT+CMGF=1%c",13);                                        
	    delay(200);            
        printf("AT+CMGS=\"9747358520\"%c",13);
	    delay(200);           
        printf("Hi :-) GSM Modem Test");                     
		delay(20); 
        printf("%c",0x1A);                                                             
		delay(200); 
status=0;
		while(irout==1);

 }

Change the main function like this

I follow this code but I press p1^3 . led not light up and also can not send sms. led light up when i press p3^6 button. Kindly check for me.
 

The code is actually turning on the LED (status) before sending SMS and turning it OFF after sending SMS. There is no button code in it. I am assuming status as LED. Write a delay code which gives exactly 2 sec delay. I will give you a working code.
 

The code is actually turning on the LED (status) before sending SMS and turning it OFF after sending SMS. There is no button code in it. I am assuming status as LED. Write a delay code which gives exactly 2 sec delay. I will give you a working code.

yes you are right. status is led. can you give sample on adding the condition as mention above. thanks.
 

Delays should be as mentioned. Are you sure that printf in keil prints to UART?


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
sbit status P3^6; // name assigned for P3^6
void main(void)
{
    IE=0x90;
    irout=1;
    status=0; 
               
    while(1)
    { 
        if(irout==1)
        {
            delay(200); //should be a delay of 50 ms
            if(irout==1)
            {
                status=1;
                delay(2000); 
                    serial_init();                                                                                            
                    printf("AT+CMGF=1%c",13);                                        
                    delay(200);             // delay od 2 sec        
                    printf("AT+CMGS=\"9747358520\"%c",13);
                    delay(200);             // delay od 2 sec 
                    printf("Hi :-) GSM Modem Test");                     
                delay(20); 
                    printf("%c",0x1A);                                                             
                delay(200); 
                status=0;
            }
        }
    }
 
}

 
OK thanks for your reply. but I still can not send sms and led not light up by pressing p1^3 button . led light up only i press the reset button. i really do not what is wrong with the code. Please help.
 

Is your button attached to the irout pin?

Also, have you implemented some form of debouncing for your button?

BigDog
 

Is your button attached to the irout pin?

Also, have you implemented some form of debouncing for your button?
BigDog

yes, I want to set irout pin to be button. so I can press irout pin to send sms as well as light up led. Do you have any idea?

- - - Updated - - -

irout pin is switch button in my circuit.
 

Ofcourse it will not work. printf will not print to uart and there is no code for uart configuration, initialization, and send uart data.


Look here for 8051 UART code.

https://saeedsolutions.blogspot.in/2012/05/interfacing-with-uart-of-8051.html


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
sbit status P3^6; // name assigned for P3^6
sbit irout P1^3; // name assigned for P1^3
 
void main(void)
{
    IE=0x90;
    irout=1;
    status=0; 
               
    while(1)
    { 
        if(irout==1)
        {
            delay(200); //should be a delay of 50 ms
            if(irout==1)
            {
                status=1;
                delay(2000); 
                    serial_init();                                                                                            
                    printf("AT+CMGF=1%c",13);                                        
                    delay(200);             // delay od 2 sec        
                    printf("AT+CMGS=\"9747358520\"%c",13);
                    delay(200);             // delay od 2 sec 
                    printf("Hi :-) GSM Modem Test");                     
                delay(20); 
                    printf("%c",0x1A);                                                             
                delay(200); 
                status=0;
            }
        }
    }
 
}

 
Ofcourse it will not work. printf will not print to uart ....

Incorrect, by default the printf() routine in KEIL is configured to output to the primary serial port, serial port 0.

The following document outlines the changes required to move the output to a secondary port, serial port 1, if the device has one.

However, initialization of the serial port before using the printf() routine is required.

C51: DIRECTING PRINTF OUTPUT TO SECOND SERIAL PORT

BigDog
 

do you know how to configure printf() to secondary port,serial port 0.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top