Linux 获取当前时间BCD

/*************************************************************************
  > File Name: char.cpp
  > Author: yuhuqiao
  > Mail: yuhuqiao@163.com 
  > Created Time: 2019年01月09日 星期三 13时40分41秒
 ************************************************************************/

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

long get_time()
{
	time_t now_time;
	now_time = time(NULL);
	struct tm *ptr;
	char str[20];
	strftime(str,sizeof(str),"%Y%m%d%H%M%S",localtime(&now_time));
	cout << "str : "<< str <<endl;
	return atol(str);
	
}




int DectoBCD(long Dec, unsigned char *Bcd, int length)  
{

	int i;  
	int temp;  
	for (i = length - 1; i >= 0; i--)  
	{  
		temp = Dec % 100;  
		Bcd[i] = ((temp / 10) << 4) + ((temp % 10) & 0x0F);  
		Dec /= 100;  
	}  
	return 0;  
}  


int main()
{
	long t = get_time();
	unsigned char tmp[7];
	DectoBCD(t,tmp,7);
	for(int i=0; i<7; i++)
	{
		printf("0x%02X\n",tmp[i]);
	}
}

 

str : 20190109174620
0x20
0x19
0x01
0x09
0x17
0x46
0x20

 

在使用STM32进行时间获取之前,需要先配置好定时器和时钟。一般情况下,可以使用STM32自带的RTC实现时间获取。下面是获取当前时间的代码示例: ```c #include "stm32f10x.h" #include "stm32f10x_rtc.h" RTC_TimeTypeDef RTC_TimeStructure; void RTC_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); // 使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); // 使能后备寄存器访问 RCC_LSEConfig(RCC_LSE_ON); // 开启LSE外部低速晶振 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // 等待LSE稳定 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // 选择RTC时钟源为LSE RCC_RTCCLKCmd(ENABLE); // 使能RTC时钟 RTC_WaitForSynchro(); // 等待RTC同步 RTC_ITConfig(RTC_IT_SEC, ENABLE); // 使能RTC秒中断 RTC_WaitForLastTask(); // 等待上一个操作完成 } int main(void) { RTC_Configuration(); // RTC配置 while (1) { RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); // 获取当前时间 // 在这里对时间进行处理或显示 delay_ms(1000); // 延时1秒 } } ``` 在上面的代码中,首先需要调用`RTC_Configuration()`函数进行RTC的初始化配置。然后在`while`循环中,通过调用`RTC_GetTime()`函数获取当前时间,返回的时间数据保存在`RTC_TimeStructure`结构体中。最后可以对时间进行处理或显示。需要注意的是,`RTC_GetTime()`函数的第一个参数是时间格式,可以选择`RTC_Format_BIN`或`RTC_Format_BCD`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值