用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)相关的知识,希望对你有一定的参考价值。

各位大神帮忙看看:
用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS),所有数据均已数值方式表示。其中年分为2字节(低位在前,高位在后),其余为一个字节。如1997年6月21日10:32:55的表示为CD0706150A037.

#include <stdio.h>

#include <time.h>

int main()

 time_t rawtime;
 struct tm * timeinfo;
 time ( &rawtime );
 timeinfo = localtime ( &rawtime );
 int year,month,day,hour,min,sec;
 year = 1900+timeinfo->tm_year;
 month = 1+timeinfo->tm_mon;
 day = timeinfo->tm_mday;
 hour = timeinfo->tm_hour;
 min = timeinfo->tm_min;
 sec = timeinfo->tm_sec;
 printf ( "当前时间:%4d-%02d-%02d %02d:%02d:%02d\\n\\n",year, month,day,hour,min,sec);
 printf ( "你需要的格式:%4d%02d%02d%02d%02d%02d\\n\\n",year, month,day,hour,min,sec);

 char hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];
 sprintf(hyy,"%02X",year/100);
 sprintf(lyy,"%02X",year%100);
 sprintf(MM,"%02X",month);
 sprintf(dd,"%02X",day);
 sprintf(hh,"%02X",hour);
 sprintf(mm,"%02X",min);
 sprintf(ss,"%02X",sec);

 printf("转化为16进制:%02s%02s%02s%02s%02s%02s%02s\\n",hyy,lyy,MM,dd,hh,mm,ss);
 //exit(0);
 return 0;
 

说明:我用VC6.0现写的,运行结果截图如下: 

追问

你太厉害了!!!还有个问题,我到时候要调用这个函数,所以我想把得出来的十六进制统一放到一个数组中,再输出。但是我放了以后就不合适了
char time[7]=hyy[0],lyy[0],MM[0],dd[0],hh[0],mm[0],ss[0];

for(int j=0;j < 7;j++)
printf("转化为16进制:%02s\n",time[i);
您能不能再帮忙看看!!感谢感谢!!

追答

char time[7]=hyy[0],lyy[0],MM[0],dd[0],hh[0],mm[0],ss[0];
同学,你这么写是错的,这是字符数组,不是整型数字;
我不是应经帮你把转化后的16进制存放到了数组中了吗,
就是这些数组啊:hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];就是为了方便你调用啊
比如,我要输出月份的16进制,就直接输出:
printf("%02s\n",MM);

追问

那能不能把这些字符数组hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2]
统一放到一个数组中呢?

追答

可以的,用用函数strcat();
char time[50]="";
strcat(time,lyy);
strcat(time,hyy);
strcat(time,MM);
strcat(time,dd);
strcat(time,hh);
strcat(time,mm);
strcat(time,ss);
就全部连成一串在time[50]中了

定义修改一下:char time[50]="";

参考技术A #include <stdio.h>
#include <time.h>  

int main () 
time_t rawtime;
time(&rawtime);
struct tm *timeinfo;
timeinfo = localtime(&rawtime);
printf("%04X%02X%02X%02X%02X%02X\\n",
1900 + timeinfo->tm_year,
1 + timeinfo->tm_mon,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec
);
return 0;

参考技术B #include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()

char szTime[32];
time_t tmtime;
struct tm *tmi;

//取当前时间
time(&tmtime);
tmi = localtime(&tmtime);

sprintf(szTime, "%04d", 1900 + tmi->tm_year);
unsigned short int iYear = atoi(szTime);

printf("%02X%02X%02X%02X%02X%02X%02X\n",
iYear&0x00ff,
iYear>>8,
1 + tmi->tm_mon,
tmi->tm_mday,
tmi->tm_hour,
tmi->tm_min,
tmi->tm_sec
);
return 0;

基于STM32的ESP8266天气时钟--------MCU数据处理及显示

前言:本章我们要实现的功能为:将获取到的天气数据进行OLED显示。
参考资料:
OLED显示屏:
关于基于stm32的0.96寸oled显示屏的学习理解心得。
cJSON数据转换:
用cJSON解析心知天气返回的数据包
C语言cJSON库的使用,解析json数据格式

文章目录

1、摘要

本章节主要有两个部分组成:

*一个是OLED屏幕的显示,关于OLED的资料有挺多的,CSDN中也有许多大佬做了比较详细的介绍,所以本章节并不会很深入地去讲解代码,只是会稍微提一下,如果大家有需要我写一个比较详细地介绍的话,也可以在评论区提出,我有时间就会写一下的。
另一个是用cJSON去解读心知天气返回的数据包。

2、硬件准备

硬件和上章节差不多,只是多了一个OLED屏幕,这里我使用的是0.96寸I2C驱动的OLED屏幕。

2.1、商品链接

