#include "stm32f10x_lib.h"
#include<string.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
unsigned short int ADC_ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
int seat=0; //接收字符串的存储位置
char RX[200];
char content[]="Hi!I am zhangzhengxin,I have another telephone number,Please call me with 18022305866,Thanks";
char NUM1[14]={0x22,0x31,0x38,0x30,0x32,0x32,0x33,0x30,0x35,0x38,0x36,0x36,0x22};
char NUM_Receive[14]="15850752650",m[]={0x22,0x41,0x54,0x22};
u8 i=0,j=0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM2_Configuration1(void);
void Delay(vu32 nTime);
void SetupLED (void) ;
void USART_Configuration(void);
void USART2_Puts(char * str);
void USART2_PutHex(char Hex);
void CLR_RX(void);
void MESSAGE(char *NUM,char *CH);
u8 Hand(u8 *a);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* Configure the system clocks */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
TIM2_Configuration1();
USART_Configuration();//串口初始化
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//开启串口接收中断
USART_ITConfig(USART2,USART_IT_TXE,ENABLE);
/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
while(1)
{
//握手
Delay(1000);
while(!Hand("OK"))
{
USART2_Puts("AT\r"); //握手
Delay(10000);
}
CLR_RX();
/////////////////////////////////////////////////////////////////////////////////////
USART2_Puts("AT+CMGR=1\r\n");//发送读取信息指令
while(!Hand("OK")); //等待设置成功
MESSAGE(NUM_Receive,content);
}
}
/*******************************************************************************
* Function Name : SysTick_Configuration
* Description : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIOB, GPIOC and AFIO clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* 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 USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configures the USART1.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
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(USART2, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd(USART2, ENABLE);
}
/*******************************************************************************
* Function Name : USART1_Puts发送字符串
*******************************************************************************/
void USART2_Puts(char * str)
{
while(*str)
{
USART_SendData(USART2, *str++);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
}
void USART2_PutHex(char Hex)
{
USART_SendData(USART2, Hex);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
/*******************************************************************************
* Function Name : TIM2_Configuration1
*******************************************************************************/
void TIM2_Configuration1(void)
{
TIM_TimeBaseInitTypeDef timInitStruct;
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 定时器基准频率64MHz
timInitStruct.TIM_Prescaler = (36000-1); // 计数频率为1KHz
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Period = 19; // 这个值实际上就是TIMX->ARR,延时开始时重新设定即可
TIM_TimeBaseInit(TIM2, &timInitStruct);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); // 计数溢出时触发中断
TIM_Cmd(TIM2, ENABLE);
TIM_ARRPreloadConfig(TIM2, ENABLE);
}
/*******************************************************************************
* Function Name : 发送短信
*******************************************************************************/
void MESSAGE(char *NUM,char *CH)
{
CLR_RX();
Delay(100);
USART2_Puts("AT+CMGS=");
USART2_Puts(NUM);
USART2_Puts("\r\n");
Delay(1000);
while(!Hand(">"))
CLR_RX();
Delay(100);
USART2_Puts(CH);
USART_SendData(USART2, 0x1a);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
Delay(1000);
while(!Hand("OK"));
CLR_RX();
Delay(100);
}
/*******************************************************************************
* Function Name : CLR_RX(void)清除串口接收字符串缓存
*******************************************************************************/
void CLR_RX(void)
{
char k;
for(k=0;k<200;k++) //将缓存内容清零
{
RX[k] = 0;
}
seat = 0; //接收字符串的起始存储位置
}
/*****************判断缓存中是否含有指定的字符串函数******************/
//函数原型:bit Hand(unsigned char *a)
//函数功能:判断缓存中是否含有指定的字符串
//输入参数:unsigned char *a 指定的字符串
//输出参数:bit 1---含有 0---不含有
//调用模块:无
/*******************************************************************/
u8 Hand(u8 *a)
{
if(strstr((char*)RX,(char *)a)!=NULL)
return 1;
else
return 0;
}
/*******************************************************************************
* Function Name : Delay
*******************************************************************************/
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert error has occurred.
* Input : - file: pointer to the source file name
* - line: assert error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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 */
USART_SendData(USART1,0x31);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
}
#endif
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE)!= RESET)//接收到数据
{
RX[seat++]=USART_ReceiveData(USART2);
}
}
|