PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > PIC - Thiết kế và Ứng dụng

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

PIC - Thiết kế và Ứng dụng Ý tưởng cho các sản phẩm sử dụng PIC/dsPIC và các sản phẩm của Microchip

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 17-06-2012, 07:03 PM   #11
longtu
Đệ tử 6 túi
 
longtu's Avatar
 
Tham gia ngày: Jul 2007
Bài gửi: 146
:
Send a message via Yahoo to longtu
Trích:
Nguyên văn bởi mynametan View Post
Chào cả nhà cho em hỏi em làm đồ án điều khiển thiết bị qua tin nhắn sms...
Khi modum gởi tin nhắn phản hồi cho điện thoại....thì pic tự nhiên nhận chuỗi ký tự đòi xử lý mà em ko hề gởi chuỗi cho modum....rồi đứng lun hệ thống...mong mọi người giúp đỡ a?????????
Tôi nhận được code và sơ đồ của bạn qua email. Bạn hãy upload đoạn code cũng như sơ đồ của bạn lên đây để mọi người cùng rút kinh nghiệm. Tôi xem nhanh code bạn và có nhận xét code của bạn như sau
- Luôn quan sát NetLight Led có chớp hay không.
- Khi nhấn Power Key, Status Led phải sáng và luôn để ý Led này trong suốt quá trình sử dụng. Vì nếu như Led này tự động tắt, rất có thể nguồn cung cấp không đủ...
- puts("at+cmgr=1"); và puts("at+cmgd=1"); Rẩt có thể chương trình của bạn đứng tại đây, bạn thiếu \r để kết thúc lệnh. Ví dụ: printf("AT+CMGR=1\r\n");
- Nói chung, code bạn viết là không chặt chẽ. Bạn cần tìm hiểu để viết code chặt chẽ hơn.
Tôi gợi ý đoạn code nhận tin nhắn sms như sau:
- Đâu tiên, các bạn phải cấu hình/thiết lập nhận tin nhắn:
AT+CNMI=1,1,0,0,0 New message indication enable.
Khi đó, mỗi lần có tin nhắn mới sẽ nhận được chuỗi data như sau:
+CMTI: "SM",<index>\r\n
Chú ý:
1. Luôn bắt đầu bằng chuỗi: "+CMTI"
2. Luôn kết thúc bằng: \r\n
==>Dựa vao 1 va 2 để biết ĐÚNG là tin nhắn tới và loại bỏ những data không đúng là tin nhắn...
Code:
/* Chú ý:
 * Tôi su dung RDA2 de nhan SMS. Khai báo cua tôi nhu sau:
 */
#USE RS232(BAUD=38400, XMIT=PIN_B6, RCV=PIN_B7, STREAM=COM_GSM)
...
//------------------------------------------------------------------------------
#define  BUFSMS_SIZE 165      //SMS size the same 160 characters.
#define  BUFSMS_MASK BUFSMS_SIZE-2
char buffSMS[BUFSMS_SIZE];

//Define:
#define OK_         0                       // Used to look up in SetSearchString( Response )
#define CMTI_       1                       // Used to look up in SetSearchString( Response )
#define READY_      3                       // Used to look up in SetSearchString( Response )
#define CRLF_       4                       // Used to look up in SetSearchString( Response )

unsigned char  OK[]     = "OK\r\n";                  // "OK"
unsigned char  CMTI[]   = "+CMTI:";               // New Message arrived
unsigned char  READY[]  = "> ";                      // Phone ready to receive text message
unsigned char  CR_LF[]  = "\r\n";                    // Carrige Return Line Feed  

unsigned char  *searchFor;                             // Flash pointer

// Private pointer
static unsigned char searchStr;

static int8 rx_i;
int8 nextIn2;
int8 rx_ack=0;    //Acknowledge Flag.

void InitBuffCom2(void);
void SetSearchString( unsigned char Response );
void EnableReceivSMS();
/* Int_rad2:
 * Có tin nhắn mới: rx_ack= 1, ngược lai rx_ack= 0.
 */
#INT_RDA2
void serial_isr2()
{
   int8 i;
   char c;
   c= buffSMS[nextIn2++]= fgetc(COM_GSM);
   //fputc(buffSMS[nextIn2-1],COM_GPS); //Could be used for testing.

   if( nextIn2 > BUFSMS_MASK)
   {
      nextIn2= 0;    //Reset write index.
      disable_interrupts(int_rda2);// Disable RX interrupt
   }
   
   if( searchFor[rx_i] == c)
   {
      rx_i++;
      if( !searchFor[rx_i] )
      {
         rx_i= 0;
         if( searchStr == CMTI_  )                //+CMTI
         {
            searchFor= CR_LF; //Wait for
            searchStr = CRLF_;
         }        
         else
         {
            rx_ack= 1;            
            disable_interrupts(int_rda2);// Disable RX interrupt
         }
      }    
   }
   
   else
   {
      rx_i= 0;       //Not valid search pattern...start again.
   }
   
}

