PCF8583 RTC examples (L152)

Name of the projects

l152_pcf8583_watch_lcd
l152_pcf8583_watch_lcd_sw_i2c

Description

The projects shows on a 2×16 LCD the current hour and date that are read from a PCF8583 RTC IC connected on hardware or software I2C.

Code snippet (initial code, project structure and VSCode files generated by VPC):

uint8_t s[4]; // buffer used for conversions

int main(void)
{
  /* local variables */
  uint8_t current_sec = 0, old_sec = 0;

  /* mandatory system initializations */
  vpc_system_init();
  //vpc_rtc_init(); /* enable if required */
  vpc_gpio_init();
  //vpc_usart2_uart_init(); /* enable if required */
  vpc_i2c1_init();
  /* third-party initializations */
  lcd_init(LCD_HD44780);

  /* do your own initializations below */
  RTC_seconds = 0; // 0 to 59
  RTC_minutes = 5; // 0 to 59
  RTC_hours = 17; // 0 to 23
  RTC_day = 13; // 1 to 31
  RTC_month = 5; // 1 to 12
  RTC_century = 20; //
  RTC_year = 14; // 0 to 99
  RTC_dayofweek = 2; // 0 to 6 (Sun, Mon, etc..)
  RTC_leapyear = 2; // 0 to 3 (o - is leapyear and 1,2,3 not)

  pcf8583_set_datetime(RTC_hours, RTC_minutes, RTC_seconds, RTC_dayofweek,
                       RTC_day, RTC_month, RTC_leapyear, RTC_century, RTC_year);

  while(1) {
    /* your forever repeating code */
    pcf8583_get_datetime(&RTC_hours, &RTC_minutes, &RTC_seconds,
                         &RTC_dayofweek, &RTC_day, &RTC_month, &RTC_leapyear,
                         &RTC_century, &RTC_year);
    current_sec = RTC_seconds;
    if (current_sec != old_sec) {
      old_sec = current_sec;
      // show time
      lcd_cursor_position(0, 0);
      byte2dec(RTC_hours, s);
      if (s[1] == ' ')
        _lcd_write_data('0');
      else
        _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      _lcd_write_data(':');
      byte2dec(RTC_minutes, s);
      if (s[1] == ' ')
        _lcd_write_data('0');
      else
        _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      _lcd_write_data(':');
      byte2dec(RTC_seconds, s);
      if (s[1] == ' ')
        _lcd_write_data('0');
      else
        _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      // show date on the second line
      lcd_cursor_position(1, 0);
      byte2dec(RTC_day, s);
      if (s[1] == ' ')
        _lcd_write_data('0');
      else
        _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      _lcd_write_data('/');
      byte2dec(RTC_month, s);
      if (s[1] == ' ')
        _lcd_write_data('0');
      else
        _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      _lcd_write_data('/');
      byte2dec(RTC_century, s);
      _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      byte2dec(RTC_year, s);
      _lcd_write_data(s[1]);
      _lcd_write_data(s[2]);
      GPIO_ToggleBits(LD2_GPIO_Port, LD2_Pin); // blink
    }

  }
  return 0;
}

Project published in repository.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.