PCF8583 RTC alarm weekdays example (L152)

Name of the project

l152_pcf8583_alarm_weekdays_lcd

Description

The project demonstrate setting the alarm that will repeat for certain days of the week, at user’s choice.

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

uint8_t pcf8583_active_alarm = 0;

void EXTI15_10_IRQHandler(void)
{
  // Here would be the B1 pin interrupt handler (do where it says "do something..."):
  if ((EXTI_GetITStatus(EXTI_Line13)) != RESET) {
    // do something...
    if (pcf8583_active_alarm) {
      pcf8583_stop_alarm();
      pcf8583_active_alarm = 0;
    }
    EXTI_ClearITPendingBit(EXTI_Line13);
  }
}

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);

	RTC_seconds = 55; // 0 to 59
	RTC_minutes = 59; // 0 to 59
	RTC_hours = 23; // 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 = 0; // 0 to 6 (Sun=0, Mon=1, etc..)
	RTC_leapyear = 2; // 0 to 3 (0 - is leapyear and 1,2,3 not)
	// 2012 was leapyear    ( leapyear = 0)
	// 2014 is not leapyear ( leapyear = 2)
	//
	pcf8583_set_datetime(RTC_hours, RTC_minutes, RTC_seconds, RTC_dayofweek,
			RTC_day, RTC_month, RTC_leapyear, RTC_century, RTC_year);
	// =====================================================================

	// =================== SET THE ALARM ===================================
	pcf8583_en_dis_alarm(PCF8583_WEEKDAYS_ALARM);
	pcf8583_set_alarm_time(0, 0, 0);
	//In which days alarm is on: SU   MO   TU   WE   TH   FR     SA
	pcf8583_set_alarm_weekdays(RESET, SET, SET, SET, SET, SET, RESET);
	// =====================================================================

  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]);
      lcd_cursor_position(0, 13);
      switch(RTC_dayofweek){
        case 0: 
          lcd_write_str((uint8_t *)"SUN");
          break;
        case 1: 
          lcd_write_str((uint8_t *)"MON");
          break;
        case 2: 
          lcd_write_str((uint8_t *)"TUE");
          break;
        case 3: 
          lcd_write_str((uint8_t *)"WED");
          break;
        case 4: 
          lcd_write_str((uint8_t *)"THU");
          break;
        case 5: 
          lcd_write_str((uint8_t *)"FRI");
          break;
        case 6: 
          lcd_write_str((uint8_t *)"SAT");
          break;
      }
      if (pcf8583_active_alarm)
        GPIO_SetBits(LD2_GPIO_Port, LD2_Pin); // alarm - LED continuously lit
      else
        GPIO_ToggleBits(LD2_GPIO_Port, LD2_Pin); // blink
    }
    if (!GPIO_ReadInputDataBit(ALARM_GPIO_Port, ALARM_Pin))
      pcf8583_active_alarm = 1;
  }
  return 0;
}

Project published in repository.

Leave a comment

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