c - 如何在stm32-discovery上显示时间

标签 c microcontroller stm32 discovery

我最近一直在试验 STM32-DISCOVERY,但我不确定如何开始使用该板的应用程序。

我想在 stm32 的显示器上显示 RTC 的时间。

我如何开始使用它?我已经为 STM32 安装了库。

最佳答案

我不确定您所说的“显示”到底是什么意思(即它是 LCD 屏幕、串行输出终端等?)。

在任何情况下,RTC 通常通过 I2C 总线连接到 STM32,因此您需要先初始化两个 Controller (RTC 和 I2C),然后才能获取/设置 RTC 上的日期和时间。

以下是初始化 RTC Controller 的方法:

void rtc_init()
{
    RTC_InitTypeDef RTC_InitStructure;
    RTC_TimeTypeDef RTC_TimeStruct;

    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);

    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);

    /* Reset RTC Domain */
    RCC_BackupResetCmd(ENABLE);
    RCC_BackupResetCmd(DISABLE);

    /* Enable the LSE OSC */
    RCC_LSEConfig(RCC_LSE_ON);

    /* Wait until LSE is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    /* Configure the RTC data register and RTC prescaler */
    RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
    RTC_InitStructure.RTC_SynchPrediv  = 0xFF;
    RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);

    /* Set the time to 00h 00mn 00s AM */
    RTC_TimeStruct.RTC_H12     = RTC_H12_AM;
    RTC_TimeStruct.RTC_Hours   = 0x00;
    RTC_TimeStruct.RTC_Minutes = 0x00;
    RTC_TimeStruct.RTC_Seconds = 0x00;
    RTC_SetTime(RTC_Format_BCD,&RTC_TimeStruct);
}

以下是获取/设置 RTC 日期和时间的方法,不包括星期几:

在头文件中:

typedef struct
{
    unsigned char second;
    unsigned char minute;
    unsigned char hour;
    unsigned char day;
    unsigned char month;
    unsigned char year;
}
rtc_data_t;

void rtc_get(rtc_data_t* rtc_data);
void rtc_set(rtc_data_t* rtc_data);

在源文件中:

#define SENSOR_ADDRESS 0xD0 // or set this according to your HW configuration

#define HEX_2_DEC(val) (((val)/16)*10+((val)%16))
#define DEC_2_HEX(val) (((val)/10)*16+((val)%10))

typedef struct
{
    unsigned char second;
    unsigned char minute;
    unsigned char hour;
    unsigned char weekday;
    unsigned char day;
    unsigned char month;
    unsigned char year;
}
raw_data_t;

void rtc_get(rtc_data_t* rtc_data)
{
    raw_data_t raw_data;
    i2c_polling_read(SENSOR_ADDRESS,0x00,sizeof(raw_data),(char*)&raw_data);
    rtc_data->second = HEX_2_DEC(raw_data.second);
    rtc_data->minute = HEX_2_DEC(raw_data.minute);
    rtc_data->hour   = HEX_2_DEC(raw_data.hour  );
    rtc_data->day    = HEX_2_DEC(raw_data.day   );
    rtc_data->month  = HEX_2_DEC(raw_data.month );
    rtc_data->year   = HEX_2_DEC(raw_data.year  );
}

void rtc_set(rtc_data_t* rtc_data)
{
    raw_data_t raw_data;
    raw_data.second = DEC_2_HEX(rtc_data->second);
    raw_data.minute = DEC_2_HEX(rtc_data->minute);
    raw_data.hour   = DEC_2_HEX(rtc_data->hour  );
    raw_data.day    = DEC_2_HEX(rtc_data->day   );
    raw_data.month  = DEC_2_HEX(rtc_data->month );
    raw_data.year   = DEC_2_HEX(rtc_data->year  );
    raw_data.weekday = RTC_Weekday_Monday; // or calculate the exact day
    i2c_polling_write(SENSOR_ADDRESS,0x00,sizeof(raw_data),(char*)&raw_data);
}

除了 I2C Controller 的初始化之外,您还需要实现读/写例程。

更新:

给定 rtc_data_t* rtc_data,这里是用于在 LCD 上显示 HH:MM:SS 的 Segger-GUI 代码:

char text[16] = {0};
sprintf(text,"%.2u:%.2u:%.2u",rtc_data->hour,rtc_data->minute,rtc_data->second);
GUI_RECT gui_rect = {0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1};
GUI_DispStringInRect(text,&gui_rect,GUI_TA_HCENTER|GUI_TA_VCENTER);

当然,您必须自己初始化 LCD Controller ...

关于c - 如何在stm32-discovery上显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21834403/

相关文章:

c - C中箭头运算符之间的空间

c - 将一个项目添加到链表会创建两个项目?

c - 如何为 PIC18 制作定时器?

c - stm32f103c8t6 USART1中断不起作用

c++ - Adafruit Fona 与 echo 服务器建立连接,但不发送任何数据

android - 分析 android-ndk 纯 C/C++ 可执行文件

c - 为什么打印到 "stdout"不按顺序执行?

c - 使用DS1620 IC和arm微 Controller 的温度计

Makefile 链接 : undefined reference to _exit

c - c中的多线程环境