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)
随机推荐
- windows环境下模仿Linux环境发起curl请求
1.到官网下载curl工具包 2.在curl.exe目录中使用(使用方式1) 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的目录. 由于博主使用的是windows 64位 的系统,因 ...
- Python类总结-析构函数和__call__方法
class Foo: def __init__(self): pass #析构函数 def __del__(self): print('解释器要销毁我了,我要做最后一次呐喊') def __call_ ...
- linux 重写rm命令
重写rm命令 replease rm to trash 必须使用root编辑/etc/bashrc vim /etc/bashrc 在最后面增加如下脚本 saferm () { if [ ! -d ...
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- Hibernate 组合主键映射
在开发过程中创建数据库表时,有时候会发现单纯的创建一个主键是不可行的,有时候就需要多个字段联合保持唯一,本文讲述如何创建组合主键的映射. 例如:记录一个班的考试成绩.学生跟科目是多对多的关系,只有一个 ...
- WPF常用控件样式( 内含一简单插件式开发示例)
最近离职,离职前面的一份外派然后又外包的工作,总觉得不妥,之后外派的办个入职手续都一再失约,干脆推了.恰逢清明时节,暴雨纷纷,于是打算先休息休息调整下状态,工作的事情还是谨慎点的好,免得影响心情.话说 ...
- 「Codechef April Lunchtime 2015」Palindromeness
「Codechef April Lunchtime 2015」Palindromeness 解题思路 : 考虑对于回文子串 \(s\) 贡献的定义: \[ value_s = [\ s[1,\lflo ...
- MySQL启动项提权
关于MySQL的启动项提权,听其名知其意.就是将一段 VBS脚本导入到 C:\Documents and Settings\All Users\「开始」菜单\程序\启动 下,如果管理员重启了服务器, ...
- 20162327WJH四则运算第二周总结
学号 20162327 <程序设计与数据结构>四则运算第二次实验报告 1.需求分析 1.本周我们进行了四则运算的后续完善,因为学习的比较欠缺,所以我负责比较简单的部分,就是只包含一个运算符 ...
- 哈希表(散列表)—Hash表解决地址冲突 C语言实现
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...