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

Status
Not open for further replies.
The printf() routine in KEIL C51 Compiler is already preconfigured to use the primary serial port 0, which is what is required for use with the AT89S51.

You still need to initialize the serial port to the correct BAUD rate.

The following example demonstrates the use of printf() in a simple "hello world" app at 1200 BAUD:

Code:
/*------------------------------------------------------------------------------
HELLO.C

Copyright 1995-2005 Keil Software, Inc.
------------------------------------------------------------------------------*/

#include <REG51.H>                /* special function register declarations   */
                                          /* for the intended 8051 derivative         */

#include <stdio.h>                /* prototype declarations for I/O functions */


/*------------------------------------------------
The main C function.  Program execution starts
here after stack initialization.
------------------------------------------------*/
void main (void) {

/*------------------------------------------------
Setup the serial port for 1200 baud at 16MHz.
------------------------------------------------*/

[COLOR="#FF0000"]    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */
[/COLOR]

/*------------------------------------------------
Note that an embedded program never exits (because
there is no operating system to return to).  It
must loop and execute forever.
------------------------------------------------*/
  while (1) {
    P1 ^= 0x01;     		    /* Toggle P1.0 each time we print */
    printf ("Hello World\n");   /* Print "Hello World" */
  }
}


You'll need to make the appropriate changes to initialize the serial port at 9600 BAUD.

BigDog
 

Hello everyone, I edit the code a;ready, but still cannot send sms . can you check for me. thanks a lot. This is the code.


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
#include <reg51.h>              
#include <stdio.h>  /* prototype declarations for I/O functions */
sbit led = P3^6 ;            
sbit irout = P1^3 ;
sbit rs=P2^0 ;
sbit rw=P2^1 ;
sbit e = P2^2 ;
unsigned char Command_CMGF[]="AT+CMGF=1\r"; // AT+CMGF for selecting Text Mode
unsigned char CtrlZ=0x1A;// CTRL+Z for sedning SMS after the message has been entered
unsigned char Command_CMGS[]="AT+CMGS =92207869\r";// recepient mobile number
unsigned char msg02[]="Hello!";
void serial_init(void);
void delay(int);
/*------------------------------------------------
Setup the serial port for 9600 baud 
------------------------------------------------*/
void serial_init(void)
{
   SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr      */                
   TMOD = 0x20; /* TMOD: timer 1, mode 2, 8-bit reload        */            
   TH1   = 0xFD;  /* TH1:  reload value for 9600 baud   */        
   TR1   = 1;         /* TR1:  timer 1 run                          */            
   TI   = 1;          /* TI:   set TI to send first char of UART    */         
}
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;
led=0; 
serial_init(); 
 
while(1)
{ if(irout==1)
{
  
 
        delay(200); 
       puts(Command_CMGF);
       delay(20);
        puts(Command_CMGS); 
         delay(200);
         puts(msg02); 
          led =0;
        while(irout==1);
 
 }
 }
 }

 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…