STC8H系列—18

#include “intrins.h”
#include <stdio.h>
#include “UnixToBeijing.h”
#include “Uart.h”
#include “OLED.h”

sbit Led1=P0^0;
sbit Led2=P0^1;
sbit Led3=P0^2;
sbit Led4=P0^3;
unsigned int count=0;
unsigned char xdata a[4]={0x00,0x00,0x00,0x00};//ntp服务器Unix格式
unsigned char code b[4]={0x83,0xAA,0x7E,0x80};
unsigned char xdata c[4]={0x00,0x00,0x00,0x00};//标准Unix格式
unsigned int xdata d[6]={0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char i;
unsigned int oled_y=0,oled_mo=0,oled_d=0,oled_h=0,oled_m=0,oled_s=0;
unsigned char idata oledBuf[8]=“”;

/AT指令*****/
u8 code s1[]=“AT+CIPCLOSE\r\n”;
u8 code s2[]=“AT+RST\r\n”;
u8 code s3[]=“AT+CIFSR\r\n”;
u8 code s4[]=“AT+CIPMUX=0\r\n”;
u8 code s5[]=“AT+CIPSTART=“UDP”,“ntp2.aliyun.com”,123\r\n”;
u8 code s6[]=“AT+CIPMODE=1\r\n”;
u8 code s7[]=“AT+CIPSEND=48\r\n”;
u8 code s8[]={0xE3,0x00,0x06,0xEC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x31,0x4E,0x31,0x34,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
u8 code k1[]=“0.0.0.0”;
u8 code k2[]=“CONNECT”;
u8 code k3[]=“>”;
u8 code k4[]=“apitag”;
u8 code k5[]=“ta”:0,“;
u8 code k6[]=“ta”:1,”;
u8 code k7[]=“WIFI GOT IP”;

void init_IO();//初始化IO
void DispTime(unsigned int d[]);//显示当前时间
bit Findkey(u8 key[],u8 len);
void GetWifiNtp();
void GetDate();
void init_timer0();//定时器0的初始化
void main()
{

  P_SW2 |= 0x80;  //扩展寄存器XFR访问使能	
	init_IO();
	init_Uart1();
	init_Uart2();	
EA=1;			
  printf("STC8H UnixTime Test!\n");
  init_timer0();
  init_IIC();//初始化硬IIC
  OLED_Init();//初始化OLED
  // sprintf((char *)oledBuf ,"Length:mm");//格式化输出
  OLED_ShowString(0,0," MG LeiYang!");
 // OLED_ShowString(0,4,oledBuf);
  OLED_ShowString(0,2,"Get Ntp Time!");
  //OLED_ShowString(0,6,"   2023/08/09");

// U32sub(a,b,c);
//c[0]=0x64;c[1]=0xe7;c[2]=0x91;c[3]=0xde;
// UnixToBeiJinTime(c,d);
// DispTime(d);
ET0=0;
GetWifiNtp();
ET0=1;
while(1)
{
if(count>=40000)
{
count=0;
ET0=0;
GetDate();
ET0=1;
}

}				

}

void DispTime(unsigned int d[])
{
printf(“%02d-”,d[0]);
printf(“%02d-”,d[1]);
printf(“%02d “,d[2]);
printf(”%02d:”,d[3]);
printf(“%02d:”,d[4]);
printf(“%02d”,d[5]);
printf(“\n”);
}

void init_IO()
{
RSTCFG=0x50; //开启RST键进入ISP模式
P0M1 = 0x00; P0M0 = 0x00; //设置P0口为准双向口
P1M1 = 0x00; P1M0 = 0x00; //设置P0口为准双向口
P2M1 = 0x00; P2M0 = 0x00; //设置P1口为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置P3口为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置P4口为准双向口
P5M1 = 0x00; P5M0 = 0x00; //设置P5口为准双向口

}

void init_timer0()//定时器0的初始化10ms
{

	AUXR &= 0x7F;		//定时器时钟12T模式
  TMOD &= 0xF0;		//设置定时器模式
  TL0 = 0x33;		//设置定时初始值
  TH0 = 0xE3;		//设置定时初始值
  TF0 = 0;		//清除TF0标志
  TR0 = 1;		//定时器0开始计时
  	
ET0=1;//Timer0 开中断	

}

void GetWifiNtp() //wifi连接上线
{
unsigned char i;
RX1_Buffer[RX1_Cnt]=‘\0’;
while(Findkey(k7,10)==0);
SendToEspStr(s3);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Buffer[RX1_Cnt]=‘\0’;
while(Findkey(k1,6))
{
SendToEspStr(s3);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Buffer[RX1_Cnt]=‘\0’;
}
SendToEspStr(s4);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
SendToEspStr(s5);
RX1_Buffer[RX1_Cnt]=‘\0’;
while(Findkey(k2,6))
{
SendToEspStr(s5);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Buffer[RX1_Cnt]=‘\0’;
}
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);

SendToEspStr(s7);	
RX1_Buffer[RX1_Cnt]='\0';
while(Findkey(k3,1))
{
   SendToEspStr(s7);
	 Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
 RX1_Buffer[RX1_Cnt]='\0';     
}
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);

Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Cnt=0x00;
for(i=0;i<48;i++)
{
Uart1Send(s8[i]);
}
//SendToEspStr(s8);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
for(i=78;i<82;i++)
{
//printf("0x%hhx ",RX1_Buffer[i]);
a[i-78]=RX1_Buffer[i];
}

Led2=0;	

Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
U32sub(a,b,c);
UnixToBeiJinTime(c,d);
DispTime(d);
oled_y=d[0];oled_mo=d[1];oled_d=d[2];
oled_h=d[3];oled_m=d[4];oled_s=d[5];
SendToEspStr(s1);
Led2=1;
}

