stc12单片机,SIM800,GPRS通信

#include <STC12C5A60S2.H>

#include"stdio.h"
#include"string.h"


typedef unsigned char uint8_t    		;
typedef unsigned int uint16_t		;

uint16_t Smog = 0 ;  //测试变量

//sim800GPRS联网----------------------------------------------------
uint8_t  idata  sendStep = 0 ;           		//发送索引
uint8_t  xdata  receiveArray[50] = {0} ; 		  	//接收数组
uint8_t  code  facilityID[10] = "20201216"   ;	//设备ID
uint8_t  idata  sendDelay = 0 ;                 //发送延时
uint8_t  xdata  sendArray[30]= {0} ;  //发送数组
uint8_t  xdata  TSArray[30]= {0} ;    //暂存数组
uint8_t  idata  subscript = 0 ;     //接收数组下标
uint8_t  heart = 0 ;                //心跳变量判断是否连接
uint8_t request = 0 ;                   //请求数据
uint8_t ERROR = 0 ;                 //记录错误次数


void delay(uint16_t  dea)
{
	while(dea--) ;

}

void Uart_Init(void)
{
	EA=1;              //打开总中断
	PCON &= 0x7F;	   //波特率不加倍
	AUXR  = 0x11;
	BRT = 0xFD ;       //波特率为9600
	ES=1;              //打开串口1中断回执
	SCON |= 0x50;      //0101 0000
	S2CON |= 0x50;
	IE2 |= 1 ;         //IE2不可位寻址,使能ES2串口中断回调
}

void Uart1Bit(char dat)
{
	SBUF=dat;
	while(!TI);
	TI=0;
}

void Uart1Byte(char *byte)
{
	while(*byte != '\0')
	{
		Uart1Bit(*byte++);
	}
}
void Uart2Bit(char dat)
{
	S2BUF=dat;
	while(!(S2CON&2));   //等待S2TI=1 说明发送完成
	S2CON &= ~2 ;      //清除 S2TI发送标志位
}

void Uart2Byte(char *byte)
{
	while(*byte != '\0')
	{
		Uart2Bit(*byte++);
	}
}




