|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-7-6 15:13:02
|
显示全部楼层
百为SIM900开发板还在焊接中,UCGUI手机界面加上了SIM900打电话功能
另外给这个系统起了个名字,暂且就叫做BPhone吧
SIM900打电话源码:
gsm.c
/**
*****************************百为技术有限公司*********************************
作者:张锡良
时间:2013年5月10日
技术支持论坛;www.stm32bbs.com
官方淘宝网店: http://baiweijishu.taobao.com
*******************************STM32技术论坛**********************************
**/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include <stddef.h>
#include <string.h>
#include <ucos_ii.h>
#include "GUI.h"
#include "ff.h"
#include "gsm.h"
#define USARTx USART2
#define RCC_APB1Periph_USARTx RCC_APB1Periph_USART2
#define GPIOx GPIOA
#define RCC_APB2Periph_GPIOx RCC_APB2Periph_GPIOA
#define GPIO_TxPin GPIO_Pin_2
#define GPIO_RxPin GPIO_Pin_3
#define GPIO_PWR GPIOB
#define GPIO_PIN_PWR GPIO_Pin_0
void GSM_lowlevel_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_PWR;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIO_PWR, &GPIO_InitStructure);
/* Configure USARTx_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOx, &GPIO_InitStructure);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOx, &GPIO_InitStructure);
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure the USARTx */
USART_Init(USARTx, &USART_InitStructure);
/* Enable the USARTx */
USART_Cmd(USARTx, ENABLE);
}
int GSM_Init()
{
char command[]="AT\x0d\x0a";
char echo_off_command[]="ATE0\x0d\x0a";
char buf[50];
GSM_lowlevel_Init();
GPIO_SetBits(GPIO_PWR,GPIO_PIN_PWR);
GUI_Delay(1500);
GPIO_ResetBits(GPIO_PWR,GPIO_PIN_PWR);
ATCommand_Send(command);
ATCommand_Receive(buf, sizeof(buf));
if(strstr(buf, "OK") == 0)
{
printf("gsm init ok!\n\r");
}
else
{
printf("gsm init error!\n\r");
return FALSE;
}
// ATCommand_Send(echo_off_command);
// ATCommand_Receive(buf, sizeof(buf));
// if(strstr(buf, "OK") == 0)
// {
// printf("gsm set echo off ok!\n\r");
// }
// else
// {
// printf("gsm set echo off error!\n\r");
// return FALSE;
// }
return TRUE;
}
void ATCommand_Send(char* buf)
{
printf("%s",buf);
while(*buf)
{
USART_SendData(USARTx, *buf++);
/* Loop until the end of transmit */
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
{
// GUI_Delay(1);
}
}
}
void ATCommand_Receive(char* buf, int bufLen)
{
int index;
GUI_Delay(10);
while(index < bufLen)
{
/* Loop until the USARTx Receive Data Register is not empty */
while(USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET)
{
// GUI_Delay(1);
}
buf[index++] = USART_ReceiveData(USARTx) & 0xFF;
}
buf[index] = '\0';
printf("%s\n\r",buf);
}
void GSM_DialNumber(char* number)
{
char command[50];
char buf[50];
GSM_Init();
GUI_Delay(2000);
memset(command,0,sizeof(command));
sprintf(command, "ATD%s;", number);
strcat(command,"\x0d\x0a");
ATCommand_Send(command);
ATCommand_Receive(buf, sizeof(buf));
} |
|