新聞中心

STM32 RTC之日歷顯示

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
使用STM32的RTC功能,制作一份私人專屬的日歷,就SO簡(jiǎn)單了。不廢話,上效果圖:

本文引用地址:http://butianyuan.cn/article/201611/321300.htm



鄙人通過串口輸出時(shí)間到超級(jí)終端的(SecureCRT 5.5這貨最好),如果配上TFT就制作一個(gè)簡(jiǎn)易的電子日歷了。這個(gè)里面主要涉及RTC的初始化,時(shí)間數(shù)據(jù)的初始化輸入,串口輸出,還有就是公歷時(shí)間和農(nóng)歷時(shí)間的轉(zhuǎn)換處理。通過串口初始化RTC數(shù)據(jù),RTC通過串口把時(shí)間顯示出來,上代碼:

工程結(jié)構(gòu)圖:


1、main.c如下:


#include"stm32f10x.h"
#include"beep.h"
#include"led.h"
#include"usart1.h"
#include"rtc.h"


int main(void)
{
USART_Config();

Beep_Init();
Beep_State(1,BeepOn);

Led_Init();
Led_Spark(LedAll,1,LedOn);

RTC_NVIC_Config(); //RTC嵌套中斷向量初始化

RTC_Display();
}

2、beep led usart這些在博客其他文章里面出現(xiàn)過,這里就不提也罷。

3、RTC.c這是本文的重點(diǎn)了。

C文件如下:


#include"stm32f10x.h"
#include"rtc.h"
#include"usart1.h"
#include"date.h"
#include"calendar.h"
#include


u8 SecondFlag=0;

struct rtc_time systmtime;

u8 const *WEEK_STR[] = {"日", "一", "二", "三", "四", "五", "六"}; //這是一個(gè)指針數(shù)組的賦值 不是指向一個(gè)數(shù)組的指針 而是一個(gè)數(shù)組的每一個(gè)元素都是一個(gè)指針

//==============================================================================================1-RTC初始化部分

void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0000); //起始地址位于FLASH

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //優(yōu)先級(jí)分組方式2


NVIC_InitStructure.NVIC_IRQChannel =RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;

NVIC_Init(&NVIC_InitStructure);
}



static void RTC_Config(void)
{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);
PWR_BackupAccessCmd(ENABLE);

BKP_DeInit();


RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET); //等待LSE準(zhǔn)備就緒
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);


RTC_WaitForSynchro();

RTC_ITConfig(RTC_IT_SEC,ENABLE);

RTC_WaitForLastTask();


RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}

//==============================================================================================2--設(shè)置時(shí)間部分

static u8 RTC_USART_Scanf(uint32_t value)
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};

while (index < 2)
{

while (USART_GetFlagStatus(USART, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(USART));
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
if((index == 2) && (tmp[index - 1] == )) //第一個(gè)輸入的是數(shù)字,第二個(gè)是按的回車鍵的情況
{
tmp[1] = tmp[0];
tmp[0] = 0x30;
}
else
{
printf("Please enter valid number between 0 and 9 -->: ");
index--;
}
}
else
{
printf("%c", tmp[index - 1]); //輸入一個(gè)顯示一個(gè)數(shù)字
}
}

index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);

if (index > value)
{
printf("Please enter valid number between 0 and %d -->: ", value);
return 0xFF;
}
return index;
}


上一頁 1 2 下一頁

關(guān)鍵詞: STM32RTC日歷顯

評(píng)論


技術(shù)專區(qū)

關(guān)閉