uint16_t i = 0 ;
void send()      //发送函数
{

	if(sendDelay==0)
	{
		sendDelay = 60 ;
		switch(sendStep)
		{
		case 0 :
			Uart1Byte("ATE0\r\n");   //关闭回显
			Uart2Byte("send-step0\r\n");

			break ;
		case 1 :
			Uart1Byte("AT+COPS?\r\n");   //查询是否连接到网络
			Uart2Byte("send-step1\r\n");
			break ;
		case 2 :

			Uart1Byte("AT+CIPSTART=\"TCP\",\"101.200.208.33\",\"2000\"\r\n");   //连接TCP
			Uart2Byte("send-step2\r\n");
			break ;
		case 3 :
			/*
			TCP连接成功就可以发送数据了
			*/
			ERROR ++ ;
			Uart1Byte("AT+CIPSEND\r\n");
			Uart2Byte("send-step3\r\n");

			break ;
		case 4 :
			/*
			发送设备信息
			*/
			if(!request)
			{
				if(!heart)
				{
					request = 1 ;

				}
				memset(TSArray,0,sizeof(TSArray));
				memset(sendArray,0,sizeof(sendArray));
				i++;
				sprintf(TSArray,"IAD,123,145,122,%d",i);   //具体内容
				sprintf(sendArray,"%s0,data:%s,,,dfsfdOK\r\n",facilityID,TSArray);   //固定格式

			}
			else
			{
				request = 0 ;
				memset(sendArray,0,sizeof(sendArray));
				sprintf(sendArray,"mobile,request:%s1,2\r\n",facilityID);   //具体内容
			}
			Uart1Byte(sendArray );
			Uart1Bit(0x1a);
			Uart1Byte("\r\n" );
			sendDelay = 200 ;

			Uart2Byte("send-step4--->");
			Uart2Byte(sendArray);
			Uart2Byte(" \r\n");

			break ;

		default :
			break ;
		}
	}
}
void receive()  //接收函数
{
	if( strstr(receiveArray,"\r\n") != NULL)
	{
		Uart2Byte("uart-->");
		Uart2Byte(receiveArray);
		Uart2Byte("\r\n");

		if(( strstr(receiveArray,"server") != NULL))
		{
			//接收上位机发来的正式数据
			heart = 100 ;

			Uart2Byte("rece-step4 ---------data ");
			Uart2Byte(receiveArray);
			Uart2Byte("\r\n");


			sendDelay = 10 ;
			sendStep = 3 ;
		}
		else
		{


			switch(sendStep)
			{
			case 0 :
			case 1 :
				if( strstr(receiveArray,"OK") != NULL)
				{
					sendStep ++ ;
					sendDelay = 60 ;
					Uart2Byte("rece-step0\r\n");
					ERROR = 0 ;
				}
				break ;
			case 2 :
				if((strstr(receiveArray,"CONNECT") != NULL ))
				{
					if(strstr(receiveArray,"FAIL") != NULL )
					{
						Uart1Byte("AT+CFUN=1,1\r\n");   //如果连接错误重启
						sendStep = 0 ;
						Uart2Byte("rece-step2CONNECT fall \r\n");
						ERROR = 0 ;
					}
					else
					{
						sendStep = 3 ;
						sendDelay = 100 ;
						Uart2Byte("rece-step2CONNECT success \r\n");
						ERROR = 0 ;
					}
				}
				break ;
			
			case 4 :

				if(( strstr(receiveArray,"servr") != NULL))
				{
					Uart2Byte("rece-step4 servr\r\n");
					sendDelay = 10 ;
					sendStep = 3 ;
				}
				else if(( strstr(receiveArray,"SEND") != NULL)) 
				{
					ERROR = 0 ;
					Uart2Byte("rece-step4-AAL-->");
					Uart2Byte(receiveArray);
					Uart2Byte("\r\n");

					if(request==1)
					{
						Uart2Byte("rece-step4 -->request==0\r\n");
						sendDelay = 10 ;
						sendStep = 3 ;
					}
				}

				else if(sendDelay<3)
				{
					sendStep = 3 ;
					Uart2Byte("rece-step4 sendDelay<3\r\n");
				}
				else  if((strstr(receiveArray,"ERROR") != NULL ) )
				{

					sendStep = 3 ;
					Uart2Byte("rece-step4 ERROR \r\n");
				}

				break ;
			default :
				break ;
			}
			if( strstr(receiveArray,"ERROR") != NULL)
			{
				ERROR ++ ;
			}
			if((strstr(receiveArray,">") != NULL ) )    //发送数据时返回>说明可以发送
			{
				sendStep = 4 ;
				Uart2Byte("rece-step3\r\n");
				ERROR = 0 ;
 
			}
			//清空接收数组
			memset(receiveArray,0,sizeof(receiveArray));
			subscript = 0 ;
 
		}
 
	}
	if((strstr(receiveArray,">") != NULL ) )    //发送数据时返回>说明可以发送
	{
		sendStep = 4 ;
		Uart2Byte("rece-step3\r\n");
		ERROR = 0 ;

		receiveArray[0] = 0 ;
		sendDelay = 0 ;

	}
	if(ERROR>10)
	{
		Uart1Byte("AT+CFUN=1,1\r\n");   //多次错误重启设备
		sendStep = 0 ;
	}

}
uint8_t  idata  T50ms = 0 ; //50ms计时
void time()
{
	if(TF0)
	{
		TF0 = 0 ;
		TL0 = 0x00;		//设置定时初值
		TH0 = 0x4C;		//设置定时初值
		T50ms = (T50ms+1)%200 ;
		if(sendDelay)
		{
			sendDelay -- ;

		}
		if(heart)
		{
			heart -- ;

		}
		if(T50ms%20==0)
		{


		}




	}



}

void Timer0Init(void)		//50毫秒@11.0592MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x4C;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}


void main()
{
	Uart_Init(); //串口中断
	Timer0Init() ;
	Uart1Byte(facilityID);
	Uart1Byte("\r\n");

	while(1)
	{

		send();
		receive() ;
		time();  //计时函数
	}
}
//=====================================================
void interUart1() interrupt 4
{
	if(RI)
	{
		RI = 0 ;
		receiveArray[subscript] = SBUF ;
		subscript++;
		receiveArray[subscript] = 0;
		if(subscript>48)
		{
			subscript = 30 ;
		}

	}

}


void interUart2() interrupt 8
{

	if(S2CON&1)   //S2RI=1说明接收完成
	{

		S2CON &= ~1 ;  //清除接收完成标志位
		Smog = S2BUF ;


	}

}







链接:https://pan.baidu.com/s/1_vJTASRzIG9HyuW95apltw
提取码:1111
复制这段内容后打开百度网盘手机App,操作更方便哦

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值