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
", 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[] = "
USART Hyperterminal Interrupts Example: USART-Hyperterminal
communication using Interrupt
";
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****/