嵌入式軟件設計第11次實驗報告

學號:140201211            姓名:張潤澤併發

組別:第3組                實驗地點:D19函數

1、實驗目的:測試

一、瞭解短信AT指令的使用方法。字體

二、掌握使用短信AT指令驅動SIM900A發送和接收短信的方法。this

2、實驗內容:3d

1.使用AT命令進行模塊測試,發送和接收短信。blog

2.編寫程序利用觸摸屏完成固定號碼短信的發送和接收。(須要加上AT測試命令模塊、手機SIM卡檢測模塊、手機信號質量檢測模塊等等。)string

3、實驗過程描述及結果展現:it

短信經常使用AT指令io

GSM短信發送菜單示例

 

實現代碼:

#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;
}
}

}

4、總結及實驗心得:

  這節課掌握了短信AT指令驅動SIM900A發送和接收短信的方法,用AT命令進行模塊測試併發送和接收短信以及編寫程序利用觸摸屏完成固定號碼短信的發送和接收。

相關文章
相關標籤/搜索