最小系统板:购买链接
USB转TTL(种类有点多,随便选一个就行,我用的是CH340这个芯片的):购买链接
ESP8266:购买链接
OLED(我用的是0.96寸4针,I2C接口):购买链接
ST-Link V2下载线:购买链接

3、软件准备

Keil编译器
VSCode编译器
XCOM串口调试助手
这边附上另一篇文章,大家可以参考学习一下
STM32----使用VSCode编写Keil

4、硬件连线

MCUESP8266
3.3VVCC
GNDGND
PB10RXD
PB11TXD
3.3VIO
3.3VRST
MCUUSB转TTL
5VVCC
GNDGND
PA9RXD
PA10TXD
MCUOLED
5VVCC
GNDGND
PB15SDA
PB13SCL

5、代码解析

5.1、OLED代码分析

参考其他博主写的技术论文
OLED显示屏:关于基于stm32的0.96寸oled显示屏的学习理解心得。

本实验使用的OLED是基于I2C通信的。所以最主要的内容有:
发送从机地址(0x78),再发送命令字节,接着发送数据字节。

/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)

   IIC_Start();
   Write_IIC_Byte(0x78);            //Slave address,SA0=0
	IIC_Wait_Ack();	
   Write_IIC_Byte(0x00);			//write command
	IIC_Wait_Ack();	
   Write_IIC_Byte(IIC_Command); 
	IIC_Wait_Ack();	
   IIC_Stop();

/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)

   IIC_Start();
   Write_IIC_Byte(0x78);			//D/C#=0; R/W#=0
	IIC_Wait_Ack();	
   Write_IIC_Byte(0x40);			//write data
	IIC_Wait_Ack();	
   Write_IIC_Byte(IIC_Data);
	IIC_Wait_Ack();	
   IIC_Stop();

根据这三个主要的内容,我们可以设置OLED显示器的显示样式。主要用到的代码有:
显示字符串函数、显示汉字函数

/**********************************
显示一个字符串
输入数据: 
x----x轴
y----y轴
*chr----字符串
Char_Size----大小(16/12)
**********************************/
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)

	unsigned char j=0;
	while (chr[j]!='\\0')
			
		OLED_ShowChar(x,y,chr[j],Char_Size);
		x+=8;
		if(x>120)x=0;y+=2;
		j++;
	


/**********************************
显示一个文字
输入数据: 
x----x轴
y----y轴
no----字库数组文字对应的位置
**********************************/
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
      			    
	u8 t,adder=0;
	OLED_Set_Pos(x,y);	
    for(t=0;t<16;t++)
		
				OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
				adder+=1;
     	
		OLED_Set_Pos(x,y+1);	
    for(t=0;t<16;t++)
				
				OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
				adder+=1;
      					

5.2、cJSON解析数据包代码

因为C语言没有字典这样的结构,所以我们解析json格式的数据就需要调用cJSON这个函数库。
主要函数接口:

/* The cJSON structure: */
typedef struct cJSON 
	struct cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
	int type;					/* The type of the item, as above. */
	char *valuestring;			/* The item's string, if type==cJSON_String */
	int valueint;				/* The item's number, if type==cJSON_Number */
	double valuedouble;			/* The item's number, if type==cJSON_Number */
	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
 cJSON;

说明:

  1. cJSON是使用链表来存储数据的,其访问方式很像一颗树。每一个节点可以有兄弟节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。只有节点是对象或数组时才可以有孩子节点。
  2. type是键(key)的类型,一共有7种取值,分别是:False,Ture,NULL,Number,String,Array,Object。
    若是Number类型,则valueint或valuedouble中存储着值。 若期望的是int,则访问valueint。
    若期望的是double,则访问valuedouble,可以得到值。
    若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。
  3. string中存放的是这个节点的名字,可理解为key的名称。

重要的接口函数:

  1. cJSON *cJSON_Parse(const char *value);
    功能:解析JSON数据包,并按照cJSON结构体的结构序列化整个数据包。可以看做是获取一个句柄。
  2. cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
    功能:获取json指定的对象成员 ;
    参数:*objec:第一个函数中获取的句柄。string:需要获取的对象 ;
    返回值:这个对象成员的句柄;
    如果json格式的对象成员直接就是字符串那么就可以直接通过结构体中的valuestring元素来获取这个成员的值。
  3. cJSON *cJSON_GetArrayItem(cJSON *array,int item);
    功能:有可能第二个函数中获取到的是成员对象值是一个数组,那么就需要用到这个函数。用来获取这个数组指定的下标对象;
    参数:*array:传入第二步中返回的值, item:想要获取这个数组的下标元素 ;
    返回值:这个数组中指定下标的对象。然后在对这个返回值重复使用第二步函数就可以获取到各个成员的值了。也就是说对象是数组的比是字符串的要多用一个cJSON_GetArrayItem函数,其他的没区别。
  4. cJSON_Delete() 用来释放你第一步获取的句柄,来释放整个内存。用在解析完后调用。

