Pages

Wednesday, September 7, 2016

Real Time Clock with 2 Alarms Using PIC18F4550 and DS1307

Real time clock with two alarms using DS1307 RTCC and PIC18F4550 microcontroller
We have seen how to make a simple real time clock and calendar using PIC18F4550 microcontroller and DS1307 RTC chip at:
Real time clock with PIC18F4550 and DS1307 RTC
Now let's see how to add 2 alarms to the previous project with some changes.
About DS1307 RTC:
The DS1307 is an 8-pin integrated circuit uses I2C communication protocol to communicate with master device which is in our case the PIC18F4550 microcontroller. This small chip can count seconds, minutes, hours, day, date, month and year with leap-year up to year 2100.
The DS1307 receives and transfers data (clock data and calendar data) as BCD format, so after receiving data we have to convert these data into decimal data, and before writing data to the DS1307 we have to convert this data from decimal to BCD format. For example we have the BCD number 33, converting this number into decimal gives 21.
The following image shows the DS1307 pin configurations:
DS1307 RTCC pin configuration PIC18F4550
And the following circuit is typical operating circuit for the DS1307 RTC:
DS1307 RTCC circuit PIC18F4550
A 3V battery can be connected between VBAT and GND as a backup supply input.
The DS1307 uses an external 32.768KHz crystal and there is no need to add any resistors or capacitors with it.
The time and calendar information is obtained by reading the appropriate register bytes. The time and calendar are set or initialized by writing the appropriate register bytes. The contents of the time and calendar registers are in the BCD format. The following table shows the DS1307 RTC registers:
DS1307 RTCC registers table PIC18F4550
More details in the DS1307 RTC datasheet.
Real time clock with 2 alarms using PIC18F4550 and DS1307 circuit:
Real time clock with alarm using PIC18F4550 and DS1307 circuit
Required Components:
  • PIC18F4550 Microcontroller
  • DS1307
  • 2004 (20x4) LCD
  • 3V Cell Battery
  • 32.768KHz crystal oscillator
  • 2 x 10K resistors
  • 10K Potentiometer
  • 470Ohm resistor
  • 3 Buttons
  • LED (For Alarm 1)
  • Buzzer (For Alarm 2)
  • +5V Power Supply
  • Protoboard
  • Jumper Wires
In this project PIC18F4550 microcontroller internal oscillator is used (8MHz) and MCLR pin function is disabled.
Real time clock with 2 alarms using PIC18F4550 and DS1307 CCS C code:
Tested with: CCS PIC C compiler PCWHD version 5.051.
// Real time clock with 2 alarms using PIC18F4550 and DS1307 CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
#use I2C(master, I2C1, FAST = 100000)
#use pwm (PWM1, FREQUENCY = 2400Hz, DUTY = 50, PWM_OFF)

short alarm1_status, alarm2_status, button_state;
char time[] = "TIME:     :  :  ";
char calendar[] = "  /  /20  ";
char alarm1[] = "ALARM1: 00:00:00";
char alarm2[] = "ALARM2: 00:00:00";
unsigned int8 second, second10, minute, minute10,
               hour, hour10, date, date10, month, month10,
               year, year10, day, alarm1_minute=0, alarm1_hour=0,
               alarm2_minute=0, alarm2_hour=0, i, j;
