STM32 控制GSM模块收发信息 F407 discovery
main.c
#include "stm32f4_discovery.h"
#include <stdio.h> #define LED1_ON GPIO_SetBits(GPIOD,GPIO_Pin_12)
#define LED1_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_12) #define LED2_ON GPIO_SetBits(GPIOD,GPIO_Pin_13)
#define LED2_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_13) extern uint8_t NbrOfDataToTransfer;
extern uint8_t NbrOfDataToRead;
extern __IO uint8_t TxCounter;
extern __IO uint16_t RxCounter; extern volatile unsigned char MsgAddEnd;
extern volatile unsigned char MsgAdd[5];
extern volatile unsigned char MsgContentEnd;
extern volatile unsigned char MsgContent[240]; void NVIC_Config(void);
void GPIO_Configuration(void);
void STM_EVAL_COMInit(void);
void USART_Configuration(int BaudRate);
void LED_Config(void);
void Delay(__IO uint32_t nCount); void SendMSG2GF(void);
void MsgRemindInit(void);
void ReadMSG(void);
void JudgeFromMsg(void); int main(void)
{
/* Configure the system clocks */
NVIC_Config(); /* NVIC Configuration */ //GPIO_Configuration(); /* Configure the GPIOs */
STM_EVAL_COMInit(); USART_Configuration(115200); /* Configure the USART1 's mode */
/* Enable the EVAL_COM1 Transmit interrupt: this interrupt is generated when the
EVAL_COM1 transmit data register is empty */
LED_Config(); //SendMSG2GF();
MsgRemindInit(); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); while (1)
{
Delay(0xFFFF);
ReadMSG();
JudgeFromMsg();
}
}
void MsgRemindInit()
{
int i;
/*AT+CNMI=2,1 短信提示*/
unsigned char CMD1[13] = {0x41 ,0x54 ,0x2B ,0x43 ,0x4E ,0x4D ,0x49 ,0x3D ,0x32 ,0x2C ,0x31 ,0x0D ,0x0A};
/*AT+CMGF=1 英文方式发送*/
unsigned char CMD2[11] = {0x41 ,0x54 ,0x2B ,0x43 ,0x4D ,0x47 ,0x46 ,0x3D ,0x31 ,0x0D ,0x0A}; for(i = 0; i < 13; ++i)
{
USART_SendData(USART1, CMD1[i]);
Delay(0xFFF);
}
Delay(0xFFFFFF);
for(i = 0; i < 11; ++i)
{
USART_SendData(USART1, CMD2[i]);
Delay(0xFFF);
}
Delay(0xFFFFFF);
} void ReadMSG()
{
int i; unsigned char CMD1[8] = {0x41 , 0x54 , 0x2B , 0x43 , 0x4D , 0x47 , 0x52 , 0x3D}; unsigned char CMD2[5] = {0xff, 0xff, 0xff, 0xff, 0xff}; // 改回000 unsigned char CMD3[2] = {0x0D, 0x0A}; while(MsgAddEnd == 0) Delay(1); for(i = 0; i < 5; ++i)
{
CMD2[i] = MsgAdd[i];
if(MsgAdd[i] == 0xff) break;
} MsgAddEnd = 0;
for(i = 0; i < 5; ++i) MsgAdd[i] = 0xff; /*tx*/ for(i = 0; i < 8; ++i)
{
USART_SendData(USART1, CMD1[i]);
Delay(0xFFF);
} for(i = 0; i < 5; ++i)
{
if(CMD2[i] != 0xff)
{
if(CMD2[i] == 0x00) break;
USART_SendData(USART1, CMD2[i]);
Delay(0xFFF);
}
else break;
} for(i = 0; i < 2; ++i)
{
USART_SendData(USART1, CMD3[i]);
Delay(0xFFF);
}
LED1_ON;
/*waiting for msg content*/
while(MsgContentEnd == 0) Delay(1);
Delay(0xff);
LED1_OFF;
MsgContentEnd = 0;
} void JudgeFromMsg()
{
int i = 0;
if(MsgContent[0] == '1')
{
LED1_ON ;
LED2_ON ;
}
if(MsgContent[0] == '0')
{
LED1_OFF;
LED2_OFF;
}
for(i = 0; i < 240; ++i) MsgContent[i] = 0xff;
}
void SendMSG2GF()
{
int i;
// Step1 : AT+CMGF=1
unsigned char CMD1[11] = {0x41 ,0x54 ,0x2B ,0x43 ,0x4D ,0x47 ,0x46 ,0x3D ,0x31 ,0x0D ,0x0A};
// Step2 : AT+CMGS="18362970179"
unsigned char CMD2[23] = {0x41 ,0x54 ,0x2B ,0x43 ,0x4D ,0x47 ,0x53 ,0x3D ,0x22 ,0x31 ,0x35 ,0x30 ,0x30 ,0x35 ,0x31 ,0x38 ,0x33 ,0x32 ,0x37 ,0x34 ,0x22 ,0x0D ,0x0A};
// Step3 : STM32
unsigned char content[5] = {0x53 ,0x54 ,0x4D ,0x33 ,0x32};
// Step4 : 0x1A
unsigned char CMD3 = 0x1A; for(i = 0; i < 11; ++i)
{
USART_SendData(USART1, CMD1[i]);
Delay(0xFFF);
}
Delay(0xFFFFFF);
for(i = 0; i < 23; ++i)
{
USART_SendData(USART1, CMD2[i]);
Delay(0xFFF);
}
Delay(0xFFFFFF);
for(i = 0; i < 5; ++i)
{
USART_SendData(USART1, content[i]);
Delay(0xFFF);
}
Delay(0xFFFFFF);
USART_SendData(USART1, CMD3);
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
} /*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
} /**
* @brief Configures COM port.
* @param COM: Specifies the COM port to be configured.
* This parameter can be one of following parameters:
* @arg COM1
* @arg COM2
* @param USART_InitStruct: pointer to a USART_InitTypeDef structure that
* contains the configuration information for the specified USART peripheral.
* @retval None
*/
void STM_EVAL_COMInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); /* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
} /*******************************************************************************
* Function Name : USART_Configuration
* Description : Configures the USART1.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(int BaudRate)
{
USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = BaudRate;
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;
USART_Init(USART1, &USART_InitStructure); /* Configure USART1 basic and asynchronous paramters */
USART_Cmd(USART1, ENABLE); /* Enable USART1 */
} void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
} void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} #ifdef USE_FULL_ASSERT /**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */
while (1)
{
}
}
#endif /*******************************************************************************
* Function Name : fputc
* Description : Retargets the C library printf function to the USART.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch); /* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
} return ch;
}
中断函数
#include "stm32f4xx_it.h" #define LED1_ON GPIO_SetBits(GPIOD,GPIO_Pin_12)
#define LED1_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_12) #define LED2_ON GPIO_SetBits(GPIOD,GPIO_Pin_13)
#define LED2_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_13) #define USARTx_IRQHANDLER USART1_IRQHandler #define TXBUFFERSIZE (countof(TxBuffer) - 1)
#define RXBUFFERSIZE 0x20 #define countof(a) (sizeof(a) / sizeof(*(a))) /* Private variables ---------------------------------------------------------*/
uint8_t TxBuffer[] = "\n\rUSART Hyperterminal Interrupts Example: USART-Hyperterminal\
communication using Interrupt\n\r";
uint8_t RxBuffer[RXBUFFERSIZE];
uint8_t NbrOfDataToTransfer = TXBUFFERSIZE;
uint8_t NbrOfDataToRead = RXBUFFERSIZE;
__IO uint8_t TxCounter = 0;
__IO uint8_t RxData;
__IO uint16_t RxCounter = 0; volatile unsigned char MsgAddBegin = 0;
volatile unsigned char MsgAddEnd = 0;
volatile unsigned char MsgAddSvCur = 0;
volatile unsigned char MsgAdd[5];
volatile unsigned char MsgContentSvCur = 0;
volatile unsigned char MsgContentBegin = 0;
volatile unsigned char MsgContentEnd = 0;
volatile unsigned char MsgContent[240]; volatile unsigned char HeadCur = 0;
#define HeadCnt 3
volatile unsigned char Head[HeadCnt + 1] = {0x2B , 0x43 , 0x4D, 0xff}; // +CM
volatile unsigned char AddHeadCur = 0;
#define AddHeadCnt 9
volatile unsigned char AddHead[AddHeadCnt + 1] = {0x54 ,0x49 ,0x3A ,0x20 ,0x22 ,0x53 ,0x4D ,0x22 ,0x2C, 0xff}; //TI: "SM",
volatile unsigned char AddTailCur = 0;
#define AddTailCnt 2
volatile unsigned char AddTail[AddTailCnt] = {0x0D, 0x0A};
volatile unsigned char ContentHeadCur = 0;
#define ContentHeadCnt 2
volatile unsigned char ContentHead[ContentHeadCnt + 1] = {0x47 ,0x52 ,0xff}; //GR: 0x47 ,0x52 ,0x3A ,0x20, 0xff
volatile unsigned char QuotationMarkCnt = 0; // 8 /* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/ /******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/ /**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
} /**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
} /**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
} /**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
} /**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
} /**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
} /**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
} /**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
} /**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
} /******************************************************************************/
/* STM32F4xx Peripherals Interrupt Handlers */
/******************************************************************************/ /**
* @brief This function handles USARTx global interrupt request.
* @param None
* @retval None
*/
void USARTx_IRQHANDLER(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
RxData = USART_ReceiveData(USART1);
//USART_SendData(USART1, RxData);
if(MsgAddBegin == 1)
{
if(RxData == 0x0D)
{
MsgAddEnd = 1;
MsgAddBegin = 0;
HeadCur = 0;
AddHeadCur = 0;
MsgAddSvCur = 0;
return;
}
MsgAdd[MsgAddSvCur] = RxData;
MsgAddSvCur++;
return;
}
if(MsgContentBegin == 1)
{
if(RxData == 0x0D)
{
MsgContentEnd = 1;
MsgContentBegin = 0;
HeadCur = 0;
ContentHeadCur = 0;
MsgContentSvCur = 0;
QuotationMarkCnt = 0;
return;
}
MsgContent[MsgContentSvCur] = RxData;
MsgContentSvCur++;
return;
} if(RxData == Head[HeadCur]) HeadCur++;
else
{
if(HeadCur == HeadCnt)
{
if(RxData == AddHead[AddHeadCur]) AddHeadCur++;
if(RxData == ContentHead[ContentHeadCur]) ContentHeadCur++; if(ContentHeadCur == ContentHeadCnt)
{
if(RxData == 0x22) QuotationMarkCnt++ ;
if(QuotationMarkCnt == 8)
{
if(RxData == 0x0A)
{
MsgContentBegin = 1;
MsgContentSvCur = 0;
}
}
return;
}
else if(AddHeadCur == AddHeadCnt)
{
MsgAddBegin = 1;
MsgAddSvCur = 0;
return ;
}
}
else HeadCur = 0;
} }
}
/******************************************************************************/
/* STM32F4xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f4xx.s). */
/******************************************************************************/ /**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/ /**
* @}
*/ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
STM32 控制GSM模块收发信息 F407 discovery的更多相关文章
- 通过GSM模块发送经纬度求救信息。
本博客作为实验笔记,仅供学习交流.(转载请注明出处) 本实验通过GSM模块:SIM900a,实现向特定手机发送sos求救信号,并且利用GPS模块:微科VK2828U7G5LF,将经纬度信息同时发送到手 ...
- GSM模块_STM32实现GPRS与服务器数据传输经验总结
硬件环境 MCU:STM32F103RET6 (调试器:J-Link) GSM模块:Ai-Thinker_A6 (安信可)(还需要配一个串口打印工具,当初选这个模块纯粹是因为价格是最便宜的) ---- ...
- GSM模块_GPRS数据传输机制和原理
通信专业术语 BSS--基站子系统,通过无线接口与移动台直接联系,负责在一定区域内和移动台通信.(GSM) BTS--基站收发台,可以看作一复杂的无线调制器,BSS的主要部分,每个分配有若干信道.(G ...
- STM32—驱动RFID-RC522模块
文章目录 一.S50(M1)卡介绍 1.S50(M1)卡基础知识 2.内部信息 3.存取控制 4.数据块的存取控制 5.控制块的存取控 6.工作原理 7.M1与读卡器的通信 二.RC522工程代码详解 ...
- 使用python的email、smtplib、poplib模块收发邮件
使用python的email.smtplib.poplib模块收发邮件 一封电子邮件的旅程是: MUA:Mail User Agent——邮件用户代理.(即类似Outlook的电子邮件软件) MTA: ...
- STM32控制永磁同步电机 | FOC电机控制算法概述
1. FOC基本概念 参考:https://www.sohu.com/a/432103720_120929980 FOC(field-oriented control)为磁场导向控制,又称为矢量控制( ...
- STM32 控制步进电机 28BYJ-48
STM32 控制步进电机 28BYJ-48 http://blog.chinaunix.net/uid-12664992-id-300272.html 步进电机驱动最简化的逻辑: //四相八拍:A- ...
- 解决logging模块日志信息重复问题
解决logging模块日志信息重复问题 问题描述 相信大家都知道python的logging模块记录日志信息的步骤: # coding:utf-8 import logging ### 创建logge ...
- STM32开发 -- 4G模块开发详解(转)
STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)
随机推荐
- python网页下载
python 2.7版本下可以运行 import urllib2 def getHtml(url): response = None requset = None headers = {'User-A ...
- 转: CreateProcessAsUser 0xC0000005访问冲突问题
转:http://blog.csdn.net/glc22/article/details/77227367 在使用CreateProcessAsUser时出现了 0xC0000005访问冲突问题, ...
- STL容器 -- Bitset
核心内容:Bitset 是 STL 中的二进制容器, 存放的时 bit 位元素, 每一位只占一个 bit 位, 取值 0 或者 1, 可以像整形元素一样按位与或非, 并且大大优化了时间和空间复杂度. ...
- CF815C Karen and Supermarket [树形DP]
题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...
- [I/O]javaI/O工作机制
摘要:IO问题可以说是当今web应用中面临的主要问题之一.因为在这个数据爆发的时代,海量的数据在网络到处流动,而在这个过程中都会涉及IO问题,可以说IO问题已经成为web应用的瓶颈之一.如何优化?以此 ...
- Redux学习之我对于其工作流程的理解和实践
目录 1 工作流程图 2 各部位职责 3 Demo 1 工作流程图 2 各部位职责 我在理解这个流程图的时候,采用的是一种容易记住的办法,并且贴切实际工作职责. 我们可以把整个Redux工 ...
- luoguP4320 道路相遇 圆方树
标题已经告诉你怎么做了..... 两点间的圆点个数即为所求 建出圆方树后打个树剖求$lca$就行..... 复杂度$O(n + q \log n)$ #include <cstdio> # ...
- [BZOJ4517][SDOI2016]排列计数(错位排列)
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1616 Solved: 985[Submit][Statu ...
- bzoj 3685: 普通van Emde Boas树
3685: 普通van Emde Boas树 Description 设计数据结构支持:1 x 若x不存在,插入x2 x 若x存在,删除x3 输出当前最小值,若不存在输出-14 输出当 ...
- Request Response 跳转页面的理解
1.response 跳转页面 private void writeContent(String content) { HttpServletResponse response = ((Servlet ...