void GetDate()
{
unsigned char i;
SendToEspStr(s5);
RX1_Buffer[RX1_Cnt]=‘\0’;
while(Findkey(k2,6))
{
SendToEspStr(s5);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Buffer[RX1_Cnt]=‘\0’;
}
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);

SendToEspStr(s7);	
RX1_Buffer[RX1_Cnt]='\0';
while(Findkey(k3,1))
{
   SendToEspStr(s7);
	 Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
 RX1_Buffer[RX1_Cnt]='\0';     
}
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);

Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
RX1_Cnt=0x00;
for(i=0;i<48;i++)
{
Uart1Send(s8[i]);
}
//SendToEspStr(s8);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
for(i=78;i<82;i++)
{
//printf("0x%hhx ",RX1_Buffer[i]);
a[i-78]=RX1_Buffer[i];
}

Led2=0;	

Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
Delay1ms(200);Delay1ms(200);Delay1ms(200);Delay1ms(200);
U32sub(a,b,c);
UnixToBeiJinTime(c,d);
DispTime(d);
oled_y=d[0];oled_mo=d[1];oled_d=d[2];
oled_h=d[3];oled_m=d[4];oled_s=d[5];
SendToEspStr(s1);

Led2=1;

}

//接收到的字符串,从中查找关键字
bit Findkey(u8 key[],u8 len)
{
u8 i=0,j=0;
bit result=0;
for(i=0;i<RX1_Cnt;i++)
{
if(RX1_Buffer[i]key[0])
{
for(j=1;j<len;j++)
{
if(RX1_Buffer[i+j]!=key[j])
break;
if(j
len-1) result=1;
}
}

	}
	return result;

}

void Timer0_isr() interrupt 1//Timer0中断入口
{
count++;

if(count%50==0)
	Led1=~Led1;

if(count%100==0)
	{
		oled_s++;
	  if(oled_s>=60)
		{
		   oled_s=0;
			 oled_m++;
		}
		if(oled_m>=60)
		{
		   oled_m=0;
			 oled_h++;
		}
		if(oled_h>=24)
		{
		   oled_h=0;
			 oled_d++;
		}
		    //sprintf((char *)oledBuf ,"        ");//格式化输出
		    sprintf((char *)oledBuf ,"  %02d-%02d-%02d",oled_y,oled_mo,oled_d);
		     OLED_ShowString(0,4,oledBuf);
		     sprintf((char *)oledBuf ,"    %02d:%02d:%02d",oled_h,oled_m,oled_s);
		     OLED_ShowString(0,6,oledBuf);
		    //sprintf((char *)oledBuf ,"        ");//格式化输出
		 
	}

}
//C51计算unix时间戳
unsigned char U32sub(unsigned char a[],unsigned char b[],unsigned char c[]);//减法a-b>0 返回1,否则返回0,值存c
unsigned char U32add(unsigned char a[],unsigned char b[],unsigned char d[]);//加法a+b无溢出返回1,否则返回0,值存d
void U32Mul(unsigned char a[],unsigned char b,unsigned char c[]);//乘法
void U32Mul1(unsigned char a[],unsigned char b[],unsigned char c[]);//乘法
char U32Cmp(unsigned char a[],unsigned char b[]);//比较大小返回1,否则返回0,值存d
void U32Div(unsigned char a[],unsigned char b[],unsigned char c[]);//除法
void U32Div1(unsigned char a[],unsigned char b,unsigned char c[]);//除法,除数为u8
void U32Mod(unsigned char a[],unsigned char b,unsigned char c[]);//求余数
void U32ModMon(unsigned char a1[],unsigned char c1[]);//求固定1421*24的余数
void UnixToBeiJinTime(unsigned char a1[],unsigned int c1[]);//Unix时间转北京时间

void UnixToBeiJinTime(unsigned char c[],unsigned int c1[])
{
unsigned char i=0,flag=0;
unsigned int time=0,hours_per_year=0,Pass4year=0;
char code Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned int month=0;//月
unsigned int days=0;//日
unsigned int hour=0;//小时
unsigned int minute=0;//分钟
unsigned int second=0;//分钟
unsigned int year=0;//年
unsigned char idata d[4]={0x01,0xE1,0x84,0x80};
unsigned char idata e[4]={0x00,0x00,0x00,0x00};
unsigned char idata f[4]={0x00,0x00,0x00,0x00};
unsigned char idata a[4]={0x00,0x00,0x00,0x00};

	 d[0]=0x00;d[1]=0x00;d[2]=0x00;d[3]=0x00;	

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!

1715598708384)]

[外链图片转存中…(img-tE6a6Rf5-1715598708385)]

[外链图片转存中…(img-GYbtgddj-1715598708385)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值