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的更多相关文章

  1. 通过GSM模块发送经纬度求救信息。

    本博客作为实验笔记,仅供学习交流.(转载请注明出处) 本实验通过GSM模块:SIM900a,实现向特定手机发送sos求救信号,并且利用GPS模块:微科VK2828U7G5LF,将经纬度信息同时发送到手 ...

  2. GSM模块_STM32实现GPRS与服务器数据传输经验总结

    硬件环境 MCU:STM32F103RET6 (调试器:J-Link) GSM模块:Ai-Thinker_A6 (安信可)(还需要配一个串口打印工具,当初选这个模块纯粹是因为价格是最便宜的) ---- ...

  3. GSM模块_GPRS数据传输机制和原理

    通信专业术语 BSS--基站子系统,通过无线接口与移动台直接联系,负责在一定区域内和移动台通信.(GSM) BTS--基站收发台,可以看作一复杂的无线调制器,BSS的主要部分,每个分配有若干信道.(G ...

  4. STM32—驱动RFID-RC522模块

    文章目录 一.S50(M1)卡介绍 1.S50(M1)卡基础知识 2.内部信息 3.存取控制 4.数据块的存取控制 5.控制块的存取控 6.工作原理 7.M1与读卡器的通信 二.RC522工程代码详解 ...

  5. 使用python的email、smtplib、poplib模块收发邮件

    使用python的email.smtplib.poplib模块收发邮件 一封电子邮件的旅程是: MUA:Mail User Agent——邮件用户代理.(即类似Outlook的电子邮件软件) MTA: ...

  6. STM32控制永磁同步电机 | FOC电机控制算法概述

    1. FOC基本概念 参考:https://www.sohu.com/a/432103720_120929980 FOC(field-oriented control)为磁场导向控制,又称为矢量控制( ...

  7. STM32 控制步进电机 28BYJ-48

    STM32 控制步进电机 28BYJ-48  http://blog.chinaunix.net/uid-12664992-id-300272.html 步进电机驱动最简化的逻辑: //四相八拍:A- ...

  8. 解决logging模块日志信息重复问题

    解决logging模块日志信息重复问题 问题描述 相信大家都知道python的logging模块记录日志信息的步骤: # coding:utf-8 import logging ### 创建logge ...

  9. STM32开发 -- 4G模块开发详解(转)

    STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)

随机推荐

  1. [Codeforces50C]Happy Farm 5 凸包

    大致题意: 平面上有n个整数点,问你最少经过多少步能够将所有点严格包围. 将点严格包围要求你走的路径完全包围给出的点且不能有点在路径上 你只能走整数点,且方向只有上下左右左上右下等8个方向,每次移动一 ...

  2. C++快速文件输入输出

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ C语言可以获得接近汇编的性能,而输入输出常常是最为耗时的过程,因此可以使用 C 语言中的 fre ...

  3. 【Python】单例模式Singleton

    前两天一个面试被问到python中单例模式有几种实现方式,只答出了可以用元类实现...然后就想不起来了. 之后翻书,原来这些之前都见过的啊.... 1.手动实现真正创建实例的方法__new__()来实 ...

  4. PHP代码重用

    代码重用 include() 和require() 都是载入文件 include()如果载入的文件不存在,提示警告错误,程序还可以继续执行 require()如果载入的文件不存在,致命性错误,程序终止 ...

  5. 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2472  Solved: 911 Description 在中国 ...

  6. js使用s:property标签接收json格式数据

    js使用s:property接收json数据时,会出现字符被转译的错误. 错误如下: 引号会被转译成'"'字符,导致解析不了. 错误原因: html的s:property接收不会出错,而js ...

  7. 【UOJ #105】【APIO2014】Beads and wires

    http://uoj.ac/problem/105 好神的dp啊. 确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟. 枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表 ...

  8. [BZOJ4872][六省联考2017]分手是祝愿(期望DP)

    4872: [Shoi2017]分手是祝愿 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 516  Solved: 342[Submit][Statu ...

  9. Android Studio --> Gradle Build设置自动

    ps:http://www.cnblogs.com/kangyi/p/4448398.html 应用场景 通常情况下我们的apps发布后也就是release模式下log是不显示的,debug模式下是显 ...

  10. Java 请求webServce接口 带参数

    public String getWebServiceByParams(String param){ //获取基金缴付记录 // Post请求的url,与get不同的是不需要带参数 URL postU ...