选择皮肤
admin
2023-04-01 02:34:24
admin\n楼主

temp

admin
2023-04-01 02:34:46
admin\n楼主
main.c
#include "STC8H.h"
#include "intrins.h"
#include "uart.h"

#define REBOOTKEY P54 //REBOOT
#define SETKEY P32 //SET

#define BAUD  0xF400                  // 1200bps @ 11.0592MHz
sbit RXB = P3^6;                        //define UART TX/RX port
sbit TXB = P3^7;

#define dla P34 //25
#define dlb P35 //50
#define dlc P36 //75

#define jda P11 //¹©µçÇл»
#define jdb P10 //Äæ±äÆ÷
#define jdc P37 //³äµç


#define WIFIRes P26 //rse?
#define WIFIR P23 //ºì
#define WIFIG P24 //ÂÌ
#define WIFIB P25 //˦
int wifimode = 0;

#define GPRSRes P27 //res
#define GPRSR P20 //ºì
#define GPRSG P21 //ÂÌ
#define GPRSB P22 //˦
int gprsmode = 0;

void Delay100ms();
int ifchar(char *p,char *ifdata);
int ifqtqwchar(unsigned char ch[],unsigned char ifdata[],int j);

float time;
float wifiledt;
float gprsledt;

void Timer0_Isr(void) interrupt 1
{
	TL0 = 0xCD;				//ÉèÖö¨Ê±³õʼֵ
	TH0 = 0xD4;				//ÉèÖö¨Ê±³õʼֵ
//	TF2 = 0;
	time++;
	if(wifimode == 0x1){
		if(wifiledt < time)
		{
			wifiledt = time + 200;
			WIFIR = !WIFIR;
			WIFIG = WIFIR;
			WIFIB = WIFIR;
		}
	}else if(wifimode == 0x02)
	{
		if(wifiledt < time)
		{
			wifiledt = time + 200;
			WIFIR = !WIFIR;
			WIFIG = 0;
			WIFIB = 0;
		}
	}else if(wifimode == 0x10)
	{
		if(wifiledt < time)
		{
			wifiledt = time + 200;
			WIFIR = 0;
			WIFIG = 0;
			WIFIB = !WIFIB;
		}
	}else if(wifimode == 0x20)
	{
		if(wifiledt < time)
		{
			wifiledt = time + 100;
			WIFIR = 0;
			WIFIG = !WIFIG;
			WIFIB = 0;
		}
	}
	//if(time == 100)
	//{
	//	P27 = !P27;
	//	time = 0;
	//}
}

void Timer0_Init(void)		//1ºÁÃë@11.0592MHz
{
	AUXR |= 0x80;			//¶¨Ê±Æ÷ʱÖÓ1Tģʽ
	TMOD &= 0xF0;			//ÉèÖö¨Ê±Æ÷ģʽ
	TMOD |= 0x01;			//ÉèÖö¨Ê±Æ÷ģʽ
	TL0 = 0xCD;				//ÉèÖö¨Ê±³õʼֵ
	TH0 = 0xD4;				//ÉèÖö¨Ê±³õʼֵ
	TF0 = 0;				//Çå³ýTF0±êÖ¾
	TR0 = 1;				//¶¨Ê±Æ÷0¿ªÊ¼¼ÆÊ±
	ET0 = 1;				//ʹÄܶ¨Ê±Æ÷0ÖжÏ
//	T2MOD = 0;				//³õʼ»¯Ä£Ê½¼Ä´æÆ÷
//	T2CON = 0;				//³õʼ»¯¿ØÖƼĴæÆ÷
//	TL2 = 0x66;				//ÉèÖö¨Ê±³õʼֵ
//	TH2 = 0xFC;				//ÉèÖö¨Ê±³õʼֵ
//	RCAP2L = 0x66;			//ÉèÖö¨Ê±ÖØÔØÖµ
//	RCAP2H = 0xFC;			//ÉèÖö¨Ê±ÖØÔØÖµ
//	TR2 = 1;				//¶¨Ê±Æ÷2¿ªÊ¼¼ÆÊ±
//	ET2 = 1;				//ʹÄܶ¨Ê±Æ÷2ÖжÏ
}

void initgsm()
{
	int i = 0;
	float times;
	UartSendStr("Init 4G\r\n");
	GPRSR = 1;
	GPRSG = 0;
	GPRSB = 0;
	gprsmode = 0;
	GPRSRes= 0;
	times = time + 1000;
	while(time < times);
	GPRSRes= 1;
	times = time + 1000;
	while(time < times);
	UartSendStr4("AT\r\n");
	times = time + 100;
	while(time < times);
	UartSendStr4("AT\r\n");
	times = time + 100;
	while(time < times);
	UartSendStr4("AT\r\n");
	times = time + 100;
	while(time < times);
	UartSendStr4("AT\r\n");
	times = time + 100;
	while(time < times);
	UartSendStr4("AT\r\n");
	times = time + 100;
	while(time < times);
	comdbuff[0] = '\0';
	comdwz = 0;
	UartSendStr("Scan ");
	UartSendStr4("AT\r\n");
	times = time + 1000;
	while(time < times);
	Uart1Send(comdbuff[0]);
	Uart1Send(comdbuff[1]);
	Uart1Send(comdbuff[2]);
	Uart1Send(comdbuff[3]);
	
	if(comcbuff[0] == '\0')
	{
		UartSendStr("NO\r\n");
		GPRSR = 0;
	}else{
		if(ifqtqwchar("OK",comdbuff,2))
		{
			UartSendStr("OK\r\n");
			comcwz = 0;
			for(i=0;i<15;i++)	comcbuff[i] = 0x00;
			UartSendStr3("AT+CWMODE=1,0\r\n");
			wifiledt = time;
			wifimode = 0x10;
			times = time + 100;
			while(time < times);
			if(ifqtqwchar("OK",comcbuff,1)){
//				wifimode = 0xff;
				comawz = 0;
				for(i = 0;i<15;i++)	comabuff[i] = 0x00;
				UartSendStr("AT+CWJAP\r\n");
				times = time + 100;
				while(time < times);
				if(ifqtqwchar("ERROR",comabuff,1)){
					wifimode = 0xff;
					WIFIR = 1;
					WIFIG = 0;
					WIFIB = 0;
				}
				wifimode = 0xff;
			}else{
				wifimode = 0x2;
				UartSendStr("AT+CWMODE=ERROR\r\n");
	Uart1Send(comcbuff[0]);
	Uart1Send(comcbuff[1]);
	Uart1Send(comcbuff[2]);
	Uart1Send(comcbuff[3]);
			}
		}else
		{
			wifiledt = time;
			wifimode = 1;
			UartSendStr("ERROR\r\n");
		}
	}
}

void initwifi()
{
	int i = 0;
	float times;
	UartSendStr("Init WiFi\r\n");
	WIFIR = 1;
	WIFIG = 0;
	WIFIB = 0;
	wifimode = 0;
	WIFIRes= 0;
	Delay100ms();
	WIFIRes= 1;
	times = time + 1000;
	while(time < times);
	UartSendStr3("ATE0\r\n");
	times = time + 100;
	while(time < times);
	comcbuff[0] = '\0';
	comcwz = 0;
	UartSendStr3("AT\r\n");
	times = time + 1000;
	while(time < times);
//	Uart1Send(comcbuff[0]);
//	Uart1Send(comcbuff[1]);
//	Uart1Send(comcbuff[2]);
//	Uart1Send(comcbuff[3]);
	if(comcbuff[0] == '\0')
	{
		WIFIR = 0;
	}else{
		if(ifqtqwchar("OK",comcbuff,2))
		{
			comcwz = 0;
			for(i=0;i<15;i++)	comcbuff[i] = 0x00;
			UartSendStr3("AT+CWMODE=1,0\r\n");
			wifiledt = time;
			wifimode = 0x10;
			times = time + 100;
			while(time < times);
			if(ifqtqwchar("OK",comcbuff,1)){
//				wifimode = 0xff;
				comawz = 0;
				for(i = 0;i<15;i++)	comabuff[i] = 0x00;
				UartSendStr("AT+CWJAP\r\n");
				times = time + 100;
				while(time < times);
				if(ifqtqwchar("ERROR",comabuff,1)){
					wifimode = 0xff;
					WIFIR = 1;
					WIFIG = 0;
					WIFIB = 0;
				}
				wifimode = 0xff;
			}else{
				wifimode = 0x2;
				UartSendStr("AT+CWMODE=ERROR\r\n");
	Uart1Send(comcbuff[0]);
	Uart1Send(comcbuff[1]);
	Uart1Send(comcbuff[2]);
	Uart1Send(comcbuff[3]);
			}
		}else
		{
			wifiledt = time;
			wifimode = 1;
		}
	}
//	UartSendStr(comabuff);
//	Uart1Send(comabuff[0]);
//	Uart1Send(comabuff[1]);
}

void main()
{
//	P2 = 0x04;
	
	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x00;

	P3M0 = 0x00;
	P3M1 = 0x00;
	P4M0 = 0x00;
	P4M1 = 0x00;
	P5M0 = 0x00;
	P5M1 = 0x00;
	
	WIFIR = 1;
	WIFIG = 0;
	WIFIB = 0;
	GPRSR = 1;
	GPRSG = 0;
	GPRSB = 0;
	dla = 1;
	dlb = 0;
	dlc = 0;
	jda = 1;
	jdb = 0;
	jdc = 0;
	P1M0 = 0x03;
	P1M1 = 0x00;
	P2M0 = 0xFF;
	P2M1 = 0x00;
	P3M0 = 0x80;
	P3M1 = 0x00;
	
	UartInit();
	UartInit3();
	UartInit4();
	
	ES = 1;                                     //ʹÄÜ´®¿ÚÖжÏ
	EA = 1;
	UartSendStr("Helo !\n");
	Timer0_Init();
	time = 0;
	while(time < 500);
	WIFIR = 0;
	GPRSR = 0;
	WIFIG = 1;
	GPRSG = 1;
	dla = 0;
	dlb = 1;
	jda = 0;
	jdb = 1;
	while(time < 1000);
	WIFIG = 0;
	GPRSG = 0;
	GPRSB = 1;
	WIFIB = 1;
	dlb = 0;
	dlc = 1;
	jdb = 0;
	jdc = 1;
	while(time < 1500);
	GPRSB = 0;
	WIFIB = 0;
	dlc = 0;
	jdc = 0;
	
	initgsm();
	initwifi();
	
	while (1)
	{
		
//		if(P30 == 0){IAP_CONTR |= 0x60;}//ÖØÆô
		
		if(REBOOTKEY == 0)
		{
			float retteme;
			retteme = time;
			retteme = retteme + 5000;
			while(REBOOTKEY == 0)
			{
				if(time > retteme)
				{
					WIFIR = 1;
					GPRSR = 1;
					retteme = retteme + 500;
					while(time < retteme);
					WIFIR = 0;
					GPRSR = 0;
					WIFIG = 1;
					GPRSG = 1;
					retteme = retteme + 500;
					while(time < retteme);
					WIFIG = 0;
					GPRSG = 0;
					GPRSB = 1;
					WIFIB = 1;
					retteme = retteme + 500;
					while(time <= retteme);
					GPRSB = 0;
					WIFIB = 0;
				}
			}
			if(time > retteme){
				IAP_CONTR |= 0x60;//ÖØÆô
//				UartSendStr("REBOOT !\r\n");
			}else{
//				UartSendStr("Relink WiFi !\r\n");
//				initwifi();
			}
		}
		if(SETKEY == 0)
		{
			float retteme;
			retteme = time;
			while(SETKEY == 0);
			retteme = retteme + 5000;
			if(time > retteme){
				IAP_CONTR |= 0x60;//ÖØÆô
//				UartSendStr("REBOOT !\r\n");
			}else{
				if(wifimode != 0x00){
//					UartSendStr("AT+CWSTARTSMART\r\n");
					wifimode = 0x20;
				}
			}
		}
	}
}


void Delay100ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 180;
	j = 73;
	do
	{
		while (--j);
	} while (--i);
}

int ifchar(unsigned char ch[],unsigned char ifdata[])
{
	unsigned char j = 0,i = 1;
	while(ch[j] != '\0'){
		if(ch[j] != ifdata[j])	i=0;
		if(ifdata[j] == '\0')	return 0;
		j++;
	}
	return i;
}
//ÆþͷȥβÅжÏ
int ifqtqwchar(unsigned char ch[],unsigned char ifdata[],int j)
{
	unsigned char x = 0,y = 0,i = 1;
	while(j > 0)
	{
		while(ifdata[y] == '\r'){
			//if(ifdata[y] == '\0')	return 0;
			//y++;
//			Uart1Send(0x30);
//			Uart1Send(ifdata[y]);
//		}
		if(ifdata[y] == '\0')	return 0;
		if(ifdata[y] != '\r')	return 0;
//			Uart1Send(0x31);
//			Uart1Send(ifdata[y]);
		y++;
		if(ifdata[y] == '\0')	return 0;
		if(ifdata[y] != '\n')	return 0;
//			Uart1Send(0x32);
//			Uart1Send(ifdata[y]);
		y++;
	}
		j--;
	}
//	UartSendStr("OK");
	while(ch[x] != '\0'){
//		Uart1Send(0x32);
//		Uart1Send(ifdata[y]);
		if(ch[x] != ifdata[y])	return 0;
		if(ifdata[y] == '\0')		return 0;
		x++;
		y++;
	}
	return i;
}