/**********************************************************************************
作者:淋哥电子
说明:GSM的RX(第2脚)接单片机串口处的TX(3),TX(3)接RX(2),然后还要共地(5)
************************************************************************************/
/*************头文件专区*************/
#include <reg52.h>
#include <intrins.h>
/*************宏定义专区***************/
#define uint unsigned int
#define uchar unsigned char
#define BLKN 4 //列锁存器数(=LED显示字数*2)
#define TOTAL 8 //待显示字个数,本例共14个
/*************变量定义专区*************/
sbit G=P1^7; //P1.7为154译码器显示允许控制信号端口,0时输出,1时输出全为高阻态.
sbit CLK=P1^6; //P1.6为595输出锁存器时钟信号端,1时输出数据,从1到0时锁存输出数据.(引脚12)
sbit SCLR=P1^5; //P1.5为595移位寄存器清零口,平时为1,为0时,输出全为0.(引脚10)
sbit dat_595=P2^0; //595的14脚
sbit Shift_Clk=P2^1; //595的11脚
unsigned char dispram[(BLKN/2)*32]={0}; //显示区缓存,1字共1*32单元
uchar code AT[]={"AT"}; //AT指令
uchar code AT_Code[]="OK"; //GSM响应
uchar idata RsBuf[100]; //接收缓存区
uchar code Receive_Mode[]={"AT+CNMI=2,1,0,0,0"}; //短信接收模式设置:通过SIM卡接收
uchar code Read_Message[]={"AT+CMGR=1"}; //读第一条短信
uchar code Delete_All_Message[]={"AT+CMGD=1,4"}; //删除所有短信
//**************字模表******************//
unsigned char code Bmp1[][32]=
{
{0xF7,0x6F,0xF7,0x6F,0xE8,0x03,0xEF,0x6B,0xCF,0x6B,0xC8,0x03,0xAB,0x6F,0x6B,0x6F,
0xE8,0x01,0xEF,0x6D,0xEF,0x6D,0xEE,0xE5,0xEE,0xEB,0xED,0xEF,0xED,0xEF,0xEB,0xEF},//"佛",0
{0xFE,0xFF,0xFE,0xFF,0xFE,0xFF,0xFE,0xFF,0xFE,0xFF,0xDE,0xFB,0xDE,0xFB,0xDE,0xFB,
0xDE,0xFB,0xDE,0xFB,0xDE,0xFB,0xDE,0xFB,0xDE,0xFB,0x80,0x03,0xDF,0xFB,0xFF,0xFF},//"山",1
{0xF9,0xF7,0x87,0x77,0xF7,0xB7,0xF7,0xB7,0x01,0xF7,0xE7,0x77,0xE3,0xB7,0xD5,0xB7,
0xD7,0xF1,0xB7,0x87,0x74,0x77,0xF7,0xF7,0xF7,0xF7,0xF7,0xF7,0xF7,0xF7,0xF7,0xF7},//"科",0
{0xFE,0xF7,0xEF,0x73,0xF3,0x37,0xF7,0x6F,0x80,0x01,0xBF,0xFB,0x70,0x17,0xFF,0xBF,
0xFF,0x7F,0x80,0x01,0xFF,0x7F,0xFF,0x7F,0xFF,0x7F,0xFF,0x7F,0xFD,0x7F,0xFE,0xFF},//"学",1
{0xEF,0xDF,0xEF,0xDF,0xEF,0xDF,0x02,0x01,0xEF,0xDF,0xEB,0xDF,0xE6,0x03,0xCE,0xF7,
0x2F,0x77,0xEF,0x6F,0xEF,0x9F,0xEF,0x9F,0xEF,0x6F,0xEE,0xF1,0xA9,0xFB,0xDF,0xFF},//"技",2
{0xFE,0xFF,0xFE,0xDF,0xFE,0xEF,0xFE,0xFF,0x00,0x01,0xFE,0xFF,0xFC,0x7F,0xFA,0xBF,
0xFA,0xDF,0xF6,0xEF,0xEE,0xE7,0xDE,0xF1,0x3E,0xFB,0xFE,0xFF,0xFE,0xFF,0xFF,0xFF},//"术",3
{0xFE,0xF7,0xEF,0x73,0xF3,0x37,0xF7,0x6F,0x80,0x01,0xBF,0xFB,0x70,0x17,0xFF,0xBF,
0xFF,0x7F,0x80,0x01,0xFF,0x7F,0xFF,0x7F,0xFF,0x7F,0xFF,0x7F,0xFD,0x7F,0xFE,0xFF},//"学",4
{0xFF,0x7F,0x07,0xBF,0x70,0x01,0x6B,0xFB,0x5F,0xFF,0x5C,0x07,0x6F,0xFF,0x77,0xFF,
0x70,0x01,0x56,0xDF,0x6E,0xDF,0x7E,0xDF,0x7D,0xDD,0x7D,0xDD,0x7B,0xDD,0x77,0xE1},//"院",5
};
void delay_ms(uint ms); //毫秒级延时
void UART_Send_Byte(uchar byte);//发送单字节
void UART_Send_Str(uchar * str,uint len);//发送字符串
void UART_Init(void); //串口初始化
void ClrRsBuf(uchar * str,uint len); //清除接受指令缓存区
uchar GSM_OK(void);//判断是否返回OK
uchar CheckAtc_Message(void);// 判断是否返回"+CMTI: "SM", 41",若返回,则收到短信
uchar GSM_Read_Message(void);// 判断读到的短信
uchar Read_One_Message(void);// 返回读到短信内容所指标志,如收到“佛”返回1,收到“科”返回2
void Read(void); //读一条短信完整过程
uchar GSM_AT(void); //发AT指令,返回OK证明已连接
uchar GSM_Receive_Mode(void); //设置接收短信模式:通过SIM卡接收
void Send_Set(void); //设置GSM
uchar Delete_Message(void); //删除SMI卡内所有短信
void SendByte_595(unsigned char byte); //595发送一个字节的数据
void display_up(uchar word1,uchar word2); //点阵显示:上滚形式
/**************************************************
*函数名称:main
*输入:无
*输出:无
*功能说明:主函数
***************************************************/
void main(void)
{
UART_Init(); // 串口初始化
Send_Set(); //GSM设置
while(1)
{
Read(); //读短信
}
}
/**************************************************
*函数名称:SendByte_595
*输入:byte
*输出:无
*功能说明:595发送一个字节的数据
***************************************************/
void SendByte_595(unsigned char byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
byte >>=1;
dat_595=CY;
Shift_Clk=1;
_nop_();
_nop_();
Shift_Clk=0;
}
}
/**************************************************
*函数名称:display_up
*输入:word1,word2 待显示的汉字
*输出:无
*功能说明:点阵显示:上滚形式
***************************************************/
void display_up(uchar word1,uchar word2)
{
unsigned char j,k,q;
for (j=0; j<16; j++)
{
for (k=0; k<15; k++)
{
for(q=0;q<BLKN/2;q++)
{
dispram[k*2+q*32] = dispram[(k+1)*2+q*32];
dispram[k*2+1+q*32] = dispram[(k+1)*2+1+q*32];
}
}
dispram[30]=Bmp1[word1][j*2]; //改变Bmp[a][b]中a的数据就可以改变显示的内容,这个为左半屏
dispram[31]=Bmp1[word1][j*2+1]; //此为右半屏
dispram[30+32]=Bmp1[word2][j*2]; //显示“时”左半屏
dispram[31+32]=Bmp1[word2][j*2+1]; //显示“时” 右半屏
delay_ms(100);
}
delay_ms(3000);//滚动暂停
}
/****************************************
*函数名称:delay_ms
*输入:uint ms延时时间 ,注意ms的数据类型
*输出:无
*功能说明:毫秒级延时
*****************************************/
void delay_ms(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/********************************
*函数名称:UART_Send_Byte
*输入:byte
*输出:无
*功能说明:发送单字节
*********************************/
void UART_Send_Byte(uchar byte)
{
SBUF=byte; //缓冲区装载要发送的字节数据
while(TI==0); //等待发送完毕,TI标志位会置1
TI=0; //清零发送完成标志位
}
/*************************************
*函数名称:UART_Send_Str
*输入:*str 字符串指针,len字符串长度
*输出:无
*功能说明:发送字符串
**************************************/
void UART_Send_Str(uchar * str,uint len)
{
while(len--)
{
UART_Send_Byte(*str++);
}
}
/*************************************
*函数名称:UART_Init
*输入:无
*输出:无
*功能说明:串口初始化
**************************************/
void UART_Init(void)
{
SCON = 0x50; // 设定串行口工作方式,8位数据位,允许接收
T2CON = 0x34; //设置定时器2,作为波特率发生器
RCAP2L = 0XDC; //9600波特率的低8位
RCAP2H = 0XFF; //9600波特率的高8位
ES=1;
TMOD = 0x01; //定时器T0工作方式1:16位方式
TR0 = 1; //启动定时器T0
TH0 = (65536-1000)/256; //设定显示屏刷新率每秒62.5帧(16毫秒每帧)
TL0 = (65536-1000)%256;
P1 = 0x3f; //CONIO端口初值
ET0=1; //允许定时器T0中断
EA=1;
}
/*************************************
*函数名称:ClrRsBuf
*输入:uchar * str uint len
*输出:无
*功能说明:清除接受指令缓存区
**************************************/
void ClrRsBuf(uchar * str,uint len)
{
uchar i;
for(i=0;i<len;i++)
str[i]=0;
}
/*************************************
*函数名称:GSM_OK
*输入:无
*输出:1:返回OK;0:不返回OK
*功能说明:判断是否返回OK
**************************************/
uchar GSM_OK(void)
{
uchar i;
for(i=0; i<sizeof(RsBuf); i++)
{
if(RsBuf[i] == 'O' && RsBuf[i+1]== 'K')
{
return 1;
}
}
return 0;
}
/***********************************************************
*函数名称:CheckAtc_Message
*输入:无
*输出:1:收到信息;0:没收到信息
*功能说明:判断是否返回"+CMTI: "SM", 41",若返回,则收到短信
**************************************************************/
uchar CheckAtc_Message(void)
{
uchar j;
for(j=0;j<sizeof(RsBuf);j++)
{
if((RsBuf[j]==0x53)&&(RsBuf[j+1]==0x4D)) //0X53 'S' 4D 'M'
return 1;
}
return 0;
}
/***********************************************************
*函数名称:GSM_Read_Message
*输入: