اطلاعیه

Collapse
No announcement yet.

مشکل با sim808 با STM32

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    مشکل با sim808 با STM32

    سلام دوستان من میخوام یک ردیاب ماشین با sim808 با هدربورد stm32f103RET6 طراحی کنم و میخوام که با فرستادن SMS لینک گوگل مپ رو به شماره ای که بهش میدم ارسال کنه. میخوام کد رو مرحله به مرحله جلو ببرم. با مبدل USB به TTL مختصات رو میتونم دریافت کنم و میتونم مختصات رو بلافاصله به شماره مورد نظر بفرستم ولی وقتی میخوام یه اس ام اس بفرستم و تحلیلش کنم که مثلا با فرستادن 1234 یک کاری برام انجام بده به عنوان مثال روشن کردن یک LED به مشکل میخورم و وقتی با دیباگر کیل دیباگ میکنم نشون میده که کلی کاراکتر A فرستاده شده به پورت سریال. این کدی که میذارم با روش polling هست ولی با اینتراپت هم که نوشتم ارور HARDFAULT میده. مشکل از چی میتونه باشه ؟
    این کدی که جواب نمید ه
    کد:
       [FONT=Yekan]#include <stm32f10x.h>[/FONT]#include "usart.h"
    #include <string.h>
    #include <stdio.h>  
    #include <stdlib.h>
    
    
    char S=0x1A;
    #define Enter    usart_sendchar('\n');
    #define CR       usart_sendchar('\r');
    #define GIM      usart_sendchar('"');
    #define SUB      usart_sendchar(S);
    char get_Enter = 0;
    uint8_t ch;
    char str1[300];
    int i;
    char str_tmp[100];
    char str_tmp2[100];
    
    
    
    
    
    
    
    
    void Send_SMS(char *text);
    void del_All_SMS(void);
    void CMGF_1(void);
    void GGNSPWR(void);
    
    
    void CGNSINF(void);
    
    
    void Delay_ms (uint32_t Time);
    
    
    
    
    volatile uint32_t msTicks;
    // the interrupt handler for the systick module 
    void SysTick_Handler(void) {
        msTicks++;
    }
    void Delay_ms (uint32_t dlyTicks) {
        uint32_t curTicks;
        curTicks = msTicks;
        while ((msTicks - curTicks)< dlyTicks);
    }
    
    
    
    
    
    
    
    
    
    
    
    
        void CMGF_1(void)
     {
        str_tmp[0]='\0';
        Delay_ms(100);       
        strcpy(str_tmp,"AT+CMGF=1");
        Delay_ms(500);       
        usart_sendstring(str_tmp);
      Enter
        CR    
        
     }
     void del_All_SMS(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CMGD=1,4");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
     
     void CGNSPWR(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CGNSPWR=1");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
    
    
     void CGNSINF(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CGNSINF");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
    
    
     void CMGR(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CMGR=1");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    
    
    void Send_SMS(char *text)
    {
    CMGF_1(); 
    Delay_ms(500);       
    str_tmp[0]='\0';
    Delay_ms(100);       
    
    
    strcpy(str_tmp,"AT+CMGS=");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    GIM
    str_tmp[0]='\0';
    Delay_ms(100);       
    
    
    strcpy(str_tmp,"+989058316399");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    GIM
    Enter
    CR
    
    
    Delay_ms(500);           
    
    
     usart_sendstring(text);
    
    
    Enter
    CR
    str_tmp[0]='\0';
    Delay_ms(500);       
    SUB
    
    
    Delay_ms(1000);
    del_All_SMS();
    }
    
    
    
    
    int main()
    {
    SystemInit();
    SysTick_Config(SystemCoreClock/1000); // setup systick timer for 1ms interrupts
    usart_init();    
    CMGF_1();
        Delay_ms(250);
    del_All_SMS();
        Delay_ms(250);
    CGNSPWR();
    Delay_ms(250);
    
    
    RCC->APB2ENR|=(1<<3);
    GPIOB->CRL &= 0xF;
    GPIOB->CRL |= 0x3;    
    while (1)
    {
        char* u;
      char* j;
        char* h;
      
            
        
        
        do {
        usart_getstring(str_tmp2);
        u=strstr(str_tmp2,"+CMTI:");
        }
        while (!u);
       CMGR();    
            u=0;
        do {
        usart_getstring(str_tmp2);
        j=strstr(str_tmp2,"+CMGR:");
        }
        while (!j);
        h=strstr(str_tmp2,"1234");
        if (h){
        
        GPIOB->BSRR |= (1<<0);
        
        
        }
            }
    
    
                
            
            
             
             
                 
    
    
    }




    اینم کد قسمت سریال
    کد:
     [FONT=Yekan]#include "stm32f10x.h"[/FONT]#include "usart.h"
        char str[200];
        char data;
    
    
    //// initialize usart
    void usart_init(void) 
     {
    
    
      RCC->APB2ENR |=  (   1UL <<  0);        /* enable clock Alternate Function  */
      AFIO->MAPR   &= ~(   1UL <<  2);        /* clear USART1 remap               */
    
    
      RCC->APB2ENR |=  (   1UL <<  2);        /* enable GPIOA clock               */
      GPIOA->CRH   &= ~(0xFFUL <<  4);        /* clear PA9, PA10                  */
      GPIOA->CRH   |=  (0x0BUL <<  4);        /* USART1 Tx (PA9) output push-pull */
      GPIOA->CRH   |=  (0x04UL <<  8);        /* USART1 Rx (PA10) input floating  */
    
    
      RCC->APB2ENR |=  (   1UL << 14);        /* enable USART#1 clock             */
    
    
      USART1->BRR   = 0x0271;                 /* 115200 baud @ PCLK2 72MHz        */
      USART1->CR1   = ((   1UL <<  2) |       /* enable RX                        */
                       (   1UL <<  3) |       /* enable TX                        */
                       (   0UL << 12) );      /* 1 start bit, 8 data bits         */
      USART1->CR2   = 0x0000;                 /* 1 stop bit                       */
      USART1->CR3   = 0x0000;                 /* no flow control                  */
    
    
      USART1->CR1  |= ((   1UL << 13) );      /* enable USART                     */
        
    //    USART1->CR1  |= ( 1UL << 5);     // RXNE interrupt enable
    //    NVIC_SetPriority(USART1_IRQn,0); /* Default priority group 0, can be 0(highest) - 31(lowest) */
    //  NVIC_EnableIRQ(USART1_IRQn);     /* Enable UART0 Interrupt */
    }
    
    
    
    
    
    
    
    
    
    
    void usart_sendchar(char data){
        while(!(USART1->SR&(1<<7)));
        USART1->DR=data;
    }
    char usart_getchar(void) 
        {
            
        while (!(USART1->SR&(1<<5)));
        data=USART1->DR ;
      return data;        
        }
    
    
    
    
    void usart_getstring(char *str) 
    {
    
    
      char *temp = str;
        
        do{
            
            *temp = usart_getchar();
            usart_sendchar (*temp) ;
            
        } while(*(temp++) != 0x0D );
        *(temp-1) = 0;
    
    
    }
    
    
    void usart_sendstring(char *s) 
    {
          while (*s) 
        {
           usart_sendchar(*s);
            s++;
        }
    }
    اینم کدی که جواب میده (ارسال مختصات بلافاصله)
    کد:
    #include <stm32f10x.h>
    #include "usart.h"
    #include <string.h>
    #include <stdio.h>  
    #include <stdlib.h>
    
    
    char S=0x1A;
    #define Enter    usart_sendchar('\n');
    #define CR       usart_sendchar('\r');
    #define GIM      usart_sendchar('"');
    #define SUB      usart_sendchar(S);
    char get_Enter = 0;
    uint8_t ch;
    char str1[300];
    int i;
    char str_tmp[100];
    char str_tmp2[100];
    
    
    
    
    
    
    
    
    void Send_SMS(char *text);
    void del_All_SMS(void);
    void CMGF_1(void);
    void GGNSPWR(void);
    
    
    void CGNSINF(void);
    
    
    void Delay_ms (uint32_t Time);
    
    
    
    
    volatile uint32_t msTicks;
    // the interrupt handler for the systick module 
    void SysTick_Handler(void) {
        msTicks++;
    }
    void Delay_ms (uint32_t dlyTicks) {
        uint32_t curTicks;
        curTicks = msTicks;
        while ((msTicks - curTicks)< dlyTicks);
    }
    
    
    
    
    
    
    
    
    
    
    
    
        void CMGF_1(void)
     {
        str_tmp[0]='\0';
        Delay_ms(100);       
        strcpy(str_tmp,"AT+CMGF=1");
        Delay_ms(500);       
        usart_sendstring(str_tmp);
      Enter
        CR    
        
     }
     void del_All_SMS(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CMGD=1,4");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
     
     void GGNSPWR(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CGNSPWR=1");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
    
    
     void CGNSINF(void)
     {
    str_tmp[0]='\0';
    Delay_ms(100);
    strcpy(str_tmp,"AT+CGNSINF");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    
    
    void Send_SMS(char *text)
    {
    CMGF_1(); 
    Delay_ms(500);       
    str_tmp[0]='\0';
    Delay_ms(100);       
    
    
    strcpy(str_tmp,"AT+CMGS=");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    GIM
    str_tmp[0]='\0';
    Delay_ms(100);       
    
    
    strcpy(str_tmp,"+989058316399");
    Delay_ms(500);       
    usart_sendstring(str_tmp);
    GIM
    Enter
    CR
    
    
    Delay_ms(500);           
    
    
     usart_sendstring(text);
    
    
    Enter
    CR
    str_tmp[0]='\0';
    Delay_ms(500);       
    SUB
    
    
    Delay_ms(1000);
    del_All_SMS();
    }
    
    
    
    
    int main()
    {
    SystemInit();
    SysTick_Config(SystemCoreClock/1000); // setup systick timer for 1ms interrupts
    usart_init();    
    CMGF_1();
    del_All_SMS();
    GGNSPWR();
    Delay_ms(250);
    CGNSINF();
    while (1)
    {
        char* u;
    
    
        
      usart_getstring(str_tmp2);
        u=strstr(str_tmp2,"+CGNSINF:");    
        
        
             if (u){
            Send_SMS(u);
             }
          
             
             
         }         
    
    
    }



    #2
    پاسخ : مشکل با sim808 با STM32

    کد برنامه ای که با اینتراپت نوشته بودم رو یک مقدار تغییر دادم الان جواب میده ولی باید بعد از هر پیام میکرو رو ریست کنم. فکر میکنین که مشکل از چی باشه ؟
    کد برنامه اصلی :
    کد:
    #include <stm32f10x.h>
    #include "usart.h"
    #include <string.h>
    #include <stdio.h>  
    #include <stdlib.h>
    #include <stdint.h>
    
    char S=0x1A;
    #define Enter    usart_sendchar('\n');
    #define CR       usart_sendchar('\r');
    #define GIM      usart_sendchar('"');
    #define SUB      usart_sendchar(S);
    #include "stm32f10x.h"
    #include <string.h>
    char get_Enter = 0;
    uint8_t ch;
     char str1[300];
    char str2[100];
    int i=0;
    char flag=0;
    char str_tmp[20];
    char str_tmp2[20];
    char* p;
    
    
    volatile uint32_t msTicks;
    
    //calling all the functions 
    
    void Send_SMS(char *text);
    void CMTI(void);
    void wait_to_get(char ch);
    void del_All_SMS(void);
    void CMGF_1(void);
    void Delay (uint32_t Time);
    void CMGR (void);
    void CGNSPWR_1(void);
    void CGNSINF(void);
    void interrupt_activation(void);
    void CGNSINF_C(void);
    void AT(void);
    void Delay (uint32_t dlyTicks);
    void SysTick_Handler(void);
    
    
    // the interrupt handler for the systick module 
    void SysTick_Handler(void) {
        msTicks++;
    }
    void Delay (uint32_t dlyTicks) {
        uint32_t curTicks;
        curTicks = msTicks;
        while ((msTicks - curTicks)< dlyTicks);
    }
    
    
    
    
    
    
    
    
    void Send_SMS(char *text)
    {
    CMGF_1(); 
    Delay(500);       
    str_tmp[0]='\0';
    Delay(100);       
    
    strcpy(str_tmp,"AT+CMGS=");
    Delay(500);       
    usart_sendstring(str_tmp);
    GIM
    str_tmp[0]='\0';
    Delay(100);       
    
    strcpy(str_tmp,"+98905xxxxxxx");
    Delay(500);       
    usart_sendstring(str_tmp);
    GIM
    Enter
    CR
    
    Delay(500);           
    
     usart_sendstring(text);
    Enter
    CR
    str_tmp[0]='\0';
    Delay(500);       
    SUB
    
    Delay(100);
    del_All_SMS();
    }
    void USART1_IRQHandler(void) {
    
            ch = USART1->DR & 0xFF; 
        if (ch == '\n'){ // 13 enter
                 
                      get_Enter =1;
            
             }
            else{
                str1[i]= ch;
                i++;
            }
     }
    
    
                
                 
                 
                 
                 
                 
                     
    
    
    
        
        void CMGF_1(void)
     {
        str_tmp[0]='\0';
        Delay(100);       
        strcpy(str_tmp,"AT+CMGF=1");
        Delay(500);       
        usart_sendstring(str_tmp);
      Enter
        CR  
        
     }
    
    
     void del_All_SMS(void)
     {
    str_tmp[0]='\0';
    Delay(100);
    strcpy(str_tmp,"AT+CMGD=1,4");
    Delay(500);       
    usart_sendstring(str_tmp);
    Enter
    CR   
    }
     
     
        void CMGR(void)
     {
        str_tmp[0]='\0';
      
        Delay(100);      
        strcpy(str_tmp,"AT+CMGR=1");
      Delay(500);    
        usart_sendstring(str_tmp);
      Enter
        CR  
        
     }
        void CGNSINF(void)
     {
        str_tmp[0]='\0';
        Delay(100);       
        strcpy(str_tmp,"AT+CGNSINF");
        Delay(500);       
        usart_sendstring(str_tmp);
      Enter
        CR      
        
     }
    void CGNSPWR_1(void)
     {
        str_tmp[0]='\0';
        Delay(100);       
        strcpy(str_tmp,"AT+CGNSPWR=1");
            Delay(500);
        usart_sendstring(str_tmp);
        Enter
        CR  
        
     }
     
     void AT(void)
     {
        str_tmp[0]='\0';  
        Delay(100);  
        strcpy(str_tmp,"AT");
            Delay(500);
        usart_sendstring(str_tmp);
        Enter
        CR  
        
     }
     
     
     
     
     
     
        
        
    int main() 
        {
        SystemInit();
      SysTick_Config(SystemCoreClock/1000); // setup systick timer for 1ms interrupts
        usart_init();   
            
      AT();
            
            p=NULL;
             
                check:AT();
      
            Delay(2000);
            
            p=strstr(str1,"OK");   
            if(p==NULL) goto check;
            
                 
      CMGF_1();
        Delay(500);
    
        CGNSPWR_1();
        
        Delay(500); 
      del_All_SMS();
        
        Delay(500);
            
        RCC->APB2ENR |= (1<<3); 
        GPIOB->CRL &= ~0xF;
      GPIOB->CRL |= 0x3;    
        
            
    while (1)
    {
        
        if (get_Enter ==1)
        {
          if (flag==0){
           ////waiting for +CMTI: from sim800
                do
         {  
          p=strstr(str1,"+CMTI:");   
          } 
             while(p==NULL);
          Delay(1000);
                    // sending AT+CMGR=1 command
                if (p!=NULL)
                {
                        CMGR();
                
                        Delay(350);
                    flag=1;
                }
            }
        if (flag==1) {
            p=strstr(str1,"+CMGR");
                if (p){
                    p=strstr(str1,"loc");
                                    
    if (p!=NULL)
    {
    
    CGNSINF();
        Delay(300);
    }
    else if (p==NULL)
    {
    del_All_SMS();
    memset(str1, 0, 300);   
        flag=0;
        
    }
    p=strstr(str1,"+CGNSINF:");
    if (p)
    {
    
    
    float a[5];
              
    char str2[80];
       const char s[2] = ",";
       char *token;
       // getting the lattitude and longitude 
       token = strtok(p, s);
       
        for (int i=0;i<5;i++){
          sprintf( str2," %s\n", token );
            a[i]=atof(str2);
        
          token = strtok(NULL, s);
        } 
    
    sprintf(str2,"https://maps.google.com/?q=%.6f,%.6f",a[3],a[4]);
    Send_SMS(str2);
    p=NULL; 
    del_All_SMS();
        flag=0;
    memset(str1, 0, 300);
    
    
    
    
        } 
    
    
    
          } 
                
                
            } 
            }  
                 
                 
            } 
                
            
        }
    جدیدترین ویرایش توسط JFET2013; ۱۶:۴۴ ۱۴۰۰/۰۵/۲۹.

    دیدگاه

    لطفا صبر کنید...
    X