找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 3853|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32使用gsm模塊打電話代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:251082 發(fā)表于 2018-6-24 11:20 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
stm32使用gsm模塊打電話代碼

單片機(jī)源程序如下:
  1. /*****************************************************       
  2. 接線說明:
  3. STM32                                        A6&A7
  4. GND                <----->        GND
  5. PA2                ------>        U_RXD
  6. PA3                <------        U_TXD

  7. //用于調(diào)試可不接
  8. STM32                                        USB-TTL模塊
  9. GND                ------>        GND
  10. TX1                ------>        RXD
  11. ***********************************************************/

  12. #include "stm32f10x.h"
  13. #include "delay.h"
  14. #include "usart.h"
  15. #include "led.h"
  16. #include "usart2.h"


  17. //常量
  18. #define Success 1U
  19. #define Failure 0U

  20. //定義變量
  21. unsigned long  Time_Cont = 0;       //定時(shí)器計(jì)數(shù)器

  22. char phoneNumber[] = "1771922xxxx";                //替換成需要被撥打電話的號碼


  23. void errorLog(int num);
  24. void phone(char *number);
  25. unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry);
  26. void Sys_Soft_Reset(void);

  27. int main(void)
  28. {       
  29.         delay_init();
  30.        
  31.         NVIC_Configuration();          //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
  32.         uart_init(9600);         //串口初始化為9600
  33.         USART2_Init(115200);        //串口2波特率9600
  34.         u2_printf("AT\r\n");
  35.         Init_LEDpin();
  36.        
  37.        
  38.        
  39.         printf("Welcome to use!\r\n");
  40.         printf("ILoveMcu.taobao.com!\r\n");
  41.        
  42.         if (sendCommand("AT\r\n", "OK", 3000, 10) == Success);
  43.         else errorLog(1);
  44.         delay_ms(10);

  45.         if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
  46.         else errorLog(2);
  47.         delay_ms(10);

  48.         if (sendCommand("AT+CREG?\r\n", "CREG: 1", 1000, 10) == Success);
  49.         else errorLog(3);
  50.         delay_ms(10);

  51.         phone(phoneNumber);                //打電話
  52.        
  53.         while(1)
  54.         {
  55.                
  56.         }
  57. }

  58. void errorLog(int num)
  59. {
  60.         printf("ERROR%d\r\n",num);
  61.         while (1)
  62.         {
  63.                 if (sendCommand("AT\r\n", "OK", 100, 10) == Success)
  64.                 {
  65.                         Sys_Soft_Reset();
  66.                 }
  67.                 delay_ms(200);
  68.         }
  69. }

  70. void Sys_Soft_Reset(void)
  71. {  
  72.     SCB->AIRCR =0X05FA0000|(u32)0x04;      
  73. }

  74. void phone(char *number)
  75. {
  76.         char send_buf[20] = {0};
  77.         memset(send_buf, 0, 20);    //清空
  78.         strcpy(send_buf, "ATD");
  79.         strcat(send_buf, number);
  80.         strcat(send_buf, ";\r\n");

  81.         if (sendCommand(send_buf, "SOUNDER", 10000, 10) == Success);
  82.         else errorLog(4);
  83. }

  84. unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
  85. {
  86.         unsigned char n;
  87.         USART2_CLR_Buf();
  88.         for (n = 0; n < Retry; n++)
  89.         {
  90.                 u2_printf(Command);                 //發(fā)送GPRS指令
  91.                
  92.                 printf("\r\n***************send****************\r\n");
  93.                 printf(Command);
  94.                
  95.                 Time_Cont = 0;
  96.                 while (Time_Cont < Timeout)
  97.                 {
  98.                         delay_ms(100);
  99.                         Time_Cont += 100;
  100.                         if (strstr(USART2_RX_BUF, Response) != NULL)
  101.                         {                               
  102.                                 printf("\r\n***************receive****************\r\n");
  103.                                 printf(USART2_RX_BUF);
  104.                                 USART2_CLR_Buf();
  105.                                 return Success;
  106.                         }
  107.                        
  108.                 }
  109.                 Time_Cont = 0;
  110.         }
  111.         printf("\r\n***************receive****************\r\n");
  112.         printf(USART2_RX_BUF);
  113.         USART2_CLR_Buf();
  114.         return Failure;
  115. }
復(fù)制代碼

所有資料51hei提供下載:
01.STM32撥打電話.rar (319.11 KB, 下載次數(shù): 42)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表