核心代码
解析数据包

/*********************************************************************************
* Function Name    : cJSON_WeatherParse,解析天气数据
* Parameter		   : JSON:天气数据包  results:保存解析后得到的有用的数据
* Return Value     : 0:成功 其他:错误
* Function Explain : 
* Create Date      : 2017.12.6 by lzn
**********************************************************************************/
int cJSON_WeatherParse(char *JSON, Results *results)

	cJSON *json,*arrayItem,*object,*subobject,*item;
	
	json = cJSON_Parse(JSON); //解析JSON数据包
	if(json == NULL)		  //检测JSON数据包是否存在语法上的错误,返回NULL表示数据包无效
	
		printf("Error before: [%s] \\r\\n",cJSON_GetErrorPtr()); //打印数据包语法错误的位置
		return 1;
	
	else
	
		if((arrayItem = cJSON_GetObjectItem(json,"results")) != NULL); //匹配字符串"results",获取数组内容
		
			int size = cJSON_GetArraySize(arrayItem);     //获取数组中对象个数
			printf("cJSON_GetArraySize: size=%d \\r\\n",size); 
			
			if((object = cJSON_GetArrayItem(arrayItem,0)) != NULL)//获取父对象内容
			
				/* 匹配子对象1 */
				if((subobject = cJSON_GetObjectItem(object,"location")) != NULL)
				
					printf("---------------------------------subobject1-------------------------------\\r\\n");
					if((item = cJSON_GetObjectItem(subobject,"id")) != NULL)   //匹配子对象1成员"id"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.id,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"name")) != NULL) //匹配子对象1成员"name"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.name,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"country")) != NULL)//匹配子对象1成员"country"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.country,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"path")) != NULL)  //匹配子对象1成员"path"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.path,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"timezone")) != NULL)//匹配子对象1成员"timezone"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.timezone,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"timezone_offset")) != NULL)//匹配子对象1成员"timezone_offset"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].location.timezone_offset,item->valuestring,strlen(item->valuestring));
					
				
				/* 匹配子对象2 */
				if((subobject = cJSON_GetObjectItem(object,"now")) != NULL)
				
					printf("---------------------------------subobject2-------------------------------\\r\\n");
					if((item = cJSON_GetObjectItem(subobject,"text")) != NULL)//匹配子对象2成员"text"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].now.text,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"code")) != NULL)//匹配子对象2成员"code"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].now.code,item->valuestring,strlen(item->valuestring));
					
					if((item = cJSON_GetObjectItem(subobject,"temperature")) != NULL) //匹配子对象2成员"temperature"
					
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",item->type,item->string,item->valuestring);
						memcpy(results[0].now.temperature,item->valuestring,strlen(item->valuestring));
					
				
				/* 匹配子对象3 */
				if((subobject = cJSON_GetObjectItem(object,"last_update")) != NULL)
				
					printf("---------------------------------subobject3-------------------------------\\r\\n");
					printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \\r\\n",subobject->type,subobject->string,subobject->valuestring);
					memcpy(results[0].last_update,item->valuestring,strlen(subobject->valuestring));
				
			 
		
	
	
	cJSON_Delete(json); //释放cJSON_Parse()分配出来的内存空间
	
	return 0;

5.3、项目代码逻辑

基本的代码已经介绍完了,其他的操作就是调用这些函数实现功能而已,这边就不全部列出来了,感觉没什么意义。
我把我的代码的逻辑分享一下吧,具体代码我会附在文末,大家可以详细看看。
初始化函数->OLED显示状态->按下按键触发中断->获取天气数据->OLED状态更新。

6、运行结果

设备上电后,等待ESP8266初始化,获取天气数据后通过cJSON解析,将状态显示到OLED屏幕上。当按下按键后,会重新获取,更新天气数据。

7、源程序

7.1、百度网盘

链接:https://pan.baidu.com/s/1fDIIgYYc9yALo2gTNgoJRw
提取码:gd2d

7.2、Github地址

Github地址

传送门:
基于STM32的ESP8266天气时钟(1)---------AT指令获取天气数据
基于STM32的ESP8266天气时钟(2)--------MCU获取天气数据
基于STM32的ESP8266天气时钟(3)--------MCU数据处理及显示
基于STM32F的ESP8266天气时钟(4)--------MCU获取时间及显示(完结)

以上是关于用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)的主要内容,如果未能解决你的问题,请参考以下文章

怎么用C语言编程数字时钟

基于STM32F103的网络天气时钟--------单片机获取天气后OLED显示

基于STM32的ESP8266天气时钟--------MCU数据处理及显示

基于STM32的ESP8266天气时钟--------MCU数据处理及显示

基于STM32的ESP8266天气时钟--------MCU数据处理及显示

Linux C进程时间:墙上时钟时间,用户CPU时间,系统CPU时钟时间