void ds1307_display(){
  second10  =  (second & 0x70) >> 4;
  second = second & 0x0F;
  minute10  =  (minute & 0x70) >> 4;
  minute = minute & 0x0F;
  hour10  =  (hour & 0x30) >> 4;
  hour = hour & 0x0F;
  date10  =  (date & 0x30) >> 4;
  date = date & 0x0F;
  month10  =  (month & 0x10) >> 4;
  month = month & 0x0F;
  year10  =  (year & 0xF0) >> 4;
  year = year & 0x0F;
  time[15]  = second  + 48;
  time[14]  = second10  + 48;
  time[12]  = minute  + 48;
  time[11]  = minute10  + 48;
  time[9]  = hour  + 48;
  time[8]  = hour10  + 48;
  calendar[9]  = year  + 48;
  calendar[8]  = year10  + 48;
  calendar[4]  = month + 48;
  calendar[3]  = month10 + 48;
  calendar[1]  = date + 48;
  calendar[0]  = date10 + 48;
  lcd_gotoxy(1, 1);                              // Go to column 1 row 1
  printf(lcd_putc, time);                        // Display time
  lcd_gotoxy(1, 2);                              // Go to column 1 row 2
  switch(day){
    case 1: lcd_putc("DATE:Su"); break;
    case 2: lcd_putc("DATE:Mo"); break;
    case 3: lcd_putc("DATE:Tu"); break;
    case 4: lcd_putc("DATE:We"); break;
    case 5: lcd_putc("DATE:Th"); break;
    case 6: lcd_putc("DATE:Fr"); break;
    case 7: lcd_putc("DATE:Sa"); break;}
  lcd_gotoxy(9, 2);                              // Go to column 9 row 2
  printf(lcd_putc, calendar);                    // Display calendar
}
void alarm_display(){                            // Display alarms info
  alarm1[12]  = alarm1_minute % 10  + 48;
  alarm1[11]  = alarm1_minute/10 + 48;
  alarm1[9]  = alarm1_hour%10  + 48;
  alarm1[8]  = alarm1_hour/10  + 48;
  lcd_gotoxy(21, 1);                             // Go to column 1 row 3
  printf(lcd_putc, alarm1);
  lcd_gotoxy(38, 1);
  if(alarm1_status) lcd_putc("ON ");
  else              lcd_putc("OFF");
  alarm2[12]  = alarm2_minute % 10  + 48;
  alarm2[11]  = alarm2_minute/10 + 48;
  alarm2[9]  = alarm2_hour%10  + 48;
  alarm2[8]  = alarm2_hour/10  + 48;
  lcd_gotoxy(21, 2);                             // Go to column 1 row 4
  printf(lcd_putc, alarm2);
  lcd_gotoxy(38, 2);
  if(alarm2_status) lcd_putc("ON ");
  else              lcd_putc("OFF");  
}
void ds1307_write(unsigned int8 address, data_){
  i2c_start();                                   // Start I2C
  i2c_write(0xD0);                               // DS1307 address
  i2c_write(address);                            // Send register address
  i2c_write(data_);                              // Write data to the selected register
  i2c_stop();                                    // Stop I2C
}
void ds1307_read(){
   i2c_start();                                  // Start I2C
   i2c_write(0xD0);                              // DS1307 address
   i2c_write(0);                                 // Send register address
   i2c_start();                                  // Restart I2C
   i2c_write(0xD1);                              // Initialize data read
   second =i2c_read(1);                          // Read seconds from register 0
   minute =i2c_read(1);                          // Read minuts from register 1
   hour = i2c_read(1);                           // Read hour from register 2
   day = i2c_read(1);                            // Read day from register 3
   date = i2c_read(1);                           // Read date from register 4
   month = i2c_read(1);                          // Read month from register 5
   year = i2c_read(0);                           // Read year from register 6
   i2c_stop();                                   // Stop I2C
}
void alarm_check(){
 if((alarm1_minute == ((minute & 0x0F) + (minute >> 4) * 10)) &&
 (alarm1_hour == ((hour & 0x0F) + (hour >> 4) * 10)) && (second == 0) && alarm1_status)
   output_high(PIN_B5);                          // Alarm1 ON
 if((alarm2_minute == ((minute & 0x0F) + (minute >> 4) * 10)) &&
 (alarm2_hour == ((hour & 0x0F) + (hour >> 4) * 10)) && (second == 0)  && alarm2_status)
  pwm_on();                                      // Alarm2 ON
}
int8 edit(int8 parameter, int8 xx, int8 yy){
  while(TRUE){
    if(input(PIN_B2) && input(PIN_B4)) button_state = 0;
    while(!input(PIN_B3)){
      parameter++;
      if(((i == 1) || (i == 6)) && parameter > 23)
        parameter = 0;
      if(((i == 2) || (i == 7)) && parameter > 59)
        parameter = 0;
      if(i == 3 && parameter > 31)
        parameter = 1;
      if(i == 4 && parameter > 12)
        parameter = 1;
      if(i == 5 && parameter > 99)
        parameter = 0;
      lcd_gotoxy(xx, yy);
    printf(lcd_putc,"%02u", parameter);
    if((i == 6) || (i == 7)){
      ds1307_read();
      ds1307_display();}
      delay_ms(200);}
    lcd_gotoxy(xx, yy);
    lcd_putc("  ");
    j = 0;
    while((input(PIN_B2) || button_state || (i==6) || (i==7)) && (input(PIN_B4) || 
      button_state || ((i != 6) && (i != 7))) && input(PIN_B3) && j < 5){
      j++;
     delay_ms(50);}
    lcd_gotoxy(xx, yy);
    printf(lcd_putc,"%02u", parameter);
    j = 0;
    while((input(PIN_B2)||button_state||(i == 6) || (i == 7)) && (input(PIN_B4) || 
      button_state || ((i != 6) && (i != 7))) && input(PIN_B3) && j < 5){
      j++;
      delay_ms(50);}
    if((i != 6) && (i != 7) && !input(PIN_B2) && !button_state){
      button_state = 1; return parameter;}
    if((i == 6) || (i == 7)){
      if(!input(PIN_B4) && (!button_state)){
        button_state = 1; return parameter;}
      ds1307_read();
      ds1307_display();
    }
  } 
}
short alarm_on_off(short number){
  short alarm;
  while(TRUE){
    if(input(PIN_B4)) button_state = 0;
    ds1307_read();
    ds1307_display();
    while(!input(PIN_B3)){
      alarm++;
      lcd_gotoxy(38, number+1);
      if(alarm)  lcd_putc("ON ");
      else       lcd_putc("OFF");
      delay_ms(200);
    }
    lcd_gotoxy(38, number+1);
    lcd_putc("   ");
    j=0;
    while(input(PIN_B3) && (input(PIN_B4) || button_state) && j < 5){
      j++;
      delay_ms(50);}
    lcd_gotoxy(38, number+1);
    if(alarm)  lcd_putc("ON ");
    else       lcd_putc("OFF");
    if(!input(PIN_B4) && !button_state){
      button_state = 1; return alarm;}
    j=0;
    while(input(PIN_B3) && (input(PIN_B4) || button_state) && j < 5){
      j++;
      delay_ms(50);}
  }
}
void main(){
  setup_oscillator(OSC_8MHZ);                      // Set internal oscillator to 8MHz
  setup_adc_ports(NO_ANALOGS);
  port_b_pullups(TRUE);                  // Enable PORTB pull-ups
  output_b(0);
  set_tris_b(0x1F);                        // Configure RB0 & RB1 as inputs
  lcd_init();                           // Initialize LCD module
  lcd_putc('\f');                       // LCD clear
  while(TRUE){
    if(input(PIN_B2) && input(PIN_B4)) button_state = 0;
    if(!input(PIN_B2) && (!button_state)){
      button_state = 1;
      // Convert BCD to decimal
      minute = minute + minute10 * 10;
      hour = hour + hour10 * 10;
      date = date + date10 * 10;
      month = month + month10 * 10;
      year = year + year10 * 10;
      // End conversion
      i=1;
      hour = edit(hour, 9, 1);
      i=2;
      minute = edit(minute, 12, 1);
      while(TRUE){
        if(input(PIN_B2)) 
          button_state = 0;
        while(!input(PIN_B3)){
          day++;
          if(day > 7)
            day = 1;
          lcd_gotoxy(6, 2);                  // Go to column 6 row 2
          switch(day){
            case 1: lcd_putc("Su"); break;
            case 2: lcd_putc("Mo"); break;
            case 3: lcd_putc("Tu"); break;
            case 4: lcd_putc("We"); break;
            case 5: lcd_putc("Th"); break;
            case 6: lcd_putc("Fr"); break;
            case 7: lcd_putc("Sa"); break;}
          delay_ms(200);
        }
        lcd_gotoxy(6, 2);
        lcd_putc("   ");
        j = 0;
        while((input(PIN_B2)||button_state) && input(PIN_B3) && j < 5){
          j++;
          delay_ms(50);}
        lcd_gotoxy(6, 2);
        switch(day){
          case 1: lcd_putc("Su"); break;
          case 2: lcd_putc("Mo"); break;
          case 3: lcd_putc("Tu"); break;
          case 4: lcd_putc("We"); break;
          case 5: lcd_putc("Th"); break;
          case 6: lcd_putc("Fr"); break;
          case 7: lcd_putc("Sa"); break;}
        if(!input(PIN_B2) && (!button_state)){
          button_state = 1;
          break;}
        j = 0;
        while((input(PIN_B2)||button_state) && input(PIN_B3) && j < 5){
          j++;
          delay_ms(50);}
      }
      i=3;
      date = edit(date, 9, 2); 
      i=4;
      month = edit(month, 12, 2);
      i=5;
      year = edit(year, 17, 2);
      // Convert decimal to BCD
      minute = ((minute/10) << 4) + (minute % 10);
      hour = ((hour/10) << 4) + (hour % 10);
      date = ((date/10) << 4) + (date % 10);
      month = ((month/10) << 4) + (month % 10);
      year = ((year/10) << 4) + (year % 10);
      // End conversion
      ds1307_write(1, minute);
      ds1307_write(2, hour);
      ds1307_write(3, day);
      ds1307_write(4, date);
      ds1307_write(5, month);
      ds1307_write(6, year);
      ds1307_write(0, 0);
    }
    if(!input(PIN_B4) && (!button_state)){
      button_state = 1;
      i=6;
      alarm1_hour = edit(alarm1_hour, 29, 1);
      i=7;
      alarm1_minute = edit(alarm1_minute, 32, 1);
      alarm1_status = alarm_on_off(0); 
      i=6;
      alarm2_hour = edit(alarm2_hour, 29, 2);
      i=7;
      alarm2_minute = edit(alarm2_minute, 32, 2);
      alarm2_status = alarm_on_off(1);
      delay_ms(200); 
    }
    if(input(PIN_B3) == 0){
      output_low(PIN_B5);
      pwm_off();
    }
    ds1307_read();                       // Read data from DS1307 RTCC
    alarm_check();                       // Check if there is an alarm
    ds1307_display();                    // Diaplay time and calendar
    alarm_display();                     // Display alarms
    delay_ms(50);
  }
}

Project video: