学号:140201206 姓名:刘宇新
组别:第7组 实验地点:D19
一、实验目的:
1.掌握通过GSM短信远程与嵌入式设备进行交互的方法。
二、实验内容:
1.编写程序把嵌入式开发板采集到的温度信息通过GSM短信传送给指定号码。(每10秒发送1次)
2.编写程序,使手机可以通过短信指令的方式控制嵌入式开发板的蜂鸣器和LED灯。(比如手机编辑“BEEP_ON”和“BEEP_OFF”控制嵌入式开发板的蜂鸣器)
三、实验过程描述及结果展示:
#include "sys.h"
#include "delay.h" #include "usart.h" #include "led.h"#include "lcd.h"#include "key.h" #include "touch.h" #include "text.h"#include "w25qxx.h" #include "malloc.h" #include "ff.h" #include "exfuns.h" #include <string.h>#include "dht11.h"const u16 POINT_COLOR_TBL[OTT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED};
extern u16 USART3_RX_STA;
extern u8 USART3_RX_BUF[400]; void LCD_Draw_Circle(u16 x0,u16 y0,u8 r);unsigned char cnt =0;u8 TimeCnt =0;unsigned char *SMS = 0;
u8 SMS_string[10]={"AT+CMGR="};//定时器6中断服务器程序
void TIM6_DAC_IRQHandler(void){ if(TIM_GetITStatus(TIM6,TIM_IT_Update)!=RESET)//判断是否是更新中{ TIM_ClearITPendingBit(TIM6,TIM_IT_Update);//清除TIM6更新中断标志TimeCnt++;}}int main(void){ // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2delay_init(168); //初始化延时函数uart_init(115200); //初始化串口波特率为115200uart3_init(115200); LED_Init(); //初始化LED KEY_Init();LCD_Init(); //LCD初始化 tp_dev.init(); //触摸屏初始化LCD_Clear(BLACK);POINT_COLOR=YELLOW;//设置字体为黄色BACK_COLOR = BLACK; //以下代码初始化定时器{ NVIC_InitTypeDef NVIC_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);//定时器初始化TIM_TimeBaseStructure.TIM_Period = 1000-1;TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);//使能指定的TIM6中断,允许更新中断NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化NVIC寄存器}TIM_Cmd(TIM6,ENABLE);//启动定时器6//以下代码初始化温湿度传感器{ GPIO_InitTypeDef GPIO_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;GPIO_Init(GPIOG,&GPIO_InitStructure);DHT11_Rst();while(DHT11_Check());}//以下代码配置蜂鸣器{ GPIO_InitTypeDef GPIO_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;GPIO_Init(GPIOF,&GPIO_InitStructure);}//初始化SIM900A{ unsigned char temp;unsigned char send_string0[] = {"AT+CNMI=2,1\x0d\x0a"};u8 send_string1[] = {"AT+CMGF=1\r\n"};u8 send_string2[] = {"AT+CSCS=\"GSM\"\r\n"};for(temp=0;temp<2;temp++){ USART_SendData(USART3,'A');while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);USART_SendData(USART3,'T');while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);USART_SendData(USART3,'0x0D');while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);USART_SendData(USART3,'0x0A');while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);for(temp=0;temp<13;temp++)//发送AT+CNMI=2,1\x0d\x0a{ USART_SendData(USART3,send_string0[temp]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);for(temp=0;temp<11;temp++)//发送AT+CMGF=1\r\n{ USART_SendData(USART3,send_string1[temp]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);for(temp=0;temp<15;temp++)//发送AT+CSCS=\"GSM\"\r\n{ USART_SendData(USART3,send_string2[temp]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);}USART3_RX_STA = 0;while(1){ if(TimeCnt>=100){ TimeCnt=0;printf("Time is out!\r\n");//以下代码把DHT11采集到的当前温度用短信的形式发送到固定号码{ u8 i;u8 send_string1[] = {"AT+CMGS=\"13021257803\"\r\n"};u8 add_ascii[]={"\x1A"};u8 sms_temperature[]={"current temperature:"};u8 temper[2];u8 temperature,humidity;DHT11_Read_Data(&temperature,&humidity);temper[0]=(temperature/10)+0x30;
temper[1]=(temperature%10)+0x30;strcat(sms_temperature,temper);strcat(sms_temperature,add_ascii);USART_ClearFlag(USART3,USART_FLAG_TC);printf("send_string1=%s\r\n",send_string1);for(i=0;i<23;i++){ USART_SendData(USART3,send_string1[i]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);printf("sms_temperature=%s\r\n",sms_temperature);
for(i=0;i<23;i++){ USART_SendData(USART3,sms_temperature[i]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}delay_ms(100);}}//以下代码监控是否有驱动BEEP的短信指令if(USART3_RX_STA & 0x8000){ char* p_display;USART3_RX_BUF[USART3_RX_STA & 0x7FFF]=0;if(p_display=strstr((const char*)USART3_RX_BUF,(const char*)"+CMTI:")){ p_display=p_display+12;SMS=p_display;{ u8 i;u8 send_string[100] = {"AT+CMGR="};u8 data_r_n[] = {"\r\n\n"};char *p_display;USART_ClearFlag(USART3,USART_FLAG_TC);strcat(send_string,SMS);strcat(send_string,data_r_n);for(i=0;i<12;i++){ USART_SendData(USART3,send_string[i]);while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);}}}if(p_display=strstr((const char*)USART3_RX_BUF,(const char*)"+32")){ if(strstr((const char*)USART3_RX_BUF,(const char*)"BEEP_ON")){ GPIO_SetBits(GPIOF,GPIO_Pin_8);}if(strstr((const char*)USART3_RX_BUF,(const char*)"BEEP_OFF")){ GPIO_ResetBits(GPIOF,GPIO_Pin_8);}}USART3_RX_STA=0;}}}四、总结及实验心得:
掌握了通过GSM短信远程与嵌入式设备进行交互的方法。