/*  
 *  Reset receive interrupt SMS data.
 */
void InitBuffCom2(void)
{
   disable_interrupts(int_rda2);// Disable receive SMS message.
   rx_i= rx_ack = 0;
   nextIn2 = 0;
   buffSMS[nextIn2] = NULL;//#define NULL '\0'
}
/*  
 *   Set desired search string
 * Usage: 
 * - Call: InitBuffCom2(); <?> //Reset receive buffSMS
 * - Call: SetSearchString( unsigned char Response )
 * - Call: AT command
 * - Call: EnableReceivSMS()
 */
void SetSearchString( unsigned char Response )
{
   disable_interrupts(int_rda2);// Disable receive SMS message.
   switch (Response)
    {
    case OK_: 
            searchFor= OK;
            break;
    case CMTI_: 
            searchFor= CMTI;
            break;
    case READY_: 
            searchFor= READY;
            break;
    default:
            break; 
    }
    searchStr = Response;                 //Used in rx_isr
    rx_i = 0;
}
/*
 * Enable Receiv SMS:
 */
void EnableReceivSMS()
{
   enable_interrupts(int_rda2);
}
/*  Check acknowledge returned from phone
 *
 *  This function checks if an acknowledge
 *  has been received from the phone. A counting loop is also
 *  included to avoid waiting for a acknowledge that never arrives.
 *
 *   Return Value:
 *     1 Success, correct acknowledge
 *     0 Error, returned "ERROR" or timed out
 */
#define  KEYHIT_DELAY   5000     //   milliseconds
Int Check_acknowledge()
{   
   unsigned int32   timeout=0;
   int retval;
   int16 temp_delay=0;
   
   retval= 0;
   rx_ack= 0;           //Zero ackowledge flag.
   while((rx_ack==0)&& (++timeout<(KEYHIT_DELAY*100)))  
      delay_us(10);
   
   if(rx_ack)           //Everything worked out fine...rx turned off
   {
      rx_ack= 0;         //Reset ackowledge flag.
      retval= 1;
   }
   else                 //A timeout could result from no acknowledge.
   {
     // fputc('A',COM_GPS);
      InitBuffCom2();
      retval= 0;
   }
   while((temp_delay++)<10000);//Delay a few miliseconds.
   return(retval);
}
/*Delete a message at index= 1
 *
 */
Void deleteSMS()
{
    InitBuffCom2();
    SetSearchString( OK_ );  
    fprintf(COM_GSM,"AT+CMGD=1\r"); 
    EnableReceivSMS();  
    if( Check_acknowledge() > 0 )  //Acknowledge = "OK"
    {          
      return;
    }
    else                          //Acknowledge != "OK"
    {
      fprintf(COM_GPS,"ERROR: No Ack!\r\n"); 
      return;
    }
}
//----------------------------------------------

Void main()
{
   ...
   enable_interrupts(int_rda2);
   
   enable_interrupts(GLOBAL);
   ...
   deleteSMS(); //Chú ý A
   InitBuffCom2();
   SetSearchString( CMTI_ );  //Waiting the new SMS message.
   EnableReceivSMS(); //Ready to receive the new SMS message.
//Main program:
   while(1)
   {
      if( rx_ack )
      {
       //Xu ly tin nhan moi.
       deleteSMS();//Chú ý B
       InitBuffCom2();
       SetSearchString( CMTI_ );  //Waiting the new SMS message.
       EnableReceivSMS(); //Ready to receive the new SMS message.
      }
   }
}
Chú ý A: deleteSMS()
- Luôn xóa tin nhắn tại index= 1, điều này có nghĩa rằng khi nhận được tin nhắn mới thì chắc chắnc rằng tin nhắn mới sẽ được lưu trong index= 1.

Chú ý B: deleteSMS()
- Khi xử lý xong tin nhắn mới, phải xóa tin nhắn ở index= 1 này để sẵn sàng nhận tin nhắn mới vào index= 1.
__________________
--------------------------------------------------------------------------------------
Phùng Minh Tuân
Email: tuan.phmt@gmail.com
"Cuộc đời sóng gió nuôi ta lớn
Bao lần thất bại dạy ta khôn".

thay đổi nội dung bởi: longtu, 17-06-2012 lúc 07:38 PM.
longtu vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 12:29 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam