stm32使用gsm模塊打電話代碼
單片機源程序如下:
- /*****************************************************
- 接線說明:
- STM32 A6&A7
- GND <-----> GND
- PA2 ------> U_RXD
- PA3 <------ U_TXD
- //用于調試可不接
- STM32 USB-TTL模塊
- GND ------> GND
- TX1 ------> RXD
- ***********************************************************/
- #include "stm32f10x.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "usart2.h"
- //常量
- #define Success 1U
- #define Failure 0U
- //定義變量
- unsigned long Time_Cont = 0; //定時器計數器
- char phoneNumber[] = "1771922xxxx"; //替換成需要被撥打電話的號碼
- void errorLog(int num);
- void phone(char *number);
- unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry);
- void Sys_Soft_Reset(void);
- int main(void)
- {
- delay_init();
-
- NVIC_Configuration(); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- uart_init(9600); //串口初始化為9600
- USART2_Init(115200); //串口2波特率9600
- u2_printf("AT\r\n");
- Init_LEDpin();
-
-
-
- printf("Welcome to use!\r\n");
- printf("ILoveMcu.taobao.com!\r\n");
-
- if (sendCommand("AT\r\n", "OK", 3000, 10) == Success);
- else errorLog(1);
- delay_ms(10);
- if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
- else errorLog(2);
- delay_ms(10);
- if (sendCommand("AT+CREG?\r\n", "CREG: 1", 1000, 10) == Success);
- else errorLog(3);
- delay_ms(10);
- phone(phoneNumber); //打電話
-
- while(1)
- {
-
- }
- }
- void errorLog(int num)
- {
- printf("ERROR%d\r\n",num);
- while (1)
- {
- if (sendCommand("AT\r\n", "OK", 100, 10) == Success)
- {
- Sys_Soft_Reset();
- }
- delay_ms(200);
- }
- }
- void Sys_Soft_Reset(void)
- {
- SCB->AIRCR =0X05FA0000|(u32)0x04;
- }
- void phone(char *number)
- {
- char send_buf[20] = {0};
- memset(send_buf, 0, 20); //清空
- strcpy(send_buf, "ATD");
- strcat(send_buf, number);
- strcat(send_buf, ";\r\n");
- if (sendCommand(send_buf, "SOUNDER", 10000, 10) == Success);
- else errorLog(4);
- }
- unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
- {
- unsigned char n;
- USART2_CLR_Buf();
- for (n = 0; n < Retry; n++)
- {
- u2_printf(Command); //發送GPRS指令
-
- printf("\r\n***************send****************\r\n");
- printf(Command);
-
- Time_Cont = 0;
- while (Time_Cont < Timeout)
- {
- delay_ms(100);
- Time_Cont += 100;
- if (strstr(USART2_RX_BUF, Response) != NULL)
- {
- printf("\r\n***************receive****************\r\n");
- printf(USART2_RX_BUF);
- USART2_CLR_Buf();
- return Success;
- }
-
- }
- Time_Cont = 0;
- }
- printf("\r\n***************receive****************\r\n");
- printf(USART2_RX_BUF);
- USART2_CLR_Buf();
- return Failure;
- }
復制代碼
所有資料51hei提供下載:
01.STM32撥打電話.rar
(319.11 KB, 下載次數: 42)
2018-6-30 02:53 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|