嵌入式软件设计第11次实验报告
学号:140201236 姓名:沈樟伟
组别:第2组 实验地点:D19
一、实验目的:
1、了解短信AT指令的使用方法。
2、掌握使用短信AT指令驱动SIM900A发送和接收短信的方法。
二、实验内容:
1.使用AT命令进行模块测试,发送和接收短信。
2.编写程序利用触摸屏完成固定号码短信的发送和接收。(需要加上AT测试命令模块、手机SIM卡检测模块、手机信号质量检测模块等等。)
三、实验过程描述及结果展示:
短信常用AT指令
程序代码:
#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> const u16 POINT_COLOR_TBL[OTT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED}; extern u16 USART3_RX_STA; extern u8 USART3_RX_BUF[400]; unsigned char cnt = 0; unsigned char *SMS = 0; u8 SMS_string[10]={"AT+CMGR="}; void LCD_Draw_Circle(u16 x0,u16 y0,u8 r); //串口3发送数据示例代码 // USART_SendData(USART3,send_string[i]); // while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);//等待发送结束 //串口3接收数据示例代码 // while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); // receive_string[i] = (USART_ReceiveData(USART3));
int main(void) {
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2 delay_init(168); //初始化延时函数 uart_init(115200); //初始化串口波特率为115200 uart3_init(115200);
LED_Init(); //初始化LED LCD_Init(); //LCD初始化 tp_dev.init(); //触摸屏初始化
LCD_Clear(BLACK); POINT_COLOR=YELLOW;//设置字体为黄色 BACK_COLOR = BLACK;
//初始化SIM900A //发AT测试命令 //设置短信的新消息提示AT+CNMI=2,1; { unsigned char temp; unsigned char send_string0[] = {"AT+CNMI=2,1\x0d\x0a"}; unsigned char send_string1[] = {"AT+CMGF=1\r\n"}; unsigned char 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+CMGP=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;
//以下代码构建短信发送菜单 { Show_Str(30,30,200,24,"命令按钮区",24,1); Show_Str(248,30,200,24,"文本区",24,1);
LCD_DrawRectangle(30,70,174,118);//AT测试按钮方程 Show_Str(42,82,200,24,"AT测试按钮",24,1); LCD_DrawRectangle(248,70,452,118);//AT测试按钮的显示文字框
LCD_DrawRectangle(30,142,222,190);//SIM卡测试按钮方程 Show_Str(42,154,204,24,"卡状态测试按钮",24,1); LCD_DrawRectangle(248,142,452,190);//
LCD_DrawRectangle(30,214,198,262); Show_Str(42,226,204,24,"信号测试按钮",24,1); LCD_DrawRectangle(248,214,452,262);
LCD_DrawLine(0,282,479,282);
Show_Str(42,338,204,24,"发送按钮",24,1); LCD_DrawRectangle(30,326,162,374);
Show_Str(168,310,204,16,"发送号码",16,1); LCD_ShowString(256,310,88,16,16,"17801017974"); LCD_DrawRectangle(248,302,452,334);
Show_Str(168,358,204,16,"发送内容",16,1); LCD_ShowString(256,358,150,16,16,"this is test text"); LCD_DrawRectangle(248,350,452,382);
LCD_DrawLine(0,402,479,402);//画分割线
Show_Str(42,458,204,24,"接收按钮:",24,1); LCD_DrawRectangle(30,446,162,494);
Show_Str(168,430,204,16,"来信号码:",24,1); LCD_DrawRectangle(248,422,452,454);
Show_Str(168,478,204,16,"提示灯",16,1);
Show_Str(42,510,204,16,"短信内容:",24,1); LCD_DrawRectangle(30,526,452,766);
//画个圆框用来做短信提示 { int i; POINT_COLOR=YELLOW;//设置字体为黄色 for(i=20;i<22;i++) { LCD_Draw_Circle(272,486,i); } } }
//以下代码扫描触摸屏检测按键 while(1) { tp_dev.scan(0); if(tp_dev.x[0]>30 && tp_dev.x[0]<174 && tp_dev.y[0]>70 &&tp_dev.y[0]<118)//AT测试按钮的按键区域 { u8 i; u8 send_string[] = {"AT\r\n"}; u8 receive_string[20] = {0}; char *p_display; receive_string[19] = '\0';
while(tp_dev.sta & 0x80)//等待触摸屏按键松开 tp_dev.scan(0); USART_ClearFlag(USART3,USART_FLAG_TC);//加上此句,串口发送的第一个字节就不会丢失 for(i=0;i<4;i++) { USART_SendData(USART3,send_string[i]); while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);//等待发送结束 }
//清除触摸值 tp_dev.x[0]=0xFFFF; tp_dev.y[0]=0xFFFF; }
if(tp_dev.x[0]>30 && tp_dev.x[0]<222 && tp_dev.y[0]>142 &&tp_dev.y[0]<190)//SIM卡状态测试按钮 { u8 i; u8 send_string[] = {"AT+CPIN?\r\n"}; u8 receive_string[32] = {0}; char *p_display; receive_string[31] = '\0';
while(tp_dev.sta & 0x80)//等待触摸屏按键松开 tp_dev.scan(0); USART_ClearFlag(USART3,USART_FLAG_TC);//加上此句,串口发送的第一个字节就不会丢失 for(i=0;i<10;i++) { USART_SendData(USART3,send_string[i]); while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);//等待发送结束 }
//清除触摸值 tp_dev.x[0]=0xFFFF; tp_dev.y[0]=0xFFFF; }
if(tp_dev.x[0]>30 && tp_dev.x[0]<198 && tp_dev.y[0]>214 &&tp_dev.y[0]<262)//信号状态测试按钮 { u8 i; u8 send_string[] = {"AT+CSQ\r\n"}; u8 receive_string[32] = {0}; char *p_display; receive_string[31] = '\0';
while(tp_dev.sta & 0x80)//等待触摸屏按键松开 tp_dev.scan(0); USART_ClearFlag(USART3,USART_FLAG_TC);//加上此句,串口发送的第一个字节就不会丢失 for(i=0;i<8;i++) { USART_SendData(USART3,send_string[i]); while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);//等待发送结束 }
//清除触摸值 tp_dev.x[0]=0xFFFF; tp_dev.y[0]=0xFFFF; }
if(tp_dev.x[0]>30 && tp_dev.x[0]<162 && tp_dev.y[0]>326 &&tp_dev.y[0]<374)//发送固定短信测试按钮 { u8 i; u8 send_string1[] = {"AT+CMGS=\"13021257803\"\r\n"}; u8 send_string2[] = {"This is test text!\x1A"};
while(tp_dev.sta & 0x80)//等待触摸屏按键松开 tp_dev.scan(0); USART_ClearFlag(USART3,USART_FLAG_TC);//加上此句,串口发送的第一个字节就不会丢失 for(i=0;i<23;i++) { USART_SendData(USART3,send_string1[i]); while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);//等待发送结束 }
delay_ms(100);
for(i=0;i<19;i++) { USART_SendData(USART3,send_string2[i]); while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);//等待发送结束 }
//清除触摸值 tp_dev.x[0]=0xFFFF; tp_dev.y[0]=0xFFFF; } if(tp_dev.x[0]>30 && tp_dev.x[0]<162 && tp_dev.y[0]>446 &&tp_dev.y[0]<494)//有短信提示后,短信接受按钮 { u8 i; u8 send_string[100] = {"AT+CMGR=\r\n"}; u8 data_r_n[] = {"\r\n\n"}; char *p_display;
while(tp_dev.sta & 0x80)//等待触摸屏按键松开 tp_dev.scan(0); 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);//等待发送结束 }
//清除同心圆,表示短信已阅读 { int i; POINT_COLOR = BLACK; for(i=1;i<20;i++) { LCD_Draw_Circle(272,486,i); } POINT_COLOR = YELLOW; }
//清除触摸值 tp_dev.x[0]=0xFFFF; tp_dev.y[0]=0xFFFF; } //以下代码根据串口3收到的信息作出相应的动作 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*)"AT\r\n"))//当收到AT的反馈时 { p_display = p_display+6; LCD_ShowString(260,82,240,24,24,(u8 *)p_display); }
if(p_display = strstr((const char*)USART3_RX_BUF,(const char*)"AT+CPIN?"))//当收到SIM卡状态查询命令的反馈时 { p_display = p_display+19; LCD_ShowString(260,154,240,24,24,(u8 *)p_display); }
if(p_display = strstr((const char*)USART3_RX_BUF,(const char*)"AT+CSQ"))//当收到信号强度测试命令的反馈时 { p_display = p_display+10; LCD_ShowString(260,226,240,24,24,(u8 *)p_display); }
if(p_display = strstr((const char*)USART3_RX_BUF,(const char*)"+CMTI:"))//当收到新消息提示符 { p_display = p_display+12; SMS = p_display; //画同心圆表示来短信了 { int i; POINT_COLOR = RED; for(i=1;i<20;i++) { LCD_Draw_Circle(272,486,i); } POINT_COLOR = YELLOW; } }
if(p_display = strstr((const char*)USART3_RX_BUF,(const char*)"+32"))//当收到消息正文后 { p_display = p_display+6; LCD_ShowString(38,534,320,16,16," "); LCD_ShowString(38,534,320,16,16,p_display);
//给出短信号码 { p_display = strstr((const char*)USART3_RX_BUF,(const char*)"+86"); p_display = p_display+3; LCD_ShowString(256,430,88,16,16," "); LCD_ShowString(256,430,88,16,16,p_display); } } USART_RX_STA=0; } } }
实验结果:
总结:
本次实验的任务是.使用AT命令进行模块测试,发送和接收短信,并编写程序利用触摸屏完成固定号码短信的发送和接收。经过我们的认真思考,总体上没有太大的问题。开始时由于手机开信号的不稳定,造成了一些不必要的问题。其次是由于在程序中加了一句输出语句,导致检验时多一次输出语句。这次实践加深了我对嵌入式的理解与兴趣。